common.rs
| 1 | //! Authenticated-user extractor shared by all protected API routes. |
| 2 | |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use axum::extract::FromRequestParts; |
| 6 | use axum::http::StatusCode; |
| 7 | use axum::http::request::Parts; |
| 8 | |
| 9 | use crate::auth::parse_session_cookie; |
| 10 | use crate::db::{RootRow, ShareRow, User}; |
| 11 | use crate::error::{ApiError, AppState}; |
| 12 | |
| 13 | pub struct AuthUser { |
| 14 | pub user: User, |
| 15 | pub roots: Vec<RootRow>, |
| 16 | /// Present when authenticated via a public share token. The single entry |
| 17 | /// in `roots` is the shared item (its path is the share's `target`), so all |
| 18 | /// file operations are scoped to it. |
| 19 | #[allow(dead_code)] |
| 20 | pub share: Option<ShareRow>, |
| 21 | } |
| 22 | |
| 23 | impl<S> FromRequestParts<S> for AuthUser |
| 24 | where |
| 25 | S: HasState + Send + Sync, |
| 26 | { |
| 27 | type Rejection = ApiError; |
| 28 | |
| 29 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 30 | let state = state.state(); |
| 31 | |
| 32 | // 1. Public share token (`?share=<token>` or `X-Share-Token`). Checked |
| 33 | // first: a request that explicitly carries a share token is a share |
| 34 | // request, even if a session is also present (so a signed-in user |
| 35 | // viewing a share link sees the shared scope). |
| 36 | if let Some(share_token) = share_token_from_request(parts) { |
| 37 | return match state.db.share_by_token(&share_token).await { |
| 38 | Some(share) if !share.is_expired() => { |
| 39 | let roots = vec![RootRow { |
| 40 | id: share.id, |
| 41 | path: share.target.clone(), |
| 42 | mode: share.mode.clone(), |
| 43 | }]; |
| 44 | let user = User { |
| 45 | id: share.creator_id, |
| 46 | name: "shared".to_string(), |
| 47 | is_admin: false, |
| 48 | active: true, |
| 49 | }; |
| 50 | Ok(AuthUser { |
| 51 | user, |
| 52 | roots, |
| 53 | share: Some(share), |
| 54 | }) |
| 55 | } |
| 56 | Some(_) => Err(ApiError::new(StatusCode::GONE, "this share has expired")), |
| 57 | None => Err(ApiError::new(StatusCode::NOT_FOUND, "share not found")), |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | // 2. Signed-in session. |
| 62 | if let Some(token) = parse_session_cookie(&parts.headers) { |
| 63 | return match state.db.session_user(&token).await { |
| 64 | Some(user) => { |
| 65 | let roots = state.db.user_roots(user.id).await; |
| 66 | Ok(AuthUser { |
| 67 | user, |
| 68 | roots, |
| 69 | share: None, |
| 70 | }) |
| 71 | } |
| 72 | None => Err(ApiError::new( |
| 73 | StatusCode::UNAUTHORIZED, |
| 74 | "session expired, please sign in again", |
| 75 | )), |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | Err(ApiError::new(StatusCode::UNAUTHORIZED, "not signed in")) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Extract the share token from a request, if present: a `?share=<token>` |
| 84 | /// query param or an `X-Share-Token` header. |
| 85 | fn share_token_from_request(parts: &Parts) -> Option<String> { |
| 86 | if let Some(q) = parts.uri.query() { |
| 87 | for pair in q.split('&') { |
| 88 | if let Some((k, v)) = pair.split_once('=') |
| 89 | && k == "share" |
| 90 | && !v.is_empty() |
| 91 | { |
| 92 | return Some(v.to_string()); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | parts |
| 97 | .headers |
| 98 | .get("x-share-token") |
| 99 | .and_then(|v| v.to_str().ok()) |
| 100 | .filter(|s| !s.is_empty()) |
| 101 | .map(|s| s.to_string()) |
| 102 | } |
| 103 | |
| 104 | /// Lets the extractor pull the concrete state type out of a generic `S`. |
| 105 | pub trait HasState { |
| 106 | fn state(&self) -> &AppState; |
| 107 | } |
| 108 | |
| 109 | impl HasState for Arc<AppState> { |
| 110 | fn state(&self) -> &AppState { |
| 111 | self |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl HasState for AppState { |
| 116 | fn state(&self) -> &AppState { |
| 117 | self |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// Extractor for admin-only routes: a signed-in user who is an admin. |
| 122 | pub struct AdminUser { |
| 123 | pub user: User, |
| 124 | } |
| 125 | |
| 126 | impl<S> FromRequestParts<S> for AdminUser |
| 127 | where |
| 128 | S: HasState + Send + Sync, |
| 129 | { |
| 130 | type Rejection = ApiError; |
| 131 | |
| 132 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 133 | let auth = AuthUser::from_request_parts(parts, state).await?; |
| 134 | if !auth.user.is_admin { |
| 135 | return Err(ApiError::new(StatusCode::FORBIDDEN, "admin only")); |
| 136 | } |
| 137 | Ok(AdminUser { user: auth.user }) |
| 138 | } |
| 139 | } |
| 140 |