use serde_json::{Map, Value};
pub const ROLES_CLAIM: &str = "roles";
pub const SERVICE_ROLE: &str = "semiont-service";
pub const WORKER_ROLE: &str = "semiont-worker";
pub const KIND_CLAIM: &str = "kind";
pub const AGENT_KIND: &str = "agent";
pub fn is_agent(claims: &Map<String, Value>) -> bool {
claims.get(KIND_CLAIM).and_then(Value::as_str) == Some(AGENT_KIND)
}
pub fn has_role(claims: &Map<String, Value>, role: &str) -> bool {
roles_of(claims).is_some_and(|roles| roles.iter().any(|r| r == role))
}
pub fn roles_of(claims: &Map<String, Value>) -> Option<Vec<String>> {
let roles: Vec<String> = claims
.get(ROLES_CLAIM)?
.as_array()?
.iter()
.filter_map(|r| r.as_str().map(str::to_owned))
.collect();
(!roles.is_empty()).then_some(roles)
}