14 releases

0.5.0 Nov 22, 2023
0.5.0-rc.2 Apr 18, 2023
0.5.0-rc.1 Jun 9, 2021
0.4.1 Sep 23, 2020
0.0.4 Nov 1, 2018

#157 in Authentication

Download history 125/week @ 2023-12-25 1/week @ 2024-01-01 70/week @ 2024-01-15 61/week @ 2024-01-22 79/week @ 2024-01-29 25/week @ 2024-02-05 83/week @ 2024-02-12 26/week @ 2024-02-19 105/week @ 2024-02-26 71/week @ 2024-03-04 77/week @ 2024-03-11 96/week @ 2024-03-18 38/week @ 2024-03-25 104/week @ 2024-04-01 4/week @ 2024-04-08

248 downloads per month

MIT/Apache

51KB
657 lines

rocket_oauth2

maintenance: passive crates.io docs.rs

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

Major Versions

  • 0.4.x, compatible with rocket 0.4.x, are based on the previous master branch
  • 0.5.x, compatible with rocket 0.5.x, are based on the newer main branch

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)
            .build()
    );
    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

~18–54MB
~1M SLoC