pub struct AccessToken { /* private fields */ }
Expand description

OAuth 2.0 Access Token

Create a new AccessToken.

Example

let access_token = AccessToken::new("Bearer", 3600, "Read Read.Write", "ASODFIUJ34KJ;LADSK");

You can also get the claims using the claims() method as well as the remaining duration that the access token is valid using the elapsed() method.

Tokens returned for personal microsoft accounts that use legacy MSA are encrypted and cannot be parsed. This bearer token may still be valid but the jwt() method will return None. For more info see: Microsoft identity platform acccess tokens

For tokens where the JWT can be parsed the elapsed() method uses the exp field in the JWT’s claims. If the claims do not contain an exp field or the token could not be parsed the elapsed() method uses the expires_in field returned in the response body to caculate the remaining time. These fields are only used once during initialization to set a timestamp for future expiration of the access token.

Example


// Claims
println!("{:#?}", access_token.claims());

// Duration left until expired.
println!("{:#?}", access_token.elapsed());

Implementations

Set the token type.

Example

let mut access_token = AccessToken::default();
access_token.set_token_type("Bearer");

Set the expies in time. This should usually be done in seconds.

Example

let mut access_token = AccessToken::default();
access_token.set_expires_in(3600);

Set the scope.

Example

let mut access_token = AccessToken::default();
access_token.set_scope("Read Read.Write");

Set the access token.

Example

let mut access_token = AccessToken::default();
access_token.set_bearer_token("ASODFIUJ34KJ;LADSK");

Set the refresh token.

Example

let mut access_token = AccessToken::default();
access_token.set_refresh_token("#ASOD323U5342");

Set the user id.

Example

let mut access_token = AccessToken::default();
access_token.set_user_id("user_id");

Set the id token.

Example

let mut access_token = AccessToken::default();
access_token.set_id_token("id_token");

Set the id token.

Example

let mut access_token = AccessToken::default();
access_token.with_id_token(IdToken::new("id_token", "code", "state", "session_state"));

Set the state.

Example

let mut access_token = AccessToken::default();
access_token.set_state("state");

Reset the access token timestmap.

Example

let mut access_token = AccessToken::default();
access_token.timestamp();
// The timestamp is in UTC.

Get the token type.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.token_type());

Set the user id.

Example

let mut access_token = AccessToken::default();
// This is the original amount that was set not the difference.
// To get the difference you can use access_token.elapsed().
println!("{:#?}", access_token.expires_in());

Get the scopes.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.scopes());

Get the access token.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.bearer_token());

Get the user id.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.user_id());

Get the refresh token.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.refresh_token());

Get the id token.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.id_token());

Get the state.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.state());

Get the timestamp.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.timestamp());

Check whether the access token is expired. An access token is considerd expired when there is a negative difference between the timestamp set for the access token and the expires_in field.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.is_expired());

Get the time left in seconds until the access token expires. See the HumanTime crate. If you just need to know if the access token is expired then use the is_expired() message which returns a boolean true for the token has expired and false otherwise.

Example

let mut access_token = AccessToken::default();
println!("{:#?}", access_token.elapsed());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more