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