use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse, Response};
use serde::Deserialize;
use serde_json::{Map, Value};
use crate::admin;
use crate::sqlite::eab::Eab;
use crate::webadmin::AdminState;
use crate::webadmin::handlers::paging::{Page, PageParams};
use crate::webadmin::handlers::params::non_empty;
use crate::webadmin::pages::auth::{PageSession, PageSessionWrite};
use crate::webadmin::pages::error::PageError;
use crate::webadmin::pages::{chrome, flash, page_value, pager, respond, respond_fragment};
#[derive(Debug, Deserialize, Default)]
pub struct CreateForm {
#[serde(default)]
pub label: String,
#[serde(default)]
pub profile: String,
}
pub async fn list_eab(
State(state): State<AdminState>,
Query(params): Query<PageParams>,
session: PageSession,
) -> Result<Html<String>, PageError> {
let page = params.resolve(&state.config);
let (items, total) = rows(page, &state).await?;
let mut context = chrome(&session, "eab", "External Account Binding");
context.insert("page".to_string(), page_value(items, total));
context.insert(
"pager".to_string(),
pager(page, total, "/ui/eab", &[], "#eab-table"),
);
context.insert(
"profiles".to_string(),
Value::Array(crate::webadmin::handlers::misc::profile_rows(&state)),
);
respond(
&state,
session.hx,
"eab/list.html",
"eab/_table.html",
context,
)
}
pub async fn get_eab(
State(state): State<AdminState>,
Path(kid): Path<String>,
session: PageSession,
) -> Result<Html<String>, PageError> {
let eab = load(&kid, &state).await?;
let mut context = chrome(&session, "eab", "Credential");
context.insert("eab".to_string(), eab);
respond(
&state,
session.hx,
"eab/detail.html",
"eab/_card.html",
context,
)
}
pub async fn create_eab(
State(state): State<AdminState>,
session: PageSessionWrite,
request_context: crate::audit::RequestContext,
axum::Form(form): axum::Form<CreateForm>,
) -> Result<Response, PageError> {
let label = non_empty(&form.label);
let profile = non_empty(&form.profile);
super::super::handlers::eab::require_mounted_profile(
&state,
profile.as_deref(),
"leave it unset",
)?;
let eab = Eab::create(label, profile, &state.database).await?;
state
.record_admin_action(
&request_context,
&session.auth.user.username,
|actor, client| {
crate::audit::admin::eab_created(
actor,
client,
&eab.kid.to_string(),
eab.profile.as_deref(),
eab.label.as_deref(),
)
},
)
.await;
tracing::info!(event = "admin_eab_created",
outcome = "success",
surface = "ui",
kid = %eab.kid,
username = %session.auth.user.username);
let page = PageParams::default().resolve(&state.config);
let (items, total) = rows(page, &state).await?;
let mut context = Map::new();
context.insert("eab".to_string(), admin::render_eab_created_json(&eab));
context.insert("page".to_string(), page_value(items, total));
context.insert(
"pager".to_string(),
pager(page, total, "/ui/eab", &[], "#eab-table"),
);
context.insert("oob".to_string(), Value::Bool(true));
let body = respond_fragment(&state, "eab/_created.html", context)?;
Ok((StatusCode::CREATED, body).into_response())
}
pub async fn revoke_eab(
State(state): State<AdminState>,
Path(kid): Path<String>,
session: PageSessionWrite,
request_context: crate::audit::RequestContext,
) -> Result<Html<String>, PageError> {
let subject = Eab::find_any_by_kid(&kid, &state.database).await?;
if !Eab::revoke(&kid, &state.database).await? {
return Err(not_found(&kid));
}
if subject.as_ref().is_some_and(|eab| eab.status == "active") {
let profile = subject.as_ref().and_then(|eab| eab.profile.as_deref());
state
.record_admin_action(
&request_context,
&session.auth.user.username,
|actor, client| crate::audit::admin::eab_revoked(actor, client, &kid, profile),
)
.await;
}
tracing::info!(event = "admin_eab_revoked",
outcome = "success",
surface = "ui",
kid = %kid,
username = %session.auth.user.username);
let eab = load(&kid, &state).await?;
let mut context = Map::new();
context.insert(
"csrf_token".to_string(),
Value::String(session.auth.session.csrf_token.clone()),
);
context.insert("eab".to_string(), eab);
context.insert(
"flash".to_string(),
flash(
"ok",
"Credential revoked. Registrations using it fail from now on.",
),
);
respond_fragment(&state, "eab/_card.html", context)
}
async fn rows(page: Page, state: &AdminState) -> Result<(Vec<Value>, i64), PageError> {
let (keys, total) = Eab::search(page.limit, page.offset, &state.database).await?;
Ok((keys.iter().map(admin::render_eab_json).collect(), total))
}
async fn load(kid: &str, state: &AdminState) -> Result<Value, PageError> {
let eab = Eab::find_any_by_kid(kid, &state.database)
.await?
.ok_or_else(|| not_found(kid))?;
Ok(admin::render_eab_json(&eab))
}
fn not_found(kid: &str) -> PageError {
PageError::not_found(format!("no such EAB credential: {kid}"))
}