use std::fmt;
use super::super::capability::CapabilityError;
use super::{BrokerError, IntentHandle, RegisteredHandle};
use crate::canvas_protocol::{Capability, SessionInvariant};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Scope {
tenant_id: String,
session_id: String,
}
impl Scope {
#[must_use]
pub fn new(tenant_id: &str, session_id: &str) -> Self {
Self {
tenant_id: tenant_id.to_owned(),
session_id: session_id.to_owned(),
}
}
#[must_use]
pub fn tenant_id(&self) -> &str {
&self.tenant_id
}
#[must_use]
pub fn session_id(&self) -> &str {
&self.session_id
}
fn holds(&self, session_id: &str, tenant_id: &str) -> bool {
same_scope(&self.session_id, &self.tenant_id, session_id, tenant_id)
}
}
pub(super) fn same_scope(session_a: &str, tenant_a: &str, session_b: &str, tenant_b: &str) -> bool {
session_a == session_b && tenant_a == tenant_b
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Caller {
scope: Scope,
user_sub: String,
}
impl Caller {
pub(super) fn of(session: &SessionInvariant) -> Self {
Self::claimed(
session.tenant_id(),
session.session_id(),
session.user_sub(),
)
}
fn claimed(tenant_id: &str, session_id: &str, user_sub: &str) -> Self {
Self {
scope: Scope::new(tenant_id, session_id),
user_sub: user_sub.to_owned(),
}
}
pub(super) fn from_widget(handle: &RegisteredHandle) -> Self {
Self::claimed(handle.tenant_id(), handle.session_id(), handle.user_sub())
}
pub(super) fn from_bar(handle: &IntentHandle) -> Self {
Self::claimed(handle.tenant_id(), handle.session_id(), handle.user_sub())
}
pub(super) fn from_tenant(tenant_id: &str) -> Self {
Self::claimed(tenant_id, "", "")
}
#[must_use]
pub const fn scope(&self) -> &Scope {
&self.scope
}
#[must_use]
pub fn user_sub(&self) -> &str {
&self.user_sub
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Permission {
Capability(Capability),
CrossTenantInspect,
}
impl fmt::Display for Permission {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Capability(ref capability) => write!(formatter, "{capability}"),
Self::CrossTenantInspect => formatter.write_str("inspect.cross_tenant"),
}
}
}
impl Permission {
pub(super) fn refusal(self) -> BrokerError {
match self {
Self::Capability(capability) => {
BrokerError::CapabilityDenied(CapabilityError::NotGranted(capability))
}
Self::CrossTenantInspect => BrokerError::CrossTenantDenied,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Reach {
Own,
AllTenants,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Subject {
Widget(String),
MlPeer,
CommandBar,
Subscription,
Tap,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Emitter {
Widget,
Intent,
Prediction,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Inspector {
Subscriptions,
Taps,
}
impl Inspector {
pub const ALL: [Self; 2] = [Self::Subscriptions, Self::Taps];
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuditAction {
Route {
signal_id: String,
kind: String,
source_id: String,
},
Register(Subject),
Revoke(Subject),
Inspect(Inspector),
Emit(Emitter),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Decision {
Allowed,
Denied(BrokerError),
}
impl Decision {
pub(super) fn of<T>(result: &Result<T, BrokerError>) -> Self {
match result.as_ref().err() {
None => Self::Allowed,
Some(error) => Self::Denied(error.clone()),
}
}
#[must_use]
pub fn missing(&self) -> Option<Permission> {
match *self {
Self::Denied(BrokerError::CapabilityDenied(CapabilityError::NotGranted(
ref capability,
))) => Some(Permission::Capability(capability.clone())),
Self::Denied(BrokerError::CrossTenantDenied) => Some(Permission::CrossTenantInspect),
Self::Denied(_) | Self::Allowed => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AuditEvent {
seq: u64,
at_ms: u64,
caller: Caller,
action: AuditAction,
reach: Reach,
decision: Decision,
}
impl AuditEvent {
pub(super) const fn new(
seq: u64,
at_ms: u64,
caller: Caller,
action: AuditAction,
reach: Reach,
decision: Decision,
) -> Self {
Self {
seq,
at_ms,
caller,
action,
reach,
decision,
}
}
#[must_use]
pub const fn seq(&self) -> u64 {
self.seq
}
#[must_use]
pub const fn at_ms(&self) -> u64 {
self.at_ms
}
#[must_use]
pub const fn caller(&self) -> &Caller {
&self.caller
}
#[must_use]
pub const fn action(&self) -> &AuditAction {
&self.action
}
#[must_use]
pub const fn reach(&self) -> Reach {
self.reach
}
#[must_use]
pub const fn decision(&self) -> &Decision {
&self.decision
}
}
#[derive(Clone)]
pub(super) struct Visibility {
caller: Caller,
reach: Reach,
}
impl Visibility {
pub(super) const fn new(caller: Caller, reach: Reach) -> Self {
Self { caller, reach }
}
pub(super) const fn caller(&self) -> &Caller {
&self.caller
}
pub(super) fn admits(&self, session_id: &str, tenant_id: &str) -> bool {
matches!(self.reach, Reach::AllTenants) || self.caller.scope.holds(session_id, tenant_id)
}
}