Skip to content

Commit

Permalink
added login input checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Josakko committed Apr 17, 2024
1 parent ed19363 commit 513b569
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/atlauncher/gui/tabs/accounts/AccountsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.atlauncher.utils.ComboItem;
import com.atlauncher.utils.OS;
import com.atlauncher.utils.SkinUtils;
import com.atlauncher.utils.Utils;
import com.atlauncher.viewmodel.base.IAccountsViewModel;
import com.atlauncher.viewmodel.base.IAccountsViewModel.LoginPostResult;
import com.atlauncher.viewmodel.base.IAccountsViewModel.LoginPreCheckResult;
Expand Down Expand Up @@ -474,6 +475,16 @@ private void clearLogin() {
*/
@SuppressWarnings("unchecked")
private void login() {
if (!Utils.isEntryValid(usernameField.getText())) {
DialogManager
.okDialog()
.setTitle(GetText.tr("Account Not Added"))
.setContent(GetText.tr("Invalid username."))
.setType(DialogManager.ERROR)
.show();
return;
}

// Pre check
LoginPreCheckResult preCheckResult = viewModel.loginPreCheck();
if (preCheckResult instanceof LoginPreCheckResult.Exists) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/atlauncher/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1676,4 +1676,8 @@ public static boolean isDevelopment() {

return false;
}

public static boolean isEntryValid(String entry) {
return !(entry == null || entry.isBlank());
}
}
36 changes: 28 additions & 8 deletions src/main/java/com/atlauncher/viewmodel/impl/AccountsViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mini2Dx.gettext.GetText;

import com.atlauncher.data.AbstractAccount;
import com.atlauncher.data.LoginResponse;
Expand All @@ -32,8 +33,10 @@
import com.atlauncher.data.OfflineAccount;
import com.atlauncher.gui.dialogs.ChangeSkinDialog;
import com.atlauncher.managers.AccountManager;
import com.atlauncher.managers.DialogManager;
import com.atlauncher.managers.LogManager;
import com.atlauncher.utils.Authentication;
import com.atlauncher.utils.Utils;
import com.atlauncher.viewmodel.base.IAccountsViewModel;

/**
Expand Down Expand Up @@ -176,16 +179,27 @@ private void addNewMojangAccount(LoginResponse response) {
pushNewAccounts();
}

private void editAccount(LoginResponse response) {
private LoginPostResult editAccount(LoginResponse response) {
AbstractAccount account = getSelectedAccount();

if (account instanceof MojangAccount) {
if (account instanceof MojangAccount && response.getRealAccountType() == "mojang") {
MojangAccount mojangAccount = (MojangAccount) account;

mojangAccount.username = loginUsername;
mojangAccount.minecraftUsername = response.getAuth().getSelectedProfile().getName();
mojangAccount.uuid = response.getAuth().getSelectedProfile().getId().toString();
if (loginRemember) {
// probably not needed at all but just in case
if (!Utils.isEntryValid(loginPassword)) {
DialogManager
.okDialog()
.setTitle(GetText.tr("Account Not Added"))
.setContent(GetText.tr("Invalid password."))
.setType(DialogManager.ERROR)
.show();
return new LoginPostResult.Error("Invalid password.");
}

mojangAccount.setPassword(loginPassword);
} else {
mojangAccount.encryptedPassword = null;
Expand All @@ -196,18 +210,22 @@ private void editAccount(LoginResponse response) {
mojangAccount.store = response.getAuth().saveForStorage();

AccountManager.saveAccounts();
} else if (account instanceof OfflineAccount) {
} else if (account instanceof OfflineAccount && response.getRealAccountType() == "offline") {
OfflineAccount offlineAccount = (OfflineAccount) account;

offlineAccount.username = loginUsername;
offlineAccount.minecraftUsername = loginUsername;
offlineAccount.store = response.getAuth().saveForStorage();

AccountManager.saveAccounts();
} else {
LogManager.warn("Did not edit any account");
return new LoginPostResult.Error("Invalid credentials.");
}

LogManager.info("Edited Account " + account);
pushNewAccounts();
return new LoginPostResult.Edited();
}

private LoginResponse loginResponse = null;
Expand All @@ -223,19 +241,19 @@ public LoginPostResult loginPost() {
return new LoginPostResult.Added();
}

editAccount(loginResponse);
LoginPostResult res = editAccount(loginResponse);
invalidateClientToken();
return new LoginPostResult.Edited();
return res;
} else if (loginResponse != null && loginResponse.getRealAccountType() == "offline") {
if (selectedAccountIndex == -1) {
addNewOfflineAccount();
invalidateClientToken();
return new LoginPostResult.Added();
}

editAccount(loginResponse);
LoginPostResult res = editAccount(loginResponse);
invalidateClientToken();
return new LoginPostResult.Edited();
return res;

}

Expand All @@ -249,9 +267,11 @@ public int getSelectedIndex() {

@Override
public void login() {
if (loginPassword == null || loginPassword.isEmpty()) {
if (!Utils.isEntryValid(loginPassword)) {
// offline acc
loginResponse = Authentication.checkAccount(loginUsername);
} else {
// mojang acc
loginResponse = Authentication.checkAccount(loginUsername, loginPassword, getClientToken());
}

Expand Down

0 comments on commit 513b569

Please sign in to comment.