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§

source§

impl AccessToken

source

pub fn new( token_type: &str, expires_in: i64, scope: &str, access_token: &str ) -> AccessToken

source

pub fn set_token_type(&mut self, s: &str) -> &mut AccessToken

Set the token type.

Example

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

pub fn set_expires_in(&mut self, expires_in: i64) -> &mut AccessToken

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);
source

pub fn set_scope(&mut self, s: &str) -> &mut AccessToken

Set the scope.

Example

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

pub fn set_bearer_token(&mut self, s: &str) -> &mut AccessToken

Set the access token.

Example

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

pub fn set_refresh_token(&mut self, s: &str) -> &mut AccessToken

Set the refresh token.

Example

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

pub fn set_user_id(&mut self, s: &str) -> &mut AccessToken

Set the user id.

Example

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

pub fn set_id_token(&mut self, s: &str) -> &mut AccessToken

Set the id token.

Example

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

pub fn with_id_token(&mut self, id_token: IdToken)

Set the id token.

Example

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

pub fn set_state(&mut self, s: &str) -> &mut AccessToken

Set the state.

Example

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

pub fn gen_timestamp(&mut self)

Reset the access token timestmap.

Example

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

pub fn token_type(&self) -> &str

Get the token type.

Example

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

pub fn expires_in(&self) -> i64

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());
source

pub fn scopes(&self) -> Option<&String>

Get the scopes.

Example

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

pub fn bearer_token(&self) -> &str

Get the access token.

Example

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

pub fn user_id(&self) -> Option<String>

Get the user id.

Example

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

pub fn refresh_token(&self) -> Option<String>

Get the refresh token.

Example

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

pub fn id_token(&self) -> Option<String>

Get the id token.

Example

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

pub fn state(&self) -> Option<String>

Get the state.

Example

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

pub fn timestamp(&self) -> Option<DateTime<Utc>>

Get the timestamp.

Example

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

pub fn is_expired(&self) -> bool

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());
source

pub fn elapsed(&self) -> Option<HumanTime>

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());
source

pub fn claims(&self) -> Option<Vec<Claim>>

source

pub fn jwt(&self) -> Option<&JsonWebToken>

Trait Implementations§

source§

impl AsFile for AccessToken

§

type Error = FromAsError

source§

fn as_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Self::Error>

source§

fn as_file_pretty<P: AsRef<Path>>(&self, path: P) -> Result<(), Self::Error>

source§

impl Clone for AccessToken

source§

fn clone(&self) -> AccessToken

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AccessToken

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for AccessToken

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for AccessToken

source§

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 FromFile<AccessToken> for AccessToken

§

type Error = FromAsError

source§

fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Self::Error>where for<'de> Self: Deserialize<'de>,

source§

impl PartialEq<AccessToken> for AccessToken

source§

fn eq(&self, other: &AccessToken) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for AccessToken

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&str> for AccessToken

§

type Error = GraphFailure

The type returned in the event of a conversion error.
source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<RequestBuilder> for AccessToken

§

type Error = GraphFailure

The type returned in the event of a conversion error.
source§

fn try_from(value: RequestBuilder) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Response> for AccessToken

§

type Error = GraphFailure

The type returned in the event of a conversion error.
source§

fn try_from(value: Response) -> Result<Self, Self::Error>where Self: for<'de> Deserialize<'de>,

Performs the conversion.
source§

impl TryFrom<Result<Response, Error>> for AccessToken

§

type Error = GraphFailure

The type returned in the event of a conversion error.
source§

fn try_from(value: Result<Response, Error>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for AccessToken

source§

impl StructuralEq for AccessToken

source§

impl StructuralPartialEq for AccessToken

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

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

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,