[FEATURE] Support 2FA in JsonAuthProvider

This commit is contained in:
Gravita 2021-04-13 21:26:35 +07:00
parent d1e671a935
commit 7886cce6f8

View file

@ -5,7 +5,9 @@
import pro.gravit.launcher.HTTPRequest;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.request.auth.AuthRequest;
import pro.gravit.launcher.request.auth.password.Auth2FAPassword;
import pro.gravit.launcher.request.auth.password.AuthPlainPassword;
import pro.gravit.launcher.request.auth.password.AuthTOTPPassword;
import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.utils.helper.SecurityHelper;
@ -15,12 +17,32 @@
public final class JsonAuthProvider extends AuthProvider {
public URL url;
public boolean enable2FA;
public String apiKey;
@Override
public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws IOException {
if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported");
JsonElement content = HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new authRequest(login, ((AuthPlainPassword) password).password, ip, apiKey)), url);
String firstPassword;
String secondPassword;
if(!enable2FA) {
if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported");
firstPassword = ((AuthPlainPassword) password).password;
secondPassword = null;
} else {
if(password instanceof AuthPlainPassword) {
firstPassword = ((AuthPlainPassword) password).password;
secondPassword = null;
} else if(password instanceof Auth2FAPassword) {
if (!(((Auth2FAPassword) password).firstPassword instanceof AuthPlainPassword)) throw new AuthException("This password type not supported");
firstPassword = ((AuthPlainPassword) ((Auth2FAPassword) password).firstPassword).password;
if (!(((Auth2FAPassword) password).secondPassword instanceof AuthTOTPPassword)) throw new AuthException("This password type not supported");
secondPassword = ((AuthTOTPPassword) ((Auth2FAPassword) password).secondPassword).totp;
} else {
throw new AuthException("This password type not supported");
}
}
JsonElement content = HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new authRequest(login, firstPassword, secondPassword, ip, apiKey)), url);
if (!content.isJsonObject())
return authError("Authentication server response is malformed");
authResult result = Launcher.gsonManager.gson.fromJson(content, authResult.class);
@ -44,18 +66,21 @@ public static class authResult {
public static class authRequest {
final String username;
final String password;
final String secondPassword;
final String ip;
String apiKey;
public authRequest(String username, String password, String ip) {
this.username = username;
this.password = password;
this.secondPassword = null;
this.ip = ip;
}
public authRequest(String username, String password, String ip, String apiKey) {
public authRequest(String username, String password, String secondPassword, String ip, String apiKey) {
this.username = username;
this.password = password;
this.secondPassword = secondPassword;
this.ip = ip;
this.apiKey = apiKey;
}