Skip to content

Commit

Permalink
removed refresh tokens from ely.by since access tokens last forever
Browse files Browse the repository at this point in the history
  • Loading branch information
Josakko committed Apr 23, 2024
1 parent 1e2c970 commit 1f4a612
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 84 deletions.
64 changes: 1 addition & 63 deletions src/main/java/com/atlauncher/data/ElybyAccount.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.atlauncher.data;

import java.util.Date;
import java.util.Optional;

import org.mini2Dx.gettext.GetText;

import com.atlauncher.data.elyby.Profile;
import com.atlauncher.gui.dialogs.LoginWithElyByDialog;
import com.atlauncher.managers.AccountManager;
import com.atlauncher.managers.DialogManager;
import com.atlauncher.managers.LogManager;
import com.atlauncher.utils.ElyByAuthAPI;
Expand All @@ -25,11 +23,6 @@ public class ElybyAccount extends AbstractAccount {
*/
public OauthTokenResponse oauthToken;

/**
* The date that the accessToken expires at.
*/
public Date accessTokenExpiresAt;

/**
* If the user must login again. This is usually the result of a failed
* accessToken refresh.
Expand All @@ -47,9 +40,6 @@ public void update(OauthTokenResponse oauthTokenResponse, Profile profile) {
this.uuid = profile.uuid;
this.username = profile.username;
this.mustLogin = false;

this.accessTokenExpiresAt = new Date();
this.accessTokenExpiresAt.setTime(this.accessTokenExpiresAt.getTime() + (oauthTokenResponse.expiresIn * 1000));
}

@Override
Expand All @@ -62,40 +52,6 @@ public String getSessionToken() {
return accessToken;
}

public boolean refreshAccessToken() {
return refreshAccessToken(false);
}

public boolean refreshAccessToken(Boolean force) {
try {
if (force || new Date().after(this.accessTokenExpiresAt)) {
LogManager.info("Access token expired. Attempting to refresh");
OauthTokenResponse oauthTokenResponse = ElyByAuthAPI.refreshAccessToken(oauthToken.refreshToken);

if (oauthTokenResponse == null) {
mustLogin = true;
AccountManager.saveAccounts();
LogManager.error("Failed to refresh accessToken");
return false;
}

this.oauthToken = oauthTokenResponse;
this.accessTokenExpiresAt = new Date();
this.accessTokenExpiresAt.setTime(this.accessTokenExpiresAt.getTime() + (oauthTokenResponse.expiresIn * 1000));

AccountManager.saveAccounts();
}
} catch (Exception e) {
mustLogin = true;
AccountManager.saveAccounts();

LogManager.logStackTrace("Exception refreshing accessToken", e);
return false;
}

return true;
}

@Override
public String getUserType() {
return "mojang";
Expand Down Expand Up @@ -127,12 +83,10 @@ public String getCurrentUsername() {

@Override
public void updateSkinPreCheck() {
this.refreshAccessToken();
}

@Override
public void changeSkinPreCheck() {
this.refreshAccessToken();
}

public boolean updateProfile(String accessToken) {
Expand Down Expand Up @@ -180,23 +134,7 @@ public boolean ensureAccountIsLoggedIn() {
}

public boolean ensureAccessTokenValid() {
if (!ensureAccountIsLoggedIn()) {
return false;
}

if (!new Date().after(accessTokenExpiresAt)) {
return true;
}

LogManager.info("Access Token has expired. Attempting to refresh it.");

try {
return refreshAccessToken();
} catch (Exception e) {
LogManager.logStackTrace("Exception while attempting to refresh access token", e);
}

return false;
return ensureAccountIsLoggedIn();
}

@Override
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/com/atlauncher/utils/ElyByAuthAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,6 @@ public static OauthTokenResponse getAccessToken(String authCode) {
return oauthTokenResponse;
}

public static OauthTokenResponse refreshAccessToken(String refreshToken) {
// ely docs is shit, literally their docs say different from what their server does, luckily its open source
// https://github.com/elyby/accounts/blob/master/api/modules/oauth/models/OauthProcess.php#L136#L201
// ops, it looks like the access tokens now last forever so this is not needed
RequestBody data = new FormBody.Builder()
.add("client_id", Constants.ELYBY_LOGIN_CLIENT_ID)
.add("client_secret", Constants.ELYBY_LOGIN_SECRET_KEY)
//.add("scope", String.join(" ", Constants.ELYBY_LOGIN_SCOPES))
.add("refresh_token", refreshToken)
.add("grant_type", "refresh_token").build();

OauthTokenResponse oauthTokenResponse = Download.build()
.setUrl(Constants.ELYBY_REFRESH_TOKEN_URL)
.header("Content-Type", "application/x-www-form-urlencoded")
.post(data)
.asClass(OauthTokenResponse.class, Gsons.DEFAULT);

oauthTokenResponse.refreshToken = refreshToken;
return oauthTokenResponse;
}

public static Profile getProfile(String accessToken) throws IOException {
Profile profile = Download.build().setUrl(Constants.ELYBY_TOKEN_INFO_URL)
.header("Authorization", "Bearer " + accessToken).asClassWithThrow(Profile.class);
Expand Down

0 comments on commit 1f4a612

Please sign in to comment.