From 3e5309e1e6206b89d8919394806db3f06e8275b3 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 10 Jun 2023 14:35:03 +0200 Subject: [PATCH] feat: update scripts to use new cuddle Signed-off-by: kjuulh --- como_api/src/controllers/auth.rs | 39 +++++++++++++++++++++++++++++--- como_api/src/router.rs | 3 ++- como_gql/src/graphql.rs | 7 ++++-- scripts/deploy.sh | 12 +++++----- scripts/local_down.sh | 2 +- scripts/local_up.sh | 2 +- scripts/render_como_templates.sh | 2 +- 7 files changed, 52 insertions(+), 15 deletions(-) diff --git a/como_api/src/controllers/auth.rs b/como_api/src/controllers/auth.rs index 1d22fe8..2918687 100644 --- a/como_api/src/controllers/auth.rs +++ b/como_api/src/controllers/auth.rs @@ -1,9 +1,12 @@ +use std::borrow::Cow; + use crate::router::AppState; use crate::zitadel::{IntrospectionConfig, IntrospectionState}; use async_sqlx_session::PostgresSessionStore; use axum::extract::{FromRef, FromRequestParts, Query, State}; -use axum::headers::Cookie; +use axum::headers::authorization::Basic; +use axum::headers::{Authorization, Cookie}; use axum::http::request::Parts; use axum::http::StatusCode; use axum::http::{header::SET_COOKIE, HeaderMap}; @@ -14,11 +17,16 @@ use axum_sessions::async_session::{Session, SessionStore}; use como_domain::users::User; use como_infrastructure::register::ServiceRegister; use oauth2::basic::BasicClient; -use oauth2::TokenIntrospectionResponse; use oauth2::{reqwest::async_http_client, AuthorizationCode, CsrfToken, Scope, TokenResponse}; +use oauth2::{RedirectUrl, TokenIntrospectionResponse}; use serde::Deserialize; use zitadel::oidc::introspection::introspect; +#[derive(Debug, Deserialize)] +pub struct ZitadelAuthParams { + return_url: Option, +} + pub async fn zitadel_auth(State(client): State) -> impl IntoResponse { let (auth_url, _csrf_token) = client .authorize_url(CsrfToken::new_random) @@ -78,7 +86,7 @@ pub async fn login_authorized( let mut headers = HeaderMap::new(); headers.insert(SET_COOKIE, cookie.parse().unwrap()); - (headers, Redirect::to("/")) + (headers, Redirect::to("http://localhost:3000/dash/home")) } pub struct AuthController; @@ -103,6 +111,8 @@ pub struct UserFromSession { impl FromRequestParts for UserFromSession where PostgresSessionStore: FromRef, + BasicClient: FromRef, + IntrospectionState: FromRef, S: Send + Sync, { type Rejection = (StatusCode, &'static str); @@ -114,6 +124,29 @@ where let session_cookie = cookie.as_ref().and_then(|cookie| cookie.get(COOKIE_NAME)); if let None = session_cookie { + let introspection_state = IntrospectionState::from_ref(state); + + let basic: Option>> = parts.extract().await.unwrap(); + + if let Some(basic) = basic { + let config = IntrospectionConfig::from_ref(&introspection_state); + + let res = introspect( + &config.introspection_uri, + &config.authority, + &config.authentication, + basic.password(), + ) + .await + .unwrap(); + + return Ok(UserFromSession { + user: User { + id: res.sub().unwrap().into(), + }, + }); + } + return Err((StatusCode::UNAUTHORIZED, "No session was found")); } diff --git a/como_api/src/router.rs b/como_api/src/router.rs index b66a6d5..ec4de68 100644 --- a/como_api/src/router.rs +++ b/como_api/src/router.rs @@ -59,7 +59,8 @@ impl Api { .context("could not parse cors origin as header")?, ) .allow_headers([axum::http::header::CONTENT_TYPE]) - .allow_methods([Method::GET, Method::POST, Method::OPTIONS]), + .allow_methods([Method::GET, Method::POST, Method::OPTIONS]) + .allow_credentials(true), ), ); diff --git a/como_gql/src/graphql.rs b/como_gql/src/graphql.rs index 7abf5da..7ac35a1 100644 --- a/como_gql/src/graphql.rs +++ b/como_gql/src/graphql.rs @@ -1,5 +1,6 @@ use crate::common::*; use crate::items::{CreatedItem, Item}; +use crate::projects::Project; use async_graphql::{Context, EmptySubscription, Object, Schema}; use como_domain::item::queries::{GetItemQuery, GetItemsQuery}; use como_domain::item::requests::{CreateItemDto, UpdateItemDto}; @@ -77,15 +78,17 @@ impl QueryRoot { &self, ctx: &Context<'_>, query: GetProjectQuery, - ) -> anyhow::Result { + ) -> anyhow::Result { project_service(ctx) .get_project(get_domain_context(ctx), query) .await + .map(|p| p.into()) } - async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { + async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result> { project_service(ctx) .get_projects(get_domain_context(ctx)) .await + .map(|p| p.into_iter().map(|p| p.into()).collect()) } } diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 4981801..5db114f 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,8 +1,8 @@ export $(cat .env | xargs) -cuddle_cli x start_deployment -cuddle_cli x render_templates -cuddle_cli x render_como_templates -cuddle_cli x build_release -cuddle_cli x push_release -cuddle_cli x deploy_release +cuddle x start_deployment +cuddle x render_templates +cuddle x render_como_templates +cuddle x build_release +cuddle x push_release +cuddle x deploy_release diff --git a/scripts/local_down.sh b/scripts/local_down.sh index 37767c0..cbb90a3 100755 --- a/scripts/local_down.sh +++ b/scripts/local_down.sh @@ -2,6 +2,6 @@ set -e -cuddle_cli render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml +cuddle render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml docker compose -f $TMP/docker-compose.local_up.yml down -v diff --git a/scripts/local_up.sh b/scripts/local_up.sh index 811227d..ab4369e 100755 --- a/scripts/local_up.sh +++ b/scripts/local_up.sh @@ -2,6 +2,6 @@ set -e -cuddle_cli render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml +cuddle render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans --build diff --git a/scripts/render_como_templates.sh b/scripts/render_como_templates.sh index 55d0ea4..47073ee 100755 --- a/scripts/render_como_templates.sh +++ b/scripts/render_como_templates.sh @@ -4,7 +4,7 @@ set -e deploymentrepo="$TMP/deployments" -CUDDLE_FETCH_POLICY=never cuddle_cli render_template \ +CUDDLE_FETCH_POLICY=never cuddle render_template \ --template-file "$TMP/.env.example.tmpl" \ --dest "$deploymentrepo/$SERVICE/env.example"