| 1 | use axum::extract::{FromRef, FromRequestParts}; |
| 2 | use axum::http::request::Parts; |
| 3 | use axum_extra::extract::CookieJar; |
| 4 | |
| 5 | use crate::domain::role::Role; |
| 6 | use crate::domain::session::Session; |
| 7 | use crate::domain::user::User; |
| 8 | use crate::error::AppError; |
| 9 | use crate::state::AppState; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | async fn authed_user<S>(parts: &mut Parts, state: &S) -> Result<User, AppError> |
| 15 | where |
| 16 | S: Send + Sync, |
| 17 | AppState: FromRef<S>, |
| 18 | { |
| 19 | let jar = CookieJar::from_request_parts(parts, state) |
| 20 | .await |
| 21 | .map_err(|_| AppError::Unauthorized)?; |
| 22 | let token = jar |
| 23 | .get("session") |
| 24 | .map(|cookie| cookie.value().to_owned()) |
| 25 | .ok_or(AppError::Unauthorized)?; |
| 26 | |
| 27 | let app = AppState::from_ref(state); |
| 28 | |
| 29 | let session = Session::find_by_token(&token, &app.db) |
| 30 | .await? |
| 31 | .ok_or(AppError::Unauthorized)?; |
| 32 | if session.is_expired() { |
| 33 | return Err(AppError::Unauthorized); |
| 34 | } |
| 35 | |
| 36 | User::read(session.user(), &app.db) |
| 37 | .await? |
| 38 | .ok_or(AppError::Unauthorized) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | pub struct CurrentUser(pub User); |
| 44 | |
| 45 | impl<S> FromRequestParts<S> for CurrentUser |
| 46 | where |
| 47 | S: Send + Sync, |
| 48 | AppState: FromRef<S>, |
| 49 | { |
| 50 | type Rejection = AppError; |
| 51 | |
| 52 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 53 | Ok(CurrentUser(authed_user(parts, state).await?)) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | pub struct RequireStudent(pub User); |
| 60 | |
| 61 | impl<S> FromRequestParts<S> for RequireStudent |
| 62 | where |
| 63 | S: Send + Sync, |
| 64 | AppState: FromRef<S>, |
| 65 | { |
| 66 | type Rejection = AppError; |
| 67 | |
| 68 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 69 | let user = authed_user(parts, state).await?; |
| 70 | if !user.get_role().at_least(Role::Student) { |
| 71 | return Err(AppError::Forbidden("requires student role or higher")); |
| 72 | } |
| 73 | Ok(RequireStudent(user)) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | pub struct RequireTeacher(pub User); |
| 80 | |
| 81 | impl<S> FromRequestParts<S> for RequireTeacher |
| 82 | where |
| 83 | S: Send + Sync, |
| 84 | AppState: FromRef<S>, |
| 85 | { |
| 86 | type Rejection = AppError; |
| 87 | |
| 88 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 89 | let user = authed_user(parts, state).await?; |
| 90 | if !user.get_role().at_least(Role::Teacher) { |
| 91 | return Err(AppError::Forbidden("requires teacher role or higher")); |
| 92 | } |
| 93 | Ok(RequireTeacher(user)) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | pub struct RequireManager(pub User); |
| 100 | |
| 101 | impl<S> FromRequestParts<S> for RequireManager |
| 102 | where |
| 103 | S: Send + Sync, |
| 104 | AppState: FromRef<S>, |
| 105 | { |
| 106 | type Rejection = AppError; |
| 107 | |
| 108 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 109 | let user = authed_user(parts, state).await?; |
| 110 | if !user.get_role().at_least(Role::Manager) { |
| 111 | return Err(AppError::Forbidden("requires manager role or higher")); |
| 112 | } |
| 113 | Ok(RequireManager(user)) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | pub struct RequireAdmin(pub User); |
| 120 | |
| 121 | impl<S> FromRequestParts<S> for RequireAdmin |
| 122 | where |
| 123 | S: Send + Sync, |
| 124 | AppState: FromRef<S>, |
| 125 | { |
| 126 | type Rejection = AppError; |
| 127 | |
| 128 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 129 | let user = authed_user(parts, state).await?; |
| 130 | if !user.get_role().at_least(Role::Admin) { |
| 131 | return Err(AppError::Forbidden("requires admin role")); |
| 132 | } |
| 133 | Ok(RequireAdmin(user)) |
| 134 | } |
| 135 | } |