[][src]Struct rocket_oauth2::TokenResponse

pub struct TokenResponse<K> { /* fields omitted */ }

The server's response to a successful token exchange, defined in in RFC 6749 §5.1.

TokenResponse<K> implements FromRequest, and is used in the callback route to complete the token exchange. Since TokenResponse accesses Cookies, it must be positioned before Cookies in routes. For more information, see the rocket.rs guide.

Example

use rocket_oauth2::TokenResponse;

// Bad! This will fail at runtime:
// "Error: Multiple `Cookies` instances are active at once."
#[get("/auth")]
fn auth_callback(mut cookies: Cookies<'_>, token: TokenResponse<Auth>) -> Redirect {
     // ...
}
use rocket_oauth2::TokenResponse;

// Good. TokenResponse will access and then release Cookies,
// and then both TokenResponse and Cookies will be given to the route.
#[get("/auth")]
fn auth_callback(token: TokenResponse<Auth>, mut cookies: Cookies<'_>) -> Redirect {
     // ...
}

Implementations

impl<K> TokenResponse<K>[src]

pub fn cast<L>(self) -> TokenResponse<L>[src]

Reinterpret this TokenResponse as if it were keyed by L instead. This function can be used to treat disparate TokenResponses as a single concrete type such as TokenResponse<()> to avoid an explosion of generic bounds.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

fn use_nongeneric_token(token: TokenResponse<()>) {
    // ...
}

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    use_nongeneric_token(token.cast());
}

pub fn as_value(&self) -> &Value[src]

Get the TokenResponse data as a raw JSON Value. It is guaranteed to be of type Object.

Example

use rocket_oauth2::TokenResponse;

struct MyProvider;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<MyProvider>) {
    let custom_data = token.as_value().get("custom_data").unwrap().as_str();
}

pub fn access_token(&self) -> &str[src]

Get the access token issued by the authorization server.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    let access_token = token.access_token();
}

pub fn token_type(&self) -> &str[src]

Get the type of token, described in RFC 6749 §7.1.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    let token_type = token.token_type();
}

pub fn expires_in(&self) -> Option<i64>[src]

Get the lifetime in seconds of the access token, if the authorization server provided one.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    if let Some(expires_in) = token.expires_in() {
        println!("Token expires in {} seconds", expires_in);
    }
}

pub fn refresh_token(&self) -> Option<&str>[src]

Get the refresh token, if the server provided one.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    if let Some(refresh_token) = token.refresh_token() {
        println!("Got a refresh token! '{}'", refresh_token);
    }
}

pub fn scope(&self) -> Option<&str>[src]

Get the (space-separated) list of scopes associated with the access token. The authorization server is required to provide this if it differs from the requested set of scopes.

If scope was not provided by the server as a string, this method will return None. For those providers, use `.as_value().get("scope") instead.

Example

use rocket_oauth2::TokenResponse;

struct GitHub;

#[rocket::get("/login/github")]
fn login_github(token: TokenResponse<GitHub>) {
    if let Some(scope) = token.scope() {
        println!("Token scope: '{}'", scope);
    }
}

Trait Implementations

impl<K: Clone> Clone for TokenResponse<K>[src]

impl<K: Debug> Debug for TokenResponse<K>[src]

impl<'a, 'r, K: 'static> FromRequest<'a, 'r> for TokenResponse<K>[src]

type Error = Error

The associated error to be returned if derivation fails.

fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>[src]

Handle the redirect callback, delegating to the Adapter to perform the token exchange.

impl<K: PartialEq> PartialEq<TokenResponse<K>> for TokenResponse<K>[src]

impl<K> StructuralPartialEq for TokenResponse<K>[src]

impl TryFrom<Value> for TokenResponse<()>[src]

type Error = Error

The type returned in the event of a conversion error.

fn try_from(data: Value) -> Result<Self, Error>[src]

Construct a TokenResponse from a Value.

Returns an Error if data is not a JSON Object, or the access_token or token_type is missing or not a string.

Auto Trait Implementations

impl<K> RefUnwindSafe for TokenResponse<K>

impl<K> Send for TokenResponse<K>

impl<K> Sync for TokenResponse<K>

impl<K> Unpin for TokenResponse<K>

impl<K> UnwindSafe for TokenResponse<K>

Blanket Implementations

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

impl<T, I> AsResult<T, I> for T where
    I: Input, 

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

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

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

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

impl<T> IntoCollection<T> for T

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.

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.

impl<T> Typeable for T where
    T: Any

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