use std::sync::Arc;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use crate::ServiceError;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Credentials {
#[serde(default, skip_serializing_if = "Option::is_none")]
cert: Option<ByteBuf>,
#[serde(default, skip_serializing_if = "Option::is_none")]
card: Option<ByteBuf>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
memberships: Vec<ByteBuf>,
}
impl Credentials {
pub fn new() -> Self {
Self::default()
}
pub fn with_cert(mut self, cert: Vec<u8>) -> Self {
self.cert = Some(ByteBuf::from(cert));
self
}
pub fn with_card(mut self, card: Vec<u8>) -> Self {
self.card = Some(ByteBuf::from(card));
self
}
pub fn with_membership(mut self, membership: Vec<u8>) -> Self {
self.memberships.push(ByteBuf::from(membership));
self
}
pub fn cert(&self) -> Option<&[u8]> {
self.cert.as_deref().map(|b| b.as_slice())
}
pub fn card(&self) -> Option<&[u8]> {
self.card.as_deref().map(|b| b.as_slice())
}
pub fn memberships(&self) -> impl Iterator<Item = &[u8]> {
self.memberships.iter().map(|b| b.as_slice())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Caller {
pub peer: String,
pub credentials: Credentials,
}
impl Caller {
pub fn new(peer: impl Into<String>, credentials: Credentials) -> Self {
Self { peer: peer.into(), credentials }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Access {
pub permission: String,
pub resource: Option<String>,
}
impl Access {
pub const SESSION: &'static str = "session";
pub fn new(permission: impl Into<String>) -> Self {
Self { permission: permission.into(), resource: None }
}
pub fn session() -> Self {
Self::new(Self::SESSION)
}
pub fn on(mut self, resource: impl Into<String>) -> Self {
self.resource = Some(resource.into());
self
}
pub fn is_session(&self) -> bool {
self.permission == Self::SESSION && self.resource.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct Admitted {
pub peer: String,
pub user: Option<String>,
pub device_roles: Option<Vec<String>>,
pub resource: Option<String>,
pub role: Option<String>,
}
impl Admitted {
pub fn new(peer: impl Into<String>) -> Self {
Self { peer: peer.into(), ..Self::default() }
}
pub fn with_user(mut self, user: impl Into<String>) -> Self {
self.user = Some(user.into());
self
}
pub fn with_device_roles(mut self, roles: Option<Vec<String>>) -> Self {
self.device_roles = roles;
self
}
pub fn with_resource(mut self, resource: impl Into<String>) -> Self {
self.resource = Some(resource.into());
self
}
pub fn with_role(mut self, role: impl Into<String>) -> Self {
self.role = Some(role.into());
self
}
pub fn has_device_role(&self, role: &str) -> bool {
self.device_roles.as_ref().is_some_and(|r| r.iter().any(|x| x == role))
}
pub fn merge(mut self, other: Admitted) -> Self {
self.user = self.user.or(other.user);
self.device_roles = self.device_roles.or(other.device_roles);
self.resource = self.resource.or(other.resource);
self.role = self.role.or(other.role);
self
}
}
pub trait Authorize: Send + Sync {
fn authorize(&self, caller: &Caller, access: &Access) -> Result<Admitted, ServiceError>;
}
impl<F> Authorize for F
where
F: Fn(&Caller, &Access) -> Result<Admitted, ServiceError> + Send + Sync,
{
fn authorize(&self, caller: &Caller, access: &Access) -> Result<Admitted, ServiceError> {
self(caller, access)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct AllowAll;
impl Authorize for AllowAll {
fn authorize(&self, caller: &Caller, access: &Access) -> Result<Admitted, ServiceError> {
let a = Admitted::new(caller.peer.clone());
Ok(match &access.resource {
Some(r) => a.with_resource(r.clone()),
None => a,
})
}
}
#[derive(Clone)]
pub struct AllOf(pub Vec<Arc<dyn Authorize>>);
impl Authorize for AllOf {
fn authorize(&self, caller: &Caller, access: &Access) -> Result<Admitted, ServiceError> {
let mut out: Option<Admitted> = None;
for a in &self.0 {
let got = a.authorize(caller, access)?;
out = Some(match out {
None => got,
Some(prev) => prev.merge(got),
});
}
out.ok_or_else(|| ServiceError::refused("no-authorizer"))
}
}
#[derive(Clone)]
pub struct AnyOf(pub Vec<Arc<dyn Authorize>>);
impl Authorize for AnyOf {
fn authorize(&self, caller: &Caller, access: &Access) -> Result<Admitted, ServiceError> {
let mut last = ServiceError::refused("no-authorizer");
for a in &self.0 {
match a.authorize(caller, access) {
Ok(got) => return Ok(got),
Err(e) => last = e,
}
}
Err(last)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::{from_cbor, to_cbor};
#[test]
fn combinators_merge_and_refuse() {
let user = |c: &Caller, _: &Access| Ok(Admitted::new(c.peer.clone()).with_user("u1"));
let role = |c: &Caller, a: &Access| {
if a.permission == "write" { Err(ServiceError::refused("role")) } else { Ok(Admitted::new(c.peer.clone()).with_role("viewer")) }
};
let caller = Caller::new("p", Credentials::new());
let all = AllOf(vec![Arc::new(user), Arc::new(role)]);
let got = all.authorize(&caller, &Access::new("read")).unwrap();
assert_eq!((got.user.as_deref(), got.role.as_deref()), (Some("u1"), Some("viewer")));
assert_eq!(all.authorize(&caller, &Access::new("write")), Err(ServiceError::refused("role")));
assert!(AllOf(vec![]).authorize(&caller, &Access::session()).is_err());
let any = AnyOf(vec![Arc::new(role), Arc::new(user)]);
assert_eq!(any.authorize(&caller, &Access::new("write")).unwrap().user.as_deref(), Some("u1"));
assert!(AnyOf(vec![]).authorize(&caller, &Access::session()).is_err());
assert_eq!(AllowAll.authorize(&caller, &Access::new("x").on("acct")).unwrap().resource.as_deref(), Some("acct"));
}
#[test]
fn credentials_travel_as_byte_strings() {
let c = Credentials::new().with_cert(vec![1, 2]).with_membership(vec![3]).with_membership(vec![4, 5]);
let bytes = to_cbor(&c).unwrap();
assert!(bytes.len() < 30, "byte strings, not arrays of numbers");
let back: Credentials = from_cbor(&bytes).unwrap();
assert_eq!(back, c);
assert_eq!(back.memberships().collect::<Vec<_>>(), [&[3u8][..], &[4, 5][..]]);
assert_eq!(from_cbor::<Credentials>(&to_cbor(&Credentials::new()).unwrap()).unwrap(), Credentials::new());
assert!(Access::session().is_session() && !Access::session().on("x").is_session());
}
}