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