| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | pub const GRAPH: &str = "https://graph.microsoft.com"; |
| 4 | pub const IC3: &str = "https://ic3.teams.office.com"; |
| 5 | pub const PRESENCE: &str = "https://presence.teams.microsoft.com"; |
| 6 | pub const SPACES: &str = "https://api.spaces.skype.com"; |
| 7 | |
| 8 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 9 | pub struct Scope { |
| 10 | pub resource: Cow<'static, str>, |
| 11 | pub name: Cow<'static, str>, |
| 12 | pub nested_app: Option<NestedApp>, |
| 13 | } |
| 14 | |
| 15 | /// An app hosted inside the tab that gets its tokens through the tab's own sign-in (nested app auth). |
| 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 17 | pub struct NestedApp { |
| 18 | pub client_id: &'static str, |
| 19 | pub redirect_uri: &'static str, |
| 20 | pub broker_redirect_uri: &'static str, |
| 21 | } |
| 22 | |
| 23 | impl Scope { |
| 24 | pub fn new(resource: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self { |
| 25 | Scope { |
| 26 | resource: resource.into(), |
| 27 | name: name.into(), |
| 28 | nested_app: None, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn for_nested_app(mut self, app: NestedApp) -> Self { |
| 33 | self.nested_app = Some(app); |
| 34 | self |
| 35 | } |
| 36 | |
| 37 | pub(crate) fn nested_app_json(&self) -> serde_json::Value { |
| 38 | match self.nested_app { |
| 39 | Some(app) => serde_json::json!({ |
| 40 | "clientId": app.client_id, |
| 41 | "redirectUri": app.redirect_uri, |
| 42 | "brokerRedirectUri": app.broker_redirect_uri, |
| 43 | }), |
| 44 | None => serde_json::Value::Null, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn graph(name: impl Into<Cow<'static, str>>) -> Self { |
| 49 | Scope::new(GRAPH, name) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl std::fmt::Display for Scope { |
| 54 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 55 | write!(formatter, "{}/{}", self.resource, self.name) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[cfg(test)] |
| 60 | mod tests { |
| 61 | use super::*; |
| 62 | |
| 63 | #[test] |
| 64 | fn displays_resource_and_name() { |
| 65 | assert_eq!( |
| 66 | Scope::graph("Chat.Read").to_string(), |
| 67 | "https://graph.microsoft.com/Chat.Read" |
| 68 | ); |
| 69 | } |
| 70 | } |