Skip to content

fix #6202#6209

Open
lcxn123 wants to merge 2 commits into
HMCL-dev:mainfrom
lcxn123:fix-authlib-refresh
Open

fix #6202#6209
lcxn123 wants to merge 2 commits into
HMCL-dev:mainfrom
lcxn123:fix-authlib-refresh

Conversation

@lcxn123

@lcxn123 lcxn123 commented Jun 23, 2026

Copy link
Copy Markdown

参考 #2048

@Glavo

Glavo commented Jun 23, 2026

Copy link
Copy Markdown
Member

/gemini review

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

Copy link
Copy Markdown
Contributor

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 simplifies the login logic in YggdrasilAccount by combining authentication and validation checks into a single conditional statement. However, this change introduces critical issues: short-circuit evaluation bypasses token validation on startup (leading to redundant token refreshes), introduces latency via unnecessary network requests during subsequent logins, and breaks offline play by failing to handle network exceptions during validation. A suggested refactoring is provided to restore proper validation and offline support.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +106 to 129
if (!authenticated || !session.hasProfileName() || !service.validate(session.getAccessToken(), session.getClientToken())) {
YggdrasilSession acquiredSession;
try {
acquiredSession = service.refresh(session.getAccessToken(), session.getClientToken(), null);
} catch (RemoteAuthenticationException e) {
if ("ForbiddenOperationException".equals(e.getRemoteName())) {
throw new CredentialExpiredException(e);
} else {
throw e;
}
}
if (acquiredSession.getSelectedProfile() == null ||
!acquiredSession.getSelectedProfile().getId().equals(profileID)) {
throw new ServerResponseMalformedException("Selected profile changed");
}
if (!acquiredSession.hasProfileName()) {
throw new ServerResponseMalformedException("Profile name is missing");
}

session = acquiredSession;
session = acquiredSession;

authenticated = true;
invalidate();
}
authenticated = true;
invalidate();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

This change introduces several critical issues:

  1. Bypassing validation on startup: Because of short-circuit evaluation (!authenticated || ...), when authenticated is false (the default state when the launcher starts), the condition evaluates to true immediately without calling service.validate(...). This forces a redundant service.refresh(...) on every startup, rotating the token and writing to disk unnecessarily even if the existing token is perfectly valid.
  2. Redundant network requests: When authenticated is true, every call to logIn() (e.g., when launching a game) will perform a blocking network request to validate the token, introducing significant latency.
  3. Breaking offline launching: If the user is offline or the auth server is down, service.validate(...) will throw a ServerDisconnectException. Since this exception is not caught, it propagates out of logIn(), completely breaking offline play for authenticated accounts.

We should restore the startup validation logic, skip validation once authenticated, and gracefully handle network errors during validation to support offline play.

        if (!authenticated || !session.hasProfileName()) {
            if (session.hasProfileName()) {
                try {
                    if (service.validate(session.getAccessToken(), session.getClientToken())) {
                        authenticated = true;
                    }
                } catch (AuthenticationException e) {
                    if (e instanceof ServerDisconnectException) {
                        authenticated = true;
                    }
                }
            }

            if (!authenticated) {
                YggdrasilSession acquiredSession;
                try {
                    acquiredSession = service.refresh(session.getAccessToken(), session.getClientToken(), null);
                } catch (RemoteAuthenticationException e) {
                    if ("ForbiddenOperationException".equals(e.getRemoteName())) {
                        throw new CredentialExpiredException(e);
                    } else {
                        throw e;
                    }
                }
                if (acquiredSession.getSelectedProfile() == null ||
                        !acquiredSession.getSelectedProfile().getId().equals(profileID)) {
                    throw new ServerResponseMalformedException("Selected profile changed");
                }
                if (!acquiredSession.hasProfileName()) {
                    throw new ServerResponseMalformedException("Profile name is missing");
                }

                session = acquiredSession;

                authenticated = true;
                invalidate();
            }
        }

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants