pub struct AuthSession {
pub id_token: String,
pub access_token: String,
pub token_type: String,
pub refresh_token: String,
pub scope: String,
pub expires: DateTime<Local>,
}Expand description
Authenticated user session containing OAuth2/OIDC tokens.
This struct holds all the token information obtained from the OAuth2 provider after successful authentication. It is stored in the cache and automatically refreshed when tokens expire.
§Fields
id_token- OpenID Connect ID token (JWT) containing user identity claimsaccess_token- OAuth2 access token for API authorizationtoken_type- Token type, typically “Bearer”refresh_token- Refresh token for obtaining new access tokensscope- Space-separated list of granted scopesexpires- Expiration timestamp for the access token
§Automatic Token Refresh
When used as an extractor in route handlers, this session is automatically refreshed if the access token has expired. The refresh process:
- Checks if
expiresis in the past - Uses
refresh_tokento request a new access token - Updates
access_token,id_token, andexpireswith fresh values - Persists the updated session to cache
§Usage as Extractor
use axum_oidc_client::auth_session::AuthSession;
async fn protected_route(session: AuthSession) -> String {
// Session is automatically refreshed if expired
format!(
"Welcome! Your session:\n\
Token Type: {}\n\
Expires: {}\n\
Scopes: {}",
session.token_type,
session.expires,
session.scope
)
}§Examples
§Accessing Token Information
use axum_oidc_client::auth_session::AuthSession;
async fn show_session(session: AuthSession) -> String {
format!("Access Token: {}", session.access_token)
}§Making Authenticated API Calls
use axum_oidc_client::auth_session::AuthSession;
use reqwest::Client;
async fn call_api(session: AuthSession) -> Result<String, Box<dyn std::error::Error>> {
let client = Client::new();
let response = client
.get("https://api.example.com/data")
.bearer_auth(&session.access_token)
.send()
.await?;
Ok(response.text().await?)
}§Checking Expiration
use axum_oidc_client::auth_session::AuthSession;
use chrono::Local;
async fn check_session(session: AuthSession) -> String {
let now = Local::now();
let is_expired = session.expires <= now;
// Note: When using the extractor, tokens are auto-refreshed
// so is_expired will typically be false
format!("Token expired: {}", is_expired)
}Fields§
§id_token: StringOpenID Connect ID token (JWT) containing user identity information
access_token: StringOAuth2 access token for authorizing API requests
token_type: StringToken type, typically “Bearer”
refresh_token: StringRefresh token for obtaining new access tokens when they expire
scope: StringSpace-separated list of OAuth2 scopes granted to this session
expires: DateTime<Local>Timestamp when the access token expires
Implementations§
Source§impl AuthSession
impl AuthSession
pub fn new(response: &AccessTokenResponse, conf: &OAuthConfiguration) -> Self
Trait Implementations§
Source§impl Clone for AuthSession
impl Clone for AuthSession
Source§fn clone(&self) -> AuthSession
fn clone(&self) -> AuthSession
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AuthSession
impl Debug for AuthSession
Source§impl<'de> Deserialize<'de> for AuthSession
impl<'de> Deserialize<'de> for AuthSession
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<S> FromRequestParts<S> for AuthSession
impl<S> FromRequestParts<S> for AuthSession
Source§impl PartialEq for AuthSession
impl PartialEq for AuthSession
Source§impl Serialize for AuthSession
impl Serialize for AuthSession
impl StructuralPartialEq for AuthSession
Auto Trait Implementations§
impl Freeze for AuthSession
impl RefUnwindSafe for AuthSession
impl Send for AuthSession
impl Sync for AuthSession
impl Unpin for AuthSession
impl UnwindSafe for AuthSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Perform the extraction.