fix #6202#6209
Conversation
|
/gemini review |
There was a problem hiding this comment.
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.
| 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(); | ||
| } |
There was a problem hiding this comment.
This change introduces several critical issues:
- Bypassing validation on startup: Because of short-circuit evaluation (
!authenticated || ...), whenauthenticatedisfalse(the default state when the launcher starts), the condition evaluates totrueimmediately without callingservice.validate(...). This forces a redundantservice.refresh(...)on every startup, rotating the token and writing to disk unnecessarily even if the existing token is perfectly valid. - Redundant network requests: When
authenticatedistrue, every call tologIn()(e.g., when launching a game) will perform a blocking network request to validate the token, introducing significant latency. - Breaking offline launching: If the user is offline or the auth server is down,
service.validate(...)will throw aServerDisconnectException. Since this exception is not caught, it propagates out oflogIn(), 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();
}
}
参考 #2048