feat: with persistent session state

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-05-28 15:34:36 +02:00
parent 1e38b2838c
commit 746fb68684
9 changed files with 661 additions and 232 deletions

View File

@@ -24,6 +24,7 @@ sqlx.workspace = true
anyhow.workspace = true
dotenv.workspace = true
tracing.workspace = true
async-sqlx-session.workspace = true
zitadel = { version = "3.3.1", features = ["axum"] }
tower = "0.4.13"

View File

@@ -1,6 +1,7 @@
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::http::request::Parts;
@@ -9,7 +10,7 @@ use axum::http::{header::SET_COOKIE, HeaderMap};
use axum::response::{IntoResponse, Redirect};
use axum::routing::get;
use axum::{async_trait, RequestPartsExt, Router, TypedHeader};
use axum_sessions::async_session::{MemoryStore, Session, SessionStore};
use axum_sessions::async_session::{Session, SessionStore};
use como_domain::users::User;
use como_infrastructure::register::ServiceRegister;
use oauth2::basic::BasicClient;
@@ -39,7 +40,7 @@ pub struct AuthRequest {
pub async fn login_authorized(
Query(query): Query<AuthRequest>,
State(store): State<MemoryStore>,
State(store): State<PostgresSessionStore>,
State(oauth_client): State<BasicClient>,
State(introspection_state): State<IntrospectionState>,
) -> impl IntoResponse {
@@ -99,13 +100,13 @@ pub struct UserFromSession {}
#[async_trait]
impl<S> FromRequestParts<S> for UserFromSession
where
MemoryStore: FromRef<S>,
PostgresSessionStore: FromRef<S>,
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str);
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let store = MemoryStore::from_ref(state);
let store = PostgresSessionStore::from_ref(state);
let cookie: Option<TypedHeader<Cookie>> = parts.extract().await.unwrap();
@@ -116,7 +117,7 @@ where
let session_cookie = session_cookie.unwrap();
tracing::info!(
tracing::debug!(
"UserFromSession: got session cookie from user agent, {}={}",
COOKIE_NAME,
session_cookie

View File

@@ -1,10 +1,10 @@
use std::env;
use anyhow::Context;
use async_sqlx_session::PostgresSessionStore;
use axum::extract::FromRef;
use axum::http::{HeaderValue, Method};
use axum::Router;
use axum_sessions::async_session::MemoryStore;
use como_infrastructure::register::ServiceRegister;
use oauth2::basic::BasicClient;
use tower::ServiceBuilder;
@@ -32,11 +32,10 @@ impl Api {
.build()
.await?;
let store = MemoryStore::new();
let oauth_client = oauth_client();
let app_state = AppState {
oauth_client,
store,
store: service_register.session_store.clone(),
introspection_state: is,
};
@@ -81,7 +80,7 @@ impl Api {
pub struct AppState {
oauth_client: BasicClient,
introspection_state: IntrospectionState,
store: MemoryStore,
store: PostgresSessionStore,
}
impl FromRef<AppState> for BasicClient {
@@ -90,7 +89,7 @@ impl FromRef<AppState> for BasicClient {
}
}
impl FromRef<AppState> for MemoryStore {
impl FromRef<AppState> for PostgresSessionStore {
fn from_ref(state: &AppState) -> Self {
state.store.clone()
}