agentmux_srv_core/identity/
google_offered.rs1pub const SERVICES: [&str; 3] = ["calendar", "drive", "gmail"];
18
19const APPROVED: &[&str] = &[];
22
23pub const SERVICES_VAR: &str = "AGENTMUX_GOOGLE_SERVICES";
25
26pub fn offered() -> Vec<String> {
28 let secret_given = std::env::var("AGENTMUX_GOOGLE_CLIENT_SECRET").is_ok_and(|v| !v.trim().is_empty());
29 offered_with(std::env::var(SERVICES_VAR).ok().as_deref(), secret_given)
30}
31
32fn offered_with(named: Option<&str>, secret_in_environment: bool) -> Vec<String> {
35 if let Some(list) = named.filter(|l| !l.trim().is_empty()) {
36 return list
37 .split(',')
38 .map(|s| s.trim().to_ascii_lowercase())
39 .filter(|s| SERVICES.contains(&s.as_str()))
40 .collect();
41 }
42 let all: &[&str] = if secret_in_environment { &SERVICES } else { APPROVED };
43 all.iter().map(|s| s.to_string()).collect()
44}
45
46pub fn service_of_scope(scope: &str) -> Option<&'static str> {
48 const API: &str = "https://www.googleapis.com/auth/";
49 if scope == "https://mail.google.com/" || scope.strip_prefix(API).is_some_and(|s| s.starts_with("gmail.")) {
50 return Some("gmail");
51 }
52 let name = scope.strip_prefix(API)?;
53 if name == "calendar" || name.starts_with("calendar.") {
54 Some("calendar")
55 } else if name == "drive" || name.starts_with("drive.") {
56 Some("drive")
57 } else {
58 None
59 }
60}
61
62pub fn first_not_offered(requested: &[String], offered: &[String]) -> Option<String> {
64 let service = requested.iter().filter_map(|s| service_of_scope(s)).find(|s| !offered.iter().any(|o| o == s))?;
65 let label = match service {
66 "calendar" => "Calendar",
67 "drive" => "Drive",
68 _ => "Gmail",
69 };
70 Some(format!("{label} isn't available in this version of AgentMux yet."))
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 fn scopes(names: &[&str]) -> Vec<String> {
78 names.iter().map(|n| format!("https://www.googleapis.com/auth/{n}")).collect()
79 }
80
81 #[test]
82 fn a_public_build_offers_what_google_approved_and_an_internal_one_everything() {
83 assert_eq!(offered_with(None, false), APPROVED.iter().map(|s| s.to_string()).collect::<Vec<_>>());
84 assert_eq!(offered_with(None, true), vec!["calendar", "drive", "gmail"]);
85 assert_eq!(offered_with(Some(" "), true), vec!["calendar", "drive", "gmail"], "a blank list names nothing");
86 }
87
88 #[test]
89 fn a_named_list_wins_and_ignores_what_it_does_not_know() {
90 assert_eq!(offered_with(Some("Drive, calendar"), false), vec!["drive", "calendar"]);
91 assert_eq!(offered_with(Some("drive,photos,"), true), vec!["drive"]);
92 }
93
94 #[test]
95 fn each_scope_belongs_to_its_service_and_the_sign_in_scopes_to_none() {
96 assert_eq!(service_of_scope("https://www.googleapis.com/auth/calendar.events.readonly"), Some("calendar"));
97 assert_eq!(service_of_scope("https://www.googleapis.com/auth/calendar"), Some("calendar"));
98 assert_eq!(service_of_scope("https://www.googleapis.com/auth/drive.file"), Some("drive"));
99 assert_eq!(service_of_scope("https://www.googleapis.com/auth/gmail.compose"), Some("gmail"));
100 assert_eq!(service_of_scope("https://mail.google.com/"), Some("gmail"));
101 for sign_in in ["openid", "email", "profile", "https://www.googleapis.com/auth/userinfo.email"] {
102 assert_eq!(service_of_scope(sign_in), None, "{sign_in}");
103 }
104 }
105
106 #[test]
107 fn a_request_stops_at_the_first_service_that_is_not_offered() {
108 let offered = vec!["drive".to_string()];
109 assert_eq!(first_not_offered(&scopes(&["drive.file"]), &offered), None);
110 assert_eq!(first_not_offered(&["openid".to_string(), "email".to_string()], &[]), None, "sign-in needs no service");
111 let why = first_not_offered(&scopes(&["drive.file", "gmail.readonly"]), &offered).unwrap();
112 assert_eq!(why, "Gmail isn't available in this version of AgentMux yet.");
113 let why = first_not_offered(&scopes(&["calendar.events.readonly"]), &offered).unwrap();
114 assert!(why.starts_with("Calendar"), "{why}");
115 assert!(first_not_offered(&scopes(&["gmail.readonly"]), &[]).is_some());
116 }
117}