From 7886cce6f8b54c850796878e3e2a2c2f4df51a21 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 13 Apr 2021 21:26:35 +0700 Subject: [PATCH] [FEATURE] Support 2FA in JsonAuthProvider --- .../auth/provider/JsonAuthProvider.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java index 01e1dcc7..087e5f4e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -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; }