Skip to navigation
Example to use jwt with rust
09.04.25
## Cargo.toml ``` jwt = {version ="*"} hmac = {version ="*"} sha2 = {version ="*"} ``` ## Call from main ``` let token = encode_token().await; let token_message = auth_token(token).await; ``` ## Lib ``` use hmac::{Hmac, Mac}; use jwt::{SignWithKey, VerifyWithKey}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::collections::BTreeMap; use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Serialize, Deserialize)] struct Claims { 1 implementation sub: String, company: String, } pub async fn encode_token() -> String { let mut token = "".to_string(); let key: Hmac
= Hmac::new_from_slice(b"some-secret").unwrap(); //let mut claims = BTreeMap::new(); let mut claims = BTreeMap::<&str, &str>::new(); let _unixtime = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs() + (60 * 60 * 24 * 365); let unixtime = &_unixtime.to_string(); claims.insert("u", "someone"); claims.insert("e", unixtime); token = claims.sign_with_key(&key).unwrap(); return token; } pub async fn auth_token(token: String) -> bool { //println!("{}", token); let mut r = false; let unix_now = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs(); let key: Hmac
= Hmac::new_from_slice(b"some-secret").unwrap(); let _claims: Result
, jwt::Error> = token.verify_with_key(&key); if _claims.is_ok() { let claims = _claims.unwrap(); let _unix_expire: Result
= claims["e"].parse(); if _unix_expire.is_ok() { let unix_expire: u64 = _unix_expire.unwrap().parse().unwrap(); if unix_expire >= unix_now { println!("{}(expire time) >= {}(time now)", unix_expire, unix_now); r = true; } } } return r; } ```
https://docs.rs/jwt/latest/jwt/
Reply
Anonymous
## more Error protected ``` let token = encode_token().await; let is_auth = auth_token(&token.unwrap()).await.unwrap(); println!("{}", is_auth); ``` ``` use hmac::{Hmac, Mac}; use jwt::{SignWithKey, VerifyWithKey}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::collections::BTreeMap; use std::error::Error; use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Serialize, Deserialize)] struct Claims { sub: String, company: String, } pub async fn encode_token() -> Result
> { let mut token = "".to_string(); let key: Hmac
= Hmac::new_from_slice(b"secret-dinglogs")?; //let mut claims = BTreeMap::new(); let mut claims = BTreeMap::<&str, &str>::new(); let _unixtime = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs() + (60 * 60 * 24 * 365); let unixtime = &_unixtime.to_string(); claims.insert("u", "someone"); claims.insert("e", unixtime); token = claims.sign_with_key(&key)?; Ok(token) } pub async fn auth_token(token: &str) -> Result
> { //println!("{}", token); let mut r = false; let unix_now = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs(); let key: Hmac
= Hmac::new_from_slice(b"secret-dinglogs").unwrap(); let _claims: Result
, jwt::Error> = token.verify_with_key(&key); if _claims.is_ok() { let claims = _claims.unwrap(); let _unix_expire: Result
= claims["e"].parse(); if _unix_expire.is_ok() { let unix_expire: u64 = _unix_expire.unwrap().parse().unwrap(); if unix_expire >= unix_now { //println!("{}(expire time) >= {}(time now)", unix_expire, unix_now); r = true; } } } Ok(r) } ```
09.04.25
Reply
Anonymous
Information Epoch 1745029931
Think hierarchically.
Home
Notebook
Contact us