30 lines
1.0 KiB
Rust
30 lines
1.0 KiB
Rust
use serde::{ Serialize, Deserialize };
|
|
use jsonwebtoken::{ encode, decode, Header, Validation, EncodingKey, DecodingKey };
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct Claims {
|
|
sub: String,
|
|
company: String,
|
|
exp: usize,
|
|
}
|
|
|
|
// https://mp.weixin.qq.com/s/KkcLH42b5vepmPZiwS6g5Q
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let my_claims = Claims {
|
|
sub: "Test Sub".into(),
|
|
company: "Test Company".into(),
|
|
exp: 1597546123497,
|
|
};
|
|
let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref()))?;
|
|
// let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref()))?;
|
|
// let token = encode(&Header::new(Algorithm::RS256), &my_claims, &EncodingKey::from_rsa_pem(include_bytes!("privkey.pem"))?)?;
|
|
|
|
println!("Token: {}", token);
|
|
|
|
let decoded_token = decode::<Claims>(&token, &DecodingKey::from_secret("secret".as_ref()), &Validation::default())?;
|
|
|
|
println!("Decoded token: {:#?}", decoded_token);
|
|
|
|
Ok(())
|
|
}
|