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, ValidationError<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, ValidationError<<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_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.

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?;
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, ValidationError<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 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 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> 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> 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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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> 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