use axum::Json;
use serde::Serialize;
use utoipa::ToSchema;
use crate::error::Result;
#[derive(Debug, Serialize, ToSchema)]
pub struct CedarPolicyResponse {
pub policy: String,
pub schema: String,
pub version: String,
}
#[utoipa::path(
get,
path = "/api/cedar/policies",
responses(
(status = 200, description = "Cedar policies and schema", body = CedarPolicyResponse),
),
tag = "cedar"
)]
pub async fn get_cedar_policies() -> Result<Json<CedarPolicyResponse>> {
let policy = include_str!("../../policies/rbac.cedar");
let schema = include_str!("../../policies/schema.cedarschema");
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
policy.hash(&mut hasher);
let version = format!("{:016x}", hasher.finish());
Ok(Json(CedarPolicyResponse {
policy: policy.to_string(),
schema: schema.to_string(),
version,
}))
}