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

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

struct GitHub;

[get("/login/github")]

fn githublogin(oauth2: OAuth2, cookies: &CookieJar<'>) -> Redirect { oauth2.get_redirect(cookies, &["user:read"]).unwrap() }

[get("/auth/github")]

fn githubcallback(token: TokenResponse, cookies: &CookieJar<'>) -> Redirect { cookies.addprivate( Cookie::build("token", token.accesstoken().tostring()) .samesite(SameSite::Lax) .finish() ); Redirect::to("/") }

[launch]

fn rocket() -> _ { rocket::build() .mount("/", routes![githubcallback, githublogin]) .attach(OAuth2::::fairing("github")) } ```

Configuration (Rocket.toml)

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: