| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use axum::{ |
| 17 |
body::Body, |
| 18 |
extract::State, |
| 19 |
http::Request, |
| 20 |
middleware::Next, |
| 21 |
response::{IntoResponse, Redirect, Response}, |
| 22 |
}; |
| 23 |
use tower_sessions::Session; |
| 24 |
|
| 25 |
use crate::config::AccessGate; |
| 26 |
use crate::AppState; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
fn path_is_exempt(path: &str) -> bool { |
| 35 |
|
| 36 |
fn hit(path: &str, p: &str) -> bool { |
| 37 |
path == p || path.strip_prefix(p).is_some_and(|rest| rest.starts_with('/')) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
hit(path, "/login") |
| 43 |
|| hit(path, "/logout") |
| 44 |
|| hit(path, "/auth") |
| 45 |
|| hit(path, "/oauth") |
| 46 |
|| hit(path, "/sso") |
| 47 |
|
| 48 |
|| hit(path, "/static") |
| 49 |
|| hit(path, "/rustdoc") |
| 50 |
|| path == "/favicon.ico" |
| 51 |
|| path == "/robots.txt" |
| 52 |
|
| 53 |
|| path == "/health" |
| 54 |
|| path == "/metrics" |
| 55 |
|| hit(path, "/stripe/webhook") |
| 56 |
|| hit(path, "/postmark") |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
fn session_is_allowed(user: Option<&crate::auth::SessionUser>) -> bool { |
| 62 |
user.is_some_and(|u| u.can_create_projects || u.is_fan_plus) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pub async fn access_gate_middleware( |
| 68 |
State(state): State<AppState>, |
| 69 |
request: Request<Body>, |
| 70 |
next: Next, |
| 71 |
) -> Response { |
| 72 |
if state.config.access_gate == AccessGate::Open { |
| 73 |
return next.run(request).await; |
| 74 |
} |
| 75 |
|
| 76 |
let path = request.uri().path(); |
| 77 |
if path_is_exempt(path) { |
| 78 |
return next.run(request).await; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
let user = match request.extensions().get::<Session>() { |
| 83 |
Some(session) => crate::auth::session_user(session).await, |
| 84 |
None => None, |
| 85 |
}; |
| 86 |
|
| 87 |
if session_is_allowed(user.as_ref()) { |
| 88 |
next.run(request).await |
| 89 |
} else { |
| 90 |
|
| 91 |
|
| 92 |
Redirect::to("/login?gate=fan_plus_or_creator").into_response() |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#[cfg(test)] |
| 97 |
mod tests { |
| 98 |
use super::*; |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn exempts_auth_and_asset_paths() { |
| 102 |
for p in [ |
| 103 |
"/login", |
| 104 |
"/login?gate=fan_plus_or_creator", |
| 105 |
"/logout", |
| 106 |
"/auth/me", |
| 107 |
"/auth/passkey/start", |
| 108 |
"/oauth/authorize", |
| 109 |
"/sso/login", |
| 110 |
"/sso/callback", |
| 111 |
"/static/style.css", |
| 112 |
"/static/images/favicon.ico", |
| 113 |
"/rustdoc/index.html", |
| 114 |
"/favicon.ico", |
| 115 |
"/robots.txt", |
| 116 |
"/health", |
| 117 |
"/metrics", |
| 118 |
"/stripe/webhook", |
| 119 |
"/postmark/inbound", |
| 120 |
] { |
| 121 |
|
| 122 |
let path = p.split('?').next().unwrap(); |
| 123 |
assert!(path_is_exempt(path), "expected exempt: {p}"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn gates_content_paths() { |
| 129 |
for p in [ |
| 130 |
"/", |
| 131 |
"/discover", |
| 132 |
"/u/someone", |
| 133 |
"/p/some-project", |
| 134 |
"/changelog", |
| 135 |
"/library", |
| 136 |
"/loginsomething", |
| 137 |
"/authority", |
| 138 |
] { |
| 139 |
assert!(!path_is_exempt(p), "expected gated: {p}"); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn only_creator_or_fan_plus_passes() { |
| 145 |
use crate::auth::SessionUser; |
| 146 |
use crate::db::{UserId, Username}; |
| 147 |
|
| 148 |
fn user(can_create_projects: bool, is_fan_plus: bool) -> SessionUser { |
| 149 |
SessionUser { |
| 150 |
id: UserId::default(), |
| 151 |
username: Username::from_trusted("t".into()), |
| 152 |
email: "t@example.com".into(), |
| 153 |
display_name: None, |
| 154 |
can_create_projects, |
| 155 |
suspended: false, |
| 156 |
is_admin: false, |
| 157 |
is_fan_plus, |
| 158 |
creator_tier: None, |
| 159 |
deactivated: false, |
| 160 |
is_sandbox: false, |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
assert!(!session_is_allowed(None), "anonymous blocked"); |
| 165 |
assert!(!session_is_allowed(Some(&user(false, false))), "plain fan blocked"); |
| 166 |
assert!(session_is_allowed(Some(&user(true, false))), "creator allowed"); |
| 167 |
assert!(session_is_allowed(Some(&user(false, true))), "fan+ allowed"); |
| 168 |
} |
| 169 |
} |
| 170 |
|