mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-04-18 22:13:04 +03:00
possible
This commit is contained in:
parent
a4de93ad1e
commit
03553470b2
6 changed files with 84 additions and 86 deletions
|
@ -46,19 +46,27 @@ public AuthProviderResult auth(String login, String password, String ip) throws
|
|||
}
|
||||
|
||||
@Override
|
||||
public AuthProviderResult oauth(int i) throws Exception {
|
||||
public AuthProviderResult oauth(int a) throws SQLException, AuthException {
|
||||
try (Connection c = mySQLHolder.getConnection()) {
|
||||
PreparedStatement s = c.prepareStatement(oauthQuery);
|
||||
String[] replaceParams = {"%i%", String.valueOf(i)};
|
||||
s.setString(1, oauthQuery.replace("?", String.valueOf(i)));
|
||||
String[] replaceParams = {"int", String.valueOf(a)};
|
||||
String[] queryParams = {"%int%"};
|
||||
String message = "Аккаунт не найден";
|
||||
for (int i = 0; i < queryParams.length; i++)
|
||||
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
|
||||
|
||||
// Execute SQL query
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
try (ResultSet set = s.executeQuery()) {
|
||||
return set.next() ? new AuthProviderResult(set.getString(1),
|
||||
SecurityHelper.randomStringToken(),
|
||||
LaunchServer.server.config.permissionsHandler.getPermissions(set.getString(1))) : authError(message);
|
||||
return set.next() ?
|
||||
new AuthProviderResult(set.getString(1),
|
||||
SecurityHelper.randomStringToken(),
|
||||
LaunchServer.server.config.permissionsHandler.getPermissions(set.getString(1))) :
|
||||
authError(message);
|
||||
}
|
||||
} }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
|
|
@ -84,6 +84,10 @@ public String getIP(){
|
|||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public AuthRequestEvent getAuthRequestEvent() {
|
||||
return authRequestEvent;
|
||||
}
|
||||
}
|
||||
|
||||
private Entry[] stageArea;
|
||||
|
@ -115,29 +119,38 @@ public static void stretchCache(ChannelHandlerContext ctx, Client client){
|
|||
}
|
||||
}
|
||||
|
||||
public static void stretchCache(String IP, AuthRequestEvent authRequestEvent){
|
||||
public static void stretchCache(String IP, AuthRequestEvent authRequestEvent) throws OAuthException {
|
||||
for(Entry e: LaunchServer.server.cacheHandler.stageArea)
|
||||
{
|
||||
if(e.getIP().equals(IP))
|
||||
if(e.isInit() && e.getIP().equals(IP))
|
||||
e.setter(authRequestEvent);
|
||||
}
|
||||
try {
|
||||
throw new OAuthException("Not found");
|
||||
} catch (OAuthException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new OAuthException("Not found");
|
||||
}
|
||||
public static void stretchCache(ChannelHandlerContext ctx, AuthRequestEvent authRequestEvent){
|
||||
public static void stretchCache(ChannelHandlerContext ctx, AuthRequestEvent authRequestEvent) throws OAuthException {
|
||||
for(Entry e: LaunchServer.server.cacheHandler.stageArea)
|
||||
{
|
||||
if(e.getIP().equals(IOHelper.getIP(ctx.channel().remoteAddress())))
|
||||
if(e.isInit() && e.getIP().equals(IOHelper.getIP(ctx.channel().remoteAddress())))
|
||||
e.setter(authRequestEvent);
|
||||
}
|
||||
try {
|
||||
throw new OAuthException("Not found");
|
||||
} catch (OAuthException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new OAuthException("Not found");
|
||||
}
|
||||
|
||||
public static void stretchCache(ChannelHandlerContext ctx, Client client, AuthRequestEvent authRequestEvent) throws OAuthException {
|
||||
Entry e = getUnused();
|
||||
e.setter(ctx, client);
|
||||
e.setter(authRequestEvent);
|
||||
LogHelper.subDebug("New Entry IP: " + e.getIP());
|
||||
}
|
||||
|
||||
public static Entry findEntry(ChannelHandlerContext ctx) throws OAuthException {
|
||||
for(Entry e: LaunchServer.server.cacheHandler.stageArea)
|
||||
{
|
||||
if(e.isInit() && e.getIP().equals(IOHelper.getIP(ctx.channel().remoteAddress())))
|
||||
if(e.getAuthRequestEvent() != null)
|
||||
return e;
|
||||
}
|
||||
throw new OAuthException("Not found");
|
||||
}
|
||||
|
||||
public static Entry getUnused() throws OAuthException {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package ru.gravit.launchserver.websocket.json.auth;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.vk.api.sdk.client.TransportClient;
|
||||
import com.vk.api.sdk.client.VkApiClient;
|
||||
import com.vk.api.sdk.client.actors.UserActor;
|
||||
|
@ -8,13 +7,10 @@
|
|||
import com.vk.api.sdk.objects.UserAuthResponse;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||
import ru.gravit.launcher.events.request.AuthRequestEvent;
|
||||
import ru.gravit.launcher.events.request.LogEvent;
|
||||
|
||||
import ru.gravit.launcher.profiles.PlayerProfile;
|
||||
import ru.gravit.launcher.request.ResultInterface;
|
||||
import ru.gravit.launcher.serialize.HInput;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.auth.AuthProviderPair;
|
||||
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
||||
|
@ -23,10 +19,9 @@
|
|||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.SimpleResponse;
|
||||
import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.util.UUID;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
|
||||
public class OAuthResponse extends SimpleResponse {
|
||||
|
@ -34,46 +29,62 @@ public class OAuthResponse extends SimpleResponse {
|
|||
TransportClient transportClient = new HttpTransportClient();
|
||||
VkApiClient vk = new VkApiClient(transportClient);
|
||||
|
||||
|
||||
public String code;
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, Client clientData) {
|
||||
try {
|
||||
if(code == null) {
|
||||
if(code == "false") {
|
||||
try {
|
||||
throw new OAuthManager.OAuthException("Empty code");
|
||||
}catch (OAuthManager.OAuthException e) {
|
||||
sendError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
UserAuthResponse authResponse = OAuthTokenGet.mToken(code, vk);
|
||||
|
||||
UserActor actor = new UserActor(authResponse.getUserId(), authResponse.getAccessToken());
|
||||
sendResultAndClose(new LogEvent("Continue in Launcher"));
|
||||
AuthProviderResult ar;
|
||||
AuthProviderPair ap;
|
||||
if(LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider") != null) {
|
||||
ar = LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider").provider.oauth(actor.getId());
|
||||
ap = LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider");
|
||||
}else if(code == null){
|
||||
OAuthManager.stretchCache(ctx, clientData);
|
||||
int trays = 0;
|
||||
OAuthManager.Entry inUSE = null;
|
||||
while(trays < 15) {
|
||||
trays++;
|
||||
try {
|
||||
inUSE = OAuthManager.findEntry(ctx);
|
||||
AuthRequestEvent event = inUSE.getAuthRequestEvent();
|
||||
sendResult(event);
|
||||
break;
|
||||
}catch (OAuthManager.OAuthException e){
|
||||
sleep(5000);
|
||||
e.printStackTrace();
|
||||
}
|
||||
} if(inUSE == null) {
|
||||
sendError("TimedOut");
|
||||
throw new OAuthManager.OAuthException("TimedOut");
|
||||
}
|
||||
}else {
|
||||
ar = LaunchServer.server.config.getAuthProviderPair().provider.oauth(actor.getId());
|
||||
ap = LaunchServer.server.config.getAuthProviderPair();
|
||||
UserAuthResponse authResponse = OAuthTokenGet.mToken(code, vk);
|
||||
|
||||
UserActor actor = new UserActor(authResponse.getUserId(), authResponse.getAccessToken());
|
||||
sendResultAndClose(new LogEvent("Continue in Launcher"));
|
||||
AuthProviderResult ar;
|
||||
AuthProviderPair ap;
|
||||
if (LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider") != null) {
|
||||
ar = LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider").provider.oauth(actor.getId());
|
||||
ap = LaunchServer.server.config.getAuthProviderPair("MySQLAuthProvider");
|
||||
} else {
|
||||
ar = LaunchServer.server.config.getAuthProviderPair().provider.oauth(actor.getId());
|
||||
ap = LaunchServer.server.config.getAuthProviderPair();
|
||||
}
|
||||
LogHelper.subDebug("found account " + ar.username);
|
||||
|
||||
PlayerProfile playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, ap.handler.auth(ar), ar.username, null, ap.textureProvider);
|
||||
AuthRequestEvent a = new AuthRequestEvent(playerProfile, ar.accessToken, ar.permissions);
|
||||
OAuthManager.stretchCache(ctx, a);
|
||||
}
|
||||
LogHelper.subDebug("found account " + ar.username);
|
||||
|
||||
UUID uuid = ap.handler.auth(ar);
|
||||
PlayerProfile playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, uuid, ar.username, "client", clientData.auth.textureProvider);
|
||||
AuthRequestEvent a = new AuthRequestEvent(playerProfile, ar.accessToken, ar.permissions);
|
||||
OAuthManager.stretchCache(ctx, a);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
sendError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "OAuthURL";
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package ru.gravit.launcher.events.request;
|
||||
|
||||
import ru.gravit.launcher.LauncherNetworkAPI;
|
||||
import ru.gravit.launcher.events.RequestEvent;
|
||||
import ru.gravit.utils.event.EventInterface;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.UUID;
|
||||
|
||||
public class OAuthRequestEvent extends RequestEvent implements EventInterface {
|
||||
|
||||
private static final UUID uuid = UUID.fromString("77e1bfd7-adf9-4f5d-87d6-a7dd068deb74");
|
||||
|
||||
@LauncherNetworkAPI
|
||||
public java.net.URL URL;
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "oauth";
|
||||
}
|
||||
|
||||
public OAuthRequestEvent(){
|
||||
}
|
||||
public OAuthRequestEvent(URL url){
|
||||
this.URL = url;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -20,6 +20,6 @@ public OAuthRequest(HWID hwid)
|
|||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "oauth";
|
||||
return "OAuthURL";
|
||||
}
|
||||
}
|
|
@ -101,7 +101,7 @@ public void registerResult(String key, Class<? extends ResultInterface> clazz) {
|
|||
|
||||
public void registerResults() {
|
||||
registerResult("auth", AuthRequestEvent.class);
|
||||
registerResult("oauth", OAuthRequestEvent.class);
|
||||
registerResult("OAuthURL", AuthRequestEvent.class);
|
||||
registerResult("checkServer", CheckServerRequestEvent.class);
|
||||
registerResult("joinServer", JoinServerRequestEvent.class);
|
||||
registerResult("launcher", LauncherRequestEvent.class);
|
||||
|
|
Loading…
Reference in a new issue