feat: update scripts to use new cuddle

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-06-10 14:35:03 +02:00
parent 88c7acd439
commit 3e5309e1e6
7 changed files with 52 additions and 15 deletions

View File

@@ -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<String>,
}
pub async fn zitadel_auth(State(client): State<BasicClient>) -> 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<S> FromRequestParts<S> for UserFromSession
where
PostgresSessionStore: FromRef<S>,
BasicClient: FromRef<S>,
IntrospectionState: FromRef<S>,
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<TypedHeader<Authorization<Basic>>> = 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"));
}

View File

@@ -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),
),
);