| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use crate::app::App; |
| 4 | |
| 5 | pub const GRAPH: &str = "https://graph.microsoft.com"; |
| 6 | pub const IC3: &str = "https://ic3.teams.office.com"; |
| 7 | pub const PRESENCE: &str = "https://presence.teams.microsoft.com"; |
| 8 | pub const OUTLOOK: &str = "https://outlook.office.com"; |
| 9 | pub const SPACES: &str = "https://api.spaces.skype.com"; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub 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)] |
| 20 | pub struct NestedApp { |
| 21 | pub client_id: &'static str, |
| 22 | pub redirect_uri: &'static str, |
| 23 | pub broker_redirect_uri: &'static str, |
| 24 | } |
| 25 | |
| 26 | impl 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 | |
| 65 | impl 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)] |
| 72 | mod tests { |
| 73 | use super::*; |
| 74 | |
| 75 | #[test] |
| 76 | fn chat_scopes_prefer_outlook() { |
| 77 | assert_eq!( |
| 78 | Scope::graph("Chat.Read").tab_order(), |
| 79 | [App::Outlook, App::Teams] |
| 80 | ); |
| 81 | assert_eq!( |
| 82 | Scope::graph("Chat.ReadWrite").tab_order(), |
| 83 | [App::Outlook, App::Teams] |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn other_scopes_prefer_teams() { |
| 89 | assert_eq!( |
| 90 | Scope::graph("ChannelMessage.Read.All").tab_order(), |
| 91 | [App::Teams, App::Outlook] |
| 92 | ); |
| 93 | assert_eq!( |
| 94 | Scope::new(IC3, "Teams.AccessAsUser.All").tab_order(), |
| 95 | [App::Teams, App::Outlook] |
| 96 | ); |
| 97 | assert_eq!( |
| 98 | Scope::new(PRESENCE, "Chat.Read").tab_order(), |
| 99 | [App::Teams, App::Outlook] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn displays_resource_and_name() { |
| 105 | assert_eq!( |
| 106 | Scope::graph("Chat.Read").to_string(), |
| 107 | "https://graph.microsoft.com/Chat.Read" |
| 108 | ); |
| 109 | } |
| 110 | } |