| Crates.io | rocket-oidc |
| lib.rs | rocket-oidc |
| version | 0.1.3 |
| created_at | 2025-05-17 05:46:12.358267+00 |
| updated_at | 2025-07-13 00:43:49.018719+00 |
| description | Rocket OpenID Connect utility crate, provides redirect route, and claims guard for OIDC |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1677673 |
| size | 150,482 |
This crate provides OIDC authentication for rocket, and the routes needed to accomplish this goal.
This is a simple utility crate that provides a FromRequest implementation, including fetching user data.
use serde_derive::{Serialize, Deserialize};
use rocket::{catch, catchers, routes, launch, get};
use rocket::State;
use rocket::fs::FileServer;
use rocket::response::{Redirect, content::RawHtml};
use rocket_oidc::{OIDCConfig, CoreClaims, OIDCGuard};
#[non_exhaustive]
#[derive(Serialize, Deserialize, Debug)]
pub struct UserGuard {
pub email: String,
pub sub: String,
pub picture: Option<String>,
pub email_verified: Option<bool>,
}
impl CoreClaims for UserGuard {
fn subject(&self) -> &str {
self.sub.as_str()
}
}
pub type Guard = OIDCGuard<UserGuard>;
#[catch(401)]
fn unauthorized() -> Redirect {
Redirect::to("/")
}
#[get("/")]
async fn index() -> RawHtml<String> {
RawHtml(format!("<h1>Hello World</h1>"))
}
#[get("/protected")]
async fn protected(guard: Guard) -> RawHtml<String> {
let userinfo = guard.userinfo;
RawHtml(format!("<h1>Hello {} {}</h1>", userinfo.given_name(), userinfo.family_name()))
}
#[launch]
async fn rocket() -> _ {
let mut rocket = rocket::build()
.mount("/", routes![index, use_api])
.register("/", catchers![unauthorized]);
rocket_oidc::setup(rocket, OIDCConfig::from_env().unwrap())
.await
.unwrap()
}
export ISSUER_URL="https://keycloak.com/realms/master"
export CLIENT_ID="my_app_client_id"
export CLIENT_SECRET="/path/to/client/secret"
export REDIRECT_URI="http://callback_url.com/"
This crate is not audited, and is very much as work in progress, as such its security cannot be garunteed.