move session management into the SessionManager from the AccountPlugin
This commit is contained in:
@@ -25,12 +25,17 @@
|
|||||||
package net.runelite.client.account;
|
package net.runelite.client.account;
|
||||||
|
|
||||||
import com.google.common.eventbus.EventBus;
|
import com.google.common.eventbus.EventBus;
|
||||||
|
import com.google.common.eventbus.Subscribe;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import java.awt.Desktop;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
@@ -41,6 +46,8 @@ import net.runelite.client.config.ConfigManager;
|
|||||||
import net.runelite.api.events.SessionClose;
|
import net.runelite.api.events.SessionClose;
|
||||||
import net.runelite.api.events.SessionOpen;
|
import net.runelite.api.events.SessionOpen;
|
||||||
import net.runelite.http.api.account.AccountClient;
|
import net.runelite.http.api.account.AccountClient;
|
||||||
|
import net.runelite.http.api.account.OAuthResponse;
|
||||||
|
import net.runelite.http.api.ws.messages.LoginResponse;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -52,8 +59,7 @@ public class SessionManager
|
|||||||
@Getter
|
@Getter
|
||||||
private AccountSession accountSession;
|
private AccountSession accountSession;
|
||||||
|
|
||||||
@Inject
|
private final EventBus eventBus;
|
||||||
private EventBus eventBus;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
@@ -61,6 +67,15 @@ public class SessionManager
|
|||||||
@Inject
|
@Inject
|
||||||
private ScheduledExecutorService executor;
|
private ScheduledExecutorService executor;
|
||||||
|
|
||||||
|
private final AccountClient loginClient = new AccountClient();
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public SessionManager(EventBus eventBus)
|
||||||
|
{
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
eventBus.register(this);
|
||||||
|
}
|
||||||
|
|
||||||
public void loadSession()
|
public void loadSession()
|
||||||
{
|
{
|
||||||
if (!SESSION_FILE.exists())
|
if (!SESSION_FILE.exists())
|
||||||
@@ -165,6 +180,16 @@ public class SessionManager
|
|||||||
|
|
||||||
log.debug("Logging out of account {}", accountSession.getUsername());
|
log.debug("Logging out of account {}", accountSession.getUsername());
|
||||||
|
|
||||||
|
AccountClient client = new AccountClient(accountSession.getUuid());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.logout();
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
log.warn("Unable to logout of session", ex);
|
||||||
|
}
|
||||||
|
|
||||||
accountSession = null; // No more account
|
accountSession = null; // No more account
|
||||||
|
|
||||||
// Restore config
|
// Restore config
|
||||||
@@ -172,4 +197,68 @@ public class SessionManager
|
|||||||
|
|
||||||
eventBus.post(new SessionClose());
|
eventBus.post(new SessionClose());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void login()
|
||||||
|
{
|
||||||
|
OAuthResponse login;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
login = loginClient.login();
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
log.warn("Unable to get oauth url", ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new session
|
||||||
|
openSession(new AccountSession(login.getUid(), Instant.now()));
|
||||||
|
|
||||||
|
if (!Desktop.isDesktopSupported())
|
||||||
|
{
|
||||||
|
log.info("Desktop is not supported. Visit {}", login.getOauthUrl());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Desktop desktop = Desktop.getDesktop();
|
||||||
|
if (!desktop.isSupported(Desktop.Action.BROWSE))
|
||||||
|
{
|
||||||
|
log.info("Desktop browser is not supported. Visit {}", login.getOauthUrl());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
desktop.browse(new URI(login.getOauthUrl()));
|
||||||
|
|
||||||
|
log.debug("Opened browser to {}", login.getOauthUrl());
|
||||||
|
}
|
||||||
|
catch (IOException | URISyntaxException ex)
|
||||||
|
{
|
||||||
|
log.warn("Unable to open login page", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onLogin(LoginResponse loginResponse)
|
||||||
|
{
|
||||||
|
log.debug("Now logged in as {}", loginResponse.getUsername());
|
||||||
|
|
||||||
|
AccountSession session = getAccountSession();
|
||||||
|
session.setUsername(loginResponse.getUsername());
|
||||||
|
|
||||||
|
// Open session, again, now that we have a username
|
||||||
|
// This triggers onSessionOpen
|
||||||
|
openSession(session);
|
||||||
|
|
||||||
|
// Save session to disk
|
||||||
|
saveSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logout()
|
||||||
|
{
|
||||||
|
closeSession();
|
||||||
|
deleteSession();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -25,16 +25,12 @@
|
|||||||
package net.runelite.client.plugins.account;
|
package net.runelite.client.plugins.account;
|
||||||
|
|
||||||
import com.google.common.eventbus.Subscribe;
|
import com.google.common.eventbus.Subscribe;
|
||||||
import java.awt.Desktop;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.runelite.api.events.SessionClose;
|
||||||
import net.runelite.client.account.AccountSession;
|
import net.runelite.client.account.AccountSession;
|
||||||
import net.runelite.client.account.SessionManager;
|
import net.runelite.client.account.SessionManager;
|
||||||
import net.runelite.api.events.SessionOpen;
|
import net.runelite.api.events.SessionOpen;
|
||||||
@@ -44,9 +40,6 @@ import net.runelite.client.ui.ClientUI;
|
|||||||
import net.runelite.client.ui.NavigationButton;
|
import net.runelite.client.ui.NavigationButton;
|
||||||
import net.runelite.client.ui.PluginToolbar;
|
import net.runelite.client.ui.PluginToolbar;
|
||||||
import net.runelite.client.util.RunnableExceptionLogger;
|
import net.runelite.client.util.RunnableExceptionLogger;
|
||||||
import net.runelite.http.api.account.AccountClient;
|
|
||||||
import net.runelite.http.api.account.OAuthResponse;
|
|
||||||
import net.runelite.http.api.ws.messages.LoginResponse;
|
|
||||||
|
|
||||||
@PluginDescriptor(
|
@PluginDescriptor(
|
||||||
name = "Account plugin",
|
name = "Account plugin",
|
||||||
@@ -67,8 +60,6 @@ public class AccountPlugin extends Plugin
|
|||||||
private NavigationButton loginButton;
|
private NavigationButton loginButton;
|
||||||
private NavigationButton logoutButton;
|
private NavigationButton logoutButton;
|
||||||
|
|
||||||
private final AccountClient loginClient = new AccountClient();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void startUp() throws Exception
|
protected void startUp() throws Exception
|
||||||
{
|
{
|
||||||
@@ -88,91 +79,22 @@ public class AccountPlugin extends Plugin
|
|||||||
|
|
||||||
private void loginClick(ActionEvent ae)
|
private void loginClick(ActionEvent ae)
|
||||||
{
|
{
|
||||||
executor.execute(RunnableExceptionLogger.wrap(this::openLoginPage));
|
executor.execute(RunnableExceptionLogger.wrap(sessionManager::login));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logoutClick(ActionEvent ae)
|
private void logoutClick(ActionEvent ae)
|
||||||
{
|
{
|
||||||
// Destroy session
|
sessionManager.logout();
|
||||||
AccountSession session = sessionManager.getAccountSession();
|
}
|
||||||
if (session != null)
|
|
||||||
{
|
|
||||||
AccountClient client = new AccountClient(session.getUuid());
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client.logout();
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
log.warn("Unable to logout of session", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionManager.closeSession(); // remove session from client
|
|
||||||
sessionManager.deleteSession(); // delete saved session file
|
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onSessionClose(SessionClose e)
|
||||||
|
{
|
||||||
// Replace logout nav button with login
|
// Replace logout nav button with login
|
||||||
PluginToolbar navigationPanel = ui.getPluginToolbar();
|
PluginToolbar navigationPanel = ui.getPluginToolbar();
|
||||||
navigationPanel.removeNavigation(logoutButton);
|
navigationPanel.removeNavigation(logoutButton);
|
||||||
navigationPanel.addNavigation(loginButton);
|
navigationPanel.addNavigation(loginButton);
|
||||||
}
|
navigationPanel.repaint();
|
||||||
|
|
||||||
private void openLoginPage()
|
|
||||||
{
|
|
||||||
OAuthResponse login;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
login = loginClient.login();
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
log.warn("Unable to get oauth url", ex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new session
|
|
||||||
sessionManager.openSession(new AccountSession(login.getUid(), Instant.now()));
|
|
||||||
|
|
||||||
if (!Desktop.isDesktopSupported())
|
|
||||||
{
|
|
||||||
log.info("Desktop is not supported. Visit {}", login.getOauthUrl());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Desktop desktop = Desktop.getDesktop();
|
|
||||||
if (!desktop.isSupported(Desktop.Action.BROWSE))
|
|
||||||
{
|
|
||||||
log.info("Desktop browser is not supported. Visit {}", login.getOauthUrl());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
desktop.browse(new URI(login.getOauthUrl()));
|
|
||||||
|
|
||||||
log.debug("Opened browser to {}", login.getOauthUrl());
|
|
||||||
}
|
|
||||||
catch (IOException | URISyntaxException ex)
|
|
||||||
{
|
|
||||||
log.warn("Unable to open login page", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe
|
|
||||||
public void onLogin(LoginResponse loginResponse)
|
|
||||||
{
|
|
||||||
log.debug("Now logged in as {}", loginResponse.getUsername());
|
|
||||||
|
|
||||||
AccountSession session = sessionManager.getAccountSession();
|
|
||||||
session.setUsername(loginResponse.getUsername());
|
|
||||||
|
|
||||||
// Open session, again, now that we have a username
|
|
||||||
// This triggers onSessionOpen
|
|
||||||
sessionManager.openSession(session);
|
|
||||||
|
|
||||||
// Save session to disk
|
|
||||||
sessionManager.saveSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
@@ -187,15 +109,11 @@ public class AccountPlugin extends Plugin
|
|||||||
|
|
||||||
log.debug("Session opened as {}", session.getUsername());
|
log.debug("Session opened as {}", session.getUsername());
|
||||||
|
|
||||||
replaceLoginWithLogout();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void replaceLoginWithLogout()
|
|
||||||
{
|
|
||||||
// Replace login nav button with logout
|
// Replace login nav button with logout
|
||||||
PluginToolbar navigationPanel = ui.getPluginToolbar();
|
PluginToolbar navigationPanel = ui.getPluginToolbar();
|
||||||
navigationPanel.removeNavigation(loginButton);
|
navigationPanel.removeNavigation(loginButton);
|
||||||
navigationPanel.addNavigation(logoutButton);
|
navigationPanel.addNavigation(logoutButton);
|
||||||
|
navigationPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user