UserToken

Struct UserToken 

Source
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

Source

pub fn new( access_token: AccessToken, refresh_token: Option<RefreshToken>, validated: ValidatedToken, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, CreationError<Infallible>>

Create a new token

See UserToken::from_token and UserToken::from_existing for more ways to create a UserToken

Source

pub async fn from_token<C>( http_client: &C, access_token: AccessToken, ) -> Result<UserToken, CreationError<<C as Client>::Error>>
where C: Client,

Available on crate feature 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?;
Source

pub async fn from_refresh_token<C>( http_client: &C, refresh_token: RefreshToken, client_id: ClientId, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, RetrieveTokenError<<C as Client>::Error>>
where C: Client,

Available on crate feature client only.

Creates a UserToken using a refresh token. Retrieves the login and scopes.

If an active user token is associated with the provided refresh token, this function will invalidate that existing user token.

Source

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, CreationError<<C as Client>::Error>>
where C: Client,

Available on crate feature client only.

Create a UserToken from an existing active user token. Retrieves login and scopes

If the token is already expired, this function will fail to produce a UserToken and return ValidationError::NotAuthorized. If you have a refresh token, you can use UserToken::from_refresh_token to refresh the token if was expired.

Consider using UserToken::from_existing_or_refresh_token to automatically refresh the token if it is expired.

§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?;
Source

pub async fn from_existing_or_refresh_token<C>( http_client: &C, access_token: AccessToken, refresh_token: RefreshToken, client_id: ClientId, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, RetrieveTokenError<<C as Client>::Error>>
where C: Client,

Available on crate feature client only.

Create a UserToken from an existing active user token or refresh token if the access token is expired. Retrieves login, client_id and scopes.

§Examples
use twitch_oauth2::{AccessToken, ClientId, 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_or_refresh_token(
    &client,
    AccessToken::from("my_access_token"),
    RefreshToken::from("my_refresh_token"),
    ClientId::from("my_client_id"),
    ClientSecret::from("my_optional_client_secret"),
).await?;
Source

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

Source

pub fn from_response( response: TwitchTokenResponse, validated: ValidatedToken, client_secret: impl Into<Option<ClientSecret>>, ) -> Result<UserToken, CreationError<Infallible>>

Assemble token from twitch responses.

Source

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

Source

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,

Available on crate features mock_api and client only.

Generate a user token from mock-api

§Examples
let token = twitch_oauth2::UserToken::mock_token(
    &reqwest::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .build()?,
    "mockclientid".into(),
    "mockclientsecret".into(),
    "user_id",
    vec![],
)
.await?;
Source

pub fn set_secret(&mut self, secret: Option<ClientSecret>)

Set the client secret

Trait Implementations§

Source§

impl Clone for UserToken

Source§

fn clone(&self) -> UserToken

Returns a duplicate 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 UserToken

Source§

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

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

impl TwitchToken for UserToken

Source§

fn token_type() -> BearerTokenType

Get the type of token.
Source§

fn client_id(&self) -> &ClientId

Client ID associated with the token. Twitch requires this in all helix API calls
Source§

fn token(&self) -> &AccessToken

Get the AccessToken for authenticating Read more
Source§

fn login(&self) -> Option<&UserNameRef>

Get the username associated to this token
Source§

fn user_id(&self) -> Option<&UserIdRef>

Get the user id associated to this token
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>>
where Self: Sized + 'async_trait, C: Client + 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,

Available on crate feature client only.
Refresh this token, changing the token to a newer one
Source§

fn expires_in(&self) -> Duration

Get current lifetime of token.
Source§

fn scopes(&self) -> &[Scope]

Retrieve scopes attached to the token
Source§

fn is_elapsed(&self) -> bool

Returns whether or not the token is expired. Read more
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>>
where Self: Sized + Sync + 'async_trait, C: Client + 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,

Available on crate feature client only.
Validate this token. Should be checked on regularly, according to https://dev.twitch.tv/docs/authentication/validate-tokens/ Read more
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>>
where Self: Sized + Send + 'async_trait, C: Client + 'async_trait, 'a: 'async_trait,

Available on crate feature client only.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T