use std::sync::Arc;
use async_trait::async_trait;
use axum::http::request::Parts;
use crate::sql::sqlx;
use crate::sql::Pool;
use super::auth::parse_basic_auth;
use super::password;
use super::password::HashLane;
use crate::login_throttle::{ClientIp, LoginAttempt, LoginScope};
#[derive(Debug, Clone)]
pub struct AuthUser {
pub id: i64,
pub username: String,
pub is_superuser: bool,
}
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
#[error("database error: {0}")]
Database(#[from] sqlx::Error),
#[error("database error: {0}")]
Exec(#[from] crate::sql::ExecError),
#[error("token is malformed or expired")]
InvalidToken,
#[error("account is inactive")]
Inactive,
#[error("login refused: {0:?}")]
Refused(crate::login_throttle::LoginRefused),
}
impl AuthError {
fn from_hash(e: super::TenancyError) -> Self {
match e {
super::TenancyError::Busy => Self::Refused(crate::login_throttle::LoginRefused::Busy),
_ => Self::InvalidToken,
}
}
}
async fn begin(
parts: &Parts,
scope: fn(String) -> LoginScope,
username: &str,
) -> Result<LoginAttempt, AuthError> {
let slug = tenant_slug(parts)?;
crate::login_throttle::shared()
.begin(&scope(slug.to_owned()), &client_ip(parts), username)
.await
.map_err(AuthError::Refused)
}
fn client_ip(parts: &Parts) -> ClientIp {
parts
.extensions
.get::<ClientIp>()
.cloned()
.unwrap_or_else(|| ClientIp::from_parts(&parts.extensions, &parts.headers))
}
fn tenant_slug(parts: &Parts) -> Result<&str, AuthError> {
match parts.extensions.get::<super::TenantSlug>() {
Some(slug) => Ok(&slug.0),
None => {
tracing::error!(
"auth backend ran without a TenantSlug; use require_auth / optional_auth"
);
Err(AuthError::InvalidToken)
}
}
}
const BASIC_CACHE_TTL: std::time::Duration = std::time::Duration::from_secs(30);
const BASIC_CACHE_CAP: usize = 1024;
type BasicCache = std::collections::HashMap<[u8; 32], (i64, std::time::Instant)>;
fn basic_cache() -> &'static std::sync::Mutex<BasicCache> {
static CACHE: std::sync::OnceLock<std::sync::Mutex<BasicCache>> = std::sync::OnceLock::new();
CACHE.get_or_init(|| std::sync::Mutex::new(BasicCache::new()))
}
fn basic_cache_key(slug: &str, username: &str, password: &str, stored_hash: &str) -> [u8; 32] {
use sha2::Digest as _;
let mut hasher = sha2::Sha256::new();
for part in [slug, username, password, stored_hash] {
hasher.update((part.len() as u64).to_le_bytes());
hasher.update(part.as_bytes());
}
hasher.finalize().into()
}
fn basic_cache_hit(key: &[u8; 32], user_id: i64) -> bool {
let Ok(cache) = basic_cache().lock() else {
return false;
};
cache
.get(key)
.is_some_and(|(id, at)| *id == user_id && at.elapsed() < BASIC_CACHE_TTL)
}
fn basic_cache_put(key: [u8; 32], user_id: i64) {
let Ok(mut cache) = basic_cache().lock() else {
return;
};
if cache.len() >= BASIC_CACHE_CAP && !cache.contains_key(&key) {
cache.retain(|_, (_, at)| at.elapsed() < BASIC_CACHE_TTL);
if cache.len() >= BASIC_CACHE_CAP {
let oldest = cache.iter().min_by_key(|(_, (_, at))| *at).map(|(k, _)| *k);
if let Some(oldest) = oldest {
cache.remove(&oldest);
}
}
}
cache.insert(key, (user_id, std::time::Instant::now()));
}
#[async_trait]
pub trait AuthBackend: Send + Sync {
async fn authenticate(&self, parts: &Parts, pool: &Pool)
-> Result<Option<AuthUser>, AuthError>;
}
pub type BoxedBackend = Arc<dyn AuthBackend>;
pub struct ModelBackend;
#[async_trait]
impl AuthBackend for ModelBackend {
async fn authenticate(
&self,
parts: &Parts,
pool: &Pool,
) -> Result<Option<AuthUser>, AuthError> {
use crate::core::Column as _;
use crate::sql::FetcherPool as _;
let auth_header = parts
.headers
.get(axum::http::header::AUTHORIZATION)
.and_then(|v| v.to_str().ok());
let (username, password) = match parse_basic_auth(auth_header) {
Some(pair) => pair,
None => return Ok(None),
};
let slug = tenant_slug(parts)?;
let scope = LoginScope::TenantBasic(slug.to_owned());
let ip = client_ip(parts);
let throttle = crate::login_throttle::shared();
let refused = AuthError::Refused;
throttle
.check(&scope, &ip, &username)
.await
.map_err(refused)?;
let users = super::auth::User::objects()
.where_(super::auth::User::username.eq(username.clone()))
.fetch(pool)
.await?;
let user = users.into_iter().next();
let cache_key = user
.as_ref()
.map(|u| basic_cache_key(slug, &username, &password, &u.password_hash));
let cached = |u: &super::auth::User| {
let uid = u.id.get().copied().unwrap_or(0);
let key = cache_key.as_ref()?;
(u.active && uid != 0 && basic_cache_hit(key, uid)).then(|| AuthUser {
id: uid,
username: u.username.clone(),
is_superuser: u.is_superuser,
})
};
if let Some(hit) = user.as_ref().and_then(cached) {
if hit.username != username {
throttle
.check(&scope, &ip, &hit.username)
.await
.map_err(refused)?;
}
return Ok(Some(hit));
}
let mut attempt = throttle
.begin(&scope, &ip, &username)
.await
.map_err(refused)?;
let Some(user) = user else {
password::verify_dummy_async_in(HashLane::Credential, &password)
.await
.map_err(AuthError::from_hash)?;
attempt.failed().await;
return Ok(None);
};
attempt
.resolve(&user.username)
.await
.map_err(AuthError::Refused)?;
if let Some(hit) = cached(&user) {
attempt.succeeded().await;
return Ok(Some(hit));
}
let ok = password::verify_async_in(HashLane::Credential, &password, &user.password_hash)
.await
.map_err(AuthError::from_hash)?;
if !user.active || !ok {
attempt.failed().await;
return Ok(None);
}
attempt.succeeded().await;
let id = user.id.get().copied().unwrap_or(0);
if let (Some(key), true) = (cache_key, id != 0) {
basic_cache_put(key, id);
}
Ok(Some(AuthUser {
id,
username: user.username,
is_superuser: user.is_superuser,
}))
}
}
#[derive(crate::Model, Debug, Clone)]
#[rustango(
table = "rustango_api_keys",
admin(
list_display = "user_id, key_prefix, label, expires_at, created_at",
ordering = "-created_at",
readonly_fields = "key_prefix, key_hash, created_at",
)
)]
pub struct ApiKey {
#[rustango(primary_key)]
pub id: crate::sql::Auto<i64>,
#[rustango(fk = "rustango_users", on = "id", on_delete = "cascade")]
pub user_id: i64,
#[rustango(max_length = 8, index)]
pub key_prefix: String,
#[rustango(max_length = 255)]
pub key_hash: String,
#[rustango(max_length = 100)]
pub label: String,
pub expires_at: Option<chrono::DateTime<chrono::Utc>>,
#[rustango(auto_now_add)]
pub created_at: crate::sql::Auto<chrono::DateTime<chrono::Utc>>,
}
#[cfg(feature = "admin")]
crate::register_admin_object_permission!("rustango_api_keys", "add", |_, _| false);
#[cfg(feature = "admin")]
crate::register_admin_superuser_fields!(ApiKey, Change, [user_id]);
#[cfg(feature = "postgres")]
pub async fn ensure_api_keys_table(pool: &crate::sql::sqlx::PgPool) -> Result<(), sqlx::Error> {
ensure_api_keys_table_pool(&crate::sql::Pool::from(pool.clone())).await
}
pub async fn ensure_api_keys_table_pool(pool: &Pool) -> Result<(), sqlx::Error> {
use crate::core::Model as _;
let snapshot = crate::migrate::SchemaSnapshot::from_models(&[ApiKey::SCHEMA]);
crate::migrate::apply_idempotent(pool, &snapshot).await?;
Ok(())
}
pub struct ApiKeyBackend;
#[async_trait]
impl AuthBackend for ApiKeyBackend {
async fn authenticate(
&self,
parts: &Parts,
pool: &Pool,
) -> Result<Option<AuthUser>, AuthError> {
use crate::core::Column as _;
use crate::sql::FetcherPool as _;
let bearer = extract_bearer(parts)?;
let Some(token) = bearer else {
return Ok(None);
};
let (prefix, secret) = match token.split_once('.') {
Some(p) => p,
None => return Ok(None), };
if prefix.len() != 8 {
return Ok(None);
}
let attempt = begin(parts, LoginScope::TenantApiKey, "").await?;
let mac = api_key_mac(api_key_secret().map_err(AuthError::from_hash)?, secret);
let keys = ApiKey::objects()
.where_(ApiKey::key_prefix.eq(prefix.to_owned()))
.fetch(pool)
.await?;
let (macs, legacy): (Vec<_>, Vec<_>) = keys
.into_iter()
.partition(|k| k.key_hash.starts_with(API_KEY_MAC_SCHEME));
let key = match macs.into_iter().find(|k| mac_eq(&k.key_hash, &mac)) {
Some(k) => Some(k),
None if legacy.is_empty() => None,
None => {
let key = password::first_verified(legacy, secret, |k| &k.key_hash)
.await
.map_err(AuthError::from_hash)?;
if let Some(k) = &key {
upgrade_api_key(pool, k, &mac).await;
}
key
}
};
let Some(key) = key else {
attempt.failed().await;
return Ok(None);
};
if key.expires_at.is_some_and(|exp| chrono::Utc::now() > exp) {
return Err(AuthError::InvalidToken);
}
attempt.succeeded().await;
let users = super::auth::User::objects()
.where_(super::auth::User::id.eq(key.user_id))
.fetch(pool)
.await?;
let Some(user) = users.into_iter().next() else {
return Ok(None);
};
if !user.active {
return Err(AuthError::Inactive);
}
Ok(Some(AuthUser {
id: user.id.get().copied().unwrap_or(0),
username: user.username,
is_superuser: user.is_superuser,
}))
}
}
const API_KEY_MAC_SCHEME: &str = "hmac-sha256$";
const API_KEY_MAC_TAG: &[u8] = b"rustango-apikey-v1.";
static API_KEY_SECRET: std::sync::OnceLock<crate::session::SessionSecret> =
std::sync::OnceLock::new();
pub fn set_api_key_secret(secret: crate::session::SessionSecret) -> bool {
API_KEY_SECRET.set(secret).is_ok()
}
fn api_key_secret() -> Result<&'static crate::session::SessionSecret, super::TenancyError> {
use crate::session::{is_prod_tier, tier_from_env, SessionSecret};
if let Some(s) = API_KEY_SECRET.get() {
return Ok(s);
}
let secret = if is_prod_tier(&tier_from_env()) {
SessionSecret::require_from_env().map_err(|e| {
tracing::error!(error = %e, "API keys need RUSTANGO_SESSION_SECRET on the prod tier");
super::TenancyError::Validation(e.to_string())
})?
} else {
SessionSecret::from_env_or_disk(std::path::Path::new(
crate::session::TENANT_SESSION_KEY_PATH,
))
};
Ok(API_KEY_SECRET.get_or_init(|| secret))
}
fn api_key_mac(key: &crate::session::SessionSecret, secret: &str) -> String {
let msg = [API_KEY_MAC_TAG, secret.as_bytes()].concat();
format!(
"{API_KEY_MAC_SCHEME}{}",
to_hex(&crate::session::sign(key, &msg))
)
}
fn mac_eq(stored: &str, mac: &str) -> bool {
use subtle::ConstantTimeEq as _;
stored.len() == mac.len() && bool::from(stored.as_bytes().ct_eq(mac.as_bytes()))
}
async fn upgrade_api_key(pool: &Pool, key: &ApiKey, mac: &str) {
use crate::core::Column as _;
use crate::sql::UpdaterPool as _;
let Some(id) = key.id.get().copied() else {
return;
};
if let Err(e) = ApiKey::objects()
.where_(ApiKey::id.eq(id))
.update()
.set("key_hash", mac.to_owned())
.execute_pool(pool)
.await
{
tracing::warn!(error = %e, key_id = id, "could not upgrade an argon2 API key");
}
}
pub async fn create_api_key(
user_id: i64,
label: &str,
expires_at: Option<chrono::DateTime<chrono::Utc>>,
pool: &Pool,
) -> Result<String, crate::tenancy::error::TenancyError> {
use crate::sql::Auto;
use rand::rngs::OsRng;
use rand::RngCore;
let mut prefix_bytes = [0u8; 4];
OsRng.fill_bytes(&mut prefix_bytes);
let prefix = to_hex(&prefix_bytes);
let mut secret_bytes = [0u8; 16];
OsRng.fill_bytes(&mut secret_bytes);
let secret = to_hex(&secret_bytes);
let hash = api_key_mac(api_key_secret()?, &secret);
let mut key = ApiKey {
id: Auto::default(),
user_id,
key_prefix: prefix.clone(),
key_hash: hash,
label: label.to_owned(),
expires_at,
created_at: Auto::default(),
};
key.save_pool(pool).await?;
Ok(format!("{prefix}.{secret}"))
}
pub struct JwtBackend {
secret: Vec<u8>,
pwf_secret: crate::session::SessionSecret,
pub ttl_secs: i64,
jti_store: Option<std::sync::Arc<dyn crate::jti_store::JtiStore>>,
warned_unchecked: std::sync::atomic::AtomicBool,
}
impl JwtBackend {
#[must_use]
pub fn new(secret: Vec<u8>) -> Self {
assert!(
secret.len() >= 32,
"JwtBackend signing key is too short; need >= 32 bytes (a shorter key is forgeable)",
);
Self {
pwf_secret: crate::session::SessionSecret::from_bytes(secret.clone()),
secret,
ttl_secs: 3600,
jti_store: None,
warned_unchecked: std::sync::atomic::AtomicBool::new(false),
}
}
#[must_use]
pub fn with_jti_store(mut self, store: std::sync::Arc<dyn crate::jti_store::JtiStore>) -> Self {
self.jti_store = Some(store);
self
}
fn warn_unchecked_revocation(&self) {
use std::sync::atomic::Ordering;
if !self.warned_unchecked.swap(true, Ordering::Relaxed) {
tracing::warn!(
target: "rustango::tenancy",
"JwtBackend has no JTI store, so tokens are not checked for revocation: a \
logged-out token works until it expires; call `with_jti_store`"
);
}
}
#[cfg(feature = "postgres")]
#[must_use]
pub fn from_session_secret(s: &super::operator_console::SessionSecret) -> Self {
Self::new(s.key().to_vec())
}
#[must_use]
pub fn issue(&self, user_id: i64) -> String {
self.sign_payload(&serde_json::json!({"sub": user_id, "exp": self.exp()}))
}
#[must_use]
pub fn issue_for_tenant(&self, user_id: i64, slug: &str) -> String {
let mut payload = serde_json::json!({"sub": user_id, "exp": self.exp()});
payload[super::jwt_lifecycle::CLAIM_TENANT] = slug.into();
self.sign_payload(&payload)
}
fn exp(&self) -> i64 {
chrono::Utc::now().timestamp() + self.ttl_secs
}
fn sign_payload(&self, payload: &serde_json::Value) -> String {
use base64::Engine;
let payload_b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD
.encode(serde_json::to_vec(payload).unwrap_or_default());
let sig = hmac_sha256(&self.secret, payload_b64.as_bytes());
let sig_b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(sig);
format!("{payload_b64}.{sig_b64}")
}
fn verify_claims(
&self,
token: &str,
scope: super::jwt_lifecycle::UserTokenScope<'_>,
) -> Option<(
i64,
Option<String>,
Option<super::auth_routes::RefreshSession>,
)> {
use base64::Engine;
use subtle::ConstantTimeEq;
let b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD;
let parts: Vec<&str> = token.split('.').collect();
let (signing_input, payload_b64, sig_b64) = match parts.as_slice() {
[header, payload, sig] => {
let h: serde_json::Value =
serde_json::from_slice(&b64.decode(header).ok()?).ok()?;
if h.get("alg").and_then(serde_json::Value::as_str) != Some("HS256") {
return None;
}
(format!("{header}.{payload}"), *payload, *sig)
}
[payload, sig] => ((*payload).to_owned(), *payload, *sig),
_ => return None,
};
let expected = hmac_sha256(&self.secret, signing_input.as_bytes());
let provided = b64.decode(sig_b64).ok()?;
if expected.ct_eq(&provided[..]).unwrap_u8() == 0 {
return None;
}
let payload: serde_json::Value =
serde_json::from_slice(&b64.decode(payload_b64).ok()?).ok()?;
let exp = payload.get("exp")?.as_i64()?;
if chrono::Utc::now().timestamp() >= exp {
return None; }
if let Some(typ) = payload.get("typ").and_then(serde_json::Value::as_str) {
if typ != "access" {
return None;
}
}
let claims = payload.as_object()?;
scope.admits(claims).ok()?;
let sub = payload.get("sub")?.as_i64()?;
let jti = payload
.get("jti")
.and_then(serde_json::Value::as_str)
.map(str::to_owned);
Some((sub, jti, super::auth_routes::RefreshSession::read(claims)))
}
}
fn hmac_sha256(secret: &[u8], msg: &[u8]) -> [u8; 32] {
use hmac::{Hmac, Mac};
use sha2::Sha256;
let mut mac = Hmac::<Sha256>::new_from_slice(secret).expect("HMAC accepts any key length");
mac.update(msg);
let bytes = mac.finalize().into_bytes();
let mut out = [0u8; 32];
out.copy_from_slice(&bytes[..32]);
out
}
#[async_trait]
impl AuthBackend for JwtBackend {
async fn authenticate(
&self,
parts: &Parts,
pool: &Pool,
) -> Result<Option<AuthUser>, AuthError> {
use crate::core::Column as _;
use crate::sql::FetcherPool as _;
let bearer = extract_bearer(parts)?;
let Some(token) = bearer else {
return Ok(None);
};
let dots = token.chars().filter(|&c| c == '.').count();
if dots != 1 && dots != 2 {
return Ok(None);
}
if dots == 1 && token.split_once('.').map(|(p, _)| p.len()) == Some(8) {
return Ok(None);
}
let scope = match parts.extensions.get::<super::TenantSlug>() {
Some(slug) => super::jwt_lifecycle::UserTokenScope::Tenant(&slug.0),
None => super::jwt_lifecycle::UserTokenScope::Unscoped,
};
let (user_id, jti, session) = match self.verify_claims(token, scope) {
Some(c) => c,
None => return Err(AuthError::InvalidToken),
};
match (self.jti_store.as_ref(), jti.as_deref()) {
(Some(store), Some(jti)) if store.is_used(jti).await => {
return Err(AuthError::InvalidToken);
}
(None, Some(_)) => self.warn_unchecked_revocation(),
_ => {}
}
let users = super::auth::User::objects()
.where_(super::auth::User::id.eq(user_id))
.fetch(pool)
.await?;
let Some(user) = users.into_iter().next() else {
return Ok(None);
};
if !user.active {
return Err(AuthError::Inactive);
}
if let Some(session) = session {
let check = super::auth_routes::SessionCheck {
pwf_secret: &self.pwf_secret,
families: self.jti_store.as_deref(),
cap_secs: None,
};
if !session.admits(&check, &user).await {
return Err(AuthError::InvalidToken);
}
}
Ok(Some(AuthUser {
id: user.id.get().copied().unwrap_or(0),
username: user.username,
is_superuser: user.is_superuser,
}))
}
}
fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn extract_bearer(parts: &Parts) -> Result<Option<&str>, AuthError> {
let Some(value) = parts.headers.get(axum::http::header::AUTHORIZATION) else {
return Ok(None);
};
let s = value.to_str().unwrap_or("");
Ok(s.strip_prefix("Bearer ").map(str::trim))
}
#[cfg(all(test, feature = "sqlite"))]
mod tests {
use super::*;
#[test]
fn api_key_mac_binds_key_and_secret() {
use crate::session::SessionSecret;
let (a, b) = (
SessionSecret::from_bytes(vec![1; 32]),
SessionSecret::from_bytes(vec![2; 32]),
);
let mac = api_key_mac(&a, "s3cret");
assert!(mac.starts_with(API_KEY_MAC_SCHEME) && mac.len() < 255);
assert!(mac_eq(&mac, &api_key_mac(&a, "s3cret")));
assert_ne!(mac, api_key_mac(&b, "s3cret"));
assert_ne!(mac, api_key_mac(&a, "s3creu"));
}
async fn revocation_warnings(backend: &JwtBackend, token: &str) -> usize {
let buf = crate::testkit::CaptureWriter::default();
let subscriber = tracing_subscriber::fmt()
.with_writer(buf.clone())
.with_ansi(false)
.finish();
let _g = tracing::subscriber::set_default(subscriber);
let pool = Pool::connect("sqlite::memory:").await.unwrap();
for _ in 0..2 {
let (parts, ()) = axum::http::Request::builder()
.header("authorization", format!("Bearer {token}"))
.body(())
.unwrap()
.into_parts();
let _ = backend.authenticate(&parts, &pool).await;
}
buf.contents().matches("not checked for revocation").count()
}
#[tokio::test]
async fn a_revocable_token_without_a_jti_store_warns_once() {
let token = super::super::jwt_lifecycle::JwtLifecycle::new(vec![7u8; 32])
.issue_pair(42)
.access;
let backend = JwtBackend::new(vec![7u8; 32]);
assert_eq!(revocation_warnings(&backend, &token).await, 1);
}
#[test]
fn basic_cache_key_binds_hash_and_tenant() {
let k = basic_cache_key("t", "alice", "pw", "$argon2id$a");
assert_eq!(k, basic_cache_key("t", "alice", "pw", "$argon2id$a"));
assert_ne!(k, basic_cache_key("t", "alice", "pw", "$argon2id$b"));
assert_ne!(k, basic_cache_key("u", "alice", "pw", "$argon2id$a"));
assert_ne!(k, basic_cache_key("t", "alice", "pw2", "$argon2id$a"));
assert_ne!(
basic_cache_key("t", "ab", "c", "h"),
basic_cache_key("t", "a", "bc", "h")
);
}
#[test]
fn basic_cache_hit_needs_the_same_user() {
let key = basic_cache_key("t-hit", "bob", "pw", "h");
assert!(!basic_cache_hit(&key, 7));
basic_cache_put(key, 7);
assert!(basic_cache_hit(&key, 7));
assert!(!basic_cache_hit(&key, 8));
}
#[tokio::test]
async fn a_locked_basic_account_is_refused_before_the_lookup() {
use crate::login_throttle::LoginRefused;
use base64::Engine as _;
let scope = LoginScope::TenantBasic("t-prelock".into());
for _ in 0..crate::account_lockout::DEFAULT_MAX_ATTEMPTS {
let a = crate::login_throttle::shared()
.begin(&scope, &ClientIp::default(), "locked-user")
.await
.unwrap();
a.failed().await;
}
let pool = Pool::connect("sqlite::memory:").await.unwrap();
let creds = base64::engine::general_purpose::STANDARD.encode("locked-user:pw");
let (parts, ()) = axum::http::Request::builder()
.header("authorization", format!("Basic {creds}"))
.extension(super::super::TenantSlug("t-prelock".into()))
.body(())
.unwrap()
.into_parts();
let r = ModelBackend.authenticate(&parts, &pool).await;
assert!(
matches!(r, Err(AuthError::Refused(LoginRefused::Throttled { .. }))),
"{r:?}"
);
}
#[tokio::test]
async fn a_token_without_a_jti_stays_silent() {
let backend = JwtBackend::new(vec![7u8; 32]);
let token = backend.issue(42);
assert_eq!(revocation_warnings(&backend, &token).await, 0);
}
}