use base64::Engine;
use serde::{Deserialize, Serialize};
use subtle::ConstantTimeEq;
pub use crate::session::{PasswordFingerprint, SessionSecret, SessionSecretError};
pub(crate) use crate::session::{session_survives, sign};
pub const COOKIE_NAME: &str = "rustango_op_session";
pub const SESSION_TTL_SECS: i64 = 12 * 60 * 60;
pub const SESSION_IDLE_SECS: i64 = 30 * 60;
const RENEW_AFTER_SECS: i64 = 60;
pub(crate) fn tenant_secret_from_env() -> Result<SessionSecret, super::TenancyError> {
use crate::session::{is_prod_tier, tier_from_env};
if is_prod_tier(&tier_from_env()) {
SessionSecret::require_from_env()
.map_err(|e| super::TenancyError::Validation(e.to_string()))
} else {
Ok(SessionSecret::from_env_or_disk(std::path::Path::new(
crate::session::TENANT_SESSION_KEY_PATH,
)))
}
}
pub(crate) struct ServerKeys {
pub(crate) tenant: SessionSecret,
pub(crate) operator: SessionSecret,
}
impl ServerKeys {
pub(crate) fn load() -> Self {
let tier = crate::session::tier_from_env();
Self::from_files(|p| crate::session::load_session_secret_for_tier(&tier, p))
}
fn from_files(load: impl Fn(&std::path::Path) -> SessionSecret) -> Self {
use crate::session::{OPERATOR_SESSION_KEY_PATH, TENANT_SESSION_KEY_PATH};
Self {
tenant: load(std::path::Path::new(TENANT_SESSION_KEY_PATH)),
operator: load(std::path::Path::new(OPERATOR_SESSION_KEY_PATH)),
}
}
#[cfg(test)]
fn dev_at(env: Option<&str>, root: &std::path::Path) -> Self {
Self::from_files(|p| SessionSecret::from_raw_or_disk(env, &root.join(p)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeySource {
Env,
DevFile,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DevKeyFile {
LoadOnly,
LoadOrCreate,
}
pub(crate) fn existing_tenant_secret(
dev_key: DevKeyFile,
) -> Result<(SessionSecret, KeySource), super::TenancyError> {
use crate::session::{is_prod_tier, tier_from_env};
existing_tenant_secret_at(
std::env::var("RUSTANGO_SESSION_SECRET").ok().as_deref(),
is_prod_tier(&tier_from_env()),
std::path::Path::new(crate::session::TENANT_SESSION_KEY_PATH),
dev_key,
)
}
fn existing_tenant_secret_at(
env: Option<&str>,
prod: bool,
dev_file: &std::path::Path,
dev_key: DevKeyFile,
) -> Result<(SessionSecret, KeySource), super::TenancyError> {
let invalid =
|e: crate::session::SessionSecretError| super::TenancyError::Validation(e.to_string());
if prod {
return env
.ok_or(crate::session::SessionSecretError::Missing)
.and_then(SessionSecret::from_b64)
.map(|s| (s, KeySource::Env))
.map_err(invalid);
}
if let Some(raw) = env {
match SessionSecret::from_b64(raw) {
Ok(s) => return Ok((s, KeySource::Env)),
Err(e) => crate::session::warn_unusable_secret(&e.to_string()),
}
}
let missing = |fix: String| {
super::TenancyError::Validation(format!(
"no tenant session key: set RUSTANGO_SESSION_SECRET, or {fix}"
))
};
match dev_key {
DevKeyFile::LoadOnly => match std::fs::read(dev_file) {
Ok(bytes) if bytes.len() >= 32 => {
Ok((SessionSecret::from_bytes(bytes), KeySource::DevFile))
}
_ => Err(missing(format!(
"start the server once so it writes {}",
dev_file.display()
))),
},
DevKeyFile::LoadOrCreate => crate::session::load_or_create_key_file(
dev_file,
"session secret",
"RUSTANGO_SESSION_SECRET",
)
.map(|bytes| (SessionSecret::from_bytes(bytes), KeySource::DevFile))
.map_err(|e| {
missing(format!(
"fix the dev key file: cannot read or write {} ({e})",
dev_file.display()
))
}),
}
}
#[derive(Debug, thiserror::Error)]
pub enum SessionError {
#[error("session cookie is malformed")]
Malformed,
#[error("session signature mismatch")]
BadSignature,
#[error("session expired")]
Expired,
#[error("session is bound to a different tenant")]
WrongTenant,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SessionPayload {
pub oid: i64,
pub exp: i64,
#[serde(default)]
pub iat: i64,
#[serde(default)]
pub pwf: PasswordFingerprint,
}
impl SessionPayload {
#[must_use]
pub fn new(operator_id: i64, ttl_secs: i64, pwf: PasswordFingerprint) -> Self {
let now = chrono::Utc::now().timestamp();
Self {
oid: operator_id,
exp: now + ttl_secs,
iat: now,
pwf,
}
}
fn is_expired(&self) -> bool {
let now = chrono::Utc::now().timestamp();
now >= self.exp || now >= self.iat.saturating_add(SESSION_TTL_SECS)
}
#[must_use]
pub fn renewed(&self) -> Option<Self> {
let now = chrono::Utc::now().timestamp();
let exp = (now + SESSION_IDLE_SECS).min(self.iat.saturating_add(SESSION_TTL_SECS));
(exp < self.exp || exp - self.exp >= RENEW_AFTER_SECS).then(|| Self {
exp,
..self.clone()
})
}
}
#[must_use]
pub fn encode(secret: &SessionSecret, payload: &SessionPayload) -> String {
let json = serde_json::to_vec(payload).expect("payload serializes");
let payload_b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(json);
let sig = sign(secret, payload_b64.as_bytes());
let sig_b64 = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(sig);
format!("{payload_b64}.{sig_b64}")
}
pub fn decode(secret: &SessionSecret, value: &str) -> Result<SessionPayload, SessionError> {
let (payload_b64, sig_b64) = value.split_once('.').ok_or(SessionError::Malformed)?;
let expected = sign(secret, payload_b64.as_bytes());
let provided = base64::engine::general_purpose::URL_SAFE_NO_PAD
.decode(sig_b64)
.map_err(|_| SessionError::Malformed)?;
if expected.ct_eq(&provided[..]).unwrap_u8() == 0 {
return Err(SessionError::BadSignature);
}
let payload_bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
.decode(payload_b64)
.map_err(|_| SessionError::Malformed)?;
let payload: SessionPayload =
serde_json::from_slice(&payload_bytes).map_err(|_| SessionError::Malformed)?;
if payload.is_expired() {
return Err(SessionError::Expired);
}
Ok(payload)
}
#[cfg(test)]
mod tests {
use super::*;
fn fp() -> PasswordFingerprint {
PasswordFingerprint::default()
}
#[test]
fn off_prod_the_dev_key_file_is_created_once() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("var").join("tenant.key");
assert!(existing_tenant_secret_at(None, true, &path, DevKeyFile::LoadOrCreate).is_err());
assert!(!path.exists(), "prod wrote a key file");
let (first, source) =
existing_tenant_secret_at(None, false, &path, DevKeyFile::LoadOrCreate).unwrap();
assert_eq!(source, KeySource::DevFile);
assert_eq!(std::fs::read(&path).unwrap(), first.key());
let server = SessionSecret::from_raw_or_disk(None, &path);
assert_eq!(
server.key(),
first.key(),
"the server must load the same key"
);
let (again, _) =
existing_tenant_secret_at(None, false, &path, DevKeyFile::LoadOrCreate).unwrap();
assert_eq!(again.key(), first.key());
}
#[test]
fn a_cli_link_verifies_under_the_servers_tenant_key() {
let root = tempfile::tempdir().unwrap();
let file = root.path().join(crate::session::TENANT_SESSION_KEY_PATH);
let (cli, _) =
existing_tenant_secret_at(None, false, &file, DevKeyFile::LoadOrCreate).unwrap();
let server = ServerKeys::dev_at(None, root.path());
let scope = crate::auth_flows::LinkScope::from(&crate::testkit::org());
let link = crate::auth_flows::PasswordReset::issue(
&scope,
"/change-password",
42,
&super::super::first_admin::link_key(&cli),
std::time::Duration::from_secs(60),
);
let key = super::super::first_admin::link_key(&server.tenant);
assert_eq!(
crate::auth_flows::PasswordReset::verify(&scope, &link, &key),
Ok(42)
);
assert_ne!(
server.operator.key(),
server.tenant.key(),
"one key per console"
);
}
#[test]
fn an_unusable_env_secret_uses_the_servers_dev_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tenant.key");
std::fs::write(&path, [7u8; 32]).unwrap();
let (_, source) =
existing_tenant_secret_at(Some("not-base64!"), false, &path, DevKeyFile::LoadOnly)
.unwrap();
assert_eq!(source, KeySource::DevFile);
assert!(
existing_tenant_secret_at(Some("not-base64!"), true, &path, DevKeyFile::LoadOnly)
.is_err()
);
let good = "A".repeat(44);
let (_, source) =
existing_tenant_secret_at(Some(&good), false, &path, DevKeyFile::LoadOnly).unwrap();
assert_eq!(source, KeySource::Env);
}
#[test]
fn round_trip_valid_payload() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let payload = SessionPayload::new(42, 3600, PasswordFingerprint::of(&secret, "$h"));
let cookie = encode(&secret, &payload);
let back = decode(&secret, &cookie).unwrap();
assert_eq!(back, payload);
}
#[test]
fn rejects_tampered_payload() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let payload = SessionPayload::new(42, 3600, fp());
let cookie = encode(&secret, &payload);
let (_, sig) = cookie.split_once('.').unwrap();
let evil_payload = base64::engine::general_purpose::URL_SAFE_NO_PAD
.encode(br#"{"oid":999,"exp":9999999999}"#);
let tampered = format!("{evil_payload}.{sig}");
let err = decode(&secret, &tampered).unwrap_err();
assert!(matches!(err, SessionError::BadSignature));
}
#[test]
fn rejects_wrong_secret() {
let s1 = SessionSecret::from_bytes(b"first-test-secret-thirty-2-bytes".to_vec());
let s2 = SessionSecret::from_bytes(b"second-test-secret-thirty2-bytes".to_vec());
let cookie = encode(&s1, &SessionPayload::new(1, 3600, fp()));
let err = decode(&s2, &cookie).unwrap_err();
assert!(matches!(err, SessionError::BadSignature));
}
#[test]
fn rejects_expired() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let payload = SessionPayload::new(1, -10, fp());
let cookie = encode(&secret, &payload);
let err = decode(&secret, &cookie).unwrap_err();
assert!(matches!(err, SessionError::Expired));
}
#[test]
fn renewal_slides_exp_up_to_the_cap() {
let now = chrono::Utc::now().timestamp();
let renewed = SessionPayload::new(1, 30, fp()).renewed().expect("stale");
assert!(renewed.exp >= now + SESSION_IDLE_SECS);
assert!(renewed.renewed().is_none(), "fresh needs no new cookie");
let mut near_cap = SessionPayload::new(1, 30, fp());
near_cap.iat = now - SESSION_TTL_SECS + 10;
assert!(near_cap.renewed().unwrap().exp <= now + 10);
let old = SessionPayload::new(1, 7 * 24 * 3600, fp());
let cut = old.renewed().expect("a far exp is pulled in");
assert!(cut.exp <= chrono::Utc::now().timestamp() + SESSION_IDLE_SECS);
}
#[test]
fn rejects_a_session_past_the_absolute_cap() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let mut payload = SessionPayload::new(1, 3600, fp());
payload.iat -= SESSION_TTL_SECS;
let err = decode(&secret, &encode(&secret, &payload)).unwrap_err();
assert!(matches!(err, SessionError::Expired));
}
#[test]
fn rejects_malformed_no_dot() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let err = decode(&secret, "not-a-cookie").unwrap_err();
assert!(matches!(err, SessionError::Malformed));
}
#[test]
fn a_new_password_hash_ends_a_same_second_session() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let now = chrono::Utc::now();
let pwf = PasswordFingerprint::of(&secret, "$old");
assert!(session_survives(
&secret,
&pwf,
now.timestamp(),
"$old",
Some(now),
None
));
assert!(!session_survives(
&secret,
&pwf,
now.timestamp(),
"$new",
Some(now),
None
));
assert!(!session_survives(
&secret,
&fp(),
now.timestamp(),
"$old",
None,
None
));
}
#[test]
fn a_later_password_changed_at_ends_a_session_with_the_same_hash() {
let secret = SessionSecret::from_bytes(b"a-test-secret-thirty-two-bytes-x".to_vec());
let pwf = PasswordFingerprint::of(&secret, "$h");
let iat = 1_700_000_000;
let at = |secs| chrono::DateTime::from_timestamp(secs, 0);
assert!(session_survives(&secret, &pwf, iat, "$h", None, None));
assert!(session_survives(&secret, &pwf, iat, "$h", at(iat), None));
assert!(!session_survives(
&secret,
&pwf,
iat,
"$h",
at(iat + 1),
None
));
}
}