Rust

issuer

Module

JWT issuers: HS256 with a shared secret, RS256 with an RSA key pair, and the token kinds built on them.

Contents

use utopia_auth::jwt::issuer::AccessToken;
Rust
pub struct AccessToken(pub Asymmetric)

Issues OAuth2 access tokens as RS256 JWTs of type at+jwt (RFC 9068).

Rust
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;
Rust
pub struct Asymmetric

Signs 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;
Rust
pub struct IdToken(pub Asymmetric)

Issues OpenID Connect ID tokens as RS256 JWTs.

use utopia_auth::jwt::issuer::Jwt;
Rust
pub struct Jwt(pub Symmetric)

Issues general-purpose HS256 JWTs with application-defined claims.

Rust
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;
Rust
pub struct RefreshToken(pub Symmetric)

Issues OAuth2 refresh tokens as HS256 JWTs.

use utopia_auth::jwt::issuer::Symmetric;
Rust
pub struct Symmetric

Signs JWTs with HS256 and a shared secret.