-
-
Save mjec/afe8c1e06d5ff40b3dca230bfc509cfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use hmac::{Hmac, Mac}; | |
use jwt::{Header, Token, VerifyWithKey}; | |
use sha2::Sha256; | |
use std::collections::BTreeMap; | |
const DATA: &str = "420"; | |
const SECRET_KEY: &str = "verysecretkey"; | |
fn main() { | |
let secret_key = Hmac::<Sha256>::new_from_slice(SECRET_KEY.as_bytes()).unwrap(); | |
let token = "eyJ0eXAiOiJKV1QiLCJraWQiOm51bGwsImFsZyI6IkhTMjU2In0.eyJpc3MiOm51bGwsInN1YiI6IjQyMCIsImF1ZCI6bnVsbCwiZXhwIjpudWxsLCJuYmYiOm51bGwsImlhdCI6bnVsbCwianRpIjpudWxsfQ._sN8Ur-b-38g4X2yQsIuhs4Z1dWjPW-7SHSFgmYa4xM"; | |
println!("{}", token); | |
match VerifyWithKey::<Token<Header, BTreeMap<String, Option<String>>, _>>::verify_with_key( | |
token, | |
&secret_key, | |
) { | |
Ok(token) => { | |
assert_eq!(Some(DATA.to_string()), token.claims()["sub"]); | |
} | |
Err(e) => { | |
panic!("Token is not valid: {}", e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment