Skip to content

fix(auth): report token info in status when using GOOGLE_WORKSPACE_CLI_TOKEN#546

Open
dumko2001 wants to merge 5 commits intogoogleworkspace:mainfrom
dumko2001:fix/auth-status-token-v2
Open

fix(auth): report token info in status when using GOOGLE_WORKSPACE_CLI_TOKEN#546
dumko2001 wants to merge 5 commits intogoogleworkspace:mainfrom
dumko2001:fix/auth-status-token-v2

Conversation

@dumko2001
Copy link
Contributor

@dumko2001 dumko2001 commented Mar 18, 2026

Description

Ensure that gws auth status correctly reports the validity, user, and scopes of an access token provided via the GOOGLE_WORKSPACE_CLI_TOKEN environment variable. Previously, the status command only checked stored credentials, which could be confusing when using direct token injection.

Checklist:

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: 07beab6

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a previous limitation in the gws auth status command where it failed to provide comprehensive information for access tokens supplied directly through an environment variable. The update ensures that the command now accurately displays the token's validity, associated user, and granted scopes, thereby improving clarity and consistency for users who manage authentication via GOOGLE_WORKSPACE_CLI_TOKEN.

Highlights

  • Enhanced gws auth status command: The gws auth status command now correctly reports the validity, user, and scopes of an access token when it is provided via the GOOGLE_WORKSPACE_CLI_TOKEN environment variable.
  • Direct Token Handling: Introduced logic to prioritize and process access tokens directly from the GOOGLE_WORKSPACE_CLI_TOKEN environment variable before falling back to stored credentials.
  • Unified Token Information Retrieval: Refactored the token information retrieval process to use a single path for fetching user details and scopes, whether the token comes from stored credentials or the environment variable.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds support for GOOGLE_WORKSPACE_CLI_TOKEN in the gws auth status command. However, the refactoring has introduced a regression where error reporting for failed token refreshes (e.g., due to an expired refresh token) is lost. My review includes a suggestion to restore this important feedback for the user.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the authentication status command by allowing it to use an access token provided directly via the GOOGLE_WORKSPACE_CLI_TOKEN environment variable. The handle_status function in src/auth_commands.rs was modified to prioritize this environment variable for obtaining an access token. If the environment variable is not set, the existing logic for loading credentials and exchanging a refresh token is used. Subsequently, the retrieved access token (from either source) is used to fetch user information and granted scopes from Google APIs, improving clarity in the reported authentication status.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the ability for the gws auth status command to report token information when an access token is provided via the GOOGLE_WORKSPACE_CLI_TOKEN environment variable, in addition to existing credential store methods. However, a regression was introduced where the error reporting for failed refresh token exchanges was removed. Previously, the system would capture and display error_description and set token_valid: false, but now, if the exchange fails, the access_token is simply None, leading to silent failures and potentially confusing user output.

@dumko2001
Copy link
Contributor Author

Restored the error reporting for failed token refresh in gws auth status. If the refresh token exchange fails, the command now correctly reports token_valid: false and populates token_error with the description from Google's response, resolving the regression identified in review.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds support for GOOGLE_WORKSPACE_CLI_TOKEN to the auth status command and refactors the logic to handle both direct tokens and tokens from credentials. My review includes two high-severity suggestions: one to improve performance by reusing the reqwest::Client instance, and another to improve correctness by properly URL-encoding the access token in the tokeninfo request.

Comment on lines +1167 to +1168
let tokeninfo_url = format!("https://oauth2.googleapis.com/tokeninfo?access_token={}", at);
if let Ok(info_resp) = http_client.get(&tokeninfo_url).send().await {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The access token is being directly embedded into the URL string. While Google access tokens are typically URL-safe, it's best practice to URL-encode parameters to prevent potential issues if a token ever contains special characters. Using reqwest's .query() method handles this encoding for you automatically and makes the code more robust.

            if let Ok(info_resp) = http_client
                .get("https://oauth2.googleapis.com/tokeninfo")
                .query(&[("access_token", &at)])
                .send()
                .await
            {

@dumko2001
Copy link
Contributor Author

Refactored handle_status to reuse a single reqwest::Client instance for all requests within the block and updated the tokeninfo call to use .query() for proper URL-encoding of the access token, as suggested in review.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds support for GOOGLE_WORKSPACE_CLI_TOKEN in the gws auth status command, allowing it to report token information when this environment variable is used. The implementation is functional, but I've identified a significant maintainability issue in src/auth_commands.rs where the logic for retrieving the access token has become overly complex and includes side effects within a let binding. I've left a comment with a recommendation to refactor this for better clarity and separation of concerns.

@dumko2001 dumko2001 force-pushed the fix/auth-status-token-v2 branch from 960d2b9 to 07beab6 Compare March 18, 2026 11:05
@dumko2001
Copy link
Contributor Author

Refactored the access token retrieval logic into a separate get_status_access_token helper function. This addresses the maintainability concerns raised in review by simplifying the main handle_status logic and ensuring side effects are handled cleanly.

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds support for GOOGLE_WORKSPACE_CLI_TOKEN to the auth status command by refactoring the token acquisition logic. My review identifies two high-severity issues: a performance regression due to using synchronous file I/O in an async context, and a bug where a JSON parsing error is not handled, leading to silent failure. Code suggestions are provided for both issues.

Comment on lines +941 to +947
let creds_json_str = if enc_path.exists() {
credential_store::load_encrypted().ok()
} else if plain_path.exists() {
std::fs::read_to_string(&plain_path).ok()
} else {
None
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This implementation uses a synchronous file read (std::fs::read_to_string) within an async function. This blocks the execution thread and is a performance regression from the previous implementation which used tokio::fs::read_to_string. To avoid blocking, you should use the asynchronous version and restructure the logic slightly. Note that credential_store::load_encrypted() is also synchronous, but this suggestion focuses on the immediate regression introduced in this change.

Suggested change
let creds_json_str = if enc_path.exists() {
credential_store::load_encrypted().ok()
} else if plain_path.exists() {
std::fs::read_to_string(&plain_path).ok()
} else {
None
};
let mut creds_json_str = if enc_path.exists() {
credential_store::load_encrypted().ok()
} else {
None
};
if creds_json_str.is_none() && plain_path.exists() {
creds_json_str = tokio::fs::read_to_string(&plain_path).await.ok();
}

Comment on lines +969 to +981
if let Ok(token_json) = resp.json::<serde_json::Value>().await {
if let Some(access_token) = token_json.get("access_token").and_then(|v| v.as_str()) {
Some(access_token.to_string())
} else {
output["token_valid"] = serde_json::json!(false);
if let Some(err) = token_json.get("error_description").and_then(|v| v.as_str()) {
output["token_error"] = serde_json::json!(err);
}
None
}
} else {
None
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a missing error handling case here. If resp.json::<serde_json::Value>().await fails (e.g., due to a malformed response from the server or a network issue), the else branch on line 979 is taken, which returns None without updating the output JSON value. This means the user will not see any error message and might be confused about why their authentication status is not being reported correctly. You should handle this error case and populate token_valid and token_error in the output.

Suggested change
if let Ok(token_json) = resp.json::<serde_json::Value>().await {
if let Some(access_token) = token_json.get("access_token").and_then(|v| v.as_str()) {
Some(access_token.to_string())
} else {
output["token_valid"] = serde_json::json!(false);
if let Some(err) = token_json.get("error_description").and_then(|v| v.as_str()) {
output["token_error"] = serde_json::json!(err);
}
None
}
} else {
None
}
match resp.json::<serde_json::Value>().await {
Ok(token_json) => {
if let Some(access_token) = token_json.get("access_token").and_then(|v| v.as_str()) {
Some(access_token.to_string())
} else {
output["token_valid"] = serde_json::json!(false);
if let Some(err) = token_json.get("error_description").and_then(|v| v.as_str()) {
output["token_error"] = serde_json::json!(err);
}
None
}
}
Err(e) => {
output["token_valid"] = serde_json::json!(false);
output["token_error"] = serde_json::json!(format!("Failed to parse token response: {e}"));
None
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants