Expand description
JWTs for fastauth, like Better Auth’s jwt plugin:
services that can’t share the session store check a signed token against a public key set
instead of calling back to the auth server.
GET /tokenanswers{ "token": "<jwt>" }for the request’s session.GET /jwkslists the public keys as a JWKS. The first key is created on first use./get-sessionalso answers with the token in aset-auth-jwtheader, listed inAccess-Control-Expose-Headers.
Tokens are EdDSA (Ed25519) by default; JwtAlgorithm::ES256 and
JwtAlgorithm::RS256 are options. They carry the user, sub (the user id), iat,
exp (15 minutes), and iss and aud (the base URL unless configured). Private keys
are stored encrypted with the app secret. With Jwt::rotation_interval, keys expire
and a new one is created on the next signing; retired keys stay in the JWKS for
Jwt::grace_period (30 days) so tokens they signed keep verifying.
use fastauth_core::prelude::*;
use fastauth_plugin_jwt::{Jwt, JwtExt};
let auth: Auth<MemoryAdapter, (), plugins![Jwt]> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("https://acme.dk")
.database(MemoryAdapter::new())
.plugin(Jwt::new().expiration_time(Duration::minutes(5)))
.build()?;
let token = auth
.jwt()
.sign(serde_json::json!({ "sub": "user_1" }).as_object().unwrap().clone())
.await?;
let claims = auth.jwt().verify(&token).await?.expect("valid");
assert_eq!(claims["sub"], "user_1");
assert_eq!(claims["iss"], "https://acme.dk");Resource servers verify tokens with verify::JwksVerifier, built from the JWKS.
Re-exports§
pub use verify::JwksVerifier;pub use verify::VerifyError;pub use jsonwebtoken;
Modules§
- verify
- Verifying fastauth JWTs against a JWKS, for resource servers that don’t run fastauth.
Structs§
- GetJwks
GET /jwks: the public keys, as Better Auth lists them. Creates the first key on first use.- GetToken
GET /token: a JWT for the request’s session.- Json
WebKey - A row of
jwks: one signing key pair. The public half is a JWK as JSON; the private half is a PKCS#8 key encrypted with the app secret (XChaCha20-Poly1305 under a key derived from it), so a database dump alone can’t mint tokens. - Jwks
- The body of
/jwks. - Jwt
- The JWT plugin:
.plugin(Jwt::new()). - JwtApi
- Signs, verifies and rotates without HTTP.
- Rotate
Keys - Server-only: retires every live key (they keep verifying for the grace period) and creates a new one that signs from now on.
- Rotated
Key - The key
RotateKeyscreated. - SignJwt
- Server-only, like Better Auth’s
signJWT: signs any claims with the current key. - Sign
JwtInput - The input of
SignJwt. - Token
Output - A signed token.
- Verify
Jwt - Server-only, like Better Auth’s
verifyJWT: checks a token against every stored key. - Verify
JwtInput - The input of
VerifyJwt. - Verify
JwtOutput - What
VerifyJwtanswers: the claims, orNonewhen the token doesn’t verify.
Enums§
- JwtAlgorithm
- The algorithm new keys use.
Constants§
- SET_
AUTH_ JWT - The response header
/get-sessioncarries the session’s JWT in.
Traits§
- JwtExt
auth.jwt(): the server API. Only compiles on anAuthwithJwtregistered.