client: add otl token requester
This commit is contained in:
@@ -27,9 +27,12 @@ package net.runelite.client;
|
|||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
import ch.qos.logback.classic.Logger;
|
import ch.qos.logback.classic.Logger;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
import com.jagex.oldscape.pub.OAuthApi;
|
||||||
|
import com.jagex.oldscape.pub.OtlTokenResponse;
|
||||||
import java.applet.Applet;
|
import java.applet.Applet;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -45,6 +48,7 @@ import java.security.SecureRandom;
|
|||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -81,6 +85,8 @@ import net.runelite.client.ui.overlay.tooltip.TooltipOverlay;
|
|||||||
import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
|
import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
|
||||||
import net.runelite.http.api.RuneLiteAPI;
|
import net.runelite.http.api.RuneLiteAPI;
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
@@ -105,6 +111,9 @@ public class RuneLite
|
|||||||
@Getter
|
@Getter
|
||||||
private static Injector injector;
|
private static Injector injector;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private OkHttpClient okHttpClient;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PluginManager pluginManager;
|
private PluginManager pluginManager;
|
||||||
|
|
||||||
@@ -314,6 +323,11 @@ public class RuneLite
|
|||||||
}
|
}
|
||||||
|
|
||||||
applet.start();
|
applet.start();
|
||||||
|
|
||||||
|
if (applet instanceof OAuthApi)
|
||||||
|
{
|
||||||
|
setupJxAuth((OAuthApi) applet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SplashScreen.stage(.57, null, "Loading configuration");
|
SplashScreen.stage(.57, null, "Loading configuration");
|
||||||
@@ -536,4 +550,83 @@ public class RuneLite
|
|||||||
System.setProperty(key, value);
|
System.setProperty(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupJxAuth(OAuthApi oAuthApi)
|
||||||
|
{
|
||||||
|
String accessToken = System.getenv("JX_ACCESS_TOKEN");
|
||||||
|
if (Strings.isNullOrEmpty(accessToken))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
log.info("Initializing OTL token requester with access token");
|
||||||
|
oAuthApi.setOtlTokenRequester(url ->
|
||||||
|
{
|
||||||
|
CompletableFuture<OtlTokenResponse> f = new CompletableFuture<>();
|
||||||
|
okHttpClient.newCall(new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.header("Authorization", "Bearer " + accessToken)
|
||||||
|
.get()
|
||||||
|
.build())
|
||||||
|
.enqueue(new Callback()
|
||||||
|
{
|
||||||
|
private void complete(boolean success, String token)
|
||||||
|
{
|
||||||
|
f.complete(new OtlTokenResponse()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean isSuccess()
|
||||||
|
{
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getToken()
|
||||||
|
{
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call call, IOException e)
|
||||||
|
{
|
||||||
|
log.error("HTTP error while performing OTL request", e);
|
||||||
|
complete(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call call, Response response) throws IOException
|
||||||
|
{
|
||||||
|
if (response.code() != 200)
|
||||||
|
{
|
||||||
|
log.error("Non-OK response performing OTL request: {}", response.code());
|
||||||
|
complete(false, null);
|
||||||
|
response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.body() == null)
|
||||||
|
{
|
||||||
|
log.error("OK response with empty body from OTL request");
|
||||||
|
complete(false, null);
|
||||||
|
response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("Successful OTL response");
|
||||||
|
complete(true, response.body().string());
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (LinkageError ex)
|
||||||
|
{
|
||||||
|
log.error("error setting up OTL requester", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user