Struct graph_oauth::Token
source · pub struct Token {Show 17 fields
pub access_token: String,
pub token_type: String,
pub expires_in: i64,
pub ext_expires_in: Option<i64>,
pub scope: Vec<String>,
pub refresh_token: Option<String>,
pub user_id: Option<String>,
pub id_token: Option<IdToken>,
pub state: Option<String>,
pub session_state: Option<String>,
pub nonce: Option<String>,
pub correlation_id: Option<String>,
pub client_info: Option<String>,
pub timestamp: Option<OffsetDateTime>,
pub expires_on: Option<OffsetDateTime>,
pub additional_fields: HashMap<String, Value>,
pub log_pii: bool,
}
Expand description
An access token is a security token issued by an authorization server as part of an OAuth 2.0 flow. It contains information about the user and the resource for which the token is intended. The information can be used to access web APIs and other protected resources. Resources validate access tokens to grant access to a client application. For more information, see Access tokens in the Microsoft Identity Platform
For more info from the specification see Successful Response
Create a new AccessToken.
§Example
let token_response = Token::new("Bearer", 3600, "ASODFIUJ34KJ;LADSK", vec!["User.Read"]);
The Token::decode method parses the id token into a JWT and returns it. Calling
Token::decode when the Token’s id_token
field is None returns an error result.
For more info see:
Microsoft identity platform access tokens
Fields§
§access_token: String
Access tokens are credentials used to access protected resources. An access token is a string representing an authorization issued to the client. The string is usually opaque to the client. Tokens represent specific scopes and durations of access, granted by the resource owner, and enforced by the resource server and authorization server.
See Access Token in the specification
token_type: String
§expires_in: i64
§ext_expires_in: Option<i64>
Legacy version of expires_in
scope: Vec<String>
§refresh_token: Option<String>
Refresh tokens are credentials used to obtain access tokens. Refresh tokens are issued to the client by the authorization server and are used to obtain a new access token when the current access token becomes invalid or expires, or to obtain additional access tokens with identical or narrower scope (access tokens may have a shorter lifetime and fewer permissions than authorized by the resource owner). Issuing a refresh token is optional at the discretion of the authorization server. If the authorization server issues a refresh token, it is included when issuing an access token
See Refresh Token in the specification
Because access tokens are valid for only a short period of time, authorization servers sometimes issue a refresh token at the same time the access token is issued. The client application can then exchange this refresh token for a new access token when needed. For more information, see Refresh tokens in the Microsoft identity platform.
user_id: Option<String>
§id_token: Option<IdToken>
§state: Option<String>
§session_state: Option<String>
§nonce: Option<String>
§correlation_id: Option<String>
§client_info: Option<String>
§timestamp: Option<OffsetDateTime>
§expires_on: Option<OffsetDateTime>
§additional_fields: HashMap<String, Value>
Any extra returned fields for AccessToken.
log_pii: bool
Implementations§
source§impl Token
impl Token
pub fn new<T: ToString, I: IntoIterator<Item = T>>( token_type: &str, expires_in: i64, access_token: &str, scope: I ) -> Token
sourcepub fn with_token_type(&mut self, s: &str) -> &mut Self
pub fn with_token_type(&mut self, s: &str) -> &mut Self
Set the token type.
§Example
let mut access_token = Token::default();
access_token.with_token_type("Bearer");
sourcepub fn with_expires_in(&mut self, expires_in: i64) -> &mut Self
pub fn with_expires_in(&mut self, expires_in: i64) -> &mut Self
Set the expies in time. This should usually be done in seconds.
§Example
let mut access_token = Token::default();
access_token.with_expires_in(3600);
sourcepub fn with_scope<T: ToString, I: IntoIterator<Item = T>>(
&mut self,
scope: I
) -> &mut Self
pub fn with_scope<T: ToString, I: IntoIterator<Item = T>>( &mut self, scope: I ) -> &mut Self
Set the scope.
§Example
let mut access_token = Token::default();
access_token.with_scope(vec!["User.Read"]);
sourcepub fn with_access_token(&mut self, s: &str) -> &mut Self
pub fn with_access_token(&mut self, s: &str) -> &mut Self
Set the access token.
§Example
let mut access_token = Token::default();
access_token.with_access_token("ASODFIUJ34KJ;LADSK");
sourcepub fn with_refresh_token(&mut self, s: &str) -> &mut Self
pub fn with_refresh_token(&mut self, s: &str) -> &mut Self
Set the refresh token.
§Example
let mut access_token = Token::default();
access_token.with_refresh_token("#ASOD323U5342");
sourcepub fn with_user_id(&mut self, s: &str) -> &mut Self
pub fn with_user_id(&mut self, s: &str) -> &mut Self
Set the user id.
§Example
let mut access_token = Token::default();
access_token.with_user_id("user_id");
sourcepub fn set_id_token(&mut self, s: &str) -> &mut Self
pub fn set_id_token(&mut self, s: &str) -> &mut Self
Set the id token.
§Example
let mut access_token = Token::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 = Token::default();
access_token.with_id_token(IdToken::new("id_token", "code", "state", "session_state"));
sourcepub fn with_state(&mut self, s: &str) -> &mut Self
pub fn with_state(&mut self, s: &str) -> &mut Self
sourcepub fn enable_pii_logging(&mut self, log_pii: bool)
pub fn enable_pii_logging(&mut self, log_pii: bool)
Enable or disable logging of personally identifiable information such as logging the id_token. This is disabled by default. When log_pii is enabled passing Token to logging or print functions will log both the bearer access token value, the refresh token value if any, and the id token value. By default these do not get logged.
sourcepub fn gen_timestamp(&mut self)
pub fn gen_timestamp(&mut self)
Timestamp field is used to tell whether the access token is expired. This method is mainly used internally as soon as the access token is deserialized from the api response for an accurate reading on when the access token expires.
You most likely do not want to use this method unless you are deserializing the access token using custom deserialization or creating your own access tokens manually.
This method resets the access token timestamp based on the expires_in field which is the total seconds that the access token is valid for starting from when the token was first retrieved.
This will reset the the timestamp from Utc Now + expires_in. This means that if calling Token::gen_timestamp will only be reliable if done when the access token is first retrieved.
§Example
let mut access_token = Token::default();
access_token.expires_in = 86999;
access_token.gen_timestamp();
println!("{:#?}", access_token.timestamp);
sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Check whether the access token is expired. Checks if expires_on timestamp is less than UTC now timestamp.
§Example
let mut access_token = Token::default();
println!("{:#?}", access_token.is_expired());
sourcepub fn is_expired_sub(&self, duration: Duration) -> bool
pub fn is_expired_sub(&self, duration: Duration) -> bool
Check whether the access token is expired sub duration. This is useful in scenarios where you want to eagerly refresh the access token before it expires to prevent a failed request.
§Example
let mut access_token = Token::default();
println!("{:#?}", access_token.is_expired_sub(time::Duration::minutes(5)));
sourcepub fn elapsed(&self) -> Option<Duration>
pub fn elapsed(&self) -> Option<Duration>
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 = Token::default();
println!("{:#?}", access_token.elapsed());
pub fn decode_header(&self) -> Result<Header>
Trait Implementations§
source§impl<'de> Deserialize<'de> for Token
impl<'de> Deserialize<'de> for Token
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 PartialEq for Token
impl PartialEq for Token
source§impl TryFrom<AuthorizationResponse> for Token
impl TryFrom<AuthorizationResponse> for Token
§type Error = AuthorizationFailure
type Error = AuthorizationFailure
source§impl TryFrom<RequestBuilder> for Token
impl TryFrom<RequestBuilder> for Token
§type Error = GraphFailure
type Error = GraphFailure
impl Eq for Token
impl StructuralPartialEq for Token
Auto Trait Implementations§
impl Freeze for Token
impl RefUnwindSafe for Token
impl Send for Token
impl Sync for Token
impl Unpin for Token
impl UnwindSafe for Token
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.