|
| 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::{transform::TransformOperation, OperationIo}; |
| 16 | +use axum::{response::IntoResponse, Json}; |
| 17 | +use hyper::StatusCode; |
| 18 | +use ulid::Ulid; |
| 19 | + |
| 20 | +use crate::{ |
| 21 | + admin::{ |
| 22 | + call_context::CallContext, |
| 23 | + model::OAuth2Session, |
| 24 | + params::UlidPathParam, |
| 25 | + response::{ErrorResponse, SingleResponse}, |
| 26 | + }, |
| 27 | + impl_from_error_for_route, |
| 28 | +}; |
| 29 | + |
| 30 | +#[derive(Debug, thiserror::Error, OperationIo)] |
| 31 | +#[aide(output_with = "Json<ErrorResponse>")] |
| 32 | +pub enum RouteError { |
| 33 | + #[error(transparent)] |
| 34 | + Internal(Box<dyn std::error::Error + Send + Sync + 'static>), |
| 35 | + |
| 36 | + #[error("OAuth 2.0 session ID {0} not found")] |
| 37 | + NotFound(Ulid), |
| 38 | +} |
| 39 | + |
| 40 | +impl_from_error_for_route!(mas_storage::RepositoryError); |
| 41 | + |
| 42 | +impl IntoResponse for RouteError { |
| 43 | + fn into_response(self) -> axum::response::Response { |
| 44 | + let error = ErrorResponse::from_error(&self); |
| 45 | + let status = match self { |
| 46 | + Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 47 | + Self::NotFound(_) => StatusCode::NOT_FOUND, |
| 48 | + }; |
| 49 | + (status, Json(error)).into_response() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub fn doc(operation: TransformOperation) -> TransformOperation { |
| 54 | + operation |
| 55 | + .id("getOAuth2Session") |
| 56 | + .summary("Get an OAuth 2.0 session") |
| 57 | + .tag("oauth2-session") |
| 58 | + .response_with::<200, Json<SingleResponse<OAuth2Session>>, _>(|t| { |
| 59 | + let [sample, ..] = OAuth2Session::samples(); |
| 60 | + let response = SingleResponse::new_canonical(sample); |
| 61 | + t.description("OAuth 2.0 session was found") |
| 62 | + .example(response) |
| 63 | + }) |
| 64 | + .response_with::<404, RouteError, _>(|t| { |
| 65 | + let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil())); |
| 66 | + t.description("OAuth 2.0 session was not found") |
| 67 | + .example(response) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +#[tracing::instrument(name = "handler.admin.v1.oauth2_session.get", skip_all, err)] |
| 72 | +pub async fn handler( |
| 73 | + CallContext { mut repo, .. }: CallContext, |
| 74 | + id: UlidPathParam, |
| 75 | +) -> Result<Json<SingleResponse<OAuth2Session>>, RouteError> { |
| 76 | + let session = repo |
| 77 | + .oauth2_session() |
| 78 | + .lookup(*id) |
| 79 | + .await? |
| 80 | + .ok_or(RouteError::NotFound(*id))?; |
| 81 | + |
| 82 | + Ok(Json(SingleResponse::new_canonical(OAuth2Session::from( |
| 83 | + session, |
| 84 | + )))) |
| 85 | +} |
0 commit comments