[FIX] JsonCoreProvider

This commit is contained in:
Gravita 2021-05-25 21:44:25 +07:00
parent 5a349afe9d
commit c2f55998e3
3 changed files with 55 additions and 12 deletions

View file

@ -44,6 +44,10 @@ public static void registerProviders() {
public abstract User getUserByUsername(String username); public abstract User getUserByUsername(String username);
public User getUserByLogin(String login) {
return getUserByUsername(login);
}
public abstract User getUserByUUID(UUID uuid); public abstract User getUserByUUID(UUID uuid);
public abstract UserSession getUserSessionByOAuthAccessToken(String accessToken) throws OAuthAccessTokenExpired; public abstract UserSession getUserSessionByOAuthAccessToken(String accessToken) throws OAuthAccessTokenExpired;

View file

@ -1,5 +1,7 @@
package pro.gravit.launchserver.auth.core; package pro.gravit.launchserver.auth.core;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.Launcher; import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent;
@ -25,7 +27,9 @@
import java.util.UUID; import java.util.UUID;
public class JsonCoreProvider extends AuthCoreProvider { public class JsonCoreProvider extends AuthCoreProvider {
private transient final Logger logger = LogManager.getLogger();
public String getUserByUsernameUrl; public String getUserByUsernameUrl;
public String getUserByLoginUrl;
public String getUserByUUIDUrl; public String getUserByUUIDUrl;
public String getUserSessionByOAuthAccessTokenUrl; public String getUserSessionByOAuthAccessTokenUrl;
public String getAuthDetailsUrl; public String getAuthDetailsUrl;
@ -77,6 +81,7 @@ public static class JsonAuthReportResponse {
public String oauthRefreshToken; public String oauthRefreshToken;
public long oauthExpire; public long oauthExpire;
public JsonUserSession session; public JsonUserSession session;
public String error;
public AuthManager.AuthReport toAuthReport() { public AuthManager.AuthReport toAuthReport() {
return new AuthManager.AuthReport(minecraftAccessToken, oauthAccessToken, oauthRefreshToken, oauthExpire, session); return new AuthManager.AuthReport(minecraftAccessToken, oauthAccessToken, oauthRefreshToken, oauthExpire, session);
@ -123,7 +128,7 @@ public static class JsonSuccessResponse {
public static class JsonGetUserSessionByOAuthTokenResponse { public static class JsonGetUserSessionByOAuthTokenResponse {
public boolean expired; public boolean expired;
public UserSession session; public JsonUserSession session;
public JsonGetUserSessionByOAuthTokenResponse() { public JsonGetUserSessionByOAuthTokenResponse() {
} }
@ -142,6 +147,14 @@ public User getUserByUsername(String username) {
return jsonRequest(new JsonGetUserByUsername(username), getUserByUsernameUrl, JsonUser.class); return jsonRequest(new JsonGetUserByUsername(username), getUserByUsernameUrl, JsonUser.class);
} }
@Override
public User getUserByLogin(String login) {
if (getUserByLoginUrl != null) {
return jsonRequest(new JsonGetUserByUsername(login), getUserByLoginUrl, JsonUser.class);
}
return super.getUserByLogin(login);
}
@Override @Override
public User getUserByUUID(UUID uuid) { public User getUserByUUID(UUID uuid) {
return jsonRequest(new JsonGetUserByUUID(uuid), getUserByUUIDUrl, JsonUser.class); return jsonRequest(new JsonGetUserByUUID(uuid), getUserByUUIDUrl, JsonUser.class);
@ -149,6 +162,9 @@ public User getUserByUUID(UUID uuid) {
@Override @Override
public UserSession getUserSessionByOAuthAccessToken(String accessToken) throws OAuthAccessTokenExpired { public UserSession getUserSessionByOAuthAccessToken(String accessToken) throws OAuthAccessTokenExpired {
if (getUserSessionByOAuthAccessTokenUrl == null) {
return null;
}
JsonGetUserSessionByOAuthTokenResponse response = jsonRequest(new JsonGetUserSessionByAccessToken(accessToken), getUserSessionByOAuthAccessTokenUrl, JsonGetUserSessionByOAuthTokenResponse.class); JsonGetUserSessionByOAuthTokenResponse response = jsonRequest(new JsonGetUserSessionByAccessToken(accessToken), getUserSessionByOAuthAccessTokenUrl, JsonGetUserSessionByOAuthTokenResponse.class);
if (response == null) return null; if (response == null) return null;
if (!response.expired) throw new OAuthAccessTokenExpired(); if (!response.expired) throw new OAuthAccessTokenExpired();
@ -186,13 +202,17 @@ public PasswordVerifyReport verifyPassword(User user, AuthRequest.AuthPasswordIn
return PasswordVerifyReport.FAILED; return PasswordVerifyReport.FAILED;
} }
} }
if (user == null) {
return jsonRequest(new JsonPasswordVerify(null, null), verifyPasswordUrl, PasswordVerifyReport.class);
}
return jsonRequest(new JsonPasswordVerify(user.getUsername(), user.getUUID()), verifyPasswordUrl, PasswordVerifyReport.class); return jsonRequest(new JsonPasswordVerify(user.getUsername(), user.getUUID()), verifyPasswordUrl, PasswordVerifyReport.class);
} }
@Override @Override
public AuthManager.AuthReport createOAuthSession(User user, AuthResponse.AuthContext context, PasswordVerifyReport report, boolean minecraftAccess) throws IOException { public AuthManager.AuthReport createOAuthSession(User user, AuthResponse.AuthContext context, PasswordVerifyReport report, boolean minecraftAccess) throws IOException {
JsonAuthReportResponse response = jsonRequest(new JsonCreateOAuthSession(user.getUsername(), user.getUUID(), minecraftAccess), createOAuthSessionUrl, JsonAuthReportResponse.class); JsonAuthReportResponse response = jsonRequest(new JsonCreateOAuthSession(user == null ? null : user.getUsername(), user == null ? null : user.getUUID(), minecraftAccess), createOAuthSessionUrl, JsonAuthReportResponse.class);
if (response == null) return null; if (response == null) return null;
if (response.error != null) throw new AuthException(response.error);
JsonUser user1 = (JsonUser) user; JsonUser user1 = (JsonUser) user;
user1.accessToken = response.minecraftAccessToken; user1.accessToken = response.minecraftAccessToken;
return response.toAuthReport(); return response.toAuthReport();
@ -291,17 +311,25 @@ public <T, R> R jsonRequest(T request, String url, Class<R> clazz) {
publisher = HttpRequest.BodyPublishers.noBody(); publisher = HttpRequest.BodyPublishers.noBody();
} }
try { try {
HttpRequest request1 = HttpRequest.newBuilder() HttpRequest.Builder request1 = HttpRequest.newBuilder()
.method("POST", publisher) .method("POST", publisher)
.uri(new URI(url)) .uri(new URI(url))
.header("Authentication", "Bearer ".concat(bearerToken))
.header("Content-Type", "application/json; charset=UTF-8") .header("Content-Type", "application/json; charset=UTF-8")
.header("Accept", "application/json") .header("Accept", "application/json")
.timeout(Duration.ofMillis(10000)) .timeout(Duration.ofMillis(10000));
.build(); if (bearerToken != null) {
HttpResponse<InputStream> response = client.send(request1, HttpResponse.BodyHandlers.ofInputStream()); request1.header("Authentication", "Bearer ".concat(bearerToken));
}
HttpResponse<InputStream> response = client.send(request1.build(), HttpResponse.BodyHandlers.ofInputStream());
int statusCode = response.statusCode(); int statusCode = response.statusCode();
if (200 > statusCode || statusCode > 300) { if (200 > statusCode || statusCode > 300) {
if (statusCode >= 500) {
logger.error("JsonCoreProvider: {} return {}", url, statusCode);
} else if (statusCode >= 300 && statusCode <= 400) {
logger.error("JsonCoreProvider: {} return {}, try redirect to {}. Redirects not supported!", url, statusCode, response.headers().firstValue("Location").orElse("Unknown"));
} else if (statusCode == 403 || statusCode == 401) {
logger.error("JsonCoreProvider: {} return {}. Please set 'bearerToken'!", url, statusCode);
}
return null; return null;
} }
try (Reader reader = new InputStreamReader(response.body())) { try (Reader reader = new InputStreamReader(response.body())) {

View file

@ -132,22 +132,33 @@ private AuthReport authWithCore(AuthResponse.AuthContext context, AuthRequest.Au
} }
return AuthReport.ofMinecraftAccessToken(null); return AuthReport.ofMinecraftAccessToken(null);
} }
User user = provider.getUserByUsername(context.login); User user = null;
if (context.login != null) {
user = provider.getUserByLogin(context.login);
if (user == null) { if (user == null) {
throw new AuthException(AuthRequestEvent.USER_NOT_FOUND_ERROR_MESSAGE); throw new AuthException(AuthRequestEvent.USER_NOT_FOUND_ERROR_MESSAGE);
} }
}
AuthCoreProvider.PasswordVerifyReport report = provider.verifyPassword(user, password); AuthCoreProvider.PasswordVerifyReport report = provider.verifyPassword(user, password);
if (report.success) { if (report.success) {
UUID uuid = user.getUUID();
AuthReport result; AuthReport result;
try { try {
result = provider.createOAuthSession(user, context, report, context.authType == AuthResponse.ConnectTypes.CLIENT && server.config.protectHandler.allowGetAccessToken(context)); result = provider.createOAuthSession(user, context, report, context.authType == AuthResponse.ConnectTypes.CLIENT && server.config.protectHandler.allowGetAccessToken(context));
} catch (IOException e) { } catch (IOException e) {
if (e instanceof AuthException) throw (AuthException) e;
logger.error(e); logger.error(e);
throw new AuthException("Internal Auth Error"); throw new AuthException("Internal Auth Error");
} }
if (user == null) {
if (result.session != null) {
user = result.session.getUser();
} else {
logger.error("AuthCoreProvider {} method createOAuthSession returns null session with login null", context.pair.name);
throw new AuthException("Internal Auth Error");
}
}
context.client.coreObject = user; context.client.coreObject = user;
internalAuth(context.client, context.authType, context.pair, user.getUsername(), uuid, user.getPermissions(), result.isUsingOAuth()); internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), result.isUsingOAuth());
return result; return result;
} else { } else {
if (report.needMoreFactor) { if (report.needMoreFactor) {