From 5d4b3dde5836f546bd11f168bdfc52d9bd187324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Wed, 15 Jan 2025 10:06:30 +0100 Subject: [PATCH] Shield Axum: Add subprovider and user routes (#18) --- .../integrations/shield-axum/src/error.rs | 2 +- .../integrations/shield-axum/src/router.rs | 4 ++- .../integrations/shield-axum/src/routes.rs | 4 +++ .../shield-axum/src/routes/subproviders.rs | 10 +++++++ .../shield-axum/src/routes/user.rs | 26 +++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/integrations/shield-axum/src/routes/subproviders.rs create mode 100644 packages/integrations/shield-axum/src/routes/user.rs diff --git a/packages/integrations/shield-axum/src/error.rs b/packages/integrations/shield-axum/src/error.rs index 56d74aa..950ce0a 100644 --- a/packages/integrations/shield-axum/src/error.rs +++ b/packages/integrations/shield-axum/src/error.rs @@ -8,7 +8,7 @@ pub struct RouteError(ShieldError); impl IntoResponse for RouteError { fn into_response(self) -> Response { - (StatusCode::INTERNAL_SERVER_ERROR, self.0.to_string()).into_response() + (StatusCode::INTERNAL_SERVER_ERROR, format!("{:?}", self.0)).into_response() } } diff --git a/packages/integrations/shield-axum/src/router.rs b/packages/integrations/shield-axum/src/router.rs index 9b952bd..8f24868 100644 --- a/packages/integrations/shield-axum/src/router.rs +++ b/packages/integrations/shield-axum/src/router.rs @@ -4,10 +4,11 @@ use axum::{ }; use shield::User; -use crate::routes::{sign_in, sign_in_callback, sign_out}; +use crate::routes::{sign_in, sign_in_callback, sign_out, subproviders, user}; pub fn auth_router() -> Router { Router::new() + .route("/subproviders", get(subproviders::)) .route("/sign-in/:provider_id", post(sign_in::)) .route("/sign-in/:provider_id/:subprovider_id", post(sign_in::)) .route("/sign-in/callback/:provider_id", get(sign_in_callback::)) @@ -16,4 +17,5 @@ pub fn auth_router( get(sign_in_callback::), ) .route("/sign-out", post(sign_out::)) + .route("/user", get(user::)) } diff --git a/packages/integrations/shield-axum/src/routes.rs b/packages/integrations/shield-axum/src/routes.rs index 47d587d..8391d6e 100644 --- a/packages/integrations/shield-axum/src/routes.rs +++ b/packages/integrations/shield-axum/src/routes.rs @@ -1,7 +1,11 @@ mod sign_in; mod sign_in_callback; mod sign_out; +mod subproviders; +mod user; pub use sign_in::*; pub use sign_in_callback::*; pub use sign_out::*; +pub use subproviders::*; +pub use user::*; diff --git a/packages/integrations/shield-axum/src/routes/subproviders.rs b/packages/integrations/shield-axum/src/routes/subproviders.rs new file mode 100644 index 0000000..be8eadb --- /dev/null +++ b/packages/integrations/shield-axum/src/routes/subproviders.rs @@ -0,0 +1,10 @@ +use axum::Json; +use shield::{SubproviderVisualisation, User}; + +use crate::{error::RouteError, extract::ExtractShield}; + +pub async fn subproviders( + ExtractShield(shield): ExtractShield, +) -> Result>, RouteError> { + Ok(Json(shield.subprovider_visualisations().await?)) +} diff --git a/packages/integrations/shield-axum/src/routes/user.rs b/packages/integrations/shield-axum/src/routes/user.rs new file mode 100644 index 0000000..dbd7b96 --- /dev/null +++ b/packages/integrations/shield-axum/src/routes/user.rs @@ -0,0 +1,26 @@ +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, + Json, +}; +use serde_json::json; +use shield::User; + +use crate::extract::ExtractUser; + +pub async fn user(ExtractUser(user): ExtractUser) -> Response { + // TODO: Send JSON error using some util. + match user { + Some(user) => { + // TODO: Include email addresses. + // let email_addresses = user.email_addresses().await; + + Json(json!({ + "id": user.id(), + "name": user.name(), + })) + .into_response() + } + None => (StatusCode::UNAUTHORIZED, "Unauthorized").into_response(), + } +}