rocket_oauth2

rocket_oauth2 helps set up OAuth 2.0 sign-in or authorization 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, Cookies, SameSite}; use rocket::Request; use rocket::response::Redirect; use rocket_oauth2::{OAuth2, TokenResponse};

struct GitHub;

[get("/login/github")]

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

[get("/auth/github")]

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

fn main() { rocket::ignite() .mount("/", routes![githubcallback, githublogin]) .attach(OAuth2::::fairing("github")) .launch(); } ```

Configuration (Rocket.toml)

toml [global.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: