Expand description
§Only Claims
If you don’t care about that header as long as the header is verified, signing and verification can be done with just a few traits.
§Signing
Claims can be any serde::Serialize
type, usually derived with
serde_derive
.
use hmac::{Hmac, Mac};
use jwt::SignWithKey;
use sha2::Sha256;
use std::collections::BTreeMap;
let key: Hmac<Sha256> = Hmac::new_from_slice(b"some-secret")?;
let mut claims = BTreeMap::new();
claims.insert("sub", "someone");
let token_str = claims.sign_with_key(&key)?;
assert_eq!(token_str, "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lIn0.5wwE1sBrs-vftww_BGIuTVDeHtc1Jsjo-fiHhDwR8m0");
§Verification
Claims can be any serde::Deserialize
type, usually derived with
serde_derive
.
use hmac::{Hmac, Mac};
use jwt::VerifyWithKey;
use sha2::Sha256;
use std::collections::BTreeMap;
let key: Hmac<Sha256> = Hmac::new_from_slice(b"some-secret")?;
let token_str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzb21lb25lIn0.5wwE1sBrs-vftww_BGIuTVDeHtc1Jsjo-fiHhDwR8m0";
let claims: BTreeMap<String, String> = token_str.verify_with_key(&key)?;
assert_eq!(claims["sub"], "someone");
§Header and Claims
If you need to customize the header, you can use the Token
struct. For
convenience, a Header
struct is provided for all of the commonly defined
fields, but any type that implements JoseHeader
can be used.
§Signing
Both header and claims have to implement serde::Serialize
.
use hmac::{Hmac, Mac};
use jwt::{AlgorithmType, Header, SignWithKey, Token};
use sha2::Sha384;
use std::collections::BTreeMap;
let key: Hmac<Sha384> = Hmac::new_from_slice(b"some-secret")?;
let header = Header {
algorithm: AlgorithmType::Hs384,
..Default::default()
};
let mut claims = BTreeMap::new();
claims.insert("sub", "someone");
let token = Token::new(header, claims).sign_with_key(&key)?;
assert_eq!(token.as_str(), "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJzb21lb25lIn0.WM_WnPUkHK6zm6Wz7zk1kmIxz990Te7nlDjQ3vzcye29szZ-Sj47rLNSTJNzpQd_");
§Verification
Both header and claims have to implement serde::Deserialize
.
use hmac::{Hmac, Mac};
use jwt::{AlgorithmType, Header, Token, VerifyWithKey};
use sha2::Sha384;
use std::collections::BTreeMap;
let key: Hmac<Sha384> = Hmac::new_from_slice(b"some-secret")?;
let token_str = "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJzb21lb25lIn0.WM_WnPUkHK6zm6Wz7zk1kmIxz990Te7nlDjQ3vzcye29szZ-Sj47rLNSTJNzpQd_";
let token: Token<Header, BTreeMap<String, String>, _> = token_str.verify_with_key(&key)?;
let header = token.header();
let claims = token.claims();
assert_eq!(header.algorithm, AlgorithmType::Hs384);
assert_eq!(claims["sub"], "someone");
Re-exports§
pub use crate::algorithm::store::Store;
pub use crate::algorithm::AlgorithmType;
pub use crate::algorithm::SigningAlgorithm;
pub use crate::algorithm::VerifyingAlgorithm;
pub use crate::claims::Claims;
pub use crate::claims::RegisteredClaims;
pub use crate::error::Error;
pub use crate::header::Header;
pub use crate::header::JoseHeader;
pub use crate::token::signed::SignWithKey;
pub use crate::token::signed::SignWithStore;
pub use crate::token::verified::VerifyWithKey;
pub use crate::token::verified::VerifyWithStore;
pub use crate::token::Unsigned;
pub use crate::token::Unverified;
pub use crate::token::Verified;
Modules§
- Algorithms capable of signing and verifying tokens. By default only the
hmac
crate’sHmac
type is supported. For more algorithms, enable the featureopenssl
and see the openssl module. Thenone
algorithm is explicitly not supported. - Convenience structs for commonly defined fields in claims.
- Convenience structs for commonly defined fields in headers.
- A structured representation of a JWT.
Structs§
- Representation of a structured JWT. Methods vary based on the signature type
S
.
Traits§
- A trait used to parse objects from base64 encoding. The return type can be either owned if the header is dynamic, or it can be borrowed if the header is a static, pre-computed value. It is implemented automatically for every type that implements DeserializeOwned for the base64 encoded JSON representation.
- A trait used to convert objects in base64 encoding. The return type can be either owned if the header is dynamic, or it can be borrowed if the header is a static, pre-computed value. It is implemented automatically for every type that implements Serialize. as a base64 encoding of the object’s JSON representation.