|
| 1 | +// Copyright 2024 The Matrix.org Foundation C.I.C. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use aide::{ |
| 16 | + axum::ApiRouter, |
| 17 | + openapi::{OAuth2Flow, OAuth2Flows, OpenApi, SecurityScheme, Server, ServerVariable}, |
| 18 | +}; |
| 19 | +use axum::{Json, Router}; |
| 20 | +use hyper::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}; |
| 21 | +use indexmap::IndexMap; |
| 22 | +use mas_http::CorsLayerExt; |
| 23 | +use mas_router::{OAuth2AuthorizationEndpoint, OAuth2TokenEndpoint, SimpleRoute}; |
| 24 | +use tower_http::cors::{Any, CorsLayer}; |
| 25 | + |
| 26 | +pub fn router<S>() -> (OpenApi, Router<S>) |
| 27 | +where |
| 28 | + S: Clone + Send + Sync + 'static, |
| 29 | +{ |
| 30 | + let mut api = OpenApi::default(); |
| 31 | + let router = ApiRouter::<S>::new() |
| 32 | + // TODO: add routes |
| 33 | + .finish_api_with(&mut api, |t| { |
| 34 | + t.title("Matrix Authentication Service admin API") |
| 35 | + .security_scheme( |
| 36 | + "oauth2", |
| 37 | + SecurityScheme::OAuth2 { |
| 38 | + flows: OAuth2Flows { |
| 39 | + client_credentials: Some(OAuth2Flow::ClientCredentials { |
| 40 | + refresh_url: Some(OAuth2TokenEndpoint::PATH.to_owned()), |
| 41 | + token_url: OAuth2TokenEndpoint::PATH.to_owned(), |
| 42 | + scopes: IndexMap::from([( |
| 43 | + "urn:mas:admin".to_owned(), |
| 44 | + "Grant access to the admin API".to_owned(), |
| 45 | + )]), |
| 46 | + }), |
| 47 | + authorization_code: Some(OAuth2Flow::AuthorizationCode { |
| 48 | + authorization_url: OAuth2AuthorizationEndpoint::PATH.to_owned(), |
| 49 | + refresh_url: Some(OAuth2TokenEndpoint::PATH.to_owned()), |
| 50 | + token_url: OAuth2TokenEndpoint::PATH.to_owned(), |
| 51 | + scopes: IndexMap::from([( |
| 52 | + "urn:mas:admin".to_owned(), |
| 53 | + "Grant access to the admin API".to_owned(), |
| 54 | + )]), |
| 55 | + }), |
| 56 | + implicit: None, |
| 57 | + password: None, |
| 58 | + }, |
| 59 | + description: None, |
| 60 | + extensions: IndexMap::default(), |
| 61 | + }, |
| 62 | + ) |
| 63 | + .security_requirement_scopes("oauth2", ["urn:mas:admin"]) |
| 64 | + .server(Server { |
| 65 | + url: "{base}".to_owned(), |
| 66 | + variables: IndexMap::from([( |
| 67 | + "base".to_owned(), |
| 68 | + ServerVariable { |
| 69 | + default: "/".to_owned(), |
| 70 | + ..ServerVariable::default() |
| 71 | + }, |
| 72 | + )]), |
| 73 | + ..Server::default() |
| 74 | + }) |
| 75 | + }); |
| 76 | + |
| 77 | + let router = router |
| 78 | + // Serve the OpenAPI spec as JSON |
| 79 | + .route( |
| 80 | + "/api/spec.json", |
| 81 | + axum::routing::get({ |
| 82 | + let res = Json(api.clone()); |
| 83 | + move || std::future::ready(res.clone()) |
| 84 | + }), |
| 85 | + ) |
| 86 | + .layer( |
| 87 | + CorsLayer::new() |
| 88 | + .allow_origin(Any) |
| 89 | + .allow_methods(Any) |
| 90 | + .allow_otel_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]), |
| 91 | + ); |
| 92 | + |
| 93 | + (api, router) |
| 94 | +} |
0 commit comments