pub struct TokenSet<S: ValidationState = Validated> { /* private fields */ }Expand description
The set of tokens returned by a successful OAuth 2.0 authorization or refresh flow.
Obtain a TokenSet by calling crate::CliTokenClient::run_authorization_flow or
crate::CliTokenClient::refresh. Persist it with crate::TokenStore.
Serializes expires_at as a Unix timestamp (u64) or null when the provider did not
return expires_in.
§Example
use loopauth::{TokenSet, Unvalidated};
let json = r#"{
"access_token": "tok123",
"refresh_token": "ref456",
"expires_at": 9999999999,
"token_type": "Bearer",
"oidc": null,
"scopes": ["openid", "email"]
}"#;
let tokens: TokenSet<Unvalidated> = serde_json::from_str(json).unwrap();
assert_eq!(tokens.access_token().as_str(), "tok123");
assert_eq!(tokens.refresh_token().unwrap().as_str(), "ref456");
assert_eq!(tokens.token_type(), "Bearer");
assert_eq!(tokens.scopes().len(), 2);
assert!(!tokens.is_expired());Implementations§
Source§impl<S: ValidationState> TokenSet<S>
impl<S: ValidationState> TokenSet<S>
Sourcepub const fn access_token(&self) -> &AccessToken
pub const fn access_token(&self) -> &AccessToken
Returns the access token.
Sourcepub const fn refresh_token(&self) -> Option<&RefreshToken>
pub const fn refresh_token(&self) -> Option<&RefreshToken>
Returns the refresh token, if present.
Sourcepub fn token_type(&self) -> &str
pub fn token_type(&self) -> &str
Returns the token type (e.g., “Bearer”).
Sourcepub const fn expires_at(&self) -> Option<SystemTime>
pub const fn expires_at(&self) -> Option<SystemTime>
Returns the time at which the access token expires, if the provider returned one.
Sourcepub fn id_token_raw(&self) -> Option<&str>
pub fn id_token_raw(&self) -> Option<&str>
Returns the raw ID token JWT string, if present.
Sourcepub fn scopes(&self) -> &[OAuth2Scope]
pub fn scopes(&self) -> &[OAuth2Scope]
Returns the scopes granted with this token set.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Returns true if the token has expired.
Returns false when the provider did not return an expiry time.
Sourcepub fn expires_within(&self, threshold: Duration) -> bool
pub fn expires_within(&self, threshold: Duration) -> bool
Returns true if the token expires within the given threshold duration,
or if the token has already expired.
Returns false when the provider did not return an expiry time.
Source§impl TokenSet<Unvalidated>
impl TokenSet<Unvalidated>
Sourcepub fn into_validated(self) -> TokenSet<Validated>
pub fn into_validated(self) -> TokenSet<Validated>
Convert an unvalidated token set to a validated one.
Callers that deserialize a stored TokenSet from JSON will receive a
TokenSet<Unvalidated>. Call this to opt into the Validated state so
that OIDC methods such as TokenSet::oidc are available.