BlueGit

ict rusty-teams

Files

rusty-teams / crates / browser / src / sign_in_ready.rs

sign_in_ready.rs

sign_in_ready.rs 3.1 KiB Download Raw
1//! Decides that Teams sign-in is finished only when the page holds a fresh access token.
2//!
3//! Local storage key names that contain `accesstoken` or `refreshtoken` are not enough: a wiped
4//! cache still has those names, and treating them as success restarts Chromium and drops the
5//! encryption cookie. Merely decrypting sealed entries is not enough either once the encryption
6//! cookie persists across restarts, because an expired session still unseals its old entries.
7
8use std::sync::LazyLock;
9
10use serde_json::Value;
11
12/// Page script. Returns `{opened, freshAccess}`. `opened` counts sealed entries decrypted with
13/// the `msal.cache.encryption` cookie. `freshAccess` is a bearer access token whose `expiresOn`
14/// is still in the future, in the clear or among the opened entries.
15pub(crate) static PAGE_EXPRESSION: LazyLock<String> =
16 LazyLock::new(|| PAGE_TEMPLATE.replace("__UNSEAL__", session::UNSEAL_SCRIPT));
17
18const PAGE_TEMPLATE: &str = r#"(async () => {
19 const sealed = [];
20 const plain = [];
21 for (let index = 0; index < localStorage.length; index++) {
22 const key = localStorage.key(index);
23 let entry;
24 try { entry = JSON.parse(localStorage.getItem(key)); } catch (error) { continue; }
25 if (!entry || typeof entry !== 'object') continue;
26 if (entry.id && entry.nonce && entry.data) sealed.push([key, entry]);
27 else plain.push(entry);
28 }
29 const unsealEntries = __UNSEAL__;
30 const openedEntries = (await unsealEntries(sealed, new Map())).map(([key, entry]) => entry);
31 const now = Date.now();
32 const fresh = (entry) => entry && entry.credentialType === 'AccessToken' && (!entry.tokenType || entry.tokenType === 'Bearer') && Number(entry.expiresOn) * 1000 > now && typeof entry.secret === 'string' && entry.secret.length > 0;
33 return {opened: openedEntries.length, freshAccess: plain.some(fresh) || openedEntries.some(fresh)};
34})()"#;
35
36/// True when the page holds a valid access token, either plain or decrypted from a sealed entry.
37/// Key names and expired unsealed entries are ignored on purpose.
38pub(crate) fn sign_in_ready_from_page(value: &Value) -> bool {
39 value
40 .get("freshAccess")
41 .and_then(Value::as_bool)
42 .unwrap_or(false)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 use serde_json::json;
49
50 #[test]
51 fn key_names_without_a_usable_token_are_not_ready() {
52 let report = json!({"opened": 0, "freshAccess": false, "keyNames": true});
53 assert!(!sign_in_ready_from_page(&report));
54 }
55
56 #[test]
57 fn opened_sealed_entries_without_a_fresh_access_token_are_not_ready() {
58 assert!(!sign_in_ready_from_page(
59 &json!({"opened": 3, "freshAccess": false})
60 ));
61 }
62
63 #[test]
64 fn a_fresh_access_token_is_ready_plain_or_sealed() {
65 assert!(sign_in_ready_from_page(
66 &json!({"opened": 0, "freshAccess": true})
67 ));
68 assert!(sign_in_ready_from_page(
69 &json!({"opened": 2, "freshAccess": true})
70 ));
71 }
72
73 #[test]
74 fn a_missing_report_is_not_ready() {
75 assert!(!sign_in_ready_from_page(&json!({})));
76 assert!(!sign_in_ready_from_page(&json!(null)));
77 }
78}