Crates.io | jwts |
lib.rs | jwts |
version | 0.5.1 |
source | src |
created_at | 2019-12-20 07:24:50.156016 |
updated_at | 2024-05-20 16:34:39.960692 |
description | A rust implementation of JSON Web Tokens. |
homepage | https://github.com/suransea/jwt-rust |
repository | https://github.com/suransea/jwt-rust |
max_upload_size | |
id | 190849 |
size | 66,012 |
A rust implementation of JSON Web Tokens.
use jwts::{Claims, jws};
use jwts::jws::Header;
use jwts::jws::alg::HS256;
let claims = Claims {
iss: Some("sea".to_owned()),
..Default::default()
};
jws::encode::<HS256>(Header::default(), &claims, b"secret").unwrap();
use jwts::{Claims, jws};
use jwts::jws::{Header, NoVerify, Token, VerifyWith};
use jwts::jws::alg::HS256;
let claims = Claims::default();
let token = jws::encode::<HS256>(Header::default(), &claims, b"secret").unwrap();
let Token {..} = jws::decode::<Claims>(&token, NoVerify).unwrap(); // no verify
let Token {..} = jws::decode::<Claims>(&token, VerifyWith::<HS256>(b"secret")).unwrap(); // verify with algorithm and key
use std::collections::HashMap;
use std::time::{Duration, SystemTime};
use jwts::Claims;
use jwts::validate::{ExpectAud, ExpectIss, ExpectJti, ExpectSub, ExpiredTime, IssuedAtTime, NotBeforeTime, Validate};
let claims = Claims {
iss: Some("sea".to_owned()),
sub: Some("subject".to_owned()),
aud: Some("audience".to_owned()),
jti: Some("id".to_owned()),
..Default::default()
};
let claims = claims
.issued_now()
.expired_in(Duration::from_secs(1))
.not_before(SystemTime::now());
claims.validate(IssuedAtTime).unwrap();
claims.validate(NotBeforeTime).unwrap();
claims.validate(ExpiredTime).unwrap();
claims.validate(ExpectIss("sea")).unwrap();
claims.validate(ExpectSub("subject")).unwrap();
claims.validate(ExpectAud("audience")).unwrap();
claims.validate(ExpectJti("id")).unwrap();
// builtin validation works with any `Serialize` type:
let claims = HashMap::from([("iss", "sea")]);
claims.validate(ExpectIss("sea")).unwrap();
use std::collections::HashMap;
use serde_derive::{Deserialize, Serialize};
use jwts::jws;
use jwts::jws::{Header, Token, VerifyWith};
use jwts::jws::alg::HS256;
#[derive(Debug, Serialize, Deserialize)]
struct CustomClaims {
iss: String,
}
let claims = CustomClaims {
iss: "sea".to_owned(),
};
let token = jws::encode::<HS256>(Header::default(), &claims, b"secret").unwrap();
let Token {..} = jws::decode::<CustomClaims>(&token, VerifyWith::<HS256>(b"secret")).unwrap();
// Or use a map directly
let claims = HashMap::from([("iss", "sea")]);
let Token {..} = jws::decode::<HashMap<String, String>>(&token, VerifyWith::<HS256>(b"secret")).unwrap();
use jwts::{Claims, Error, jws};
use jwts::jws::{Algorithm, Header, Token, VerifyWith};
pub struct None;
impl Algorithm for None {
type SignKey = ();
type VerifyKey = ();
fn name() -> &'static str {
"None"
}
fn sign(data: impl AsRef<[u8]>, key: &Self::SignKey) -> Result<Vec<u8>, Error> {
Ok([].into())
}
fn verify(data: impl AsRef<[u8]>, sig: impl AsRef<[u8]>, key: &Self::VerifyKey) -> Result<(), Error> {
sig.as_ref().is_empty().then_some(()).ok_or(Error::InvalidSignature)
}
}
let claims = Claims::default();
let token = jws::encode::<None>(Header::default(), &claims, &()).unwrap();
let Token {..} = jws::decode::<Claims>(&token, VerifyWith::<None>(&())).unwrap();
use jwts::{Claims, Error, jws};
use jwts::jws::{Algorithm, Header, Token, Verify};
use jwts::jws::alg::HS256;
pub struct CustomVerify;
impl Verify<Claims> for CustomVerify {
fn verify(&self, f2s: &str, signature: &[u8], header: &Header, payload: &Claims) -> Result<(), Error> {
HS256::verify(f2s, signature, b"secret")
}
}
let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZWEifQ.L0DLtDjydcSK-c0gTyOYbmUQ_LUCZzqAGCINn2OLhFs";
let Token {..} = jws::decode::<Claims>(&token, CustomVerify).unwrap();
use jwts::Claims;
use jwts::validate::{Validate, Validation};
pub struct CustomValidation;
impl Validation<Claims> for CustomValidation {
type Error = ();
fn validate(&self, claims: &Claims) -> Result<(), Self::Error> {
claims.aud.is_some().then_some(()).ok_or(())
}
}
let claims = Claims {
aud: Some("audience".to_owned()),
..Default::default()
};
claims.validate(CustomValidation).unwrap();
Sign and verify using crate aws-lc-rs.
<= 0.2 | >= 0.4 |
---|---|
Token::sign |
jws::encode |
Token::decode |
jws::decode with NoVerify |
Token::verify_with_key |
jws::decode with VerifyWith |
Token::verify_with_key_resolver |
jws::decode with custom verify |
Token::validate_claims |
Validate::validate |
RFC 7519 JSON Web Token (JWT)
RFC 7515 JSON Web Signature (JWS)
RFC 7518 JSON Web Algorithms (JWA)