//[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 } impl JWTUtil { fn new(secret: &str) -> JWTUtil { let key: Hmac = 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") } }