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§
source§impl AccessToken
impl AccessToken
pub fn new( token_type: &str, expires_in: i64, scope: &str, access_token: &str ) -> AccessToken
sourcepub fn set_token_type(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn set_expires_in(&mut self, expires_in: i64) -> &mut AccessToken
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);
sourcepub fn set_scope(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn set_bearer_token(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn set_refresh_token(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn set_user_id(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn set_id_token(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn with_id_token(&mut self, id_token: IdToken)
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"));
sourcepub fn set_state(&mut self, s: &str) -> &mut AccessToken
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");
sourcepub fn gen_timestamp(&mut self)
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.
sourcepub fn token_type(&self) -> &str
pub fn token_type(&self) -> &str
Get the token type.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.token_type());
sourcepub fn expires_in(&self) -> i64
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());
sourcepub fn scopes(&self) -> Option<&String>
pub fn scopes(&self) -> Option<&String>
Get the scopes.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.scopes());
sourcepub fn bearer_token(&self) -> &str
pub fn bearer_token(&self) -> &str
Get the access token.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.bearer_token());
sourcepub fn user_id(&self) -> Option<String>
pub fn user_id(&self) -> Option<String>
Get the user id.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.user_id());
sourcepub fn refresh_token(&self) -> Option<String>
pub fn refresh_token(&self) -> Option<String>
Get the refresh token.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.refresh_token());
sourcepub fn id_token(&self) -> Option<String>
pub fn id_token(&self) -> Option<String>
Get the id token.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.id_token());
sourcepub fn state(&self) -> Option<String>
pub fn state(&self) -> Option<String>
Get the state.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.state());
sourcepub fn timestamp(&self) -> Option<DateTime<Utc>>
pub fn timestamp(&self) -> Option<DateTime<Utc>>
Get the timestamp.
Example
let mut access_token = AccessToken::default();
println!("{:#?}", access_token.timestamp());
sourcepub fn is_expired(&self) -> bool
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());
sourcepub fn elapsed(&self) -> Option<HumanTime>
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());
pub fn claims(&self) -> Option<Vec<Claim>>
pub fn jwt(&self) -> Option<&JsonWebToken>
Trait Implementations§
source§impl AsFile for AccessToken
impl AsFile for AccessToken
source§impl Clone for AccessToken
impl Clone for AccessToken
source§fn clone(&self) -> AccessToken
fn clone(&self) -> AccessToken
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for AccessToken
impl Debug for AccessToken
source§impl Default for AccessToken
impl Default for AccessToken
source§impl<'de> Deserialize<'de> for AccessToken
impl<'de> Deserialize<'de> for AccessToken
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>,
source§impl FromFile<AccessToken> for AccessToken
impl FromFile<AccessToken> for AccessToken
source§impl PartialEq<AccessToken> for AccessToken
impl PartialEq<AccessToken> for AccessToken
source§fn eq(&self, other: &AccessToken) -> bool
fn eq(&self, other: &AccessToken) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for AccessToken
impl Serialize for AccessToken
source§impl TryFrom<&str> for AccessToken
impl TryFrom<&str> for AccessToken
source§impl TryFrom<RequestBuilder> for AccessToken
impl TryFrom<RequestBuilder> for AccessToken
§type Error = GraphFailure
type Error = GraphFailure
source§impl TryFrom<Response> for AccessToken
impl TryFrom<Response> for AccessToken
§type Error = GraphFailure
type Error = GraphFailure
impl Eq for AccessToken
impl StructuralEq for AccessToken
impl StructuralPartialEq for AccessToken
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§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.