Browse Source

actix web

marion 4 years ago
parent
commit
16cd8b6a88
5 changed files with 210 additions and 42 deletions
  1. 162 39
      Cargo.lock
  2. 4 1
      Cargo.toml
  3. 3 0
      src/guessing_game/mod.rs
  4. 38 0
      src/jwt_test/mod.rs
  5. 3 2
      src/main.rs

+ 162 - 39
Cargo.lock

@@ -1,84 +1,207 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
+[[package]]
+name = "base64"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
+
+[[package]]
+name = "block-buffer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
+dependencies = [
+ "generic-array",
+]
+
 [[package]]
 name = "cfg-if"
-version = "0.1.10"
+version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
 [[package]]
-name = "getrandom"
-version = "0.1.14"
+name = "cpuid-bool"
+version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
+checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
+
+[[package]]
+name = "crypto-mac"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "58bcd97a54c7ca5ce2f6eb16f6bede5b0ab5f0055fedc17d2f0b4466e21671ca"
 dependencies = [
- "cfg-if",
- "libc",
- "wasi",
+ "generic-array",
+ "subtle",
+]
+
+[[package]]
+name = "digest"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "generic-array"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
+dependencies = [
+ "typenum",
+ "version_check",
 ]
 
 [[package]]
 name = "hello-rust"
 version = "0.1.0"
 dependencies = [
- "rand",
+ "hmac",
+ "jwt",
+ "serde",
+ "sha2",
+]
+
+[[package]]
+name = "hmac"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "deae6d9dbb35ec2c502d62b8f7b1c000a0822c3b0794ba36b3149c0a1c840dff"
+dependencies = [
+ "crypto-mac",
+ "digest",
+]
+
+[[package]]
+name = "itoa"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
+
+[[package]]
+name = "jwt"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0edf66902f26e7304b52abf9091f33227b55b041007fe98b7d12e79d78f7caaa"
+dependencies = [
+ "base64",
+ "crypto-mac",
+ "digest",
+ "hmac",
+ "serde",
+ "serde_json",
+ "sha2",
+]
+
+[[package]]
+name = "opaque-debug"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
+dependencies = [
+ "proc-macro2",
 ]
 
 [[package]]
-name = "libc"
-version = "0.2.69"
+name = "ryu"
+version = "1.0.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005"
+checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
 
 [[package]]
-name = "ppv-lite86"
-version = "0.2.6"
+name = "serde"
+version = "1.0.123"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
+checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
+dependencies = [
+ "serde_derive",
+]
 
 [[package]]
-name = "rand"
-version = "0.7.3"
+name = "serde_derive"
+version = "1.0.123"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
+checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31"
 dependencies = [
- "getrandom",
- "libc",
- "rand_chacha",
- "rand_core",
- "rand_hc",
+ "proc-macro2",
+ "quote",
+ "syn",
 ]
 
 [[package]]
-name = "rand_chacha"
-version = "0.2.2"
+name = "serde_json"
+version = "1.0.61"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
+checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a"
 dependencies = [
- "ppv-lite86",
- "rand_core",
+ "itoa",
+ "ryu",
+ "serde",
 ]
 
 [[package]]
-name = "rand_core"
-version = "0.5.1"
+name = "sha2"
+version = "0.9.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de"
 dependencies = [
- "getrandom",
+ "block-buffer",
+ "cfg-if",
+ "cpuid-bool",
+ "digest",
+ "opaque-debug",
 ]
 
 [[package]]
-name = "rand_hc"
-version = "0.2.0"
+name = "subtle"
+version = "2.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
+checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2"
+
+[[package]]
+name = "syn"
+version = "1.0.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
 dependencies = [
- "rand_core",
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
 ]
 
 [[package]]
-name = "wasi"
-version = "0.9.0+wasi-snapshot-preview1"
+name = "typenum"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
+
+[[package]]
+name = "version_check"
+version = "0.9.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
+checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"

+ 4 - 1
Cargo.toml

@@ -7,4 +7,7 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-rand = "0.7.3"
+jwt = "0.12.0"
+hmac = "0.9.0"
+sha2 = "0.9.2"
+serde = {version = "1.0", features = ["derive"] }

+ 3 - 0
src/guessing_game/mod.rs

@@ -1,3 +1,6 @@
+//[dependencies]
+//rand = "0.7.3"
+
 use std::io;
 use rand::Rng;
 use std::cmp::Ordering;

+ 38 - 0
src/jwt_test/mod.rs

@@ -0,0 +1,38 @@
+//[dependencies]
+//jwt = "0.12.0"
+//hmac = "0.9.0"
+//sha2 = "0.9.2"
+//serde = {version = "1.0", features = ["derive"] }
+
+use hmac::{Hmac, NewMac};
+use jwt::VerifyWithKey;
+use sha2::Sha256;
+use serde::{Serialize, Deserialize};
+
+pub fn main() {
+    let jwt_util = JWTUtil::new("0wsszP*VI4#)@Ekmq");
+    let claims = jwt_util.verify("eyJhbGciOiJIUzI1NiIsImV4cCI6MTYxNDg0MjUzNCwidHlwIjoiSldUIn0.eyJpZCI6MTAwMSwiZXhwIjoxNjE0ODQyNTM0fQ.tuBCDxAqTjI0SbABL_jv9DEGWdbLl_YiYxGI5RMWqxw");
+    println!("{:?}", claims);
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+struct Claims {
+    id: i64,
+    exp: i64
+}
+
+pub struct JWTUtil {
+    key: Hmac<Sha256>
+}
+
+impl JWTUtil {
+    fn new(secret: &str) -> JWTUtil {
+        let key: Hmac<Sha256> = Hmac::new_varkey(secret.as_bytes())
+            .expect("HMAC can take key of any size");
+        JWTUtil { key }
+    }
+
+    fn verify(&self, token_str: &str) -> Claims {
+        token_str.verify_with_key(&self.key).expect("invalid")
+    }
+}

+ 3 - 2
src/main.rs

@@ -7,7 +7,8 @@
 // mod qr_code_test;
 // mod spider_test;
 // mod actix_test;
-mod guessing_game;
+// mod guessing_game;
+mod jwt_test;
 
 // 将其他模块的成员声明为当前模块直接可用的,等同于java的静态引用
 // 若省略这句,使用时就需要加上模块名,如random_test::Random
@@ -19,6 +20,6 @@ fn main()  {
     // qr_code_test::test(2, 3);
     // spider_test::test("https://www.baidu.com").unwrap();
     // actix_test::http_listener() // return -> std::io::Result<()>
-    guessing_game::main();
+    jwt_test::main();
 }