pub struct JwtSession { /* private fields */ }Expand description
Request-scoped JWT session manager.
JwtSession is an axum FromRequest extractor that captures the
JwtSessionService from router state and pre-reads any tokens it needs
(including the body when refresh_source = Body { field }).
Handlers use it to call rotate or
logout without manually fishing tokens out of the
request.
§Trade-off
Because this extractor may consume the request body (when the refresh
source is Body { field }), handlers that also need a typed body extractor
(e.g., a login handler that parses LoginReq) cannot combine
JwtSession with another body extractor. Those handlers should inject
State<JwtSessionService> directly instead.
§Example
async fn refresh(jwt: JwtSession) -> Result<Json<TokenPair>> {
Ok(Json(jwt.rotate().await?))
}
async fn logout(jwt: JwtSession) -> Result<StatusCode> {
jwt.logout().await?;
Ok(StatusCode::NO_CONTENT)
}Implementations§
Source§impl JwtSession
impl JwtSession
Sourcepub fn current(&self) -> Option<&Session>
pub fn current(&self) -> Option<&Session>
Returns the Session injected by JwtLayer, if present.
Sourcepub async fn authenticate(
&self,
user_id: &str,
meta: &SessionMeta,
) -> Result<TokenPair>
pub async fn authenticate( &self, user_id: &str, meta: &SessionMeta, ) -> Result<TokenPair>
Authenticate a user and issue a new TokenPair.
Delegates directly to JwtSessionService::authenticate.
Sourcepub async fn rotate(&self) -> Result<TokenPair>
pub async fn rotate(&self) -> Result<TokenPair>
Rotate the refresh token and return a fresh TokenPair.
Finds the refresh token according to refresh_source in the config.
Sourcepub async fn logout(&self) -> Result<()>
pub async fn logout(&self) -> Result<()>
Revoke the session associated with the current access token.
Finds the access token according to access_source in the config.
Sourcepub async fn list(&self, user_id: &str) -> Result<Vec<Session>>
pub async fn list(&self, user_id: &str) -> Result<Vec<Session>>
List all active sessions for the given user.
Sourcepub async fn revoke(&self, user_id: &str, id: &str) -> Result<()>
pub async fn revoke(&self, user_id: &str, id: &str) -> Result<()>
Revoke a specific session by its ULID identifier.
Sourcepub async fn revoke_all(&self, user_id: &str) -> Result<()>
pub async fn revoke_all(&self, user_id: &str) -> Result<()>
Revoke all sessions for the given user.