OpenID Connect support for Rocket.
use oauth2::{url::Url, ClientId, ClientSecret};
use openidconnect::IssuerUrl;
use rocket::{get, launch, response::Redirect, routes, Build, Rocket};
use rocket_oidc::{OidcApplication, OidcUser};
#[get("/")]
async fn authed_user(user: OidcUser) -> String {
format!(
"Hello, {}!",
user.name().unwrap_or_else(|| "nameless user".to_string())
)
}
#[get("/", rank = 2)]
async fn user() -> Redirect {
Redirect::to("/oidc_goto_auth")
}
#[launch]
async fn rocket() -> Rocket<Build> {
let issuer_url = IssuerUrl::new("https://oidc.endpoint.here/".to_string()).unwrap();
let client_id = ClientId::new("client-id".to_string());
let client_secret = ClientSecret::new("YOUR_CLIENT_SECRET".to_string());
let oidc = OidcApplication::new(
Url::parse("http://your.application.host/").unwrap(),
issuer_url,
client_id,
client_secret,
)
.await
.unwrap();
rocket_oidc::attach(rocket::build(), oidc).mount("/", routes![authed_user, user])
}