Struct twitch_oauth2_auth_flow::TwitchAuthHook[][src]

pub struct TwitchAuthHook<C> { /* fields omitted */ }
Expand description

Token generator using OAuth authorization code flow

Spins up a small webserver that listens for a response from the user after they are redirected to the redirect URL by twitch.

See [auth_flow] for a more integrated way of using this.

Make a new TwitchAuthHook and call generate_url() and make the user navigate to the url. Retrieve the token with receive_auth_token.

Example

use twitch_oauth2_auth_flow::TwitchAuthHook;
use twitch_oauth2::{TwitchToken, UserToken, Scope};
use url::Url;

let mut hook = TwitchAuthHook::new(
    twitch_oauth2::client::surf_http_client,  // or twitch_oauth2::client::reqwest_http_client
    "my_client_id".to_string(),
    "my_client_secret".to_string(),
    vec![Scope::ChatRead, Scope::ChatEdit, Scope::ChannelModerate, Scope::ModerationRead],
    Url::parse("http://localhost:8081/twitch/token")?,
)?;

let (url, _) = hook.generate_url();
give_url_to_user(url);
let token = hook.receive_auth_token();

Implementations

impl<RE, C, F> TwitchAuthHook<C> where
    RE: Error + Send + Sync + 'static,
    C: Copy + FnOnce(HttpRequest) -> F,
    F: Future<Output = Result<HttpResponse, RE>>, 
[src]

pub fn new(
    client: C,
    client_id: String,
    client_secret: String,
    scopes: impl Into<Option<Vec<Scope>>>,
    redirect_url: Url
) -> Result<TwitchAuthHook<C>, HookError<RE>>
[src]

Construct a new TwitchAuthHook

pub fn generate_url(&mut self) -> (Url, CsrfToken)[src]

Generate the url and csrf token associated with this TwitchAuthHook

pub fn set_port(&mut self, port: u16)[src]

Override the implicit port for the server as given by the redirect url.

Useful if the application is behind a reverse-proxy

pub async fn receive_auth_token(self) -> Result<UserToken, HookError<RE>>[src]

Spin up a server to retrieve the token from the user.

This token will only be valid for around 4 hours, but you can refresh the token with UserToken::refresh_token

Auto Trait Implementations

impl<C> RefUnwindSafe for TwitchAuthHook<C> where
    C: RefUnwindSafe

impl<C> Send for TwitchAuthHook<C> where
    C: Send

impl<C> Sync for TwitchAuthHook<C> where
    C: Sync

impl<C> Unpin for TwitchAuthHook<C> where
    C: Unpin

impl<C> UnwindSafe for TwitchAuthHook<C> where
    C: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V

impl<T> WithSubscriber for T[src]

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
    S: Into<Dispatch>, 
[src]

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

fn with_current_subscriber(self) -> WithDispatch<Self>[src]

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more