Struct graph_oauth::oauth::AccessToken
source · [−]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
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
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
type Error = GraphFailure
type Error = GraphFailure
The type returned in the event of a conversion error.
Performs the conversion.
type Error = GraphFailure
type Error = GraphFailure
The type returned in the event of a conversion error.
Auto Trait Implementations
impl RefUnwindSafe for AccessToken
impl Send for AccessToken
impl Sync for AccessToken
impl Unpin for AccessToken
impl UnwindSafe for AccessToken
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.
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