BlueGit

ict rusty-teams

Files

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

scope.rs

scope.rs 1.9 KiB Download Raw
1use std::borrow::Cow;
2
3pub const GRAPH: &str = "https://graph.microsoft.com";
4pub const IC3: &str = "https://ic3.teams.office.com";
5pub const PRESENCE: &str = "https://presence.teams.microsoft.com";
6pub const SPACES: &str = "https://api.spaces.skype.com";
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub 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)]
17pub struct NestedApp {
18 pub client_id: &'static str,
19 pub redirect_uri: &'static str,
20 pub broker_redirect_uri: &'static str,
21}
22
23impl 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
53impl 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)]
60mod 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}