use actix_web::{dev::Payload, web, FromRequest, HttpMessage, HttpRequest, HttpResponse};
use chrono::{Duration, Utc};
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
use rusqlite::OptionalExtension;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::future::{ready, Ready};
use uuid::Uuid;
const JWT_SECRET_ENV: &str = "IRONBRIDGE_JWT_SECRET";
fn jwt_secret() -> &'static [u8] {
use std::sync::OnceLock;
static SECRET: OnceLock<Vec<u8>> = OnceLock::new();
SECRET.get_or_init(|| match std::env::var(JWT_SECRET_ENV) {
Ok(s) if s.len() >= 32 => s.into_bytes(),
Ok(s) => {
eprintln!(
"[WARN] {JWT_SECRET_ENV} is set but only {} bytes; a signing secret must be \
at least 32. Ignoring it and using a random per-process secret instead.",
s.len()
);
random_secret()
}
Err(_) => {
eprintln!(
"[INFO] {JWT_SECRET_ENV} is not set; signing sessions with a random \
per-process secret. Sessions will not survive a restart. Set \
{JWT_SECRET_ENV} (>=32 bytes) to keep sessions across restarts or share \
them across instances."
);
random_secret()
}
})
}
fn random_secret() -> Vec<u8> {
use rand::RngCore;
let mut buf = vec![0u8; 32];
rand::thread_rng().fill_bytes(&mut buf);
buf
}
const JWT_EXPIRY_HOURS: i64 = 24;
const REFRESH_TOKEN_EXPIRY_DAYS: i64 = 30;
const LOGIN_MAX_FAILURES: usize = 10;
const LOGIN_WINDOW_SECS: u64 = 900;
fn login_failures(
) -> &'static std::sync::Mutex<std::collections::HashMap<String, Vec<std::time::Instant>>> {
use std::sync::OnceLock;
static FAILURES: OnceLock<
std::sync::Mutex<std::collections::HashMap<String, Vec<std::time::Instant>>>,
> = OnceLock::new();
FAILURES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
}
fn login_throttled(key: &str) -> bool {
let mut map = login_failures().lock().unwrap();
let now = std::time::Instant::now();
if let Some(times) = map.get_mut(key) {
times.retain(|t| now.duration_since(*t).as_secs() < LOGIN_WINDOW_SECS);
times.len() >= LOGIN_MAX_FAILURES
} else {
false
}
}
fn record_login_failure(key: &str) {
let mut map = login_failures().lock().unwrap();
let now = std::time::Instant::now();
let times = map.entry(key.to_string()).or_default();
times.retain(|t| now.duration_since(*t).as_secs() < LOGIN_WINDOW_SECS);
times.push(now);
}
fn clear_login_failures(key: &str) {
login_failures().lock().unwrap().remove(key);
}
fn throttle_key(req: &HttpRequest) -> String {
req.peer_addr()
.map(|a| a.ip().to_string())
.unwrap_or_else(|| "unknown".to_string())
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum SubscriptionTier {
#[default]
Free,
Pro,
Enterprise,
}
impl SubscriptionTier {
pub fn as_str(&self) -> &'static str {
match self {
Self::Free => "free",
Self::Pro => "pro",
Self::Enterprise => "enterprise",
}
}
pub fn from_str(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"free" => Some(Self::Free),
"pro" => Some(Self::Pro),
"enterprise" => Some(Self::Enterprise),
_ => None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionFeatures {
pub max_workspaces: Option<u32>,
pub max_sessions: Option<u32>,
pub max_agents: Option<u32>,
pub max_swarms: Option<u32>,
pub realtime_sync: bool,
pub cross_device_sync: bool,
pub priority_support: bool,
pub team_collaboration: bool,
pub analytics: bool,
pub api_access: bool,
pub custom_integrations: bool,
}
impl SubscriptionFeatures {
pub fn for_tier(tier: SubscriptionTier) -> Self {
match tier {
SubscriptionTier::Free => Self {
max_workspaces: Some(10),
max_sessions: Some(100),
max_agents: Some(5),
max_swarms: Some(1),
realtime_sync: true,
cross_device_sync: true,
priority_support: false,
team_collaboration: false,
analytics: false,
api_access: false,
custom_integrations: false,
},
SubscriptionTier::Pro => Self {
max_workspaces: Some(100),
max_sessions: None, max_agents: Some(100),
max_swarms: Some(20),
realtime_sync: true,
cross_device_sync: true,
priority_support: true,
team_collaboration: false,
analytics: true,
api_access: true,
custom_integrations: false,
},
SubscriptionTier::Enterprise => Self {
max_workspaces: None, max_sessions: None,
max_agents: None,
max_swarms: None,
realtime_sync: true,
cross_device_sync: true,
priority_support: true,
team_collaboration: true,
analytics: true,
api_access: true,
custom_integrations: true,
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub id: String,
pub email: String,
pub display_name: String,
#[serde(skip_serializing)]
#[allow(dead_code)]
pub password_hash: String,
pub subscription_tier: SubscriptionTier,
pub subscription_expires_at: Option<i64>,
pub created_at: i64,
pub updated_at: i64,
pub last_login_at: Option<i64>,
pub email_verified: bool,
pub avatar_url: Option<String>,
pub metadata: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicUser {
pub id: String,
pub email: String,
pub display_name: String,
pub subscription_tier: SubscriptionTier,
pub subscription_expires_at: Option<i64>,
pub created_at: i64,
pub email_verified: bool,
pub avatar_url: Option<String>,
pub features: SubscriptionFeatures,
}
impl From<User> for PublicUser {
fn from(user: User) -> Self {
Self {
id: user.id,
email: user.email,
display_name: user.display_name,
subscription_tier: user.subscription_tier,
subscription_expires_at: user.subscription_expires_at,
created_at: user.created_at,
email_verified: user.email_verified,
avatar_url: user.avatar_url,
features: SubscriptionFeatures::for_tier(user.subscription_tier),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
pub sub: String,
pub email: String,
pub tier: String,
pub iat: i64,
pub exp: i64,
pub token_type: String,
}
pub const DISABLE_AUTH_ENV: &str = "IRONBRIDGE_DISABLE_AUTH";
pub const REQUIRE_AUTH_ENV: &str = "IRONBRIDGE_REQUIRE_AUTH";
pub const MIN_PASSWORD_CHARS: usize = 8;
const COMMON_PASSWORDS: &[&str] = &[
"123456",
"1234567",
"12345678",
"123456789",
"1234567890",
"12345678910",
"987654321",
"qwerty",
"qwerty123",
"qwerty1234",
"qwertyuiop",
"1q2w3e4r",
"1q2w3e4r5t",
"zaq12wsx",
"qazwsxedc",
"asdfghjkl",
"1qaz2wsx",
"q1w2e3r4",
"password",
"password1",
"password12",
"password123",
"password1234",
"passw0rd",
"p@ssw0rd",
"p@ssword",
"passwort",
"contrasena",
"motdepasse",
"letmein",
"letmein1",
"letmein123",
"welcome",
"welcome1",
"welcome123",
"admin123",
"administrator",
"root1234",
"changeme",
"changeit",
"secret123",
"default1",
"guest123",
"trustno1",
"iloveyou",
"whatever",
"computer",
"internet",
"starwars",
"superman",
"batman123",
"pokemon123",
"dragon123",
"monkey123",
"freedom1",
"shadow123",
"master123",
"hello123",
"charlie1",
"jennifer",
"michael1",
"jordan23",
"princess",
"princess1",
"sunshine",
"sunshine1",
"football",
"football1",
"baseball",
"baseball1",
"basketball",
"liverpool",
"liverpool1",
"arsenal1",
"chelsea1",
"barcelona",
"juventus1",
"corvette",
"harley123",
"nintendo",
"creative",
"midnight",
"flowerpot",
"butterfly",
"chocolate",
"cocacola",
"pepsi123",
"samsung1",
"michelle",
"danielle",
"nicholas",
"jonathan",
"samantha",
"elizabeth",
"alexander",
"anthony1",
"matthew1",
"andrew123",
"joshua123",
"robert123",
"thomas123",
"william1",
"abc12345",
"abcd1234",
"a1b2c3d4",
"asdf1234",
"test1234",
"temp1234",
"demo1234",
"user1234",
"login123",
"access123",
"system123",
"server123",
"database1",
"backup123",
"qwe123456",
"111111111",
"000000000",
"121212121",
"iloveyou1",
"princess123",
];
pub fn is_common_password(password: &str) -> bool {
COMMON_PASSWORDS
.iter()
.any(|common| password.eq_ignore_ascii_case(common))
}
pub fn auth_required() -> bool {
use std::sync::OnceLock;
static REQUIRED: OnceLock<bool> = OnceLock::new();
*REQUIRED.get_or_init(|| {
let disabled = std::env::var(DISABLE_AUTH_ENV)
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
!disabled
})
}
fn path_requires_auth(path: &str) -> bool {
const OPEN_EXACT: &[&str] = &[
"/health",
"/api/health",
"/api/system/health",
"/api/system/providers/health",
"/docs",
];
const OPEN_PREFIXES: &[&str] = &["/auth/", "/sso/", "/oidc/", "/docs/"];
if OPEN_EXACT.contains(&path) {
return false;
}
if OPEN_PREFIXES.iter().any(|p| path.starts_with(p)) {
return false;
}
true
}
fn is_websocket_route(req: &actix_web::dev::ServiceRequest) -> bool {
matches!(req.path(), "/ws" | "/recording/ws")
}
pub(crate) fn bearer_subprotocol_token(
headers: &actix_web::http::header::HeaderMap,
) -> Option<String> {
headers
.get("Sec-WebSocket-Protocol")
.and_then(|h| h.to_str().ok())
.and_then(|v| {
let mut parts = v.split(',').map(|s| s.trim());
match (parts.next(), parts.next()) {
(Some(scheme), Some(tok)) if scheme.eq_ignore_ascii_case("bearer") => {
Some(tok.to_string())
}
_ => None,
}
})
}
fn request_token(req: &actix_web::dev::ServiceRequest) -> Option<AuthenticatedUser> {
let claims = req
.headers()
.get("Authorization")
.and_then(|h| h.to_str().ok())
.and_then(|s| s.strip_prefix("Bearer "))
.and_then(validate_token_claims)
.or_else(|| {
if !is_websocket_route(req) {
return None;
}
let from_subprotocol =
bearer_subprotocol_token(req.headers()).and_then(|t| validate_token_claims(&t));
from_subprotocol.or_else(|| {
req.query_string()
.split('&')
.find_map(|pair| pair.strip_prefix("token="))
.and_then(|t| urlencoding::decode(t).ok())
.and_then(|t| validate_token_claims(&t))
})
})?;
if let Some(state) = req.app_data::<web::Data<crate::api::state::AppState>>() {
if let Ok(db) = state.db.lock() {
if !token_not_revoked(&db.conn, &claims.sub, claims.iat) {
return None;
}
}
}
Some(AuthenticatedUser {
user_id: claims.sub,
email: claims.email,
tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
})
}
pub async fn require_auth(
req: actix_web::dev::ServiceRequest,
next: actix_web::middleware::Next<impl actix_web::body::MessageBody + 'static>,
) -> Result<
actix_web::dev::ServiceResponse<actix_web::body::EitherBody<impl actix_web::body::MessageBody>>,
actix_web::Error,
> {
let preflight = req.method() == actix_web::http::Method::OPTIONS;
let gated = auth_required() && !preflight && path_requires_auth(req.path());
let authorized = !gated || request_token(&req).is_some();
if authorized {
next.call(req)
.await
.map(actix_web::dev::ServiceResponse::map_into_left_body)
} else {
let (request, _payload) = req.into_parts();
let response = HttpResponse::Unauthorized()
.json(serde_json::json!({
"success": false,
"data": null,
"error": "Authentication required. Send a valid Bearer token.",
}))
.map_into_right_body();
Ok(actix_web::dev::ServiceResponse::new(request, response))
}
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct AuthenticatedUser {
pub user_id: String,
pub email: String,
pub tier: SubscriptionTier,
}
impl FromRequest for AuthenticatedUser {
type Error = actix_web::Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
if let Some(user) = req.extensions().get::<AuthenticatedUser>() {
return ready(Ok(user.clone()));
}
if let Some(auth_header) = req.headers().get("Authorization") {
if let Ok(auth_str) = auth_header.to_str() {
if let Some(token) = auth_str.strip_prefix("Bearer ") {
if let Some(claims) = validate_token_claims(token) {
let current =
req.app_data::<web::Data<crate::api::state::AppState>>()
.and_then(|state| {
state.db.lock().ok().map(|db| {
token_not_revoked(&db.conn, &claims.sub, claims.iat)
})
})
.unwrap_or(true);
if current {
return ready(Ok(AuthenticatedUser {
user_id: claims.sub,
email: claims.email,
tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
}));
}
}
}
}
}
ready(Ok(AuthenticatedUser {
user_id: String::new(),
email: String::new(),
tier: SubscriptionTier::Free,
}))
}
}
#[derive(Debug, Deserialize)]
pub struct RegisterRequest {
pub email: String,
pub password: String,
pub display_name: String,
}
#[derive(Debug, Deserialize)]
pub struct LoginRequest {
pub email: String,
pub password: String,
}
#[derive(Debug, Deserialize)]
pub struct RefreshTokenRequest {
pub refresh_token: String,
}
#[derive(Debug, Serialize)]
pub struct AuthResponse {
pub user: PublicUser,
pub access_token: String,
pub refresh_token: String,
pub expires_in: i64,
}
#[derive(Debug, Deserialize)]
pub struct UpdateProfileRequest {
pub display_name: Option<String>,
pub avatar_url: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct ChangePasswordRequest {
pub current_password: String,
pub new_password: String,
}
#[derive(Debug, Deserialize)]
pub struct UpgradeSubscriptionRequest {
pub tier: String,
#[allow(dead_code)]
pub payment_token: Option<String>,
}
pub fn hash_password_argon2(password: &str) -> Result<String, String> {
use argon2::password_hash::{rand_core::OsRng, PasswordHasher, SaltString};
use argon2::Argon2;
let salt = SaltString::generate(&mut OsRng);
Argon2::default()
.hash_password(password.as_bytes(), &salt)
.map(|h| h.to_string())
.map_err(|e| e.to_string())
}
fn legacy_sha256(password: &str, salt: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(password.as_bytes());
hasher.update(salt.as_bytes());
format!("{:x}", hasher.finalize())
}
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut diff = 0u8;
for (x, y) in a.iter().zip(b.iter()) {
diff |= x ^ y;
}
diff == 0
}
pub fn verify_password(password: &str, stored_hash: &str, salt: &str) -> (bool, bool) {
if stored_hash.starts_with("$argon2") {
use argon2::password_hash::{PasswordHash, PasswordVerifier};
use argon2::Argon2;
let parsed = match PasswordHash::new(stored_hash) {
Ok(p) => p,
Err(_) => return (false, false),
};
let ok = Argon2::default()
.verify_password(password.as_bytes(), &parsed)
.is_ok();
(ok, false)
} else {
let ok = constant_time_eq(
legacy_sha256(password, salt).as_bytes(),
stored_hash.as_bytes(),
);
(ok, ok)
}
}
pub fn generate_access_token(user: &User) -> Option<String> {
let now = Utc::now();
let exp = now + Duration::hours(JWT_EXPIRY_HOURS);
let claims = Claims {
sub: user.id.clone(),
email: user.email.clone(),
tier: user.subscription_tier.as_str().to_string(),
iat: now.timestamp(),
exp: exp.timestamp(),
token_type: "access".to_string(),
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(jwt_secret()),
)
.ok()
}
pub fn generate_refresh_token(user: &User) -> Option<String> {
let now = Utc::now();
let exp = now + Duration::days(REFRESH_TOKEN_EXPIRY_DAYS);
let claims = Claims {
sub: user.id.clone(),
email: user.email.clone(),
tier: user.subscription_tier.as_str().to_string(),
iat: now.timestamp(),
exp: exp.timestamp(),
token_type: "refresh".to_string(),
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(jwt_secret()),
)
.ok()
}
pub fn validate_token_claims(token: &str) -> Option<Claims> {
let validation = Validation::new(Algorithm::HS256);
let token_data =
decode::<Claims>(token, &DecodingKey::from_secret(jwt_secret()), &validation).ok()?;
let claims = token_data.claims;
if claims.exp < Utc::now().timestamp() {
return None;
}
Some(claims)
}
pub fn validate_token(token: &str) -> Option<AuthenticatedUser> {
validate_token_claims(token).map(|claims| AuthenticatedUser {
user_id: claims.sub,
email: claims.email,
tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
})
}
fn user_column_exists(conn: &rusqlite::Connection, col: &str) -> rusqlite::Result<bool> {
let mut stmt = conn.prepare("PRAGMA table_info(users)")?;
let names: Vec<String> = stmt
.query_map([], |r| r.get::<_, String>(1))?
.collect::<rusqlite::Result<_>>()?;
Ok(names.iter().any(|n| n == col))
}
fn token_not_revoked(conn: &rusqlite::Connection, user_id: &str, iat: i64) -> bool {
let valid_after: i64 = conn
.query_row(
"SELECT COALESCE(tokens_valid_after, 0) FROM users WHERE id = ?1",
[user_id],
|r| r.get(0),
)
.unwrap_or(0);
iat >= valid_after
}
pub fn validate_refresh_token(token: &str) -> Option<AuthenticatedUser> {
let validation = Validation::new(Algorithm::HS256);
let token_data =
decode::<Claims>(token, &DecodingKey::from_secret(jwt_secret()), &validation).ok()?;
let claims = token_data.claims;
if claims.token_type != "refresh" {
return None;
}
if claims.exp < Utc::now().timestamp() {
return None;
}
Some(AuthenticatedUser {
user_id: claims.sub,
email: claims.email,
tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
})
}
pub fn init_auth_tables(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
password_salt TEXT NOT NULL,
subscription_tier TEXT NOT NULL DEFAULT 'free',
subscription_expires_at INTEGER,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
last_login_at INTEGER,
email_verified INTEGER DEFAULT 0,
avatar_url TEXT,
metadata TEXT,
tokens_valid_after INTEGER NOT NULL DEFAULT 0
)",
[],
)?;
if !user_column_exists(conn, "tokens_valid_after")? {
conn.execute(
"ALTER TABLE users ADD COLUMN tokens_valid_after INTEGER NOT NULL DEFAULT 0",
[],
)?;
}
conn.execute(
"CREATE TABLE IF NOT EXISTS refresh_tokens (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
token_hash TEXT NOT NULL,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
revoked INTEGER DEFAULT 0,
device_info TEXT,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS user_sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
device_id TEXT,
device_name TEXT,
platform TEXT,
ip_address TEXT,
last_active_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_refresh_tokens_user_id ON refresh_tokens(user_id)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_user_sessions_user_id ON user_sessions(user_id)",
[],
)?;
Ok(())
}
pub async fn register(
app_state: web::Data<crate::api::state::AppState>,
body: web::Json<RegisterRequest>,
) -> HttpResponse {
let db = app_state.db.lock().unwrap();
if let Err(e) = init_auth_tables(&db.conn) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Database error: {}", e)
}));
}
if body.email.is_empty() || !body.email.contains('@') {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "Invalid email address"
}));
}
if body.password.len() < MIN_PASSWORD_CHARS {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "Password must be at least 8 characters"
}));
}
if is_common_password(&body.password) {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "That password is one of the most commonly used ones and is \
guessed first in an attack. Please choose another."
}));
}
if body.display_name.is_empty() {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "Display name is required"
}));
}
let existing: rusqlite::Result<Option<String>> = db
.conn
.query_row(
"SELECT id FROM users WHERE email = ?1",
rusqlite::params![body.email.to_lowercase()],
|row| row.get(0),
)
.optional();
if let Ok(Some(_)) = existing {
return HttpResponse::Conflict().json(serde_json::json!({
"success": false,
"error": "Email already registered"
}));
}
let user_id = Uuid::new_v4().to_string();
let salt = String::new();
let password_hash = match hash_password_argon2(&body.password) {
Ok(h) => h,
Err(e) => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to hash password: {}", e)
}))
}
};
let now = Utc::now().timestamp();
let result = db.conn.execute(
"INSERT INTO users (id, email, display_name, password_hash, password_salt,
subscription_tier, created_at, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
rusqlite::params![
user_id,
body.email.to_lowercase(),
body.display_name,
password_hash,
salt,
"free",
now,
now,
],
);
if let Err(e) = result {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to create user: {}", e)
}));
}
let user = User {
id: user_id,
email: body.email.to_lowercase(),
display_name: body.display_name.clone(),
password_hash,
subscription_tier: SubscriptionTier::Free,
subscription_expires_at: None,
created_at: now,
updated_at: now,
last_login_at: None,
email_verified: false,
avatar_url: None,
metadata: None,
};
let access_token = match generate_access_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate access token"
}))
}
};
let refresh_token = match generate_refresh_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate refresh token"
}))
}
};
HttpResponse::Created().json(serde_json::json!({
"success": true,
"data": AuthResponse {
user: PublicUser::from(user),
access_token,
refresh_token,
expires_in: JWT_EXPIRY_HOURS * 3600,
}
}))
}
pub async fn login(
req: HttpRequest,
app_state: web::Data<crate::api::state::AppState>,
body: web::Json<LoginRequest>,
) -> HttpResponse {
let key = throttle_key(&req);
if login_throttled(&key) {
return HttpResponse::TooManyRequests().json(serde_json::json!({
"success": false,
"error": "Too many failed login attempts. Try again later.",
}));
}
let db = app_state.db.lock().unwrap();
if let Err(e) = init_auth_tables(&db.conn) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Database error: {}", e)
}));
}
let user_result: rusqlite::Result<(
String,
String,
String,
String,
String,
String,
Option<i64>,
i64,
i64,
Option<i64>,
i32,
Option<String>,
)> = db.conn.query_row(
"SELECT id, email, display_name, password_hash, password_salt,
subscription_tier, subscription_expires_at, created_at, updated_at,
last_login_at, email_verified, avatar_url
FROM users WHERE email = ?1",
rusqlite::params![body.email.to_lowercase()],
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
row.get(5)?,
row.get(6)?,
row.get(7)?,
row.get(8)?,
row.get(9)?,
row.get(10)?,
row.get(11)?,
))
},
);
let (
id,
email,
display_name,
stored_hash,
salt,
tier_str,
sub_expires,
created_at,
updated_at,
_last_login,
verified,
avatar,
) = match user_result {
Ok(data) => data,
Err(_) => {
record_login_failure(&key);
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Invalid email or password"
}));
}
};
let (matches, needs_upgrade) = verify_password(&body.password, &stored_hash, &salt);
if !matches {
record_login_failure(&key);
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Invalid email or password"
}));
}
clear_login_failures(&key);
if needs_upgrade {
if let Ok(upgraded) = hash_password_argon2(&body.password) {
let _ = db.conn.execute(
"UPDATE users SET password_hash = ?1, password_salt = ?2 WHERE id = ?3",
rusqlite::params![upgraded, "", id],
);
}
}
let now = Utc::now().timestamp();
let _ = db.conn.execute(
"UPDATE users SET last_login_at = ?1 WHERE id = ?2",
rusqlite::params![now, id],
);
let user = User {
id,
email,
display_name,
password_hash: stored_hash,
subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
subscription_expires_at: sub_expires,
created_at,
updated_at,
last_login_at: Some(now),
email_verified: verified == 1,
avatar_url: avatar,
metadata: None,
};
let access_token = match generate_access_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate access token"
}))
}
};
let refresh_token = match generate_refresh_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate refresh token"
}))
}
};
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": AuthResponse {
user: PublicUser::from(user),
access_token,
refresh_token,
expires_in: JWT_EXPIRY_HOURS * 3600,
}
}))
}
pub async fn refresh_token(
app_state: web::Data<crate::api::state::AppState>,
body: web::Json<RefreshTokenRequest>,
) -> HttpResponse {
let claims = match validate_token_claims(&body.refresh_token) {
Some(c) if c.token_type == "refresh" => c,
_ => {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Invalid or expired refresh token"
}))
}
};
let db = app_state.db.lock().unwrap();
if !token_not_revoked(&db.conn, &claims.sub, claims.iat) {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Invalid or expired refresh token"
}));
}
let auth_user = AuthenticatedUser {
user_id: claims.sub,
email: claims.email,
tier: SubscriptionTier::from_str(&claims.tier).unwrap_or_default(),
};
let user_result: rusqlite::Result<(
String,
String,
String,
String,
Option<i64>,
i64,
i64,
i32,
Option<String>,
)> = db.conn.query_row(
"SELECT id, email, display_name, subscription_tier, subscription_expires_at,
created_at, updated_at, email_verified, avatar_url
FROM users WHERE id = ?1",
rusqlite::params![auth_user.user_id],
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
row.get(5)?,
row.get(6)?,
row.get(7)?,
row.get(8)?,
))
},
);
let (id, email, display_name, tier_str, sub_expires, created_at, updated_at, verified, avatar) =
match user_result {
Ok(data) => data,
Err(_) => {
return HttpResponse::NotFound().json(serde_json::json!({
"success": false,
"error": "User not found"
}))
}
};
let user = User {
id,
email,
display_name,
password_hash: String::new(), subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
subscription_expires_at: sub_expires,
created_at,
updated_at,
last_login_at: None,
email_verified: verified == 1,
avatar_url: avatar,
metadata: None,
};
let access_token = match generate_access_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate access token"
}))
}
};
let new_refresh_token = match generate_refresh_token(&user) {
Some(t) => t,
None => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": "Failed to generate refresh token"
}))
}
};
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": AuthResponse {
user: PublicUser::from(user),
access_token,
refresh_token: new_refresh_token,
expires_in: JWT_EXPIRY_HOURS * 3600,
}
}))
}
pub async fn get_profile(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
) -> HttpResponse {
if auth_user.user_id.is_empty() {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Not authenticated"
}));
}
let db = app_state.db.lock().unwrap();
let user_result: rusqlite::Result<(
String,
String,
String,
String,
Option<i64>,
i64,
i64,
i32,
Option<String>,
)> = db.conn.query_row(
"SELECT id, email, display_name, subscription_tier, subscription_expires_at,
created_at, updated_at, email_verified, avatar_url
FROM users WHERE id = ?1",
rusqlite::params![auth_user.user_id],
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
row.get(5)?,
row.get(6)?,
row.get(7)?,
row.get(8)?,
))
},
);
match user_result {
Ok((
id,
email,
display_name,
tier_str,
sub_expires,
created_at,
updated_at,
verified,
avatar,
)) => {
let user = User {
id,
email,
display_name,
password_hash: String::new(),
subscription_tier: SubscriptionTier::from_str(&tier_str).unwrap_or_default(),
subscription_expires_at: sub_expires,
created_at,
updated_at,
last_login_at: None,
email_verified: verified == 1,
avatar_url: avatar,
metadata: None,
};
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": PublicUser::from(user)
}))
}
Err(_) => HttpResponse::NotFound().json(serde_json::json!({
"success": false,
"error": "User not found"
})),
}
}
pub async fn update_profile(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
body: web::Json<UpdateProfileRequest>,
) -> HttpResponse {
if auth_user.user_id.is_empty() {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Not authenticated"
}));
}
{
let db = app_state.db.lock().unwrap();
let now = Utc::now().timestamp();
let mut updates = Vec::new();
let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();
if let Some(ref name) = body.display_name {
updates.push("display_name = ?");
params.push(Box::new(name.clone()));
}
if let Some(ref avatar) = body.avatar_url {
updates.push("avatar_url = ?");
params.push(Box::new(avatar.clone()));
}
if updates.is_empty() {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "No fields to update"
}));
}
updates.push("updated_at = ?");
params.push(Box::new(now));
let query = format!("UPDATE users SET {} WHERE id = ?", updates.join(", "));
params.push(Box::new(auth_user.user_id.clone()));
let params_refs: Vec<&dyn rusqlite::ToSql> = params.iter().map(|p| p.as_ref()).collect();
if let Err(e) = db.conn.execute(&query, params_refs.as_slice()) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to update profile: {}", e)
}));
}
}
get_profile(app_state, auth_user).await
}
pub async fn change_password(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
body: web::Json<ChangePasswordRequest>,
) -> HttpResponse {
if auth_user.user_id.is_empty() {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Not authenticated"
}));
}
if body.new_password.len() < MIN_PASSWORD_CHARS {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "New password must be at least 8 characters"
}));
}
if is_common_password(&body.new_password) {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "That password is one of the most commonly used ones and is \
guessed first in an attack. Please choose another."
}));
}
let db = app_state.db.lock().unwrap();
let creds: rusqlite::Result<(String, String)> = db.conn.query_row(
"SELECT password_hash, password_salt FROM users WHERE id = ?1",
rusqlite::params![auth_user.user_id],
|row| Ok((row.get(0)?, row.get(1)?)),
);
let (stored_hash, salt) = match creds {
Ok(c) => c,
Err(_) => {
return HttpResponse::NotFound().json(serde_json::json!({
"success": false,
"error": "User not found"
}))
}
};
let (matches, _) = verify_password(&body.current_password, &stored_hash, &salt);
if !matches {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Current password is incorrect"
}));
}
let new_salt = String::new();
let new_hash = match hash_password_argon2(&body.new_password) {
Ok(h) => h,
Err(e) => {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to hash password: {}", e)
}))
}
};
let now = Utc::now().timestamp();
if let Err(e) = db.conn.execute(
"UPDATE users SET password_hash = ?1, password_salt = ?2, updated_at = ?3,
tokens_valid_after = ?4 WHERE id = ?5",
rusqlite::params![new_hash, new_salt, now, now + 1, auth_user.user_id],
) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to update password: {}", e)
}));
}
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"message": "Password updated successfully"
}))
}
pub async fn get_subscription(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
) -> HttpResponse {
if auth_user.user_id.is_empty() {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Not authenticated"
}));
}
let db = app_state.db.lock().unwrap();
let result: rusqlite::Result<(String, Option<i64>)> = db.conn.query_row(
"SELECT subscription_tier, subscription_expires_at FROM users WHERE id = ?1",
rusqlite::params![auth_user.user_id],
|row| Ok((row.get(0)?, row.get(1)?)),
);
match result {
Ok((tier_str, expires_at)) => {
let tier = SubscriptionTier::from_str(&tier_str).unwrap_or_default();
let features = SubscriptionFeatures::for_tier(tier);
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": {
"tier": tier_str,
"expiresAt": expires_at,
"features": features,
"isActive": expires_at.map(|exp| exp > Utc::now().timestamp()).unwrap_or(true),
}
}))
}
Err(_) => HttpResponse::NotFound().json(serde_json::json!({
"success": false,
"error": "User not found"
})),
}
}
pub async fn upgrade_subscription(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
body: web::Json<UpgradeSubscriptionRequest>,
) -> HttpResponse {
if auth_user.user_id.is_empty() {
return HttpResponse::Unauthorized().json(serde_json::json!({
"success": false,
"error": "Not authenticated"
}));
}
let new_tier = match SubscriptionTier::from_str(&body.tier) {
Some(t) => t,
None => {
return HttpResponse::BadRequest().json(serde_json::json!({
"success": false,
"error": "Invalid subscription tier"
}))
}
};
let db = app_state.db.lock().unwrap();
let now = Utc::now().timestamp();
let expires_at = if new_tier == SubscriptionTier::Free {
None
} else {
Some(now + (365 * 24 * 3600))
};
if let Err(e) = db.conn.execute(
"UPDATE users SET subscription_tier = ?1, subscription_expires_at = ?2, updated_at = ?3 WHERE id = ?4",
rusqlite::params![new_tier.as_str(), expires_at, now, auth_user.user_id],
) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to upgrade subscription: {}", e)
}));
}
let features = SubscriptionFeatures::for_tier(new_tier);
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": {
"tier": new_tier.as_str(),
"expiresAt": expires_at,
"features": features,
"message": "Subscription upgraded successfully"
}
}))
}
pub async fn logout(
app_state: web::Data<crate::api::state::AppState>,
auth_user: AuthenticatedUser,
) -> HttpResponse {
let now = Utc::now().timestamp();
let db = app_state.db.lock().unwrap();
if let Err(e) = db.conn.execute(
"UPDATE users SET tokens_valid_after = ?1 WHERE id = ?2",
rusqlite::params![now + 1, auth_user.user_id],
) {
return HttpResponse::InternalServerError().json(serde_json::json!({
"success": false,
"error": format!("Failed to invalidate session: {}", e)
}));
}
HttpResponse::Ok().json(serde_json::json!({
"success": true,
"message": "Logged out successfully"
}))
}
pub fn configure_auth_routes(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/auth")
.route("/register", web::post().to(register))
.route("/login", web::post().to(login))
.route("/refresh", web::post().to(refresh_token))
.route("/logout", web::post().to(logout))
.route("/profile", web::get().to(get_profile))
.route("/profile", web::put().to(update_profile))
.route("/password", web::put().to(change_password))
.route("/subscription", web::get().to(get_subscription))
.route(
"/subscription/upgrade",
web::post().to(upgrade_subscription),
),
);
}
#[cfg(test)]
mod security_tests {
use super::*;
#[test]
fn a_token_is_revoked_once_valid_after_passes_its_issue_time() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
init_auth_tables(&conn).unwrap();
conn.execute(
"INSERT INTO users (id, email, display_name, password_hash, password_salt,
created_at, updated_at, tokens_valid_after)
VALUES ('u1','u1@e.co','U','h','', 0, 0, 0)",
[],
)
.unwrap();
assert!(token_not_revoked(&conn, "u1", 1000));
conn.execute(
"UPDATE users SET tokens_valid_after = 1500 WHERE id = 'u1'",
[],
)
.unwrap();
assert!(
!token_not_revoked(&conn, "u1", 1000),
"old token still current"
);
assert!(
token_not_revoked(&conn, "u1", 1500),
"cut-off itself accepted"
);
assert!(token_not_revoked(&conn, "u1", 2000), "newer token current");
assert!(token_not_revoked(&conn, "nobody", 1));
}
#[test]
fn the_most_guessed_passwords_are_refused_whatever_their_case() {
assert!(is_common_password("password"));
assert!(is_common_password("12345678"));
assert!(is_common_password("Password"));
assert!(is_common_password("PASSWORD"));
assert!(is_common_password("PaSsWoRd"));
assert!(is_common_password("QwErTy123"));
assert!(is_common_password("password123"));
assert!(is_common_password("passw0rd"));
assert!(is_common_password("p@ssw0rd"));
assert!(is_common_password("letmein123"));
assert!(is_common_password("qwertyuiop"));
assert!(is_common_password("qwerty"));
assert!(is_common_password("123456"));
assert!(!is_common_password("Aud1t-Probe-Passw0rd!"));
assert!(!is_common_password("correct horse battery staple"));
assert!(!is_common_password("wR7#pq2Lm!vZ"));
assert!(!is_common_password("mypasswordislong"));
assert!(!is_common_password("password!extra"));
}
#[test]
fn logout_revokes_tokens_issued_up_to_and_including_that_second() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
init_auth_tables(&conn).unwrap();
conn.execute(
"INSERT INTO users (id, email, display_name, password_hash, password_salt,
created_at, updated_at, tokens_valid_after)
VALUES ('u1','u1@e.co','U','h','', 0, 0, 0)",
[],
)
.unwrap();
let now = 1_000_000i64;
assert!(
token_not_revoked(&conn, "u1", now),
"token is current before logout"
);
conn.execute(
"UPDATE users SET tokens_valid_after = ?1 WHERE id = ?2",
rusqlite::params![now + 1, "u1"],
)
.unwrap();
assert!(
!token_not_revoked(&conn, "u1", now - 60),
"token from before logout must be rejected"
);
assert!(
!token_not_revoked(&conn, "u1", now),
"token minted in the same second as logout must be rejected"
);
assert!(
token_not_revoked(&conn, "u1", now + 1),
"a token issued after logout must still work, or login is broken"
);
}
#[test]
fn login_throttle_blocks_after_the_budget_and_resets_on_success() {
let key = format!("test-key-{}", uuid::Uuid::new_v4());
assert!(!login_throttled(&key));
for _ in 0..LOGIN_MAX_FAILURES {
record_login_failure(&key);
}
assert!(login_throttled(&key), "not throttled after the budget");
clear_login_failures(&key);
assert!(
!login_throttled(&key),
"a success did not reset the counter"
);
}
#[test]
fn login_throttle_allows_up_to_the_budget() {
let key = format!("test-key-{}", uuid::Uuid::new_v4());
for _ in 0..(LOGIN_MAX_FAILURES - 1) {
record_login_failure(&key);
}
assert!(!login_throttled(&key));
}
#[test]
fn a_token_signed_now_round_trips_and_tampering_is_rejected() {
let user = User {
id: "u1".into(),
email: "u1@example.com".into(),
display_name: "U".into(),
password_hash: String::new(),
subscription_tier: SubscriptionTier::Free,
subscription_expires_at: None,
created_at: 0,
updated_at: 0,
last_login_at: None,
email_verified: false,
avatar_url: None,
metadata: None,
};
let token = generate_access_token(&user).expect("signed");
assert!(validate_token(&token).is_some());
let mut bad = token.clone();
bad.pop();
bad.push(if token.ends_with('a') { 'b' } else { 'a' });
assert!(validate_token(&bad).is_none(), "a tampered token verified");
}
#[test]
fn a_token_forged_with_the_old_hardcoded_secret_is_rejected() {
use jsonwebtoken::{encode, EncodingKey, Header};
const OLD_SECRET: &[u8] = b"ironbridge_jwt_secret_key_change_in_production_2024";
let claims = Claims {
sub: "attacker".into(),
email: "attacker@evil.test".into(),
tier: "enterprise".into(),
iat: Utc::now().timestamp(),
exp: (Utc::now() + Duration::hours(1)).timestamp(),
token_type: "access".into(),
};
let forged = encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(OLD_SECRET),
)
.expect("forge");
assert!(
validate_token(&forged).is_none(),
"a token forged with the leaked constant still validated"
);
}
#[test]
fn new_hashes_are_argon2_and_verify() {
let hash = hash_password_argon2("correct horse battery staple").expect("hash");
assert!(
hash.starts_with("$argon2"),
"not an argon2 PHC string: {hash}"
);
let (ok, upgrade) = verify_password("correct horse battery staple", &hash, "");
assert!(ok);
assert!(!upgrade, "an argon2 hash should not ask to be upgraded");
let (bad, _) = verify_password("wrong password", &hash, "");
assert!(!bad);
}
#[test]
fn argon2_salts_are_unique_per_hash() {
let a = hash_password_argon2("same").unwrap();
let b = hash_password_argon2("same").unwrap();
assert_ne!(a, b);
}
#[test]
fn a_legacy_hash_verifies_and_requests_upgrade() {
let salt = "legacy-salt";
let legacy = legacy_sha256("hunter2", salt);
assert!(!legacy.starts_with("$argon2"));
let (ok, upgrade) = verify_password("hunter2", &legacy, salt);
assert!(ok, "legacy password did not verify");
assert!(upgrade, "legacy verify did not flag an upgrade");
let (bad, _) = verify_password("not it", &legacy, salt);
assert!(!bad);
}
#[test]
fn legacy_compare_rejects_mismatched_lengths() {
assert!(!constant_time_eq(b"abc", b"abcd"));
assert!(constant_time_eq(b"abcd", b"abcd"));
assert!(!constant_time_eq(b"abcd", b"abce"));
}
}
#[cfg(test)]
mod enforcement_tests {
use super::*;
#[test]
fn the_gate_is_default_deny_with_a_small_open_list() {
assert!(path_requires_auth("/api/sessions"));
assert!(path_requires_auth("/api/swarms/x/agents"));
assert!(path_requires_auth("/sync/snapshot"));
assert!(path_requires_auth("/recording/sessions"));
assert!(path_requires_auth("/audit"));
assert!(path_requires_auth("/retention/policies"));
assert!(path_requires_auth("/webhooks"));
assert!(path_requires_auth("/graphql"));
assert!(path_requires_auth("/ws"));
assert!(path_requires_auth("/some/future/scope"));
assert!(!path_requires_auth("/api/health"));
assert!(!path_requires_auth("/health"));
assert!(!path_requires_auth("/api/system/health"));
assert!(!path_requires_auth("/auth/login"));
assert!(!path_requires_auth("/auth/register"));
assert!(!path_requires_auth("/sso/login"));
assert!(!path_requires_auth("/oidc/callback"));
assert!(!path_requires_auth("/docs"));
assert!(!path_requires_auth("/docs/openapi.yaml"));
assert!(path_requires_auth("/docs-admin"));
assert!(path_requires_auth("/docsecrets"));
assert!(path_requires_auth("/authorize"));
assert!(path_requires_auth("/ssoconfig"));
assert!(path_requires_auth("/oidcadmin"));
assert!(path_requires_auth("/healthz"));
}
#[tokio::test]
async fn the_gate_opens_and_closes_with_a_valid_token() {
use actix_web::{middleware::from_fn, web, App, HttpResponse};
async fn ok() -> HttpResponse {
HttpResponse::Ok().json(serde_json::json!({ "ok": true }))
}
let app = actix_web::test::init_service(
App::new().wrap(from_fn(require_auth)).service(
web::scope("/api")
.route("/health", web::get().to(ok))
.route("/sessions", web::get().to(ok)),
),
)
.await;
let user = User {
id: "u1".into(),
email: "u1@example.com".into(),
display_name: "U".into(),
password_hash: String::new(),
subscription_tier: SubscriptionTier::Free,
subscription_expires_at: None,
created_at: 0,
updated_at: 0,
last_login_at: None,
email_verified: false,
avatar_url: None,
metadata: None,
};
let token = generate_access_token(&user).expect("token");
let get = |uri: &str, bearer: Option<&str>| {
let mut r = actix_web::test::TestRequest::get().uri(uri);
if let Some(b) = bearer {
r = r.insert_header(("Authorization", format!("Bearer {b}")));
}
r.to_request()
};
if auth_required() {
let s = actix_web::test::call_service(&app, get("/api/sessions", None)).await;
assert_eq!(s.status(), 401);
let s = actix_web::test::call_service(&app, get("/api/sessions", Some(&token))).await;
assert_eq!(s.status(), 200);
let s = actix_web::test::call_service(&app, get("/api/health", None)).await;
assert_eq!(s.status(), 200);
} else {
let s = actix_web::test::call_service(&app, get("/api/sessions", None)).await;
assert_eq!(s.status(), 200);
let s = actix_web::test::call_service(&app, get("/api/health", None)).await;
assert_eq!(s.status(), 200);
}
}
#[test]
fn query_token_accepted_only_on_websocket_routes() {
let user = User {
id: "u1".into(),
email: "u1@example.com".into(),
display_name: "U".into(),
password_hash: String::new(),
subscription_tier: SubscriptionTier::Free,
subscription_expires_at: None,
created_at: 0,
updated_at: 0,
last_login_at: None,
email_verified: false,
avatar_url: None,
metadata: None,
};
let token = generate_access_token(&user).expect("token");
let plain = actix_web::test::TestRequest::get()
.uri(&format!("/api/sessions?token={token}"))
.to_srv_request();
assert!(
request_token(&plain).is_none(),
"a ?token= on a non-WebSocket route must not authenticate"
);
let spoof = actix_web::test::TestRequest::get()
.uri(&format!("/api/sessions?token={token}"))
.insert_header(("Upgrade", "websocket"))
.to_srv_request();
assert!(
request_token(&spoof).is_none(),
"a forged Upgrade header on a non-WS route must not authenticate from the URL"
);
let ws_query = actix_web::test::TestRequest::get()
.uri(&format!("/ws?token={token}"))
.to_srv_request();
assert!(
request_token(&ws_query).is_some(),
"a ?token= on the /ws route must authenticate"
);
let ws_subproto = actix_web::test::TestRequest::get()
.uri("/ws")
.insert_header(("Sec-WebSocket-Protocol", format!("bearer, {token}")))
.to_srv_request();
assert!(
request_token(&ws_subproto).is_some(),
"a bearer subprotocol on /ws must authenticate"
);
let hdr = actix_web::test::TestRequest::get()
.uri("/api/sessions")
.insert_header(("Authorization", format!("Bearer {token}")))
.to_srv_request();
assert!(
request_token(&hdr).is_some(),
"the Authorization header must still authenticate ordinary requests"
);
}
}