Skip to main content

agentmux_srv_core/identity/
google_offered.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Which Google services AgentMux's own client may ask for in this build
5//! (SPEC_GOOGLE_PHASE_B_AGENTMUXS_OWN_CLIENT_2026_11_01.md D6, §8).
6//!
7//! Google reviews each tier of scopes per app, and an unreviewed one shows the person an unverified-app
8//! screen and counts toward a 100-user cap. A public build therefore asks only for a service whose tier
9//! Google has approved. The list is data: one edit in [`APPROVED`] when a tier is approved, no schema change.
10//! An internal build (the people who run `agentmux-dev`, up to 100 users) turns every service on through the
11//! same environment its client secret comes from.
12//!
13//! A client the person brought (BYO, the organization's internal one) is not limited: Google reviews it
14//! against their own project, not ours.
15
16/// Every service a Google account can be connected for, as the grant calls them.
17pub const SERVICES: [&str; 3] = ["calendar", "drive", "gmail"];
18
19/// The services whose tier Google has approved for AgentMux's own client, and which a public build offers.
20/// Stage 1 adds "drive", stage 2 "calendar", stage 3 "gmail" (§8). None is approved yet.
21const APPROVED: &[&str] = &[];
22
23/// The environment variable that names the offered services, comma separated, in a developer's build.
24pub const SERVICES_VAR: &str = "AGENTMUX_GOOGLE_SERVICES";
25
26/// The services AgentMux's own Google client offers in this build.
27pub 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
32/// [`offered`] with the environment given: the list a developer named, else every service for a build run with
33/// its client secret in the environment (an internal one), else the approved ones.
34fn 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
46/// The service a Google scope belongs to, or `None` for the sign-in scopes (`openid`, `email`, `profile`).
47pub 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
62/// The sentence for the first of `requested` whose service is not in `offered`, if any.
63pub 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}