agentmux_srv_core/identity/
auth_patterns.rs1#[derive(Debug, Clone, PartialEq)]
13pub enum AuthPatternMatch {
14 OAuthUrl(String),
16 DeviceCode {
18 code: String,
20 verification_url: String,
22 },
23 LoginSuccess { email: Option<String> },
26}
27
28pub fn match_line(provider_id: &str, line: &str) -> Option<AuthPatternMatch> {
32 let t = agentmux_common::login_pty::strip_terminal_codes(line);
34 let from_text = match_clean_line(provider_id, &t.text);
35 if from_text.as_ref().is_some_and(|m| !matches!(m, AuthPatternMatch::OAuthUrl(_))) {
38 return from_text;
39 }
40 t.link_uris
42 .iter()
43 .find_map(|uri| match match_clean_line(provider_id, uri) {
44 Some(url @ AuthPatternMatch::OAuthUrl(_)) => Some(url),
45 _ => None,
46 })
47 .or(from_text)
48}
49
50fn match_clean_line(provider_id: &str, line: &str) -> Option<AuthPatternMatch> {
52 for matcher in patterns_for(provider_id) {
53 if let Some(m) = matcher(line) {
54 return Some(m);
55 }
56 }
57 if is_api_key_provider(provider_id) {
64 return None;
65 }
66 if let Some(url) = extract_first_https_url(line) {
67 if looks_like_oauth_url(&url) {
68 return Some(AuthPatternMatch::OAuthUrl(url));
69 }
70 }
71 None
72}
73
74fn is_api_key_provider(provider_id: &str) -> bool {
75 matches!(provider_id, "pi")
76}
77
78type LineMatcher = fn(&str) -> Option<AuthPatternMatch>;
79
80fn patterns_for(provider_id: &str) -> &'static [LineMatcher] {
81 match provider_id {
82 "claude" => &[match_claude_url, match_logged_in_as],
83 "codex" => &[match_codex_device_code, match_codex_url, match_logged_in_as],
84 "gemini" => &[match_gemini_url, match_logged_in_as],
85 "copilot" => &[match_copilot_device_code, match_logged_in_as],
86 "pi" => &[],
90 _ => &[],
91 }
92}
93
94fn match_claude_url(line: &str) -> Option<AuthPatternMatch> {
99 if let Some(url) = extract_first_https_url(line) {
104 if url.contains("claude.com/cai/oauth")
105 || url.contains("claude.ai/oauth")
106 || url.contains("anthropic.com/oauth")
107 || url.contains("console.anthropic.com")
108 {
109 return Some(AuthPatternMatch::OAuthUrl(url));
110 }
111 }
112 None
113}
114
115fn match_codex_url(line: &str) -> Option<AuthPatternMatch> {
116 if let Some(url) = extract_first_https_url(line) {
117 if url.contains("auth.openai.com")
118 || url.contains("platform.openai.com")
119 || url.contains("openai.com/oauth")
120 {
121 return Some(AuthPatternMatch::OAuthUrl(url));
122 }
123 }
124 None
125}
126
127fn match_codex_device_code(line: &str) -> Option<AuthPatternMatch> {
131 let code = line.trim();
132 let (head, tail) = code.split_once('-')?;
133 let part = |s: &str, len: std::ops::RangeInclusive<usize>| len.contains(&s.len()) && s.chars().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit());
134 (part(head, 4..=5) && part(tail, 4..=6)).then(|| AuthPatternMatch::DeviceCode { code: code.to_string(), verification_url: String::new() })
135}
136
137fn match_gemini_url(line: &str) -> Option<AuthPatternMatch> {
138 if let Some(url) = extract_first_https_url(line) {
139 if url.contains("accounts.google.com") || url.contains("oauth2.googleapis.com") {
140 return Some(AuthPatternMatch::OAuthUrl(url));
141 }
142 }
143 None
144}
145
146fn match_copilot_device_code(line: &str) -> Option<AuthPatternMatch> {
147 let line_low = line.to_lowercase();
153 let mentions_device = line_low.contains("github.com/login/device")
154 || line_low.contains("one-time code")
155 || line_low.contains("enter code");
156 if !mentions_device {
157 return None;
158 }
159 let code = extract_device_code(line);
160 if let Some(code) = code {
161 return Some(AuthPatternMatch::DeviceCode {
163 code,
164 verification_url: "https://github.com/login/device".to_string(),
165 });
166 }
167 None
168}
169
170fn match_logged_in_as(line: &str) -> Option<AuthPatternMatch> {
171 let line_low = line.to_lowercase();
172 if line_low.contains("not authenticated")
177 || line_low.contains("not logged in")
178 || line_low.contains("n't authenticated")
179 || line_low.contains("n't logged in")
180 || line_low.contains("failed to authenticate")
181 || line_low.contains("authentication failed")
182 {
183 return None;
184 }
185 if !line_low.contains("logged in") && !line_low.contains("authenticated") {
186 return None;
187 }
188 let email = extract_email(line);
191 Some(AuthPatternMatch::LoginSuccess { email })
192}
193
194fn extract_first_https_url(line: &str) -> Option<String> {
199 let start = line.find("https://")?;
200 let tail = &line[start..];
201 let end = tail
204 .find(|c: char| c.is_whitespace() || c == '\'' || c == '"' || c == '`' || c == ')')
205 .unwrap_or(tail.len());
206 let url = &tail[..end];
207 let trimmed = url.trim_end_matches(|c: char| matches!(c, '.' | ',' | ';' | '!' | '?'));
213 if trimmed.is_empty() {
214 None
215 } else {
216 Some(trimmed.to_string())
217 }
218}
219
220fn looks_like_oauth_url(url: &str) -> bool {
221 let u = url.to_lowercase();
222 u.contains("oauth")
223 || u.contains("/authorize")
224 || u.contains("/login")
225 || u.contains("/auth")
226 || u.contains("device")
227}
228
229fn extract_email(line: &str) -> Option<String> {
230 for token in line.split_whitespace() {
233 let token = token.trim_matches(|c: char| !c.is_alphanumeric() && c != '@' && c != '.' && c != '-' && c != '_' && c != '+');
234 if !token.contains('@') {
235 continue;
236 }
237 let Some((local, domain)) = token.split_once('@') else {
244 continue;
245 };
246 if local.is_empty() || domain.is_empty() {
247 continue;
248 }
249 if !domain.contains('.') {
250 continue;
251 }
252 return Some(token.to_string());
253 }
254 None
255}
256
257fn extract_device_code(line: &str) -> Option<String> {
258 for token in line.split(|c: char| c.is_whitespace() || c == ':' || c == ',' || c == '.') {
261 if token.len() == 9 {
262 let bytes = token.as_bytes();
263 let is_code = bytes[4] == b'-'
264 && bytes[..4]
265 .iter()
266 .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
267 && bytes[5..]
268 .iter()
269 .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit());
270 if is_code {
271 return Some(token.to_string());
272 }
273 }
274 }
275 None
276}
277
278#[cfg(test)]
279mod tests {
280 use super::*;
281
282 #[test]
283 fn claude_url_matched() {
284 let line = "Open this URL in your browser to authorize: https://console.anthropic.com/oauth/authorize?response_type=code&state=xyz";
285 let m = match_line("claude", line);
286 assert!(matches!(m, Some(AuthPatternMatch::OAuthUrl(_))));
287 if let Some(AuthPatternMatch::OAuthUrl(u)) = m {
288 assert!(u.contains("anthropic.com/oauth"));
289 assert!(u.contains("response_type=code"));
290 }
291 }
292
293 #[test]
294 fn codex_url_matched() {
295 let line = "Please visit https://auth.openai.com/u/login/identifier?state=abc to continue";
296 let m = match_line("codex", line);
297 assert!(matches!(m, Some(AuthPatternMatch::OAuthUrl(u)) if u.contains("openai.com")));
298 }
299
300 #[test]
301 fn gemini_url_matched() {
302 let line = "Visit https://accounts.google.com/o/oauth2/auth?client_id=abc&scope=...";
303 let m = match_line("gemini", line);
304 assert!(matches!(m, Some(AuthPatternMatch::OAuthUrl(u)) if u.contains("google.com")));
305 }
306
307 #[test]
308 fn copilot_device_code_matched() {
309 let line = "! First copy your one-time code: ABCD-1234";
310 let m = match_line("copilot", line);
311 if let Some(AuthPatternMatch::DeviceCode { code, verification_url }) = m {
312 assert_eq!(code, "ABCD-1234");
313 assert_eq!(verification_url, "https://github.com/login/device");
314 } else {
315 panic!("expected DeviceCode, got {m:?}");
316 }
317 }
318
319 #[test]
320 fn copilot_device_url_line_matched() {
321 let line = "Please visit https://github.com/login/device and enter code WXYZ-5678";
322 let m = match_line("copilot", line);
323 if let Some(AuthPatternMatch::DeviceCode { code, .. }) = m {
324 assert_eq!(code, "WXYZ-5678");
325 } else {
326 panic!("expected DeviceCode, got {m:?}");
327 }
328 }
329
330 #[test]
331 fn login_success_with_email() {
332 let m = match_line("claude", "Successfully logged in as asaf@example.com");
333 if let Some(AuthPatternMatch::LoginSuccess { email }) = m {
334 assert_eq!(email.as_deref(), Some("asaf@example.com"));
335 } else {
336 panic!("expected LoginSuccess, got {m:?}");
337 }
338 }
339
340 #[test]
341 fn login_success_without_email() {
342 let m = match_line("claude", "You are now authenticated.");
343 assert!(matches!(m, Some(AuthPatternMatch::LoginSuccess { email: None })));
344 }
345
346 #[test]
347 fn login_success_skips_negative_forms() {
348 for line in [
352 "Error: not authenticated",
353 "you are not authenticated",
354 "user isn't authenticated yet",
355 "You aren't logged in",
356 "failed to authenticate",
357 "Authentication failed",
358 ] {
359 let m = match_line("claude", line);
360 assert!(m.is_none(), "expected None for {line:?}, got {m:?}");
361 }
362 }
363
364 #[test]
365 fn fallback_https_matches_for_unknown_providers() {
366 let line = "Open https://example.com/oauth/authorize?state=x";
370 let m = match_line("future-provider", line);
371 assert!(matches!(m, Some(AuthPatternMatch::OAuthUrl(_))));
372 }
373
374 #[test]
375 fn fallback_skips_non_oauth_https() {
376 let line = "See https://docs.example.com/getting-started for details.";
379 let m = match_line("future-provider", line);
380 assert!(m.is_none());
381 }
382
383 #[test]
384 fn api_key_providers_match_nothing() {
385 for provider in ["pi"] {
389 let m = match_line(provider, "Get your API key at https://example.com/keys");
390 assert!(m.is_none(), "provider {provider} unexpectedly matched");
391 }
392 }
393
394 #[test]
395 fn url_extraction_trims_trailing_punctuation() {
396 let cases = [
401 ("Go to https://example.com/login.", "https://example.com/login"),
402 ("Open https://example.com/auth, then press Enter", "https://example.com/auth"),
403 ("Visit https://example.com/login!", "https://example.com/login"),
404 ("Done at https://example.com/oauth?state=xyz?", "https://example.com/oauth?state=xyz"),
405 ];
406 for (line, expected) in cases {
407 let url = extract_first_https_url(line).expect("url");
408 assert_eq!(url, expected, "for line: {line}");
409 }
410 }
411
412 #[test]
413 fn url_extraction_preserves_internal_punctuation() {
414 let line = "Open https://example.com/path.html?key=value&other=v2";
417 let url = extract_first_https_url(line).expect("url");
418 assert_eq!(url, "https://example.com/path.html?key=value&other=v2");
419 }
420
421 #[test]
422 fn api_key_provider_fallback_returns_none() {
423 for provider in ["pi"] {
429 let line = "Get your API key at https://example.com/auth/keys";
430 assert!(match_line(provider, line).is_none(), "{provider} matched");
431 }
432 let m = match_line("unknown-provider", "Open https://example.com/oauth/authorize");
434 assert!(matches!(m, Some(AuthPatternMatch::OAuthUrl(_))));
435 }
436
437 #[test]
438 fn email_extractor_ignores_placeholders() {
439 assert!(extract_email("Sign in as <email>").is_none());
441 assert!(extract_email("Twitter handle: @example").is_none());
443 }
444
445 #[test]
446 fn device_code_extractor_recognises_correct_shape() {
447 assert_eq!(extract_device_code("code: ABCD-1234"), Some("ABCD-1234".to_string()));
448 assert_eq!(extract_device_code("the code is XYZW-0987 right here"), Some("XYZW-0987".to_string()));
449 assert_eq!(extract_device_code("code: ABC-1234"), None);
451 assert_eq!(extract_device_code("code: ABCD_1234"), None);
452 assert_eq!(extract_device_code("code: abcd-1234"), None);
454 }
455
456 const CLAUDE_AUTHORIZE: &str =
457 "https://claude.com/cai/oauth/authorize?code=true&client_id=abc-123&code_challenge=xyz&state=st-789";
458
459 #[test]
460 fn claude_matches_its_current_authorize_url() {
461 let line = format!("If the browser didn't open, visit: {CLAUDE_AUTHORIZE}");
462 assert_eq!(match_line("claude", &line), Some(AuthPatternMatch::OAuthUrl(CLAUDE_AUTHORIZE.to_string())));
463 }
464
465 #[test]
466 fn a_url_inside_colour_codes_is_matched_without_them() {
467 let line = format!("\x1b[1;34m{CLAUDE_AUTHORIZE}\x1b[0m");
468 assert_eq!(match_line("claude", &line), Some(AuthPatternMatch::OAuthUrl(CLAUDE_AUTHORIZE.to_string())));
469 }
470
471 #[test]
472 fn an_osc8_link_target_wins_over_wrapped_visible_text() {
473 let line = format!("\x1b]8;;{CLAUDE_AUTHORIZE}\x07https://claude.com/cai/oauth/auth\x1b]8;;\x07");
475 assert_eq!(match_line("claude", &line), Some(AuthPatternMatch::OAuthUrl(CLAUDE_AUTHORIZE.to_string())));
476 }
477
478 #[test]
479 fn a_device_code_in_the_visible_text_wins_over_a_link_target() {
480 let line = "Please visit ]8;;https://github.com/login/devicehttps://github.com/login/device]8;; and enter code ABCD-1234";
481 assert!(matches!(match_line("copilot", line), Some(AuthPatternMatch::DeviceCode { ref code, .. }) if code == "ABCD-1234"), "{:?}", match_line("copilot", line));
482 }
483
484 #[test]
485 fn a_non_ascii_email_on_a_coloured_success_line_survives() {
486 let line = "[32mSuccessfully logged in as josé@example.com[0m";
487 assert_eq!(match_line("claude", line), Some(AuthPatternMatch::LoginSuccess { email: Some("josé@example.com".to_string()) }));
488 }
489
490 #[test]
492 fn codex_device_auth_code_line_is_a_code_and_only_that() {
493 assert_eq!(
494 match_line("codex", " RC63-52E85"),
495 Some(AuthPatternMatch::DeviceCode { code: "RC63-52E85".into(), verification_url: String::new() })
496 );
497 assert!(matches!(match_line("codex", " https://auth.openai.com/codex/device"), Some(AuthPatternMatch::OAuthUrl(_))));
498 for text in ["2. Enter this one-time code (expires in 15 minutes)", "Device codes are a common phishing target.", "gpt-5-codex", "rc63-52e85", ""] {
499 assert!(match_line("codex", text).is_none(), "{text}");
500 }
501 assert!(match_line("claude", "RC63-52E85").is_none());
503 }
504}