Skip to content

fixing server switch handling and oidc metadata fetch#1991

Merged
jezekra1 merged 1 commit intomainfrom
fix/fix-cli-oauth
Feb 2, 2026
Merged

fixing server switch handling and oidc metadata fetch#1991
jezekra1 merged 1 commit intomainfrom
fix/fix-cli-oauth

Conversation

@jjeliga
Copy link
Contributor

@jjeliga jjeliga commented Feb 2, 2026

Summary

Fixing some CLI login logic

Linked Issues

None

Documentation

  • No Docs Needed:

If this PR adds new feature or changes existing. Make sure documentation is adjusted accordingly. If the docs is not needed, please explain why.

Signed-off-by: Jan Jeliga <jeliga.jan@gmail.com>
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 2, 2026
@jjeliga jjeliga requested a review from jezekra1 February 2, 2026 15:20
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jjeliga, 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 focuses on enhancing the robustness and reliability of the CLI's authentication process. It includes improvements to how OIDC metadata is retrieved and refines the state management when switching between different authentication servers during the login flow. These changes aim to provide a more stable and predictable user experience when interacting with various server configurations.

Highlights

  • OIDC Metadata Fetching: The mechanism for fetching OpenID Connect (OIDC) metadata has been updated to use httpx.AsyncClient instead of authlib.integrations.httpx_client.AsyncOAuth2Client. This change likely improves the reliability or consistency of HTTP requests for OIDC discovery.
  • Server Switch Handling in CLI Login: The CLI login logic has been refined to correctly manage the active server and authentication server during token validation. The system now proactively sets the active server before attempting to load an authentication token and ensures that the previous server state is restored if token validation fails due to expiration or other OAuth2 errors. This prevents unintended state changes during failed login attempts.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, 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 fixes server switching logic and how OIDC metadata is fetched. The change to use httpx.AsyncClient for fetching OIDC metadata is a good improvement. However, the refactoring of the server switching logic in server.py introduces a potential issue by eagerly saving configuration changes before validation, which could lead to an inconsistent state if the process is interrupted. I've added a detailed comment with a suggestion to fix this while maintaining the goal of improving the code structure.

Comment on lines 140 to 178
# Validate that the token is still valid by attempting to load it

# Temporarily set the server as active to test the token, this does not save the change yet
# Keep the original active server/auth server in case of failure
previous_server = config.auth_manager.active_server
previous_auth_server = config.auth_manager.active_auth_server
config.auth_manager._auth.active_server = server
config.auth_manager._auth.active_auth_server = auth_server

config.auth_manager.active_server = server
config.auth_manager.active_auth_server = auth_server

try:
token = await config.auth_manager.load_auth_token()
if not token:
# No token available, need to log in
# Restore previous state until login completes
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
config.auth_manager.active_server = previous_server
config.auth_manager.active_auth_server = previous_auth_server
# Fall through to login flow below
else:
# Token is valid, switch to this server (setters handle saving)
config.auth_manager.active_server = server
config.auth_manager.active_auth_server = auth_server
console.success(f"Logged in to [cyan]{server}[/cyan].")
return
except InvalidGrantError:
# Token refresh failed due to invalid/expired refresh token
log_in_message = "Your session has expired. Please log in again."
# Restore previous state until login completes
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
config.auth_manager.active_server = previous_server
config.auth_manager.active_auth_server = previous_auth_server
# Fall through to login flow below
except OAuth2Error as e:
# Other OAuth2 protocol errors - report but don't continue
console.error(f"OAuth2 error: {e.description}")
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
config.auth_manager.active_server = previous_server
config.auth_manager.active_auth_server = previous_auth_server
sys.exit(1)
except RuntimeError as e:
# Network or OIDC discovery errors - report but don't continue
console.error(f"Failed to validate authentication: {e}")
console.hint("Check your network connection and try again.")
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
config.auth_manager.active_server = previous_server
config.auth_manager.active_auth_server = previous_auth_server
sys.exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While replacing direct access to the private _auth attribute with the public property setters is a good refactoring goal, the current implementation introduces a regression in behavior. The property setters immediately save the configuration to disk.

This changes the logic to an "optimistic" update:

  1. The new server is set as active and saved to disk before validation.
  2. If validation fails, an attempt is made to revert the change and save again.

This creates a race condition. If the process is terminated during token validation (e.g., by Ctrl+C or a crash), the configuration file will be left in an inconsistent state, with the new server marked as active even though the switch was not successful.

The previous logic was safer as it only modified the configuration in memory and saved it to disk once, upon successful validation. To restore this safer transactional behavior, the temporary switch should not be persisted.

A long-term solution would be to refactor AuthManager to better support this pattern, but for now, it's best to revert to the previous logic for this block to avoid potential configuration corruption.

        # Validate that the token is still valid by attempting to load it

        # Temporarily set the server as active to test the token, this does not save the change yet
        previous_server = config.auth_manager.active_server
        previous_auth_server = config.auth_manager.active_auth_server
        config.auth_manager._auth.active_server = server
        config.auth_manager._auth.active_auth_server = auth_server

        try:
            token = await config.auth_manager.load_auth_token()
            if not token:
                # No token available, need to log in
                # Restore previous state until login completes
                config.auth_manager._auth.active_server = previous_server
                config.auth_manager._auth.active_auth_server = previous_auth_server
                # Fall through to login flow below
            else:
                # Token is valid, switch to this server (setters handle saving)
                config.auth_manager.active_server = server
                config.auth_manager.active_auth_server = auth_server
                console.success(f"Logged in to [cyan]{server}[/cyan].")
                return
        except InvalidGrantError:
            # Token refresh failed due to invalid/expired refresh token
            log_in_message = "Your session has expired. Please log in again."
            # Restore previous state until login completes
            config.auth_manager._auth.active_server = previous_server
            config.auth_manager._auth.active_auth_server = previous_auth_server
            # Fall through to login flow below
        except OAuth2Error as e:
            # Other OAuth2 protocol errors - report but don't continue
            console.error(f"OAuth2 error: {e.description}")
            config.auth_manager._auth.active_server = previous_server
            config.auth_manager._auth.active_auth_server = previous_auth_server
            sys.exit(1)
        except RuntimeError as e:
            # Network or OIDC discovery errors - report but don't continue
            console.error(f"Failed to validate authentication: {e}")
            console.hint("Check your network connection and try again.")
            config.auth_manager._auth.active_server = previous_server
            config.auth_manager._auth.active_auth_server = previous_auth_server
            sys.exit(1)

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 2, 2026
@jezekra1 jezekra1 merged commit 3ee9a02 into main Feb 2, 2026
10 of 11 checks passed
@jezekra1 jezekra1 deleted the fix/fix-cli-oauth branch February 2, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants