Organize device inventory by customer and restrict token access to its audience

This commit is contained in:
2026-11-17 21:55:24 +01:00
parent aeb9adc649
commit 0afaca1d64
39 changed files with 1950 additions and 225 deletions
+37
View File
@@ -25,6 +25,10 @@ pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
"/api/v1/tenants/{tenant}/devices/{asset}/revocation",
post(revoke_device),
)
.route(
"/api/v1/tenants/{tenant}/devices/{asset}/archive",
post(archive_device),
)
.route(
"/api/v1/tenants/{tenant}/operation-plans/{plan}/revocation",
post(revoke_plan),
@@ -63,6 +67,39 @@ async fn audit(
sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,$4,$5,now())").bind(p.tenant).bind(Uuid::new_v4()).bind(&p.name).bind(event).bind(json!({"decision":data,"session_hash":p.session_hash,"approval_origin":p.approval_origin(),"testsimulation":p.testsimulation})).execute(&mut **tx).await?;
Ok(())
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Archive {
archived: bool,
}
async fn archive_device(
State(s): State<crate::State>,
h: HeaderMap,
Path((tenant, asset)): Path<(Uuid, Uuid)>,
Json(input): Json<Archive>,
) -> Result<Json<Value>, Error> {
let event = if input.archived {
"device_archived"
} else {
"device_restored"
};
let (p, mut tx) = context(&s, &h, tenant, "devices:revoke", event).await?;
let current: Option<chrono::DateTime<chrono::Utc>> = sqlx::query_scalar(
"SELECT archived_at FROM assets WHERE tenant_id=$1 AND asset_id=$2 FOR UPDATE",
)
.bind(tenant)
.bind(asset)
.fetch_optional(&mut *tx)
.await?
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
if current.is_some() != input.archived {
sqlx::query("UPDATE assets SET archived_at=CASE WHEN $3 THEN now() END WHERE tenant_id=$1 AND asset_id=$2")
.bind(tenant).bind(asset).bind(input.archived).execute(&mut *tx).await?;
audit(&mut tx, &p, event, json!({"asset_id":asset})).await?;
}
tx.commit().await?;
Ok(Json(json!({"asset_id":asset, "archived":input.archived})))
}
pub async fn revoke_device(
State(s): State<crate::State>,
h: HeaderMap,