Struct rocket_oauth2::OAuth2[][src]

pub struct OAuth2<K>(_);
Expand description

Utilities for OAuth authentication in Rocket applications.

Implementations

Create an OAuth2 fairing. The fairing will read the configuration in config_name and register itself in the application so that TokenResponse<K> can be used.

Example

use rocket::fairing::AdHoc;
use rocket_oauth2::{HyperRustlsAdapter, OAuth2, OAuthConfig};

struct GitHub;

#[rocket::launch]
fn rocket() -> _ {
    rocket::build().attach(OAuth2::<GitHub>::fairing("github"))
}

Create an OAuth2 fairing with a custom adapter and configuration.

Example

use rocket::fairing::AdHoc;
use rocket_oauth2::{HyperRustlsAdapter, OAuth2, OAuthConfig, StaticProvider};

struct MyProvider;

#[rocket::launch]
fn rocket() -> _ {
    rocket::build()
        .attach(AdHoc::on_ignite("OAuth Config", |rocket| async {
            let config = OAuthConfig::new(
                StaticProvider {
                    auth_uri: "auth uri".into(),
                    token_uri: "token uri".into(),
                },
                "client id".to_string(),
                "client secret".to_string(),
                Some("http://localhost:8000/auth".to_string()),
            );
            rocket.attach(OAuth2::<MyProvider>::custom(HyperRustlsAdapter::default(), config))
        }))
}

Prepare an authentication redirect. This sets a state cookie and returns a Redirect to the authorization endpoint.

Example

use rocket::http::CookieJar;
use rocket::response::Redirect;
use rocket_oauth2::OAuth2;

struct GitHub;

#[rocket::get("/login/github")]
fn github_login(oauth2: OAuth2<GitHub>, cookies: &CookieJar<'_>) -> Redirect {
    oauth2.get_redirect(cookies, &["user:read"]).unwrap()
}

Prepare an authentication redirect. This sets a state cookie and returns a Redirect to the authorization endpoint. Unlike get_redirect, this method accepts additional query parameters in extras; this can be used to provide or request additional information to or from providers.

Example

use rocket::http::CookieJar;
use rocket::response::Redirect;
use rocket_oauth2::OAuth2;

struct Reddit;

#[rocket::get("/login/reddit")]
fn reddit_login(oauth2: OAuth2<Reddit>, cookies: &CookieJar<'_>) -> Redirect {
    oauth2.get_redirect_extras(cookies, &["identity"], &[("duration", "permanent")]).unwrap()
}

Request a new access token given a refresh token. The refresh token must have been returned by the provider in a previous TokenResponse.

Example

use rocket_oauth2::OAuth2;

struct GitHub;

#[rocket::get("/")]
async fn index(oauth2: OAuth2<GitHub>) {
    // get previously stored refresh_token
    oauth2.refresh(refresh_token).await.unwrap();
}

Trait Implementations

Formats the value using the given formatter. Read more

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Returns true if launch should be aborted and false otherwise.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.