mas_handlers/oauth2/
webfinger.rs

1// Copyright 2024, 2025 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5// Please see LICENSE files in the repository root for full details.
6
7use axum::{Json, extract::State, response::IntoResponse};
8use axum_extra::{extract::Query, typed_header::TypedHeader};
9use headers::ContentType;
10use mas_router::UrlBuilder;
11use oauth2_types::webfinger::WebFingerResponse;
12use serde::Deserialize;
13
14#[derive(Deserialize)]
15pub(crate) struct Params {
16    resource: String,
17
18    // TODO: handle multiple rel=
19    #[serde(default)]
20    rel: Option<String>,
21}
22
23fn jrd() -> mime::Mime {
24    "application/jrd+json".parse().unwrap()
25}
26
27#[tracing::instrument(name = "handlers.oauth2.webfinger.get", skip_all)]
28pub(crate) async fn get(
29    Query(params): Query<Params>,
30    State(url_builder): State<UrlBuilder>,
31) -> impl IntoResponse {
32    // TODO: should we validate the subject?
33    let subject = params.resource;
34
35    let wants_issuer = params
36        .rel
37        .iter()
38        .any(|i| i == "http://openid.net/specs/connect/1.0/issuer");
39
40    let res = if wants_issuer {
41        WebFingerResponse::new(subject).with_issuer(url_builder.oidc_issuer())
42    } else {
43        WebFingerResponse::new(subject)
44    };
45
46    (TypedHeader(ContentType::from(jrd())), Json(res))
47}