jwt
ModuleSigned JSON Web Tokens (compact JWS, RFC 7515): issuers that sign claims with HS256 or RS256, and a verifier that checks the signature and the registered claims.
Time is an argument (now, Unix seconds), never read from the clock.
Contents
3 submodules, and what the module defines itself.
use utopia_auth::jwt::Claim;pub enum ClaimA registered JWT claim name, such as iss or exp.
use utopia_auth::jwt::Header;pub enum HeaderA JOSE header parameter of a JWT: typ, alg or kid.
use utopia_auth::jwt::hs256;pub fn hs256(secret: &[u8], input: &[u8]) -> Vec<u8>The HS256 signature of input: its HMAC-SHA256 under secret, as raw bytes.
use utopia_auth::jwt::rs256;pub fn rs256(private_key: &[u8], input: &[u8]) -> Result<Vec<u8>, crate::Error>The RS256 signature of input with a PEM RSA private key, as raw bytes.
Fails with Error::Exception when the key
cannot be read or cannot sign.
jwt::issuer
Own pageJWT issuers: HS256 with a shared secret, RS256 with an RSA key pair, and the token kinds built on them.
use utopia_auth::jwt::issuer::AccessToken;pub struct AccessToken(pub Asymmetric)Issues OAuth2 access tokens as RS256 JWTs of type at+jwt (RFC 9068).
use php_std::Array;
use utopia_auth::jwt::{generate_key_pair, AccessToken, Asymmetric};
let pair = generate_key_pair(2048)?;
let issuer = AccessToken(Asymmetric::new(pair.private_key.as_bytes(), pair.public_key.as_bytes(), b"https://cloud.example", Some(b"key-1"))?);
let audience = Array::from_list(vec!["https://api.example".into()]);
let token = issuer.issue(b"user", &audience, b"client", 0, 3600, &[], None, &Array::new(), 1_700_000_000)?;
assert!(token.starts_with("eyJ0eXAiOiJhdCtqd3QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImtleS0xIn0."));use utopia_auth::jwt::issuer::Asymmetric;pub struct AsymmetricSigns JWTs with RS256 and an RSA key pair.
The keys are PEM strings, parsed when used: construction only checks they are not empty.
use utopia_auth::jwt::issuer::IdToken;pub struct IdToken(pub Asymmetric)Issues OpenID Connect ID tokens as RS256 JWTs.
use utopia_auth::jwt::issuer::Jwt;pub struct Jwt(pub Symmetric)Issues general-purpose HS256 JWTs with application-defined claims.
use php_std::Array;
use utopia_auth::jwt::{Audience, Jwt, Symmetric};
let issuer = Jwt(Symmetric::new(b"secret", b"https://cloud.example", None)?);
let token = issuer.issue(&Audience::One(b"app".to_vec()), 60, &Array::new(), 1_700_000_000)?;
assert_eq!(token.split('.').count(), 3);
assert!(token.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9."));use utopia_auth::jwt::issuer::RefreshToken;pub struct RefreshToken(pub Symmetric)Issues OAuth2 refresh tokens as HS256 JWTs.
use utopia_auth::jwt::issuer::Symmetric;pub struct SymmetricSigns JWTs with HS256 and a shared secret.
jwt::key
Own pageSigning keys: PEM keys read inline or from a file:// path, RSA key
pair generation, and signing on OpenSSL with constant-time private key
operations.
use utopia_auth::jwt::key::KeyPair;pub struct KeyPairA PEM-encoded RSA key pair, for an Asymmetric issuer.
use utopia_auth::jwt::key::Rsa;pub struct RsaThe raw parameters of an RSA key, as published in a JWK.
use utopia_auth::jwt::key::generate_key_pair;pub fn generate_key_pair(bits: i64) -> Result<KeyPair, Error>A fresh RSA key pair of bits bits.
Fails with Error::Exception below 512 bits or when generation fails.
jwt::verifier
Own pageJWT verification: the signature checks and the verifier of registered claims.
use utopia_auth::jwt::verifier::Hs256;pub struct Hs256Checks HS256 signatures with a shared secret.
use utopia_auth::jwt::verifier::Rs256;pub struct Rs256Checks RS256 signatures with a PEM RSA public key.
use utopia_auth::jwt::verifier::Verifier;pub struct Verifier<S: SignatureCheck>Verifies a JWT: its signature, header and registered claims.
The expectations are fixed at construction: iss, acceptable aud
values and typ are checked when given; exp is required unless
allow_expired; nbf/iat are always checked; leeway tolerates skew.
use php_std::{Array, Value};
use utopia_auth::jwt::{Audience, Hs256, Jwt, Symmetric, Verifier};
let token = Jwt(Symmetric::new(b"secret", b"iss", None)?).issue(&Audience::One(b"app".to_vec()), 60, &Array::new(), 1_000)?;
let verifier = Verifier::new(Hs256::new(b"secret")?, Some(b"iss"), None, None, false, 0)?;
let claims = verifier.verify(token.as_bytes(), 1_030)?;
assert_eq!(claims.get_str("aud"), Some(&Value::from("app")));
let error = verifier.verify(token.as_bytes(), 2_000).unwrap_err();
assert_eq!(error.to_string(), "Token has expired");use utopia_auth::jwt::verifier::Audience;pub enum AudienceAn aud value: one recipient, or an array of them.
use utopia_auth::jwt::verifier::SignatureCheck;pub trait SignatureCheckHow a Verifier checks signatures: the JWS alg it expects and the
check itself. Implement it to verify another algorithm.