pub struct UserToken {
pub access_token: AccessToken,
pub login: UserName,
pub user_id: UserId,
pub refresh_token: Option<RefreshToken>,
pub never_expiring: bool,
/* private fields */
}
Expand description
An User Token from the OAuth implicit code flow or OAuth authorization code flow
Used for requests that need an authenticated user. See also AppAccessToken
See UserToken::builder
for authenticating the user using the OAuth authorization code flow
.
Fields§
§access_token: AccessToken
The access token used to authenticate requests with
login: UserName
Username of user associated with this token
user_id: UserId
User ID of the user associated with this token
refresh_token: Option<RefreshToken>
The refresh token used to extend the life of this user token
never_expiring: bool
Token will never expire
This is only true for old client IDs, like https://twitchapps.com/tmi and others
Implementations§
Source§impl UserToken
impl UserToken
Sourcepub fn new(
access_token: AccessToken,
refresh_token: Option<RefreshToken>,
validated: ValidatedToken,
client_secret: impl Into<Option<ClientSecret>>,
) -> Result<UserToken, ValidationError<Infallible>>
pub fn new( access_token: AccessToken, refresh_token: Option<RefreshToken>, validated: ValidatedToken, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, ValidationError<Infallible>>
Create a new token
See UserToken::from_token
and UserToken::from_existing
for more ways to create a UserToken
Sourcepub async fn from_token<C>(
http_client: &C,
access_token: AccessToken,
) -> Result<UserToken, ValidationError<<C as Client>::Error>>where
C: Client,
Available on crate feature client
only.
pub async fn from_token<C>(
http_client: &C,
access_token: AccessToken,
) -> Result<UserToken, ValidationError<<C as Client>::Error>>where
C: Client,
client
only.Create a UserToken from an existing active user token. Retrieves login
, client_id
and scopes
If the token is already expired, this function will fail to produce a UserToken
and return ValidationError::NotAuthorized
§Examples
use twitch_oauth2::{AccessToken, UserToken};
// Make sure you enable the feature "reqwest" for twitch_oauth2 if you want to use reqwest
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let token = UserToken::from_token(&client, AccessToken::from("my_access_token")).await?;
Sourcepub async fn from_existing<C>(
http_client: &C,
access_token: AccessToken,
refresh_token: impl Into<Option<RefreshToken>>,
client_secret: impl Into<Option<ClientSecret>>,
) -> Result<UserToken, ValidationError<<C as Client>::Error>>where
C: Client,
Available on crate feature client
only.
pub async fn from_existing<C>(
http_client: &C,
access_token: AccessToken,
refresh_token: impl Into<Option<RefreshToken>>,
client_secret: impl Into<Option<ClientSecret>>,
) -> Result<UserToken, ValidationError<<C as Client>::Error>>where
C: Client,
client
only.Create a UserToken from an existing active user token. Retrieves login
, client_id
and scopes
If the token is already expired, this function will fail to produce a UserToken
and return ValidationError::NotAuthorized
§Examples
use twitch_oauth2::{AccessToken, ClientSecret, RefreshToken, UserToken};
// Make sure you enable the feature "reqwest" for twitch_oauth2 if you want to use reqwest
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let token = UserToken::from_existing(
&client,
AccessToken::from("my_access_token"),
RefreshToken::from("my_refresh_token"),
ClientSecret::from("my_client_secret"),
)
.await?;
Sourcepub fn from_existing_unchecked(
access_token: impl Into<AccessToken>,
refresh_token: impl Into<Option<RefreshToken>>,
client_id: impl Into<ClientId>,
client_secret: impl Into<Option<ClientSecret>>,
login: UserName,
user_id: UserId,
scopes: Option<Vec<Scope>>,
expires_in: Option<Duration>,
) -> UserToken
pub fn from_existing_unchecked( access_token: impl Into<AccessToken>, refresh_token: impl Into<Option<RefreshToken>>, client_id: impl Into<ClientId>, client_secret: impl Into<Option<ClientSecret>>, login: UserName, user_id: UserId, scopes: Option<Vec<Scope>>, expires_in: Option<Duration>, ) -> UserToken
Assemble token without checks.
§Notes
If expires_in
is None
, we’ll assume token.is_elapsed
is always false
Sourcepub fn from_response(
response: TwitchTokenResponse,
validated: ValidatedToken,
client_secret: impl Into<Option<ClientSecret>>,
) -> Result<UserToken, ValidationError<Infallible>>
pub fn from_response( response: TwitchTokenResponse, validated: ValidatedToken, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, ValidationError<Infallible>>
Assemble token from twitch responses.
Sourcepub fn builder(
client_id: ClientId,
client_secret: ClientSecret,
redirect_url: Url,
) -> UserTokenBuilder
pub fn builder( client_id: ClientId, client_secret: ClientSecret, redirect_url: Url, ) -> UserTokenBuilder
Create a UserTokenBuilder
to get a token with the OAuth Authorization Code
Sourcepub async fn mock_token<C>(
http_client: &C,
client_id: ClientId,
client_secret: ClientSecret,
user_id: impl AsRef<str>,
scopes: Vec<Scope>,
) -> Result<UserToken, UserTokenExchangeError<<C as Client>::Error>>where
C: Client,
Available on crate features mock_api
and client
only.
pub async fn mock_token<C>(
http_client: &C,
client_id: ClientId,
client_secret: ClientSecret,
user_id: impl AsRef<str>,
scopes: Vec<Scope>,
) -> Result<UserToken, UserTokenExchangeError<<C as Client>::Error>>where
C: Client,
mock_api
and client
only.Sourcepub fn set_secret(&mut self, secret: Option<ClientSecret>)
pub fn set_secret(&mut self, secret: Option<ClientSecret>)
Set the client secret
Trait Implementations§
Source§impl TwitchToken for UserToken
impl TwitchToken for UserToken
Source§fn token_type() -> BearerTokenType
fn token_type() -> BearerTokenType
Source§fn client_id(&self) -> &ClientId
fn client_id(&self) -> &ClientId
Source§fn token(&self) -> &AccessToken
fn token(&self) -> &AccessToken
Source§fn refresh_token<'a, 'life0, 'async_trait, C>(
&'life0 mut self,
http_client: &'a C,
) -> Pin<Box<dyn Future<Output = Result<(), RefreshTokenError<<C as Client>::Error>>> + Send + 'async_trait>>
fn refresh_token<'a, 'life0, 'async_trait, C>( &'life0 mut self, http_client: &'a C, ) -> Pin<Box<dyn Future<Output = Result<(), RefreshTokenError<<C as Client>::Error>>> + Send + 'async_trait>>
client
only.Source§fn expires_in(&self) -> Duration
fn expires_in(&self) -> Duration
Source§fn is_elapsed(&self) -> bool
fn is_elapsed(&self) -> bool
Source§fn validate_token<'a, 'life0, 'async_trait, C>(
&'life0 self,
http_client: &'a C,
) -> Pin<Box<dyn Future<Output = Result<ValidatedToken, ValidationError<<C as Client>::Error>>> + Send + 'async_trait>>
fn validate_token<'a, 'life0, 'async_trait, C>( &'life0 self, http_client: &'a C, ) -> Pin<Box<dyn Future<Output = Result<ValidatedToken, ValidationError<<C as Client>::Error>>> + Send + 'async_trait>>
client
only.Source§fn revoke_token<'a, 'async_trait, C>(
self,
http_client: &'a C,
) -> Pin<Box<dyn Future<Output = Result<(), RevokeTokenError<<C as Client>::Error>>> + Send + 'async_trait>>
fn revoke_token<'a, 'async_trait, C>( self, http_client: &'a C, ) -> Pin<Box<dyn Future<Output = Result<(), RevokeTokenError<<C as Client>::Error>>> + Send + 'async_trait>>
client
only.