spoolframe_protocol/federation/assertion.rs
1//! Delegated authentication (P8-03): the server a user is at home on (SB)
2//! vouches for that user to the server whose item they want to play (SA),
3//! and SA answers with a short-lived token for that one item.
4//!
5//! [`AssertUser`] arriving on SB's connection is the proof: the connection
6//! proves SB's endpoint key, and only a live pair's connection reaches it.
7//! There is no signature, and an assertion cannot be replayed on another
8//! connection. SB asserts only its own local accounts, never a portable
9//! guest of its, and names each by its stable user id, so a rename on SB
10//! changes nothing on SA.
11//!
12//! SA checks that the item is in a library shared with SB, and mints a
13//! random bearer token, bound to the device's endpoint id: a token that
14//! leaks is useless from another device. P8-04 redeems it on SA's client
15//! protocol.
16
17use std::fmt;
18
19use serde::{Deserialize, Serialize};
20
21/// What a token opens. A play token opens one item's playback; a
22/// download token (P8-07) opens one item's download.
23#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum TokenScope {
26 #[default]
27 Play,
28 /// One item's download (P8-07): preparing it, asking how it goes,
29 /// cancelling it, reading its file and hearing `DownloadChanged`. The
30 /// session it opens lives while the download is made and read, not
31 /// minutes as a play's.
32 Download,
33 /// A scope this build does not know: refused.
34 #[serde(other)]
35 Unknown,
36}
37
38impl TokenScope {
39 /// Its name on the wire, and in the store.
40 pub fn name(self) -> &'static str {
41 match self {
42 TokenScope::Play => "play",
43 TokenScope::Download => "download",
44 TokenScope::Unknown => "unknown",
45 }
46 }
47}
48
49/// SB vouches for its local `user`, who wants `item` of SA's on the
50/// device `client_endpoint`: `Token`, or `AssertionRefused`.
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
52pub struct AssertUser {
53 /// The user's stable id on SB: a UUID, never a portable user's id.
54 pub user: String,
55 /// Always empty (F8-08): a server never sends its users' names to
56 /// another, which knows them by `user` alone. Kept for the wire, to
57 /// go in the federation protocol's next version; what a server reads
58 /// of it is cut to [`crate::MAX_HELLO_TEXT`] characters.
59 pub display_name: String,
60 /// The device's endpoint id, hex: the only one the token works from.
61 pub client_endpoint: String,
62 /// The item, by SA's own id (what SB's `fed:` id carries).
63 pub item: String,
64 /// What the token is for; a play when left out.
65 #[serde(default)]
66 pub scope: TokenScope,
67 /// The user's track preferences on SB (P8-04): what SA chooses the
68 /// audio and subtitles by when the play sends no selection, as SB
69 /// would. Left out, SA's defaults.
70 #[serde(default, skip_serializing_if = "Option::is_none")]
71 pub preferences: Option<spoolframe_types::TrackPreferences>,
72 /// A download token's one download, by SA's id (P8-07): what the
73 /// device's own token reads. Left out, the token's session prepares
74 /// the item's download itself (SB's own, which keeps the record).
75 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub download: Option<String>,
77}
78
79impl AssertUser {
80 /// This assertion with its name cut to [`crate::MAX_HELLO_TEXT`]
81 /// characters: what a server reads of it.
82 pub fn within_bounds(mut self) -> Self {
83 self.display_name = super::server_name(self.display_name);
84 self
85 }
86}
87
88/// SA's token for an [`AssertUser`].
89#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
90pub struct TokenGranted {
91 /// The bearer token: whoever holds it, from the device it is bound to,
92 /// may do what its scope says with the item, once.
93 pub token: String,
94 pub scope: TokenScope,
95 /// When it stops working, Unix seconds: minutes from now.
96 pub expires_at: i64,
97}
98
99impl fmt::Debug for TokenGranted {
100 // The token opens an item until it expires: keep it out of logs.
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 f.debug_struct("TokenGranted")
103 .field("token", &"<redacted>")
104 .field("scope", &self.scope)
105 .field("expires_at", &self.expires_at)
106 .finish()
107 }
108}
109
110/// Why SA minted no token. Each is counted against the pair.
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(rename_all = "snake_case")]
113pub enum AssertionRefused {
114 /// The item is in no library shared with the pair, or is not SA's own.
115 NotShared,
116 /// The user is not named by a stable user id: a portable guest, or
117 /// nothing that reads.
118 InvalidUser,
119 /// The device's endpoint id does not read.
120 InvalidDevice,
121 /// A scope SA does not know.
122 InvalidScope,
123 /// SA cannot say now (its catalog or store failed): try again later.
124 Unavailable,
125 /// A reason this build does not know.
126 #[serde(other)]
127 Unknown,
128}
129
130impl fmt::Display for AssertionRefused {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 f.write_str(match self {
133 AssertionRefused::NotShared => "the item is not shared with this server",
134 AssertionRefused::InvalidUser => "that is not a user the server may vouch for",
135 AssertionRefused::InvalidDevice => "the device's id does not read",
136 AssertionRefused::InvalidScope => "a token for that is not made",
137 AssertionRefused::Unavailable => "the other server cannot answer now",
138 AssertionRefused::Unknown => "refused for a reason this build does not know",
139 })
140 }
141}