//! `GET` and `POST /userinfo` (OpenID Connect Core section 5.3): the claims of the person a user access //! token was issued for. //! //! The token is verified against this provider's own published keys: signature, `iss`, `aud` (this //! provider), `exp` and `iat`, and the `openid` scope. The token is then checked against the session it //! names: a session that ended, or a disabled account, refuses the token at once, even before it expires. use http::header::{AUTHORIZATION, HeaderValue, WWW_AUTHENTICATE}; use im_core::jwt; use im_core::time::unix_now; use serde_json::{Value, json}; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::request::headers; use topcoat::router::response::Response; use topcoat::router::{StatusCode, route}; use super::signing; use super::token::person_claims; use super::{USERINFO_AUDIENCE, issuer, json_response}; use crate::state::AppState; /// `GET /userinfo` and `POST /userinfo`, both with a Bearer token. #[route([GET, POST] "/userinfo")] async fn userinfo(cx: &Cx) -> Result { let app = app_context::(cx); let Some(token) = bearer(headers(cx).get(AUTHORIZATION)) else { return Ok(unauthorized()); }; let Some(claims) = signing::verify_access(&app.store, token).await? else { return Ok(unauthorized()); }; let now = unix_now(); if jwt::check_claims(&claims, issuer(app), USERINFO_AUDIENCE, now).is_err() { return Ok(unauthorized()); } let scope = claims .get("scope") .and_then(Value::as_str) .unwrap_or_default(); if !scope.split_whitespace().any(|word| word == "openid") { return Ok(unauthorized()); } let (Some(sub), Some(sid)) = ( claims.get("sub").and_then(Value::as_str), claims.get("sid").and_then(Value::as_str), ) else { return Ok(unauthorized()); }; let Some(session) = app.store.live_session(sid).await? else { return Ok(unauthorized()); }; let Some(user) = app .store .user_by_id(sub) .await? .filter(|u| !u.is_disabled()) else { return Ok(unauthorized()); }; if session.user_id != user.id { return Ok(unauthorized()); } // A token outlives nothing that its client loses: a disabled client's tokens are refused at once. let Some(client_id) = claims.get("azp").and_then(Value::as_str) else { return Ok(unauthorized()); }; if !app .store .client_by_id(client_id) .await? .is_some_and(|client| !client.is_disabled()) { return Ok(unauthorized()); } let mut body = person_claims(&user, scope); body.insert("sub".into(), json!(user.id)); Ok(json_response(StatusCode::OK, &Value::Object(body))) } /// The token of a `Bearer` authorization header, or `None`. pub(crate) fn bearer(value: Option<&HeaderValue>) -> Option<&str> { let (scheme, token) = value?.to_str().ok()?.split_once(' ')?; (scheme.eq_ignore_ascii_case("bearer") && !token.is_empty()).then_some(token) } /// `401 invalid_token` with the RFC 6750 challenge. One answer for every reason. fn unauthorized() -> Response { let mut response = json_response(StatusCode::UNAUTHORIZED, &json!({"error": "invalid_token"})); response.headers_mut().insert( WWW_AUTHENTICATE, HeaderValue::from_static("Bearer error=\"invalid_token\""), ); response }