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
Auto Trait Implementations
impl<K> !RefUnwindSafe for OAuth2<K>
impl<K> !UnwindSafe for OAuth2<K>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V