[−][src]Crate jwts
Rust implementation of JSON Web Tokens, see https://tools.ietf.org/html/rfc7519
Examples
Sign
use jwts::Claims; use jwts::jws::{Algorithm, Key, Token}; let mut claims = Claims::new(); claims.iss = Some("sea".to_owned()); let mut token = Token::with_payload(claims); // custom the header like: // token.header.cty = Some("application/example".to_owned()); let key = Key::new(b"secret", Algorithm::HS256); let token = token.sign(&key).unwrap(); assert_eq!(token, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZWEifQ.L0DLtDjydcSK-c0gTyOYbmUQ_LUCZzqAGCINn2OLhFs");
Verify
use jwts::{Claims, ValidationConfig}; use jwts::jws::{Algorithm, Key, Token}; let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZWEiLCJleHAiOjEwNTc3MDkxMDU2LCJuYmYiOjE1NzcwOTEwNTYsImlhdCI6MTU3NzA5MTA1Nn0.4HwFlFB3LMhVc2xpsGBGSO3ut1KmnFdF8JrsL589ytw"; let key = Key::new(b"secret", Algorithm::HS256); let verified: Token<Claims> = Token::verify_with_key(token, &key).unwrap(); // use key resolver like: // let verified: Token<Claims> = Token::verify_with_key_resolver(token, |header, payload| { // // return a Key here // }).unwrap(); println!("{:?}", verified); // validate claims let config = ValidationConfig { iat_validation: true, nbf_validation: true, exp_validation: true, expected_iss: Some("sea".to_owned()), expected_sub: None, expected_aud: None, expected_jti: None, }; verified.validate_claims(&config).unwrap();
Custom Claims
use jwts::jws::{Algorithm, Key, Token}; #[macro_use] extern crate serde_derive; #[derive(Debug, Serialize, Deserialize)] struct CustomClaims { iss: String, } let claims = CustomClaims { iss: "sea".to_owned(), }; let mut token = Token::with_payload(claims); let key = Key::new(b"secret", Algorithm::HS256); let token = token.sign(&key).unwrap(); let token: Token<CustomClaims> = Token::decode(&token).unwrap(); // here decode without verification for demonstration println!("{:?}", token);
Re-exports
pub use self::error::Error; |
pub use self::error::ErrorKind; |
Modules
error | Errors |
jws | JSON Web Signature, see https://tools.ietf.org/html/rfc7515 |
Structs
Claims | Registered Claim Names, see https://tools.ietf.org/html/rfc7519#section-4.1 |
ValidationConfig | Configs for validation. |