super-voice-shared/src/daemon/scopes.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! Super Voice's auth scope catalog: the scopes every daemon understands |
3 | | //! ([`super_engine_protocol::scopes::CORE_SCOPES`]) plus Super Voice's own, |
4 | | //! for both of its features. |
5 | | //! Shared so the daemon (which validates `/auth/request`) and the consent |
6 | | //! dialog (which describes each scope to the user) can't drift. When they |
7 | | //! drift, a scope the daemon accepts but the consent binary doesn't recognize |
8 | | //! renders the "unknown scope — deny is safe" warning on a legitimate prompt, |
9 | | //! teaching users to distrust real requests. |
10 | | |
11 | | use super_engine_protocol::scopes; |
12 | | |
13 | | use crate::SUPER_VOICE; |
14 | | |
15 | | /// Every scope token the daemon understands, in wire (`snake_case`) form. A |
16 | | /// token may be granted any non-empty subset. Source of truth for |
17 | | /// `/auth/request` validation and the consent dialog; mirrors the scope |
18 | | /// catalog in `docs/protocol/auth.md`. |
19 | 2 | pub fn known_scopes() -> impl Iterator<Item = &'static str> { |
20 | 2 | scopes::known_scopes(&SUPER_VOICE) |
21 | 2 | } |
22 | | |
23 | | /// True if `s` is a recognized scope token. |
24 | | #[must_use] |
25 | 20 | pub fn is_known_scope(s: &str) -> bool { |
26 | 20 | scopes::is_known_scope(&SUPER_VOICE, s) |
27 | 20 | } |
28 | | |
29 | | #[cfg(test)] |
30 | | mod tests { |
31 | | use super::{is_known_scope, known_scopes}; |
32 | | |
33 | | /// The whole catalog, spelled out: one list for the one product, so a |
34 | | /// scope added or dropped is a decision someone made here. |
35 | | #[test] |
36 | 1 | fn the_catalog_is_super_voice_s() { |
37 | 1 | let mut known: Vec<&str> = known_scopes().collect(); |
38 | 1 | known.sort_unstable(); |
39 | 1 | assert_eq!( |
40 | 1 | known, |
41 | 1 | [ |
42 | 1 | "audio_visualization", |
43 | 1 | "daemon_status", |
44 | 1 | "global_transcriptions", |
45 | 1 | "playback_events", |
46 | 1 | "recording_events", |
47 | 1 | "secrets", |
48 | 1 | "settings", |
49 | 1 | "speak", |
50 | 1 | "status", |
51 | 1 | "synthesize", |
52 | 1 | "transcribe", |
53 | 1 | "voices", |
54 | 1 | ] |
55 | | ); |
56 | 1 | } |
57 | | |
58 | | #[test] |
59 | 1 | fn old_personas_and_garbage_are_rejected() { |
60 | 1 | for s in ["client", "widget", "", "Settings", "transcribe ", "global"] { |
61 | 6 | assert!(!is_known_scope(s), "{s:?} must not be a known scope"); |
62 | | } |
63 | 1 | } |
64 | | } |