Skip to main content

hyprstream_core/services/oauth/
userinfo.rs

1//! OpenID Connect UserInfo endpoint (OIDC Core Section 5.3).
2//!
3//! Returns claims about the authenticated user based on the access token's
4//! scopes: `openid` → sub, `profile` → name, `email` → email.
5//!
6//! Authentication is enforced by the `require_bearer_token` middleware layer
7//! applied to the protected route group. By the time this handler runs, the
8//! token has already been validated and the caller's identity is in extensions.
9
10use std::sync::Arc;
11
12use axum::extract::{Extension, State};
13use axum::http::StatusCode;
14use axum::response::{IntoResponse, Response};
15use axum::Json;
16
17use super::auth::AuthenticatedUser;
18use super::state::OAuthState;
19
20/// GET/POST /oauth/userinfo
21pub async fn userinfo(
22    State(state): State<Arc<OAuthState>>,
23    Extension(caller): Extension<AuthenticatedUser>,
24) -> Response {
25    let Some(user_store) = state.user_store_reader() else {
26        tracing::error!("UserInfo unavailable: relational user store is not configured");
27        return (
28            StatusCode::SERVICE_UNAVAILABLE,
29            Json(serde_json::json!({"error": "temporarily_unavailable"})),
30        )
31            .into_response();
32    };
33    let profile = match user_store.get_profile(&caller.user).await {
34        Ok(Some(profile)) => profile,
35        Ok(None) => {
36            tracing::error!(username = %caller.user, "UserInfo subject has no credential profile");
37            return (
38                StatusCode::INTERNAL_SERVER_ERROR,
39                Json(serde_json::json!({"error": "server_error"})),
40            )
41                .into_response();
42        }
43        Err(error) => {
44            tracing::error!(username = %caller.user, %error, "UserInfo credential lookup failed");
45            return (
46                StatusCode::SERVICE_UNAVAILABLE,
47                Json(serde_json::json!({"error": "temporarily_unavailable"})),
48            )
49                .into_response();
50        }
51    };
52    let Some(stable_sub) = profile.sub else {
53        tracing::error!(username = %caller.user, "UserInfo profile has no stable subject");
54        return (
55            StatusCode::INTERNAL_SERVER_ERROR,
56            Json(serde_json::json!({"error": "server_error"})),
57        )
58            .into_response();
59    };
60
61    let mut response = serde_json::json!({
62        "sub": stable_sub,
63        "preferred_username": caller.user,
64    });
65    if let Some(name) = profile.name {
66        response["name"] = serde_json::Value::String(name);
67    }
68    if let Some(email) = profile.email {
69        response["email"] = serde_json::Value::String(email);
70    }
71    if let Some(verified) = profile.email_verified {
72        response["email_verified"] = serde_json::Value::Bool(verified);
73    }
74
75    (StatusCode::OK, Json(response)).into_response()
76}