chore: remove unused code

This commit is contained in:
Jon Seager 2025-11-25 19:14:38 +00:00
parent 42d0f71eb1
commit 7d4c7dda28
No known key found for this signature in database
4 changed files with 18 additions and 75 deletions

View file

@ -60,9 +60,10 @@ impl TokenRepository for SqlTokenRepository {
.await
.map_err(|err| {
if let sqlx::Error::Database(db_err) = &err
&& db_err.is_unique_violation() {
return RepositoryError::conflict("token already exists");
}
&& db_err.is_unique_violation()
{
return RepositoryError::conflict("token already exists");
}
RepositoryError::unexpected(err.to_string())
})?;

View file

@ -47,9 +47,10 @@ impl UserRepository for SqlUserRepository {
.await
.map_err(|err| {
if let sqlx::Error::Database(db_err) = &err
&& db_err.is_unique_violation() {
return RepositoryError::conflict("user already exists");
}
&& db_err.is_unique_violation()
{
return RepositoryError::conflict("user already exists");
}
RepositoryError::unexpected(err.to_string())
})?;

View file

@ -1,9 +1,7 @@
use axum::{
async_trait,
extract::{FromRequestParts, Request, State},
extract::{FromRequestParts, Request},
http::{StatusCode, header, request::Parts},
middleware::Next,
response::{IntoResponse, Response},
};
use tower_cookies::Cookies;
@ -32,9 +30,10 @@ impl FromRequestParts<AppState> for AuthenticatedUser {
// Try to authenticate via session cookie first
if let Ok(cookies) = Cookies::from_request_parts(parts, state).await
&& let Some(user) = authenticate_via_session(state, &cookies).await {
return Ok(AuthenticatedUser(user));
}
&& let Some(user) = authenticate_via_session(state, &cookies).await
{
return Ok(AuthenticatedUser(user));
}
// Fall back to Bearer token authentication
let auth_header = parts
@ -103,65 +102,6 @@ async fn authenticate_via_session(state: &AppState, cookies: &Cookies) -> Option
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
pub fn get_authenticated_user(request: &Request) -> Option<&User> {
request

View file

@ -140,10 +140,11 @@ fn build_months(prepared_events: Vec<TimelinePreparedEvent>) -> Vec<TimelineMont
for prepared in prepared_events {
if let Some(last) = months.last_mut()
&& last.anchor == prepared.anchor {
last.events.push(prepared.view);
continue;
}
&& last.anchor == prepared.anchor
{
last.events.push(prepared.view);
continue;
}
months.push(TimelineMonthView {
anchor: prepared.anchor,