useceleris_server/claims.rs
1use std::time::Duration;
2
3/// What one set of credentials grants. Your server decides the claims for a
4/// user that it has authenticated. Nothing from the client can make them
5/// wider.
6///
7/// `Claims` has no default. You must choose the channels and the permissions,
8/// so unrestricted access is never granted by omission (AUTH-02).
9/// [`Claims::new`] sets the other fields to their defaults: no reference, no
10/// replay and no echo.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct Claims {
13 /// The channels that the credentials can connect to.
14 pub channels: ChannelScope,
15
16 /// The access to the segments of those channels.
17 pub permissions: SegmentPermissions,
18
19 /// Identifies the token, such as a user id, in presence and in
20 /// deliveries. It must not be empty and must not contain a colon, CR or
21 /// LF. `None` leaves it out.
22 pub reference: Option<String>,
23
24 /// The history that the server replays each time the connection joins a
25 /// segment.
26 pub replay: Replay,
27
28 /// Lets a connection receive its own publishes.
29 pub allow_echo: bool,
30} // end struct Claims
31
32impl Claims {
33 /// Claims with the given channels and permissions, no reference, no
34 /// replay and no echo.
35 pub fn new(channels: ChannelScope, permissions: SegmentPermissions) -> Claims {
36 Claims {
37 channels,
38 permissions,
39 reference: None,
40 replay: Replay::None,
41 allow_echo: false,
42 }
43 } // end fn new
44} // end impl Claims
45
46/// The channels that credentials can connect to.
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub enum ChannelScope {
49 /// Every channel of the application.
50 All,
51
52 /// Only the listed channel references. List at least one, and each one
53 /// only once. A reference has 1 to 255 ASCII letters, digits, hyphens (-)
54 /// or underscores (_).
55 Restricted(Vec<String>),
56} // end enum ChannelScope
57
58/// The read and write access to segments.
59#[derive(Clone, Debug, PartialEq, Eq)]
60pub enum SegmentPermissions {
61 /// The same access to every segment.
62 All { read: bool, write: bool },
63
64 /// Access to the listed segments only, each one listed once. With no
65 /// segments, the credentials can connect but cannot read or write.
66 Restricted(Vec<SegmentClaim>),
67} // end enum SegmentPermissions
68
69/// The access to one segment.
70#[derive(Clone, Debug, PartialEq, Eq)]
71pub struct SegmentClaim {
72 /// Must not be empty and must not contain CR or LF.
73 pub segment_id: String,
74
75 pub read: bool,
76
77 pub write: bool,
78} // end struct SegmentClaim
79
80/// The history that the server replays when a connection joins a segment.
81#[derive(Clone, Copy, Debug, PartialEq, Eq)]
82pub enum Replay {
83 /// Replays nothing.
84 None,
85
86 /// Replays all the history that the server keeps for the segment.
87 Backlog,
88
89 /// Replays what the segment received in this period before the join: whole
90 /// milliseconds, from 0 to 4294967295. Zero replays nothing older than the
91 /// join, which is different from [`Replay::None`].
92 Lookback(Duration),
93} // end enum Replay