Expand description
§wabot-addon-auth-jwt
JWT signing and the guard middleware that verifies them. Port of
wabot-ts/src/addon/auth/jwt (JwtConfig, JwtSigner,
JwtGuardMiddleware, @jwtGuard).
ⓘ
c.register_instance::<JwtConfig>(Arc::new(JwtConfig::from_env()?));
register_singletons!(&c, JwtSigner, JwtGuard);
// controllers that read Auth must be transient:
register_transients!(&c, AccountController);ⓘ
#[middleware(JwtGuard)]
#[get("/me")]
async fn me(&self) -> RestResult<Profile> {
let claims: Claims = self.auth.require_as()?;
…
}§Audiences and cookies
Every session is signed with the same secret, so a token minted for
one kind of user would otherwise work on another’s endpoints. The
aud claim is what separates them, and jwt_guard! declares a
guard that requires one:
ⓘ
jwt_guard!(AdminGuard, audience = "admin", cookie = "admin_session");Separate cookie names let sessions of different user types coexist in one browser without overwriting each other.
Re-exports§
pub use refresh::register_refresh_token_repository;pub use refresh::InMemoryRefreshTokenRepository;pub use refresh::Jwt;pub use refresh::RefreshError;pub use refresh::RefreshToken;pub use refresh::RefreshTokenData;pub use refresh::RefreshTokenRepository;pub use refresh::Session;pub use refresh::SessionOptions;
Modules§
- env_
names - Env var names, matching the TS config.
- refresh
- Refresh tokens and sessions.
Refresh tokens — long-lived sessions that outlive an access token.
Port of
JwtRefreshToken/JwtRefreshTokenRepository/Jwt.
Macros§
- jwt_
guard - Declare a guard with an audience and/or its own cookie names.
Structs§
- JwtConfig
- Secret, algorithm and lifetimes. Register one before the guard.
- JwtGuard
- The default guard: any valid token, from the
Authorizationheader orJwtConfig::cookie_name. - JwtGuard
Options - Per-endpoint guard settings. TS analogue:
IJwtGuardOptions. - JwtSigner
- Signs access tokens from a set of claims.
- JwtToken
- A signed token and when it expires. TS analogue:
JwtTokenDto. - JwtVerifier
- Verifies tokens and fills the request’s
Auth. Stateless and shared — it writes into the request’s container, which the middleware receives as an argument. - Sign
Options - Options for one signing call.
Enums§
Functions§
- cookie_
value - Read one cookie out of the request’s
cookiefields.