use std::fmt;
use serde_json::Value;
pub const CAPS_CLAIM: &str = "caps";
pub const APP_CLAIM: &str = "app";
pub const CAPS_MAX: usize = 16;
pub const CAP_MAX_BYTES: usize = 32;
pub const REASON_CAP_CEILING: &str = "cap_ceiling";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum PassCap {
ChatRead,
ChatPost,
ChatRespond,
VoiceJoin,
VoiceVideo,
VoiceRecord,
}
impl PassCap {
pub const ALL: [PassCap; 6] = [
PassCap::ChatRead,
PassCap::ChatPost,
PassCap::ChatRespond,
PassCap::VoiceJoin,
PassCap::VoiceVideo,
PassCap::VoiceRecord,
];
pub const fn as_str(self) -> &'static str {
match self {
PassCap::ChatRead => "chat:read",
PassCap::ChatPost => "chat:post",
PassCap::ChatRespond => "chat:respond",
PassCap::VoiceJoin => "voice:join",
PassCap::VoiceVideo => "voice:video",
PassCap::VoiceRecord => "voice:record",
}
}
pub fn parse(s: &str) -> Option<PassCap> {
PassCap::ALL.into_iter().find(|c| c.as_str() == s)
}
pub const fn enforced(self) -> &'static str {
match self {
PassCap::ChatRead => "required in every claim; reading is not gated further",
PassCap::ChatPost => {
"POST /api/v1/channels/{id}/messages, /messages/{id}/forward, /messages/{id}/edit, \
/channels/{id}/scheduled, /scheduled/{id}/edit, /scheduled/{id}/send_now"
}
PassCap::ChatRespond => "reserved for the answer routes (not enforced yet)",
PassCap::VoiceJoin => {
"POST /api/v1/channels/{id}/voice/join, /channels/{id}/call/join, \
/channels/{id}/call/ring"
}
PassCap::VoiceVideo => {
"camera and screen-share leases: POST /api/v1/me/voice/state with self_video or \
self_stream true; without it a voice or call join is audio-only (its \
self_video is dropped) and the join answer's media shows camera_allowed and \
screen_allowed false"
}
PassCap::VoiceRecord => {
"only when the session's App sets recording.cap_required: POST \
/api/v1/channels/{id}/recording/start, /recordings/{id}/stop, \
/recordings/{id}/audio/retention, and /channels/{id}/update with a recording \
field; without it RECORD_CALLS is left out of the session's reported perms. \
Consent is never gated"
}
}
}
const fn bit(self) -> u8 {
match self {
PassCap::ChatRead => 1,
PassCap::ChatPost => 2,
PassCap::ChatRespond => 4,
PassCap::VoiceJoin => 8,
PassCap::VoiceVideo => 16,
PassCap::VoiceRecord => 32,
}
}
}
impl fmt::Display for PassCap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct CapSet(u8);
impl CapSet {
const MASK: u8 = 0b11_1111;
pub const fn empty() -> CapSet {
CapSet(0)
}
pub const fn with(self, cap: PassCap) -> CapSet {
CapSet(self.0 | cap.bit())
}
pub const fn contains(self, cap: PassCap) -> bool {
self.0 & cap.bit() != 0
}
pub const fn intersect(self, other: CapSet) -> CapSet {
CapSet(self.0 & other.0)
}
pub fn iter(self) -> impl Iterator<Item = PassCap> {
PassCap::ALL.into_iter().filter(move |c| self.contains(*c))
}
pub fn names(self) -> Vec<&'static str> {
self.iter().map(PassCap::as_str).collect()
}
pub const fn bits(self) -> u8 {
self.0
}
pub const fn from_bits(bits: u8) -> Option<CapSet> {
if bits & !Self::MASK == 0 {
Some(CapSet(bits))
} else {
None
}
}
}
impl FromIterator<PassCap> for CapSet {
fn from_iter<I: IntoIterator<Item = PassCap>>(iter: I) -> CapSet {
iter.into_iter().fold(CapSet::empty(), CapSet::with)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CapsError {
NotArray,
Empty,
TooMany,
NotString,
Malformed(String),
Duplicate(String),
NoChatRead,
}
impl fmt::Display for CapsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CapsError::NotArray => f.write_str("caps must be an array of strings"),
CapsError::Empty => f.write_str("caps must not be empty"),
CapsError::TooMany => write!(f, "caps holds more than {CAPS_MAX} entries"),
CapsError::NotString => f.write_str("a caps entry is not a string"),
CapsError::Malformed(s) => write!(f, "caps entry {s:?} is not <noun>:<verb>"),
CapsError::Duplicate(s) => write!(f, "caps entry {s:?} appears twice"),
CapsError::NoChatRead => f.write_str("caps must contain chat:read"),
}
}
}
impl std::error::Error for CapsError {}
fn word_ok(w: &str) -> bool {
let b = w.as_bytes();
!b.is_empty()
&& b[0].is_ascii_lowercase()
&& b.iter()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'_')
}
pub fn is_cap_entry(s: &str) -> bool {
(3..=CAP_MAX_BYTES).contains(&s.len())
&& s.split_once(':')
.is_some_and(|(n, v)| word_ok(n) && word_ok(v))
}
pub fn parse_caps_claim(v: &Value) -> Result<CapSet, CapsError> {
let Value::Array(items) = v else {
return Err(CapsError::NotArray);
};
if items.is_empty() {
return Err(CapsError::Empty);
}
if items.len() > CAPS_MAX {
return Err(CapsError::TooMany);
}
let mut seen: Vec<&str> = Vec::with_capacity(items.len());
let mut set = CapSet::empty();
for item in items {
let Value::String(s) = item else {
return Err(CapsError::NotString);
};
if !is_cap_entry(s) {
return Err(CapsError::Malformed(s.clone()));
}
if seen.contains(&s.as_str()) {
return Err(CapsError::Duplicate(s.clone()));
}
seen.push(s);
if let Some(c) = PassCap::parse(s) {
set = set.with(c);
}
}
if !set.contains(PassCap::ChatRead) {
return Err(CapsError::NoChatRead);
}
Ok(set)
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn caps_claim_table() {
let ok = |v: Value| parse_caps_claim(&v).map(CapSet::names);
assert_eq!(
ok(json!([
"chat:read",
"chat:post",
"chat:respond",
"voice:join"
])),
Ok(vec!["chat:read", "chat:post", "chat:respond", "voice:join"])
);
assert_eq!(
ok(json!(["voice:video", "chat:read", "voice:join"])),
Ok(vec!["chat:read", "voice:join", "voice:video"])
);
assert_eq!(ok(json!(["chat:read"])), Ok(vec!["chat:read"]));
assert_eq!(
ok(json!(["voice:join", "chat:read"])),
Ok(vec!["chat:read", "voice:join"])
);
assert_eq!(
ok(json!(["chat:read", "files:upload", "x2:y_3"])),
Ok(vec!["chat:read"])
);
let err = |v: Value| parse_caps_claim(&v).err();
assert_eq!(err(json!(null)), Some(CapsError::NotArray));
assert_eq!(err(json!("chat:read")), Some(CapsError::NotArray));
assert_eq!(err(json!({"chat:read": true})), Some(CapsError::NotArray));
assert_eq!(err(json!([])), Some(CapsError::Empty));
assert_eq!(err(json!(["chat:read", 1])), Some(CapsError::NotString));
assert_eq!(err(json!(["chat:read", null])), Some(CapsError::NotString));
for bad in [
"Chat:read",
"chat:Read",
"chat",
"chat:",
":read",
"chat:read:x",
"chat read",
"1chat:read",
"chat:-read",
] {
assert_eq!(
err(json!(["chat:read", bad])),
Some(CapsError::Malformed(bad.to_owned())),
"{bad}"
);
}
let long = format!("chat:{}", "r".repeat(CAP_MAX_BYTES - 4));
assert_eq!(long.len(), CAP_MAX_BYTES + 1);
assert_eq!(
err(json!(["chat:read", long])),
Some(CapsError::Malformed(long.clone()))
);
assert!(is_cap_entry(&long[..CAP_MAX_BYTES]));
assert_eq!(
err(json!(["chat:read", "chat:read"])),
Some(CapsError::Duplicate("chat:read".into()))
);
assert_eq!(
err(json!(["chat:post", "voice:join"])),
Some(CapsError::NoChatRead)
);
let many: Vec<String> = (0..=CAPS_MAX).map(|i| format!("x:y{i}")).collect();
assert_eq!(err(json!(many)), Some(CapsError::TooMany));
let mut sixteen: Vec<String> = (1..CAPS_MAX).map(|i| format!("x:y{i}")).collect();
sixteen.push("chat:read".into());
assert_eq!(ok(json!(sixteen)), Ok(vec!["chat:read"]));
}
#[test]
fn cap_set_bits() {
let all: CapSet = PassCap::ALL.into_iter().collect();
assert_eq!(all.bits(), 0b11_1111);
for c in PassCap::ALL {
assert_eq!(PassCap::parse(c.as_str()), Some(c));
let one = CapSet::empty().with(c);
assert_eq!(CapSet::from_bits(one.bits()), Some(one));
assert!(one.contains(c));
}
assert_eq!(CapSet::from_bits(0b100_0000), None);
assert_eq!(PassCap::parse("voice:video"), Some(PassCap::VoiceVideo));
assert_eq!(PassCap::parse("voice:record"), Some(PassCap::VoiceRecord));
assert_eq!(
CapSet::from_bits(32).map(CapSet::names),
Some(vec!["voice:record"])
);
let a = CapSet::empty()
.with(PassCap::ChatRead)
.with(PassCap::VoiceJoin);
let b = CapSet::empty()
.with(PassCap::ChatRead)
.with(PassCap::ChatPost);
assert_eq!(a.intersect(b).names(), vec!["chat:read"]);
}
}