add staff password login

This commit is contained in:
Elijah R 2024-05-01 16:30:47 -04:00
parent 96f12f36f2
commit 379c8ba85d

View file

@ -4,6 +4,7 @@ import org.apache.commons.text.StringEscapeUtils;
import org.java_websocket.handshake.ServerHandshake;
import javax.imageio.ImageIO;
import javax.security.auth.login.CredentialException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -34,7 +35,8 @@ public class CollabVMClient {
private ResetVote _currentVote;
private boolean _usesAccountAuth;
// Futures
private CompletableFuture<AccountLoginResult> _loginFuture;
private CompletableFuture<AccountLoginResult> _accountLoginFuture;
private CompletableFuture<Integer> _passwordLoginFuture;
// Constructor
@SuppressWarnings("unchecked")
@ -90,13 +92,21 @@ public class CollabVMClient {
* @return A CompletableFuture that will complete when the login attempt is finished
*/
public CompletableFuture<AccountLoginResult> loginAccount(String token) {
this._loginFuture = new CompletableFuture<>();
this._accountLoginFuture = new CompletableFuture<>();
this.sendGuac("login", token);
return this._loginFuture;
return this._accountLoginFuture;
}
// Getters and setters
/**
* Log into the VM using a staff password
* @param password The password to log in with
* @return A CompletableFuture that will complete when the login attempt is finished with the new rank
*/
public CompletableFuture<Integer> loginPassword(String password) {
this._passwordLoginFuture = new CompletableFuture<>();
this.sendGuac("admin", "2", password);
return this._passwordLoginFuture;
}
/**
* If not connected, sets the username to request when connecting.
@ -310,7 +320,7 @@ public class CollabVMClient {
}
case "login": {
boolean success = msgArr[1].equals("1");
this._loginFuture.complete(new AccountLoginResult(success, success ? null : msgArr[2]));
this._accountLoginFuture.complete(new AccountLoginResult(success, success ? null : msgArr[2]));
break;
}
case "auth": {
@ -342,6 +352,23 @@ public class CollabVMClient {
}
case "admin": {
switch (msgArr[1]) {
case "0":
switch (msgArr[2]) {
case "0":
this._passwordLoginFuture.completeExceptionally(new CredentialException("Invalid password"));
break;
case "1":
this._rank = Rank.Admin;
this._permissions = Permissions.All();
this._passwordLoginFuture.complete(Rank.Admin);
break;
case "3":
this._rank = Rank.Moderator;
this._permissions = new Permissions(Integer.parseInt(msgArr[3]));
this._passwordLoginFuture.complete(Rank.Moderator);
break;
}
break;
}
break;