Skip to main content

Crate fastauth_plugin_jwt

Crate fastauth_plugin_jwt 

Source
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 /token answers { "token": "<jwt>" } for the request’s session.
  • GET /jwks lists the public keys as a JWKS. The first key is created on first use.
  • /get-session also answers with the token in a set-auth-jwt header, listed in Access-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.
JsonWebKey
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.
RotateKeys
Server-only: retires every live key (they keep verifying for the grace period) and creates a new one that signs from now on.
RotatedKey
The key RotateKeys created.
SignJwt
Server-only, like Better Auth’s signJWT: signs any claims with the current key.
SignJwtInput
The input of SignJwt.
TokenOutput
A signed token.
VerifyJwt
Server-only, like Better Auth’s verifyJWT: checks a token against every stored key.
VerifyJwtInput
The input of VerifyJwt.
VerifyJwtOutput
What VerifyJwt answers: the claims, or None when the token doesn’t verify.

Enums§

JwtAlgorithm
The algorithm new keys use.

Constants§

SET_AUTH_JWT
The response header /get-session carries the session’s JWT in.

Traits§

JwtExt
auth.jwt(): the server API. Only compiles on an Auth with Jwt registered.