use adk_auth::VisitorTokens;
use adk_core::principal::SubjectRef;
use adk_core::subject::{SUBJECT_LINK_CONSUMED_CODE, SubjectRights};
use adk_core::{AdkError, RequestContext};
use axum::extract::State;
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Extension, Json, Router};
use chrono::Utc;
use serde_json::json;
pub const AUTH_TIME_METADATA: &str = "auth_time";
#[derive(Clone)]
pub struct SubjectRightsEndpoints {
rights: SubjectRights,
tokens: VisitorTokens,
max_sign_in_age: chrono::Duration,
}
impl std::fmt::Debug for SubjectRightsEndpoints {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SubjectRightsEndpoints")
.field("max_sign_in_age", &self.max_sign_in_age)
.finish_non_exhaustive()
}
}
impl SubjectRightsEndpoints {
pub fn new(rights: SubjectRights, tokens: VisitorTokens) -> Self {
Self { rights, tokens, max_sign_in_age: chrono::Duration::minutes(10) }
}
#[must_use]
pub fn with_max_sign_in_age(mut self, age: chrono::Duration) -> Self {
self.max_sign_in_age = age;
self
}
}
pub fn subject_rights_router(endpoints: SubjectRightsEndpoints) -> Router {
Router::new()
.route("/subject/export", get(export))
.route("/subject/erase", post(erase))
.route("/subject/link", post(link))
.with_state(endpoints)
}
fn refuse(status: StatusCode, code: &str, message: &str) -> Response {
(status, Json(json!({ "error": { "code": code, "message": message } }))).into_response()
}
fn failed(error: &AdkError) -> Response {
tracing::error!(error = %error, "subject rights request failed");
let status =
StatusCode::from_u16(error.http_status_code()).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
refuse(status, error.code, &error.message)
}
fn caller(context: Option<RequestContext>) -> Result<(SubjectRef, RequestContext), Response> {
match context {
Some(context) => Ok((SubjectRef::human(context.user_id.clone()), context)),
None => Err(refuse(
StatusCode::UNAUTHORIZED,
"subject.unauthenticated",
"subject rights need a signed-in caller; configure a RequestContextExtractor",
)),
}
}
async fn export(
State(endpoints): State<SubjectRightsEndpoints>,
Extension(context): Extension<Option<RequestContext>>,
) -> Response {
let (subject, _) = match caller(context) {
Ok(caller) => caller,
Err(response) => return response,
};
match endpoints.rights.export(&subject).await {
Ok(export) => Json(export).into_response(),
Err(error) => failed(&error),
}
}
async fn erase(
State(endpoints): State<SubjectRightsEndpoints>,
Extension(context): Extension<Option<RequestContext>>,
) -> Response {
let (subject, _) = match caller(context) {
Ok(caller) => caller,
Err(response) => return response,
};
match endpoints.rights.erase(&subject).await {
Ok(report) => (StatusCode::ACCEPTED, Json(report)).into_response(),
Err(error) => failed(&error),
}
}
async fn link(
State(endpoints): State<SubjectRightsEndpoints>,
Extension(context): Extension<Option<RequestContext>>,
headers: HeaderMap,
) -> Response {
let (subject, context) = match caller(context) {
Ok(caller) => caller,
Err(response) => return response,
};
let signed_in = context
.metadata
.get(AUTH_TIME_METADATA)
.and_then(|seconds| seconds.parse::<i64>().ok())
.and_then(|seconds| chrono::DateTime::from_timestamp(seconds, 0));
if signed_in.is_none_or(|at| Utc::now() - at > endpoints.max_sign_in_age) {
return refuse(
StatusCode::UNAUTHORIZED,
"subject.link.sign_in_required",
"linking a visitor id needs a fresh sign-in; sign in again and retry",
);
}
let token = headers
.get("awp-subject")
.and_then(|value| value.to_str().ok())
.map(str::to_owned)
.or_else(|| {
headers
.get(header::COOKIE)
.and_then(|value| value.to_str().ok())
.and_then(VisitorTokens::from_cookie_header)
.map(str::to_owned)
});
let Some(visitor) = token.and_then(|token| endpoints.tokens.verify(&token)) else {
return refuse(
StatusCode::BAD_REQUEST,
"subject.link.no_visitor",
"the request carries no valid visitor token to link",
);
};
match endpoints.rights.link(&visitor.subject(), &subject).await {
Ok(_) => {
let mut response = Json(json!({ "linked": true })).into_response();
let cleared = format!(
"{}=; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=0",
adk_auth::VISITOR_COOKIE
);
if let Ok(value) = HeaderValue::from_str(&cleared) {
response.headers_mut().insert(header::SET_COOKIE, value);
}
response
}
Err(error) if error.code == SUBJECT_LINK_CONSUMED_CODE => {
refuse(StatusCode::CONFLICT, error.code, &error.message)
}
Err(error) => failed(&error),
}
}