chore: remove unused code
This commit is contained in:
parent
42d0f71eb1
commit
7d4c7dda28
4 changed files with 18 additions and 75 deletions
|
|
@ -60,7 +60,8 @@ impl TokenRepository for SqlTokenRepository {
|
||||||
.await
|
.await
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
if let sqlx::Error::Database(db_err) = &err
|
if let sqlx::Error::Database(db_err) = &err
|
||||||
&& db_err.is_unique_violation() {
|
&& db_err.is_unique_violation()
|
||||||
|
{
|
||||||
return RepositoryError::conflict("token already exists");
|
return RepositoryError::conflict("token already exists");
|
||||||
}
|
}
|
||||||
RepositoryError::unexpected(err.to_string())
|
RepositoryError::unexpected(err.to_string())
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,8 @@ impl UserRepository for SqlUserRepository {
|
||||||
.await
|
.await
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
if let sqlx::Error::Database(db_err) = &err
|
if let sqlx::Error::Database(db_err) = &err
|
||||||
&& db_err.is_unique_violation() {
|
&& db_err.is_unique_violation()
|
||||||
|
{
|
||||||
return RepositoryError::conflict("user already exists");
|
return RepositoryError::conflict("user already exists");
|
||||||
}
|
}
|
||||||
RepositoryError::unexpected(err.to_string())
|
RepositoryError::unexpected(err.to_string())
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
extract::{FromRequestParts, Request, State},
|
extract::{FromRequestParts, Request},
|
||||||
http::{StatusCode, header, request::Parts},
|
http::{StatusCode, header, request::Parts},
|
||||||
middleware::Next,
|
|
||||||
response::{IntoResponse, Response},
|
|
||||||
};
|
};
|
||||||
use tower_cookies::Cookies;
|
use tower_cookies::Cookies;
|
||||||
|
|
||||||
|
|
@ -32,7 +30,8 @@ impl FromRequestParts<AppState> for AuthenticatedUser {
|
||||||
|
|
||||||
// Try to authenticate via session cookie first
|
// Try to authenticate via session cookie first
|
||||||
if let Ok(cookies) = Cookies::from_request_parts(parts, state).await
|
if let Ok(cookies) = Cookies::from_request_parts(parts, state).await
|
||||||
&& let Some(user) = authenticate_via_session(state, &cookies).await {
|
&& let Some(user) = authenticate_via_session(state, &cookies).await
|
||||||
|
{
|
||||||
return Ok(AuthenticatedUser(user));
|
return Ok(AuthenticatedUser(user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,65 +102,6 @@ async fn authenticate_via_session(state: &AppState, cookies: &Cookies) -> Option
|
||||||
state.user_repo.get(session.user_id).await.ok()
|
state.user_repo.get(session.user_id).await.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Middleware that optionally extracts authentication from the request
|
|
||||||
/// Sets AuthenticatedUser extension if valid token is found
|
|
||||||
pub async fn optional_auth_middleware(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
mut request: Request,
|
|
||||||
next: Next,
|
|
||||||
) -> Response {
|
|
||||||
if let Some(user) = extract_user_from_request(&state, &request).await {
|
|
||||||
request.extensions_mut().insert(AuthenticatedUser(user));
|
|
||||||
}
|
|
||||||
next.run(request).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Middleware that requires authentication
|
|
||||||
/// Returns 401 if no valid token is found
|
|
||||||
pub async fn require_auth_middleware(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
mut request: Request,
|
|
||||||
next: Next,
|
|
||||||
) -> Response {
|
|
||||||
match extract_user_from_request(&state, &request).await {
|
|
||||||
Some(user) => {
|
|
||||||
request.extensions_mut().insert(AuthenticatedUser(user));
|
|
||||||
next.run(request).await
|
|
||||||
}
|
|
||||||
None => StatusCode::UNAUTHORIZED.into_response(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn extract_user_from_request(state: &AppState, request: &Request) -> Option<User> {
|
|
||||||
// Extract token from Authorization header
|
|
||||||
let auth_header = request.headers().get(header::AUTHORIZATION)?;
|
|
||||||
let auth_str = auth_header.to_str().ok()?;
|
|
||||||
|
|
||||||
// Check for "Bearer <token>" format
|
|
||||||
let token = auth_str.strip_prefix("Bearer ")?;
|
|
||||||
|
|
||||||
// Hash the token to look it up in the database
|
|
||||||
let token_hash = hash_token(token);
|
|
||||||
|
|
||||||
// Look up the token
|
|
||||||
let token_record = state.token_repo.get_by_token_hash(&token_hash).await.ok()?;
|
|
||||||
|
|
||||||
// Check if token is revoked
|
|
||||||
if token_record.is_revoked() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update last used timestamp (fire and forget - don't block on this)
|
|
||||||
let token_repo = state.token_repo.clone();
|
|
||||||
let token_id = token_record.id;
|
|
||||||
tokio::spawn(async move {
|
|
||||||
let _ = token_repo.update_last_used(token_id).await;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get the user
|
|
||||||
state.user_repo.get(token_record.user_id).await.ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper to extract authenticated user from request extensions
|
/// Helper to extract authenticated user from request extensions
|
||||||
pub fn get_authenticated_user(request: &Request) -> Option<&User> {
|
pub fn get_authenticated_user(request: &Request) -> Option<&User> {
|
||||||
request
|
request
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,8 @@ fn build_months(prepared_events: Vec<TimelinePreparedEvent>) -> Vec<TimelineMont
|
||||||
|
|
||||||
for prepared in prepared_events {
|
for prepared in prepared_events {
|
||||||
if let Some(last) = months.last_mut()
|
if let Some(last) = months.last_mut()
|
||||||
&& last.anchor == prepared.anchor {
|
&& last.anchor == prepared.anchor
|
||||||
|
{
|
||||||
last.events.push(prepared.view);
|
last.events.push(prepared.view);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue