mas_handlers/oauth2/
webfinger.rs

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