#rocket #oauth2

rocket_oauth2

OAuth2 for Rocket applications

11 unstable releases

0.5.0-rc.1 Jun 9, 2021
0.4.1 Sep 23, 2020
0.4.0 Aug 26, 2020
0.3.1 Jul 19, 2020
0.0.4 Nov 1, 2018

#73 in Authentication

Download history 102/week @ 2021-02-26 226/week @ 2021-03-05 188/week @ 2021-03-12 247/week @ 2021-03-19 176/week @ 2021-03-26 158/week @ 2021-04-02 161/week @ 2021-04-09 154/week @ 2021-04-16 112/week @ 2021-04-23 106/week @ 2021-04-30 119/week @ 2021-05-07 179/week @ 2021-05-14 140/week @ 2021-05-21 67/week @ 2021-05-28 99/week @ 2021-06-04 160/week @ 2021-06-11

620 downloads per month

MIT/Apache

46KB
631 lines

rocket_oauth2

crates.io docs.rs

rocket_oauth2 helps set up an OAuth 2.0 client in Rocket applications.

Quickstart Example

For more detailed examples and explanations, see the crate documentation and the projects in the repository's examples directory.

Code

use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::Request;
use rocket::response::Redirect;
use rocket_oauth2::{OAuth2, TokenResponse};

struct GitHub;

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

#[get("/auth/github")]
fn github_callback(token: TokenResponse<GitHub>, cookies: &CookieJar<'_>) -> Redirect
{
    cookies.add_private(
        Cookie::build("token", token.access_token().to_string())
            .same_site(SameSite::Lax)
            .finish()
    );
    Redirect::to("/")
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![github_callback, github_login])
        .attach(OAuth2::<GitHub>::fairing("github"))
}

Configuration (Rocket.toml)

[default.oauth.github]
provider = "GitHub"
client_id = "..."
client_secret = "..."
redirect_uri = "http://localhost:8000/auth/github"

License

rocket_oauth2 is licensed under either of the following, at your option:

Dependencies

~9–12MB
~262K SLoC