account: use http redirect for oauth login response

This commit is contained in:
Adam
2022-05-29 12:36:50 -04:00
parent 4aa1a3f6e8
commit b9d9cab697
5 changed files with 79 additions and 63 deletions

View File

@@ -57,12 +57,12 @@ public class AccountClient
this.apiBase = apiBase; this.apiBase = apiBase;
} }
public OAuthResponse login() throws IOException public OAuthResponse login(int port) throws IOException
{ {
HttpUrl url = apiBase.newBuilder() HttpUrl url = apiBase.newBuilder()
.addPathSegment("account") .addPathSegment("account")
.addPathSegment("login") .addPathSegment("login")
.addQueryParameter("uuid", uuid.toString()) .addQueryParameter("port", Integer.toString(port))
.build(); .build();
log.debug("Built URI: {}", url); log.debug("Built URI: {}", url);

View File

@@ -35,5 +35,5 @@ public class AccountSession
{ {
private final UUID uuid; private final UUID uuid;
private final Instant created; private final Instant created;
private String username; private final String username;
} }

View File

@@ -25,12 +25,14 @@
package net.runelite.client.account; package net.runelite.client.account;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.sun.net.httpserver.HttpServer;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.time.Instant; import java.time.Instant;
@@ -42,13 +44,11 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.SessionClose; import net.runelite.client.events.SessionClose;
import net.runelite.client.events.SessionOpen; import net.runelite.client.events.SessionOpen;
import net.runelite.client.util.LinkBrowser; import net.runelite.client.util.LinkBrowser;
import net.runelite.client.ws.WSClient;
import net.runelite.http.api.account.OAuthResponse; import net.runelite.http.api.account.OAuthResponse;
import net.runelite.http.api.ws.messages.LoginResponse; import okhttp3.HttpUrl;
@Singleton @Singleton
@Slf4j @Slf4j
@@ -59,26 +59,29 @@ public class SessionManager
private final EventBus eventBus; private final EventBus eventBus;
private final ConfigManager configManager; private final ConfigManager configManager;
private final WSClient wsClient;
private final File sessionFile; private final File sessionFile;
private final AccountClient accountClient; private final AccountClient accountClient;
private final Gson gson; private final Gson gson;
private final String oauthRedirect;
private HttpServer server;
@Inject @Inject
private SessionManager( private SessionManager(
@Named("sessionfile") File sessionfile, @Named("sessionfile") File sessionfile,
ConfigManager configManager, ConfigManager configManager,
EventBus eventBus, EventBus eventBus,
WSClient wsClient,
AccountClient accountClient, AccountClient accountClient,
Gson gson) Gson gson,
@Named("runelite.oauth.redirect") String oauthRedirect
)
{ {
this.configManager = configManager; this.configManager = configManager;
this.eventBus = eventBus; this.eventBus = eventBus;
this.wsClient = wsClient;
this.sessionFile = sessionfile; this.sessionFile = sessionfile;
this.accountClient = accountClient; this.accountClient = accountClient;
this.gson = gson; this.gson = gson;
this.oauthRedirect = oauthRedirect;
eventBus.register(this); eventBus.register(this);
} }
@@ -113,7 +116,7 @@ public class SessionManager
return; return;
} }
openSession(session, false); openSession(session);
} }
private void saveSession() private void saveSession()
@@ -141,19 +144,12 @@ public class SessionManager
} }
/** /**
* Set the given session as the active session and open a socket to the * Set the given session as the active session
* server with the given session
* *
* @param session session * @param session session
*/ */
private void openSession(AccountSession session, boolean openSocket) private void openSession(AccountSession session)
{ {
// Change session on the websocket
if (openSocket)
{
wsClient.changeSession(session.getUuid());
}
accountSession = session; accountSession = session;
if (session.getUsername() != null) if (session.getUsername() != null)
@@ -168,8 +164,6 @@ public class SessionManager
private void closeSession() private void closeSession()
{ {
wsClient.changeSession(null);
if (accountSession == null) if (accountSession == null)
{ {
return; return;
@@ -197,49 +191,85 @@ public class SessionManager
public void login() public void login()
{ {
// If a session is already open, use that id. Otherwise generate a new id. if (server == null)
UUID uuid = wsClient.getSessionId() != null ? wsClient.getSessionId() : UUID.randomUUID(); {
accountClient.setUuid(uuid); try
{
startServer();
}
catch (IOException ex)
{
log.error("Unable to start http server", ex);
return;
}
}
final OAuthResponse login; final OAuthResponse login;
try try
{ {
login = accountClient.login(); login = accountClient.login(server.getAddress().getPort());
} }
catch (IOException ex) catch (IOException ex)
{ {
log.warn("Unable to get oauth url", ex); log.error("Unable to get oauth url", ex);
return; return;
} }
// Create new session
openSession(new AccountSession(login.getUid(), Instant.now()), true);
// Navigate to login link // Navigate to login link
LinkBrowser.browse(login.getOauthUrl()); LinkBrowser.browse(login.getOauthUrl());
} }
@Subscribe
public void onLoginResponse(LoginResponse loginResponse)
{
log.debug("Now signed in as {}", loginResponse.getUsername());
AccountSession session = getAccountSession();
session.setUsername(loginResponse.getUsername());
// Open session, again, now that we have a username
// This triggers onSessionOpen
// The socket is already opened here anyway so we pass true for openSocket
openSession(session, true);
// Save session to disk
saveSession();
}
public void logout() public void logout()
{ {
closeSession(); closeSession();
deleteSession(); deleteSession();
} }
private void startServer() throws IOException
{
server = HttpServer.create(new InetSocketAddress("localhost", 0), 1);
server.createContext("/oauth", req ->
{
try
{
final HttpUrl url = HttpUrl.get("http://localhost" + req.getRequestURI());
final String username = url.queryParameter("username");
final UUID sessionId = UUID.fromString(url.queryParameter("sessionId"));
log.debug("Now signed in as {}", username);
// open the session, which triggers the sessonopen event
AccountSession session = new AccountSession(sessionId, Instant.now(), username);
openSession(session);
// Save session to disk
saveSession();
req.getResponseHeaders().set("Location", oauthRedirect);
req.sendResponseHeaders(302, 0);
}
catch (Exception e)
{
log.warn("failure serving oauth response", e);
req.sendResponseHeaders(400, 0);
req.getResponseBody().write(e.getMessage().getBytes(StandardCharsets.UTF_8));
}
finally
{
req.close();
stopServer();
}
});
log.debug("Starting server {}", server);
server.start();
}
private void stopServer()
{
log.debug("Stopping server {}", server);
server.stop(0);
server = null;
}
} }

View File

@@ -21,3 +21,4 @@ runelite.static.base=https://static.runelite.net
runelite.ws=https://api.runelite.net/ws runelite.ws=https://api.runelite.net/ws
runelite.config=https://static.runelite.net/config.json runelite.config=https://static.runelite.net/config.json
runelite.osrstwitter.link=https://twitter.com/OldSchoolRS runelite.osrstwitter.link=https://twitter.com/OldSchoolRS
runelite.oauth.redirect=https://runelite.net/logged-in

View File

@@ -29,14 +29,11 @@ import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule; import com.google.inject.testing.fieldbinder.BoundFieldModule;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.client.RuneLite; import net.runelite.client.RuneLite;
import net.runelite.client.account.AccountSession;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import org.junit.Assert; import org.junit.Assert;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@@ -89,9 +86,6 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfig() throws IOException public void testGetConfig() throws IOException
{ {
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUsername("test");
manager.setConfiguration("test", "key", "moo"); manager.setConfiguration("test", "key", "moo");
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
@@ -101,9 +95,6 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfigDefault() throws IOException public void testGetConfigDefault() throws IOException
{ {
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUsername("test");
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
Assert.assertEquals("default", conf.key()); Assert.assertEquals("default", conf.key());
} }
@@ -111,9 +102,6 @@ public class ConfigManagerTest
@Test @Test
public void testSetConfig() throws IOException public void testSetConfig() throws IOException
{ {
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUsername("test");
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
conf.key("new value"); conf.key("new value");
@@ -123,9 +111,6 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfigDescriptor() throws IOException public void testGetConfigDescriptor() throws IOException
{ {
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUsername("test");
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
ConfigDescriptor descriptor = manager.getConfigDescriptor(conf); ConfigDescriptor descriptor = manager.getConfigDescriptor(conf);
Assert.assertEquals(2, descriptor.getItems().size()); Assert.assertEquals(2, descriptor.getItems().size());