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