BlueGit

ict rusty-teams

Files

rusty-teams / crates / session / src / scope.rs

scope.rs

scope.rs 2.9 KiB Download Raw
1use std::borrow::Cow;
2
3use crate::app::App;
4
5pub const GRAPH: &str = "https://graph.microsoft.com";
6pub const IC3: &str = "https://ic3.teams.office.com";
7pub const PRESENCE: &str = "https://presence.teams.microsoft.com";
8pub const OUTLOOK: &str = "https://outlook.office.com";
9pub const SPACES: &str = "https://api.spaces.skype.com";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Scope {
13 pub resource: Cow<'static, str>,
14 pub name: Cow<'static, str>,
15 pub nested_app: Option<NestedApp>,
16}
17
18/// An app hosted inside the tab that gets its tokens through the tab's own sign-in (nested app auth).
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct NestedApp {
21 pub client_id: &'static str,
22 pub redirect_uri: &'static str,
23 pub broker_redirect_uri: &'static str,
24}
25
26impl Scope {
27 pub fn new(resource: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
28 Scope {
29 resource: resource.into(),
30 name: name.into(),
31 nested_app: None,
32 }
33 }
34
35 pub fn for_nested_app(mut self, app: NestedApp) -> Self {
36 self.nested_app = Some(app);
37 self
38 }
39
40 pub(crate) fn nested_app_json(&self) -> serde_json::Value {
41 match self.nested_app {
42 Some(app) => serde_json::json!({
43 "clientId": app.client_id,
44 "redirectUri": app.redirect_uri,
45 "brokerRedirectUri": app.broker_redirect_uri,
46 }),
47 None => serde_json::Value::Null,
48 }
49 }
50
51 pub fn graph(name: impl Into<Cow<'static, str>>) -> Self {
52 Scope::new(GRAPH, name)
53 }
54
55 /// Tabs to try, best first. The other tab is the fallback, as in the Python client.
56 pub fn tab_order(&self) -> [App; 2] {
57 if self.resource == GRAPH && self.name.starts_with("Chat.") {
58 [App::Outlook, App::Teams]
59 } else {
60 [App::Teams, App::Outlook]
61 }
62 }
63}
64
65impl std::fmt::Display for Scope {
66 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 write!(formatter, "{}/{}", self.resource, self.name)
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn chat_scopes_prefer_outlook() {
77 assert_eq!(Scope::graph("Chat.Read").tab_order(), [App::Outlook, App::Teams]);
78 assert_eq!(Scope::graph("Chat.ReadWrite").tab_order(), [App::Outlook, App::Teams]);
79 }
80
81 #[test]
82 fn other_scopes_prefer_teams() {
83 assert_eq!(Scope::graph("ChannelMessage.Read.All").tab_order(), [App::Teams, App::Outlook]);
84 assert_eq!(Scope::new(IC3, "Teams.AccessAsUser.All").tab_order(), [App::Teams, App::Outlook]);
85 assert_eq!(Scope::new(PRESENCE, "Chat.Read").tab_order(), [App::Teams, App::Outlook]);
86 }
87
88 #[test]
89 fn displays_resource_and_name() {
90 assert_eq!(Scope::graph("Chat.Read").to_string(), "https://graph.microsoft.com/Chat.Read");
91 }
92}