Implement almost all previous features

This commit is contained in:
Lennart
2025-06-08 19:38:33 +02:00
parent 95889e3df1
commit 00eb43f048
41 changed files with 906 additions and 916 deletions

View File

@@ -32,6 +32,7 @@ rrule.workspace = true
headers.workspace = true
tower.workspace = true
futures-core.workspace = true
tower-sessions.workspace = true
[dev-dependencies]
rstest = { workspace = true }

View File

@@ -1,3 +1,5 @@
use crate::auth::User;
use super::AuthenticationProvider;
use axum::{extract::Request, response::Response};
use futures_core::future::BoxFuture;
@@ -8,6 +10,7 @@ use std::{
task::{Context, Poll},
};
use tower::{Layer, Service};
use tower_sessions::Session;
use tracing::{Instrument, info_span};
pub struct AuthenticationLayer<AP: AuthenticationProvider> {
@@ -71,8 +74,15 @@ where
let ap = self.auth_provider.clone();
let mut inner = self.inner.clone();
// request.extensions_mut();
Box::pin(async move {
if let Some(session) = request.extensions().get::<Session>() {
if let Ok(Some(user_id)) = session.get::<String>("user").await {
if let Ok(Some(user)) = ap.get_principal(&user_id).await {
request.extensions_mut().insert(user);
}
}
}
if let Some(auth) = auth_header {
let user_id = auth.username();
let password = auth.password();
@@ -84,6 +94,7 @@ where
request.extensions_mut().insert(user);
}
}
let response = inner.call(request).await?;
Ok(response)
})

View File

@@ -1,6 +1,6 @@
use axum::{
body::Body,
extract::FromRequestParts,
extract::{FromRequestParts, OptionalFromRequestParts},
response::{IntoResponse, Response},
};
use chrono::{DateTime, Utc};
@@ -8,7 +8,7 @@ use derive_more::Display;
use http::{HeaderValue, StatusCode, header};
use rustical_xml::ValueSerialize;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::{convert::Infallible, fmt::Display};
use crate::Secret;
@@ -144,3 +144,14 @@ impl<S: Send + Sync + Clone> FromRequestParts<S> for User {
.ok_or(UnauthorizedError)
}
}
impl<S: Send + Sync + Clone> OptionalFromRequestParts<S> for User {
type Rejection = Infallible;
async fn from_request_parts(
parts: &mut http::request::Parts,
_state: &S,
) -> Result<Option<Self>, Self::Rejection> {
Ok(parts.extensions.get::<Self>().cloned())
}
}

View File

@@ -1,3 +1,4 @@
use axum::response::IntoResponse;
use http::StatusCode;
#[derive(Debug, thiserror::Error)]
@@ -39,3 +40,9 @@ impl Error {
}
}
}
impl IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
(self.status_code(), self.to_string()).into_response()
}
}