Add local password-TOTP sessions and scoped revocable MCP tokens

This commit is contained in:
2026-11-17 20:05:28 +01:00
parent 7fe20df34f
commit 9fa0fb81ff
17 changed files with 1804 additions and 27 deletions
+43 -6
View File
@@ -55,6 +55,7 @@ pub struct Auth {
operator_b: Option<String>,
pub origin: String,
pub oidc: Option<crate::oidc::Provider>,
pub local: Option<crate::local_auth::LocalAuth>,
development: bool,
}
impl Auth {
@@ -79,7 +80,10 @@ impl Auth {
Ok(())
}
pub fn operator_enabled(&self, name: &str, tenant: Uuid) -> bool {
(self.oidc.is_some() && name.starts_with("oidc:") && name.len() == 69)
self.local
.as_ref()
.is_some_and(|local| local.enabled(name, tenant))
|| (self.oidc.is_some() && name.starts_with("oidc:") && name.len() == 69)
|| (name == "simulated-testprincipal-a"
&& tenant.to_string() == TENANT_A
&& self.username.is_some())
@@ -125,10 +129,11 @@ impl Auth {
url.scheme() == "https" && url.origin().ascii_serialization() == origin,
"UI origin must be an exact HTTPS origin"
);
let local = crate::local_auth::LocalAuth::configured()?;
let oidc = crate::oidc::configured(profile, &origin).await?;
anyhow::ensure!(
development || oidc.is_some(),
"production OIDC not_configured"
development || local.is_some() || oidc.is_some(),
"production authentication not_configured"
);
Ok(Self {
sessions: Mutex::new(HashMap::new()),
@@ -139,6 +144,7 @@ impl Auth {
operator_b: secret("RMM_OPERATOR_B")?,
origin,
oidc,
local,
development,
})
}
@@ -195,7 +201,9 @@ impl Auth {
} else {
None
};
if let Some((tenant, operator)) = local {
if raw.starts_with("rmm_mcp_") {
crate::mcp_tokens::authenticate(state, raw, selected).await?
} else if let Some((tenant, operator)) = local {
let tenant =
Uuid::parse_str(tenant).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Principal {
@@ -253,6 +261,14 @@ impl Auth {
}
p
};
if p.name.starts_with("local:")
&& !self
.local
.as_ref()
.is_some_and(|local| local.enabled(&p.name, p.tenant))
{
return Err(StatusCode::FORBIDDEN);
}
if selected.is_some_and(|t| t != p.tenant) {
return Err(StatusCode::NOT_FOUND);
}
@@ -260,9 +276,10 @@ impl Auth {
.await
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
let active: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM memberships WHERE principal=$1 AND revoked_at IS NULL)",
"SELECT EXISTS(SELECT 1 FROM memberships WHERE principal=$1 AND revoked_at IS NULL AND ($2=false OR kind='human'))",
)
.bind(&p.name)
.bind(p.name.starts_with("local:"))
.fetch_one(&mut *tx)
.await
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
@@ -446,9 +463,29 @@ pub async fn login(
.insert("content-type", HeaderValue::from_static("application/json"));
Ok(response)
}
pub async fn local_login(
State(state): State<crate::State>,
h: HeaderMap,
input: Result<Json<crate::local_auth::Login>, axum::extract::rejection::JsonRejection>,
) -> Result<Response, StatusCode> {
state.auth.origin(&h)?;
state.auth.rate_limit()?;
let local = state.auth.local.as_ref().ok_or(StatusCode::NOT_FOUND)?;
let Json(input) = input.map_err(|_| StatusCode::UNAUTHORIZED)?;
let principal = local.authenticate(&state.pool, input).await?;
let mut response = state.auth.establish(principal)?;
*response.status_mut() = StatusCode::OK;
response.headers_mut().remove("location");
*response.body_mut() =
axum::body::Body::from("{\"authenticated\":true,\"testsimulation\":false}");
response
.headers_mut()
.insert("content-type", HeaderValue::from_static("application/json"));
Ok(response)
}
pub async fn configuration(State(state): State<crate::State>) -> Json<serde_json::Value> {
Json(
json!({"oidc_configured":state.auth.oidc.is_some(),"oidc_login_url":state.auth.oidc.as_ref().map(|_|"/auth/oidc/login"),"dev_login_enabled":state.auth.username.is_some()}),
json!({"local_login_enabled":state.auth.local.is_some(),"oidc_configured":state.auth.oidc.is_some(),"oidc_login_url":state.auth.oidc.as_ref().map(|_|"/auth/oidc/login"),"dev_login_enabled":state.auth.username.is_some()}),
)
}
pub async fn oidc_login(State(state): State<crate::State>) -> Result<Response, StatusCode> {