diff --git a/frameworks/Rust/axum-xitca/.cargo/config.toml b/frameworks/Rust/axum-xitca/.cargo/config.toml index c32d7b2..ee0584d 100644 --- a/frameworks/Rust/axum-xitca/.cargo/config.toml +++ b/frameworks/Rust/axum-xitca/.cargo/config.toml @@ -1,2 +1,2 @@ [target.x86_64-unknown-linux-gnu] -rustflags = ["-C", "target-cpu=native", "-Z", "threads=8"] +rustflags = ["-C", "target-cpu=native"] diff --git a/frameworks/Rust/axum-xitca/Cargo.lock b/frameworks/Rust/axum-xitca/Cargo.lock index 2abc976..655b700 100644 --- a/frameworks/Rust/axum-xitca/Cargo.lock +++ b/frameworks/Rust/axum-xitca/Cargo.lock @@ -145,6 +145,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64" version = "0.22.1" @@ -184,6 +190,12 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "bytes" version = "1.7.2" @@ -216,6 +228,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "dotenvy" version = "0.15.7" @@ -285,8 +306,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -388,6 +411,30 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "jsonwebtoken" +version = "9.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ae10193d25051e74945f1ea2d0b42e03cc3b890f7e4cc5faa44997d808193f" +dependencies = [ + "base64 0.21.7", + "js-sys", + "pem", + "ring", + "serde", + "serde_json", + "simple_asn1", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -484,6 +531,40 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.4" @@ -528,6 +609,16 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "pem" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e459365e590736a54c3fa561947c84837534b8e9af6fc5bf781307e82658fae" +dependencies = [ + "base64 0.22.1", + "serde", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -542,6 +633,7 @@ dependencies = [ "axum", "dotenvy", "http-body", + "jsonwebtoken", "serde", "serde_json", "serde_repr", @@ -577,6 +669,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro2" version = "1.0.86" @@ -648,6 +746,21 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -761,6 +874,18 @@ dependencies = [ "libc", ] +[[package]] +name = "simple_asn1" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adc4e5204eb1910f40f9cfa375f6f05b68c3abac4b6fd879c8ff5e7ae8a0a085" +dependencies = [ + "num-bigint", + "num-traits", + "thiserror", + "time", +] + [[package]] name = "slab" version = "0.4.9" @@ -796,6 +921,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + [[package]] name = "syn" version = "2.0.77" @@ -849,6 +980,37 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tokio" version = "1.40.0" @@ -927,7 +1089,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" dependencies = [ "async-compression", - "base64", + "base64 0.22.1", "bitflags 2.6.0", "bytes", "futures-core", @@ -1039,6 +1201,12 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "uuid" version = "1.10.0" @@ -1066,6 +1234,61 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + [[package]] name = "winapi" version = "0.3.9" diff --git a/frameworks/Rust/axum-xitca/Cargo.toml b/frameworks/Rust/axum-xitca/Cargo.toml index f235248..9272be9 100644 --- a/frameworks/Rust/axum-xitca/Cargo.toml +++ b/frameworks/Rust/axum-xitca/Cargo.toml @@ -28,3 +28,4 @@ serde_json = { version = "1.0.128" } serde_repr = "0.1.19" http-body = "1.0.1" xitca-unsafe-collection = "0.2.0" +jsonwebtoken = "9.3.0" diff --git a/frameworks/Rust/axum-xitca/src/utils/jwt.rs b/frameworks/Rust/axum-xitca/src/utils/jwt.rs new file mode 100644 index 0000000..0efaa3e --- /dev/null +++ b/frameworks/Rust/axum-xitca/src/utils/jwt.rs @@ -0,0 +1,65 @@ +use jsonwebtoken::{ + decode, encode, Algorithm, DecodingKey, EncodingKey, Header, TokenData, Validation, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Claims { + // aud: String, // Optional. Audience + pub exp: usize, // Required (validate_exp defaults to true in validation). Expiration time (as UTC timestamp) + pub iat: usize, // Optional. Issued at (as UTC timestamp) + // iss: String, // Optional. Issuer + // nbf: usize, // Optional. Not Before (as UTC timestamp) + pub sub: String, // Optional. Subject (whom token refers to) +} + +pub fn encode_jwt(claims: &Claims, key: &[u8]) -> Result { + encode( + &Header::new(Algorithm::HS256), + &claims, + &EncodingKey::from_secret(key), + ) +} + +pub fn decode_jwt( + token: &str, + key: &[u8], +) -> Result, jsonwebtoken::errors::Error> { + decode::( + token, + &DecodingKey::from_secret(key), + &Validation::default(), + ) +} + +#[cfg(test)] +mod tests { + use std::time::{SystemTime, UNIX_EPOCH}; + + use super::*; + + #[test] + fn jwt_works() { + let key = "xfy"; + let now = SystemTime::now(); + let seconds_since_epoch: usize = now + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() + .try_into() + .unwrap(); + let sub = "xfy".to_string(); + let claims = Claims { + exp: seconds_since_epoch + 1000, + iat: seconds_since_epoch, + sub: sub.clone(), + }; + let jwt = encode_jwt(&claims, key.as_ref()).unwrap(); + + let token_data = decode_jwt(&jwt, key.as_ref()).unwrap(); + println!("{token_data:?}"); + + assert_eq!(token_data.header.alg, Algorithm::HS256); + assert_eq!(token_data.claims.sub, sub); + } +} diff --git a/frameworks/Rust/axum-xitca/src/utils/mod.rs b/frameworks/Rust/axum-xitca/src/utils/mod.rs index dbe3e01..2ebdbd4 100644 --- a/frameworks/Rust/axum-xitca/src/utils/mod.rs +++ b/frameworks/Rust/axum-xitca/src/utils/mod.rs @@ -1,6 +1,8 @@ use tokio::signal; use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter}; +pub mod jwt; + /// Initializes the logger for tracing. pub fn init_logger() { let formatting_layer = fmt::layer()