|
| 1 | +// Copyright 2025 New Vector Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 4 | +// Please see LICENSE files in the repository root for full details. |
| 5 | + |
| 6 | +use aide::{OperationIo, transform::TransformOperation}; |
| 7 | +use axum::{Json, response::IntoResponse}; |
| 8 | +use hyper::StatusCode; |
| 9 | +use mas_axum_utils::record_error; |
| 10 | +use mas_storage::{RepositoryAccess, upstream_oauth2::UpstreamOAuthProviderRepository}; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + admin::{ |
| 14 | + call_context::CallContext, |
| 15 | + model::UpstreamOAuthProvider, |
| 16 | + params::UlidPathParam, |
| 17 | + response::{ErrorResponse, SingleResponse}, |
| 18 | + }, |
| 19 | + impl_from_error_for_route, |
| 20 | +}; |
| 21 | + |
| 22 | +#[derive(Debug, thiserror::Error, OperationIo)] |
| 23 | +#[aide(output_with = "Json<ErrorResponse>")] |
| 24 | +pub enum RouteError { |
| 25 | + #[error(transparent)] |
| 26 | + Internal(Box<dyn std::error::Error + Send + Sync + 'static>), |
| 27 | + |
| 28 | + #[error("Provider not found")] |
| 29 | + NotFound, |
| 30 | +} |
| 31 | + |
| 32 | +impl_from_error_for_route!(mas_storage::RepositoryError); |
| 33 | + |
| 34 | +impl IntoResponse for RouteError { |
| 35 | + fn into_response(self) -> axum::response::Response { |
| 36 | + let error = ErrorResponse::from_error(&self); |
| 37 | + let sentry_event_id = record_error!(self, Self::Internal(_)); |
| 38 | + let status = match self { |
| 39 | + Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 40 | + Self::NotFound => StatusCode::NOT_FOUND, |
| 41 | + }; |
| 42 | + |
| 43 | + (status, sentry_event_id, Json(error)).into_response() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub fn doc(operation: TransformOperation) -> TransformOperation { |
| 48 | + operation |
| 49 | + .id("getUpstreamOAuthProvider") |
| 50 | + .summary("Get upstream OAuth provider") |
| 51 | + .tag("upstream-oauth-provider") |
| 52 | + .response_with::<200, Json<SingleResponse<UpstreamOAuthProvider>>, _>(|t| { |
| 53 | + let [sample, ..] = UpstreamOAuthProvider::samples(); |
| 54 | + t.description("The upstream OAuth provider") |
| 55 | + .example(SingleResponse::new_canonical(sample)) |
| 56 | + }) |
| 57 | + .response_with::<404, Json<ErrorResponse>, _>(|t| t.description("Provider not found")) |
| 58 | +} |
| 59 | + |
| 60 | +#[tracing::instrument(name = "handler.admin.v1.upstream_oauth_providers.get", skip_all)] |
| 61 | +pub async fn handler( |
| 62 | + CallContext { mut repo, .. }: CallContext, |
| 63 | + id: UlidPathParam, |
| 64 | +) -> Result<Json<SingleResponse<UpstreamOAuthProvider>>, RouteError> { |
| 65 | + let provider = repo |
| 66 | + .upstream_oauth_provider() |
| 67 | + .lookup(*id) |
| 68 | + .await? |
| 69 | + .ok_or(RouteError::NotFound)?; |
| 70 | + |
| 71 | + Ok(Json(SingleResponse::new_canonical( |
| 72 | + UpstreamOAuthProvider::from(provider), |
| 73 | + ))) |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use hyper::{Request, StatusCode}; |
| 79 | + use mas_data_model::{ |
| 80 | + UpstreamOAuthProvider, UpstreamOAuthProviderClaimsImports, |
| 81 | + UpstreamOAuthProviderDiscoveryMode, UpstreamOAuthProviderOnBackchannelLogout, |
| 82 | + UpstreamOAuthProviderPkceMode, UpstreamOAuthProviderTokenAuthMethod, |
| 83 | + }; |
| 84 | + use mas_iana::jose::JsonWebSignatureAlg; |
| 85 | + use mas_storage::{ |
| 86 | + RepositoryAccess, |
| 87 | + upstream_oauth2::{UpstreamOAuthProviderParams, UpstreamOAuthProviderRepository}, |
| 88 | + }; |
| 89 | + use oauth2_types::scope::{OPENID, Scope}; |
| 90 | + use sqlx::PgPool; |
| 91 | + use ulid::Ulid; |
| 92 | + |
| 93 | + use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup}; |
| 94 | + |
| 95 | + async fn create_test_provider(state: &mut TestState) -> UpstreamOAuthProvider { |
| 96 | + let mut repo = state.repository().await.unwrap(); |
| 97 | + |
| 98 | + let params = UpstreamOAuthProviderParams { |
| 99 | + issuer: Some("https://accounts.google.com".to_owned()), |
| 100 | + human_name: Some("Google".to_owned()), |
| 101 | + brand_name: Some("google".to_owned()), |
| 102 | + discovery_mode: UpstreamOAuthProviderDiscoveryMode::Oidc, |
| 103 | + pkce_mode: UpstreamOAuthProviderPkceMode::Auto, |
| 104 | + jwks_uri_override: None, |
| 105 | + authorization_endpoint_override: None, |
| 106 | + token_endpoint_override: None, |
| 107 | + userinfo_endpoint_override: None, |
| 108 | + fetch_userinfo: true, |
| 109 | + userinfo_signed_response_alg: None, |
| 110 | + client_id: "google-client-id".to_owned(), |
| 111 | + encrypted_client_secret: Some("encrypted-secret".to_owned()), |
| 112 | + token_endpoint_signing_alg: None, |
| 113 | + token_endpoint_auth_method: UpstreamOAuthProviderTokenAuthMethod::ClientSecretPost, |
| 114 | + id_token_signed_response_alg: JsonWebSignatureAlg::Rs256, |
| 115 | + response_mode: None, |
| 116 | + scope: Scope::from_iter([OPENID]), |
| 117 | + claims_imports: UpstreamOAuthProviderClaimsImports::default(), |
| 118 | + additional_authorization_parameters: vec![], |
| 119 | + forward_login_hint: false, |
| 120 | + on_backchannel_logout: UpstreamOAuthProviderOnBackchannelLogout::DoNothing, |
| 121 | + ui_order: 0, |
| 122 | + }; |
| 123 | + |
| 124 | + let provider = repo |
| 125 | + .upstream_oauth_provider() |
| 126 | + .add(&mut state.rng(), &state.clock, params) |
| 127 | + .await |
| 128 | + .unwrap(); |
| 129 | + |
| 130 | + Box::new(repo).save().await.unwrap(); |
| 131 | + |
| 132 | + provider |
| 133 | + } |
| 134 | + |
| 135 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 136 | + async fn test_get_provider(pool: PgPool) { |
| 137 | + setup(); |
| 138 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 139 | + let admin_token = state.token_with_scope("urn:mas:admin").await; |
| 140 | + let provider = create_test_provider(&mut state).await; |
| 141 | + |
| 142 | + let request = Request::get(format!( |
| 143 | + "/api/admin/v1/upstream-oauth-providers/{}", |
| 144 | + provider.id |
| 145 | + )) |
| 146 | + .bearer(&admin_token) |
| 147 | + .empty(); |
| 148 | + |
| 149 | + let response = state.request(request).await; |
| 150 | + response.assert_status(StatusCode::OK); |
| 151 | + let body: serde_json::Value = response.json::<serde_json::Value>(); |
| 152 | + |
| 153 | + assert_eq!(body["data"]["type"], "upstream-oauth-provider"); |
| 154 | + assert_eq!(body["data"]["id"], provider.id.to_string()); |
| 155 | + assert_eq!(body["data"]["attributes"]["human_name"], "Google"); |
| 156 | + |
| 157 | + insta::assert_json_snapshot!(body, @r###" |
| 158 | + { |
| 159 | + "data": { |
| 160 | + "type": "upstream-oauth-provider", |
| 161 | + "id": "01FSHN9AG0MZAA6S4AF7CTV32E", |
| 162 | + "attributes": { |
| 163 | + "issuer": "https://accounts.google.com", |
| 164 | + "human_name": "Google", |
| 165 | + "brand_name": "google", |
| 166 | + "created_at": "2022-01-16T14:40:00Z", |
| 167 | + "disabled_at": null |
| 168 | + }, |
| 169 | + "links": { |
| 170 | + "self": "/api/admin/v1/upstream-oauth-providers/01FSHN9AG0MZAA6S4AF7CTV32E" |
| 171 | + } |
| 172 | + }, |
| 173 | + "links": { |
| 174 | + "self": "/api/admin/v1/upstream-oauth-providers/01FSHN9AG0MZAA6S4AF7CTV32E" |
| 175 | + } |
| 176 | + } |
| 177 | + "###); |
| 178 | + } |
| 179 | + |
| 180 | + #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")] |
| 181 | + async fn test_not_found(pool: PgPool) { |
| 182 | + setup(); |
| 183 | + let mut state = TestState::from_pool(pool).await.unwrap(); |
| 184 | + let admin_token = state.token_with_scope("urn:mas:admin").await; |
| 185 | + |
| 186 | + let provider_id = Ulid::nil(); |
| 187 | + let request = Request::get(format!( |
| 188 | + "/api/admin/v1/upstream-oauth-providers/{provider_id}" |
| 189 | + )) |
| 190 | + .bearer(&admin_token) |
| 191 | + .empty(); |
| 192 | + |
| 193 | + let response = state.request(request).await; |
| 194 | + response.assert_status(StatusCode::NOT_FOUND); |
| 195 | + } |
| 196 | +} |
0 commit comments