Skip to content

feat(auth): support Domain-Wide Delegation for Service Accounts#543

Open
dumko2001 wants to merge 7 commits intogoogleworkspace:mainfrom
dumko2001:feat/issue-528-impersonation-v2
Open

feat(auth): support Domain-Wide Delegation for Service Accounts#543
dumko2001 wants to merge 7 commits intogoogleworkspace:mainfrom
dumko2001:feat/issue-528-impersonation-v2

Conversation

@dumko2001
Copy link
Contributor

@dumko2001 dumko2001 commented Mar 18, 2026

Description

Enable user impersonation (Domain-Wide Delegation) for Service Account authentication via the GOOGLE_WORKSPACE_IMPERSONATE_USER environment variable. This allows administrators to perform actions on behalf of other users in the Workspace domain, which is a standard requirement for enterprise automation.

Fixes #528

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 feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

@dumko2001 dumko2001 requested a review from jpoehnelt as a code owner March 18, 2026 09:17
@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: a32bb8d

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 introduces a significant enhancement for enterprise environments by enabling service accounts to impersonate specific users within a Google Workspace domain. This capability is essential for automating tasks that require user-specific permissions, streamlining operations without the need for individual user credentials.

Highlights

  • Domain-Wide Delegation Support: Implemented support for Domain-Wide Delegation (user impersonation) for Service Account authentication. This feature is activated by setting the GOOGLE_WORKSPACE_IMPERSONATE_USER environment variable, enabling service accounts to act on behalf of other users within a Google Workspace domain.
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 adds support for Domain-Wide Delegation for Service Accounts by reading the GOOGLE_WORKSPACE_IMPERSONATE_USER environment variable. The implementation is mostly correct, but I've identified a security concern regarding the logging of the impersonated user's email without sanitization. This could lead to terminal escape sequence injection. Additionally, the pull request lacks tests for this new functionality, which is mentioned as a requirement in the PR's checklist.

@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 adds support for Domain-Wide Delegation, which is a great feature for enterprise automation. However, I've found two significant issues with the current implementation. First, there is a critical bug where tokens for the service account and impersonated users will collide in the cache, leading to incorrect authentication states. Second, there is a high-severity security issue where user-provided data from an environment variable is logged without sanitization, creating a risk of terminal escape sequence injection. Both issues need to be addressed before merging.

@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 adds support for Domain-Wide Delegation for Service Accounts by allowing user impersonation via the GOOGLE_WORKSPACE_IMPERSONATE_USER environment variable. The implementation correctly uses the yup-oauth2 builder pattern to add the subject for impersonation. My review includes one high-severity security recommendation to prevent terminal escape sequence injection by sanitizing the user-provided value in a debug log message.

- Sanitize impersonated user email in debug log to prevent terminal escape injection
- Fix critical token cache collision by using unique cache file per impersonated user (SHA-256 hash)
- Add tests proving the cache collision bug and validating the fix

Addresses gemini-code-assist security and critical feedback on PR googleworkspace#543
@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

The pull request successfully introduces support for Domain-Wide Delegation for Service Accounts, enabling user impersonation. This feature is implemented by generating unique token cache paths for impersonated users, effectively preventing token collisions. The changes include a new sa_token_cache_path function and its integration into the authentication flow, along with comprehensive test cases that validate the new logic and demonstrate the resolution of a previous token collision bug.

Use all 32 bytes of SHA-256 hash for maximum collision resistance
as suggested in code review feedback.
@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 adds support for Domain-Wide Delegation for Service Accounts by allowing user impersonation via the GOOGLE_WORKSPACE_IMPERSONATE_USER environment variable. The implementation correctly creates unique token cache files for impersonated users to prevent collisions, using a hash of the user's email. The changes are well-tested.

However, I've identified one high-severity issue: the existing logic for cleaning up stale, encrypted token caches is not updated to handle the new cache file naming scheme. This will lead to leftover files and persistent warnings for users in certain scenarios. I've left a comment with details on how to address this.

When encrypted credentials become undecryptable (e.g., key migration),
clean up all impersonation token cache files (sa_imp_*.json) in addition
to standard token caches to prevent stale file warnings.
@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

The pull request introduces support for Domain-Wide Delegation for Service Accounts, which is a valuable feature for enterprise automation. The changes include a new function to compute unique token cache paths for impersonated users, modifications to the token retrieval logic to incorporate this, and a cleanup mechanism for stale impersonation cache files. New tests have been added to cover the token cache path logic, demonstrating the fix for token collision issues. Overall, the changes are well-structured and address the stated objective.

Sanitize path and error strings in eprintln! calls during token
cache cleanup to prevent terminal escape sequence injection.
@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

The pull request successfully implements Domain-Wide Delegation (impersonation) for Service Accounts, addressing the critical issue of token cache collisions by introducing unique, hash-based cache paths for impersonated users. The changes are well-structured, integrating the new cache path logic into the authentication flow and including appropriate cleanup for stale cache files. Comprehensive unit tests have been added to validate the new functionality and demonstrate the fix for the token collision bug. No high or critical issues were identified during the review.

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.

Issue Utilizing With a Delegated Gmail Account

2 participants