Skip to main content

grr_cli/commands/
auth.rs

1//! Auth CLI commands: account-level authentication.
2//!
3//! No typed service client is involved: `login` needs only the OAuth
4//! handle, and `status` reaches Gmail's profile through the shared
5//! Discovery call path (`gmail.users.getProfile`) — the same engine as
6//! `grr api call` and the generated tree.
7
8use crate::commands::api;
9use crate::core::auth::GoogleAuth;
10use crate::discovery;
11use crate::output::{OutputFormat, print_output};
12use anyhow::Result;
13use clap::Subcommand;
14use serde_json::{Value, json};
15
16#[derive(Subcommand, Debug)]
17pub enum AuthCommands {
18    /// Fresh login. Default is the PKCE browser (loopback) flow; --device
19    /// prints a URL + code for headless environments instead.
20    Login(LoginArgs),
21    /// Show the authenticated account (fails when no valid credential)
22    Status(StatusArgs),
23    /// Create and store an OAuth client. Release builds ship with one, so
24    /// this is only needed when using your own client.
25    Setup(crate::commands::setup::SetupArgs),
26}
27
28#[derive(clap::Args, Debug)]
29pub struct LoginArgs {
30    /// Use the OAuth device flow instead of the browser loopback flow.
31    /// Needs a client of type "TVs and Limited Input devices"
32    /// (GRR_OAUTH__DEVICE_CLIENT_ID); the browser flow's Desktop client is
33    /// not accepted by Google's device endpoint.
34    #[arg(long)]
35    pub device: bool,
36
37    /// Log in as a NAMED account (e.g. work), keeping a separate token per
38    /// account. Omit for the default account.
39    #[arg(long)]
40    pub account: Option<String>,
41
42    /// Ask for one service's scopes in addition to the default set (e.g.
43    /// docs, chat, searchconsole). Every login merges with what the account
44    /// already granted, so widening in steps keeps each consent page small
45    /// enough for Google to finish.
46    #[arg(long, conflicts_with = "all_scopes")]
47    pub service: Option<String>,
48
49    /// Ask for every scope the embedded index can express. One big consent
50    /// page: on an unverified project Google's screen stops submitting past
51    /// roughly 27 ticked boxes, so prefer `--service` unless you know the
52    /// project is verified.
53    #[arg(long)]
54    pub all_scopes: bool,
55
56    /// Output format
57    #[arg(short, long, value_enum, default_value = "json")]
58    pub format: OutputFormat,
59}
60
61#[derive(clap::Args, Debug)]
62pub struct StatusArgs {
63    /// Show the status of a NAMED account instead of the default one
64    #[arg(long)]
65    pub account: Option<String>,
66
67    /// Output format
68    #[arg(short, long, value_enum, default_value = "json")]
69    pub format: OutputFormat,
70}
71
72fn token_preview(token: &str) -> String {
73    format!("{}...", &token[..std::cmp::min(20, token.len())])
74}
75
76/// The scope set `auth login` should ask for, from the flags.
77///
78/// Default: [`crate::core::auth::login_scopes`], the small set that keeps the
79/// consent page submittable. `--service X` adds that service's scopes (the
80/// index decides them — the same source `grr api describe` reads), while
81/// `--all-scopes` asks for the whole index. Every login sends
82/// `include_granted_scopes`, so each of these merges with the account's
83/// existing grants rather than replacing them.
84fn requested_login_scopes(login: &LoginArgs) -> Result<Vec<String>> {
85    if login.all_scopes {
86        return Ok(crate::core::auth::scopes().to_vec());
87    }
88    let mut set = crate::core::auth::login_scopes();
89    if let Some(service) = &login.service {
90        let scopes = crate::core::auth::scopes_for_service(service).ok_or_else(|| {
91            crate::core::error::GrrError::Config(format!(
92                "unknown service `{service}`: `grr api list` shows the ids the index knows"
93            ))
94        })?;
95        set.extend(scopes);
96        set.sort_unstable();
97        set.dedup();
98    }
99    Ok(set)
100}
101
102pub async fn handle_auth_cmd(auth: &GoogleAuth, cmd: AuthCommands) -> Result<()> {
103    match cmd {
104        // Handled before client construction in cli.rs; this arm is only a
105        // guard so the match stays exhaustive.
106        AuthCommands::Setup(_) => unreachable!("auth setup handled before client construction"),
107        AuthCommands::Login(login) => {
108            // A named account rebinds to that account's own token store;
109            // the default account keeps today's exact entry naming, so an
110            // existing login is untouched. A fresh handle is built either
111            // way (a cheap store + HTTP-client construction).
112            let auth = match &login.account {
113                Some(name) => auth.with_account_store(name).await?,
114                None => auth.with_default_account_store().await?,
115            };
116            if login.device {
117                return handle_device_login(&auth, login.format).await;
118            }
119            let scopes = requested_login_scopes(&login)?;
120            let storage = auth.login(&scopes).await?;
121            print_output(
122                &json!({
123                    "authenticated": true,
124                    "account": login.account,
125                    "token_backend": auth.token_backend(),
126                    "scopes_requested": scopes.len(),
127                    "token_preview": token_preview(&storage.access_token)
128                }),
129                login.format,
130            )?;
131            Ok(())
132        }
133        AuthCommands::Status(status) => {
134            let auth = match &status.account {
135                Some(name) => auth.with_account_store(name).await?,
136                None => auth.with_default_account_store().await?,
137            };
138            let profile = fetch_profile(&auth).await?;
139            print_output(
140                &json!({
141                    "authenticated": true,
142                    "account": status.account,
143                    "email": field(&profile, "emailAddress"),
144                    "messages_total": field(&profile, "messagesTotal"),
145                    "threads_total": field(&profile, "threadsTotal"),
146                    "history_id": field(&profile, "historyId"),
147                }),
148                status.format,
149            )?;
150            Ok(())
151        }
152    }
153}
154
155/// The profile check behind `auth status`, via the shared Discovery path.
156///
157/// The payload keys are remapped to the long-standing `auth status`
158/// contract (snake_case, `authenticated` flag), so scripts reading the
159/// output did not change when the typed Gmail client left the CLI.
160async fn fetch_profile(auth: &GoogleAuth) -> Result<Value> {
161    let (service, method) =
162        discovery::resolve("gmail.users.getProfile").map_err(anyhow::Error::msg)?;
163    let mut params = serde_json::Map::new();
164    params.insert("userId".into(), json!("me"));
165    api::call_method(
166        Some(auth),
167        service,
168        method,
169        params,
170        api::CallOptions::default(),
171    )
172    .await
173}
174
175fn field(profile: &Value, key: &str) -> Value {
176    profile.get(key).cloned().unwrap_or(Value::Null)
177}
178
179async fn handle_device_login(auth: &GoogleAuth, format: OutputFormat) -> Result<()> {
180    let mut challenge = auth.request_device_code().await?;
181    // stderr, not stdout: `--format json` writes its payload to stdout, and a
182    // prose line ahead of it breaks `... -f json | jq`.
183    eprintln!(
184        "Visit {} and enter code: {}",
185        challenge.verification_url, challenge.user_code
186    );
187    loop {
188        tokio::time::sleep(challenge.retry_after()).await;
189        if challenge.is_expired() {
190            anyhow::bail!("device code expired; rerun `grr auth login --device`");
191        }
192        if let Some(storage) = auth.poll_device_code(&mut challenge).await? {
193            print_output(
194                &json!({
195                    "authenticated": true,
196                    "token_backend": auth.token_backend(),
197                    "token_preview": token_preview(&storage.access_token)
198                }),
199                format,
200            )?;
201            return Ok(());
202        }
203    }
204}