| 1 | use serde::Deserialize; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 4 | pub enum App { |
| 5 | Teams, |
| 6 | } |
| 7 | |
| 8 | impl App { |
| 9 | pub const ALL: [App; 1] = [App::Teams]; |
| 10 | |
| 11 | pub fn origins(self) -> &'static [&'static str] { |
| 12 | match self { |
| 13 | App::Teams => &[ |
| 14 | "https://teams.cloud.microsoft", |
| 15 | "https://teams.microsoft.com", |
| 16 | ], |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn start_url(self) -> &'static str { |
| 21 | match self { |
| 22 | App::Teams => "https://teams.cloud.microsoft/", |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// A static page on the app origin: keeps the MSAL cache reachable without running the app. |
| 27 | pub fn park_url(self) -> &'static str { |
| 28 | match self { |
| 29 | App::Teams => "https://teams.cloud.microsoft/robots.txt", |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn is_park_url(self, url: &str) -> bool { |
| 34 | url.starts_with(self.park_url()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl std::fmt::Display for App { |
| 39 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 40 | formatter.write_str(match self { |
| 41 | App::Teams => "teams", |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const LOGIN_ORIGINS: [&str; 2] = [ |
| 47 | "https://login.microsoftonline.com", |
| 48 | "https://login.live.com", |
| 49 | ]; |
| 50 | |
| 51 | #[derive(Debug, Clone, Deserialize)] |
| 52 | pub struct Target { |
| 53 | #[serde(rename = "type")] |
| 54 | pub kind: String, |
| 55 | #[serde(default)] |
| 56 | pub url: String, |
| 57 | #[serde(default, rename = "webSocketDebuggerUrl")] |
| 58 | pub websocket_url: Option<String>, |
| 59 | } |
| 60 | |
| 61 | pub fn find_app_tab(targets: &[Target], app: App) -> Option<&Target> { |
| 62 | targets.iter().find(|target| { |
| 63 | target.kind == "page" |
| 64 | && target.websocket_url.is_some() |
| 65 | && app |
| 66 | .origins() |
| 67 | .iter() |
| 68 | .any(|origin| target.url.starts_with(origin)) |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | pub fn is_login_url(url: &str) -> bool { |
| 73 | LOGIN_ORIGINS.iter().any(|origin| url.starts_with(origin)) |
| 74 | } |
| 75 | |
| 76 | pub fn has_login_tab(targets: &[Target]) -> bool { |
| 77 | targets |
| 78 | .iter() |
| 79 | .any(|target| target.kind == "page" && is_login_url(&target.url)) |
| 80 | } |
| 81 | |
| 82 | #[cfg(test)] |
| 83 | mod tests { |
| 84 | use super::*; |
| 85 | |
| 86 | fn targets() -> Vec<Target> { |
| 87 | serde_json::from_str( |
| 88 | r#"[ |
| 89 | {"type":"service_worker","url":"https://teams.cloud.microsoft/sw.js","webSocketDebuggerUrl":"ws://x/1"}, |
| 90 | {"type":"page","url":"https://outlook.cloud.microsoft/mail/","webSocketDebuggerUrl":"ws://x/2"}, |
| 91 | {"type":"page","url":"https://teams.cloud.microsoft/v2/","webSocketDebuggerUrl":"ws://x/3"}, |
| 92 | {"type":"page","url":"about:blank"} |
| 93 | ]"#, |
| 94 | ) |
| 95 | .unwrap() |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn finds_page_tabs_per_app() { |
| 100 | let targets = targets(); |
| 101 | assert_eq!( |
| 102 | find_app_tab(&targets, App::Teams) |
| 103 | .unwrap() |
| 104 | .websocket_url |
| 105 | .as_deref(), |
| 106 | Some("ws://x/3") |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn ignores_non_page_targets_and_missing_tabs() { |
| 112 | let only_worker = &targets()[..1]; |
| 113 | assert!(find_app_tab(only_worker, App::Teams).is_none()); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn parked_tabs_are_found_by_origin_and_recognised() { |
| 118 | let parked: Vec<Target> = serde_json::from_str( |
| 119 | r#"[{"type":"page","url":"https://teams.cloud.microsoft/robots.txt","webSocketDebuggerUrl":"ws://x/9"}, |
| 120 | {"type":"page","url":"https://outlook.cloud.microsoft/owa/favicon.ico","webSocketDebuggerUrl":"ws://x/8"}]"#, |
| 121 | ) |
| 122 | .unwrap(); |
| 123 | let teams = find_app_tab(&parked, App::Teams).unwrap(); |
| 124 | assert!(App::Teams.is_park_url(&teams.url)); |
| 125 | assert!(!App::Teams.is_park_url("https://teams.cloud.microsoft/v2/")); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn detects_login_tab() { |
| 130 | let mut targets = targets(); |
| 131 | assert!(!has_login_tab(&targets)); |
| 132 | targets.push(Target { |
| 133 | kind: "page".into(), |
| 134 | url: "https://login.microsoftonline.com/common/oauth2".into(), |
| 135 | websocket_url: None, |
| 136 | }); |
| 137 | assert!(has_login_tab(&targets)); |
| 138 | } |
| 139 | } |