use super::{AppState, response::*}; use std::sync::Arc; use axum::body::Body; use axum::extract::{RawQuery, State}; use axum::http::StatusCode; use axum::http::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CACHE_CONTROL, CONTENT_TYPE}; use axum::response::{IntoResponse, Response}; use serde_json::json; use crate::database::SCHEMA_VERSION; use crate::oidc::lookup; pub(super) const ISSUER_REL: &str = "http://openid.net/specs/connect/1.0/issuer"; pub(super) async fn webfinger( State(state): State>>, RawQuery(query): RawQuery, ) -> Response { let params = query_pairs(query); let Some(resource) = lookup(¶ms, "resource") else { return HttpError::bad_request("missing resource parameter").plain(); }; let account = resource.strip_prefix("acct:").unwrap_or(resource); let served = account .rsplit_once('@') .map(|(_, domain)| domain.trim_end_matches('.').to_ascii_lowercase()) .is_some_and(|domain| state.config.webfinger_domains.contains(&domain)); if !served { return HttpError(StatusCode::NOT_FOUND, "resource not served here".to_owned()).plain(); } let wanted: Vec<&str> = params .iter() .filter(|(key, _)| key == "rel") .map(|(_, value)| value.as_str()) .collect(); let links = if wanted.is_empty() || wanted.contains(&ISSUER_REL) { json!([{ "rel": ISSUER_REL, "href": state.config.issuer() }]) } else { json!([]) }; let body = json!({ "subject": resource, "links": links }); respond( StatusCode::OK, vec![ (CONTENT_TYPE, "application/jrd+json".to_owned()), (ACCESS_CONTROL_ALLOW_ORIGIN, "*".to_owned()), ], Body::from(body.to_string()), ) } pub(super) async fn health(State(state): State>>) -> Response { let schema = state.flows.schema_version(); let healthy = matches!(schema, Ok(SCHEMA_VERSION)); if !healthy { tracing::error!(?schema, expected = SCHEMA_VERSION, "health check failed"); } let status = if healthy { StatusCode::OK } else { StatusCode::SERVICE_UNAVAILABLE }; let body = json!({ "status": if healthy { "ok" } else { "unavailable" }, "version": env!("CARGO_PKG_VERSION"), "schema": schema.ok(), }); (status, [(CACHE_CONTROL, "no-store")], axum::Json(body)).into_response() } pub(super) async fn discovery(State(state): State>>) -> Response { let config = &state.config; let body = json!({ "issuer": config.issuer(), "authorization_endpoint": config.endpoint("/authorize"), "token_endpoint": config.endpoint("/token"), "userinfo_endpoint": config.endpoint("/userinfo"), "jwks_uri": config.endpoint("/.well-known/jwks.json"), "scopes_supported": ["openid", "profile", "email", "groups"], "response_types_supported": ["code"], "response_modes_supported": ["query"], "grant_types_supported": ["authorization_code"], "authorization_response_iss_parameter_supported": true, "subject_types_supported": ["public"], "id_token_signing_alg_values_supported": ["RS256", "ES256"], "claims_supported": ["sub", "aud", "exp", "iat", "iss", "name", "preferred_username", "nonce", "auth_time", "email", "email_verified", "groups"], "code_challenge_methods_supported": ["S256"], "token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"], }); axum::Json(body).into_response() } pub(super) async fn jwks(State(state): State>>) -> Response { axum::Json(state.keys.jwks()).into_response() } pub(super) async fn client_metadata(State(state): State>>) -> Response { ( [(CACHE_CONTROL, "public, max-age=3600")], axum::Json(&state.client_metadata), ) .into_response() } pub(super) async fn client_jwks(State(state): State>>) -> Response { ( [(CACHE_CONTROL, "public, max-age=300")], axum::Json(&state.client_jwks), ) .into_response() }