1use data_encoding::BASE32_NOPAD;
8use hmac::{Hmac, KeyInit, Mac};
9use sha1::Sha1;
10
11use crate::protocol::dom::{attr_value, strip_text, tags_where};
12
13pub fn two_factor_secret(html: &str) -> Option<String> {
15 let (_, _, inner) = tags_where(html, |_, attrs| {
16 attrs
17 .iter()
18 .any(|(name, _)| name == "data-two-factor-secret")
19 })
20 .into_iter()
21 .next()?;
22 Some(strip_text(&inner))
23}
24
25pub fn recovery_codes(html: &str) -> Vec<String> {
28 let Some((_, _, inner)) = tags_where(html, |_, attrs| {
29 attrs.iter().any(|(name, _)| name == "data-recovery-codes")
30 })
31 .into_iter()
32 .next() else {
33 return Vec::new();
34 };
35 tags_where(&inner, |tag, _| tag == "li")
36 .into_iter()
37 .map(|(_, _, code)| strip_text(&code))
38 .collect()
39}
40
41pub fn two_factor_state(html: &str) -> Option<String> {
44 tags_where(html, |tag, attrs| {
45 tag == "form" && attr_value(attrs, "data-two-factor").is_some()
46 })
47 .into_iter()
48 .next()
49 .and_then(|(_, attrs, _)| attr_value(&attrs, "data-two-factor"))
50}
51
52pub fn intent_skips_validation(html: &str, intent: &str) -> bool {
55 tags_where(html, |tag, attrs| {
56 tag == "button"
57 && attr_value(attrs, "name").as_deref() == Some("intent")
58 && attr_value(attrs, "value").as_deref() == Some(intent)
59 })
60 .iter()
61 .any(|(_, attrs, _)| attr_value(attrs, "formnovalidate").is_some())
62}
63
64pub fn authenticator_code_at(secret: &str, unix_secs: u64) -> String {
70 let key = BASE32_NOPAD
71 .decode(secret.trim().as_bytes())
72 .expect("the secret is base32");
73 let mut mac = Hmac::<Sha1>::new_from_slice(&key).expect("HMAC takes a key of any length");
74 mac.update(&(unix_secs / 30).to_be_bytes());
75 let digest = mac.finalize().into_bytes();
76 let offset = usize::from(digest[19] & 0x0f);
77 let binary = u32::from_be_bytes([
78 digest[offset] & 0x7f,
79 digest[offset + 1],
80 digest[offset + 2],
81 digest[offset + 3],
82 ]);
83 format!("{:06}", binary % 1_000_000)
84}
85
86pub fn authenticator_code(secret: &str) -> String {
88 authenticator_code_at(secret, now())
89}
90
91pub fn now() -> u64 {
93 std::time::SystemTime::now()
94 .duration_since(std::time::UNIX_EPOCH)
95 .expect("the clock is after 1970")
96 .as_secs()
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
105 fn the_code_matches_rfc_6238() {
106 let secret = BASE32_NOPAD.encode(b"12345678901234567890");
107 assert_eq!(authenticator_code_at(&secret, 59), "287082");
108 assert_eq!(authenticator_code_at(&secret, 1_111_111_109), "081804");
109 assert_eq!(authenticator_code_at(&secret, 2_000_000_000), "279037");
110 }
111}