minified-auth/com.dervarex.minified.auth
AuthManager
public class AuthManagerConstructors
public AuthManager()Methods
void addStateChangeListener(LoginStateChangeListener)LoginState getLoginState()Returns a thread safe snapshot of the current LoginState.String getLoginStateJson()List<UUID> getSavedAccounts()Lists every account that has a saved session.User getUser()boolean hasSessionSaved()void init(Path, EventBus)Initializes the auth manager using the given directory.void init(Path)void init(String, EventBus)Initializes the auth manager using the default application data directory.void init(String)User loadSavedSession(UUID)Loads the saved session of one account without refreshing it, for when there is no network
The access token may have expired, that's fine for singleplayer but online servers will refuse it
Will not work if disk is on fire, though we're already working on a fire extinguisherUser login()Starts the device-code login flow and blocks until the login completes.User loginWithSavedSession()Attempts to load a saved session from disk, refreshes it if possible, and returns the corresponding User.User loginWithSavedSession(UUID)Loads the saved session of one account and refreshes it, like loginWithSavedSession() does
for the most recent oneboolean removeSavedSession(UUID)Forgets an account: deletes its saved session, and session.enc too if that one belongs to itvoid removeStateChangeListener(LoginStateChangeListener)void resetLoginState()Refreshes the login state.String startDeviceCodeLoginAsync()Starts the device-code login flow on a background thread and returns immediately.public static void addStateChangeListener(LoginStateChangeListener listener)| Parameters | ||
|---|---|---|
listener | LoginStateChangeListener | |
getLoginState()
public static LoginState getLoginState()LoginState.
Safe to be called from any thread (e.g. UI polling loops).
| Returns | ||
|---|---|---|
LoginState | A copy of the current login state. | |
public static String getLoginStateJson()| Returns | ||
|---|---|---|
String | The current login state as a JSON string, which can be used for UI display or debugging purposes. | |
public static List<UUID> getSavedAccounts() throws IOExceptionloginWithSavedSession()| Returns | ||
|---|---|---|
List<UUID> | The UUIDs of all accounts with a saved session, no order | |
| Throws | ||
IOException | If the session directory exists but can't be read | |
public static User getUser()| Returns | ||
|---|---|---|
User | The currently logged-in user, or null if no session is active.
Note that this does not check if the session is still valid. | |
hasSessionSaved()
public static boolean hasSessionSaved()| Returns | ||
|---|---|---|
boolean | If there is a saved session on disk(uses the directories given in init(BaseDir)
Note that this does not check if the session is still valid. | |
public static void init(Path baseDir, EventBus eventBus)init(Path)
public static void init(Path BaseDir)| Parameters | ||
|---|---|---|
BaseDir | Path | |
public static void init(String launcherName, EventBus eventBus)
It is recommended to use init(Path baseDir) instead
if you want full control over the storage location.
| Parameters | ||
|---|---|---|
launcherName | String | The launcher name used to create the application directory |
eventBus | EventBus | The event bus to push event updates to |
init(String)
public static void init(String launcherName)| Parameters | ||
|---|---|---|
launcherName | String | |
public static User loadSavedSession(UUID uuid)| Parameters | ||
|---|---|---|
uuid | UUID | The account to load |
| Returns | ||
User | The user from the saved session, or null if there is none | |
| Throws | ||
LoginFailedException | If the session file exists but can't be decrypted or read | |
public static User login()
While this method is running, the device verification URL and user code are published to
the shared loginState as soon as they are issued by the provider. This allows a
UI thread to call getLoginState() (or getLoginStateJson()) and display
the URL/code before login() returns.
Typical use: call startDeviceCodeLoginAsync() from UI code and poll the state.
Use this blocking method only in special cases where a background thread is guaranteed,
and you explicitly want a synchronous result.
Example:
new Thread(AuthManager::login).start();
while (true) {
LoginState state = AuthManager.getLoginState();
if (state.userCode != null && state.verificationUri != null) {
System.out.println(state.verificationUri + " -> " + state.userCode);
break;
}
try { Thread.sleep(100); } catch (InterruptedException ignored) {}
}| Returns | ||
|---|---|---|
User | ||
public static User loginWithSavedSession()| Returns | ||
|---|---|---|
User | The logged-in user, or nul if no valid session could be found. | |
public static User loginWithSavedSession(UUID uuid)loginWithSavedSession() does
for the most recent one| Parameters | ||
|---|---|---|
uuid | UUID | The account to log in with |
| Returns | ||
User | The logged in user, or null if there is no saved session for it or it could not be refreshed | |
public static boolean removeSavedSession(UUID uuid) throws IOExceptionsession.enc too if that one belongs to it| Parameters | ||
|---|---|---|
uuid | UUID | The account to remove |
| Returns | ||
boolean | True if a saved session was deleted | |
| Throws | ||
IOException | If a session file can't be deleted | |
public static void removeStateChangeListener(LoginStateChangeListener listener)| Parameters | ||
|---|---|---|
listener | LoginStateChangeListener | |
resetLoginState()
public static void resetLoginState()public static String startDeviceCodeLoginAsync()
Typical use: call this from UI code, then poll getLoginState() (or JSON) to
show the verification URL and user code while the login is pending.
Use login() only in special cases where a synchronous, blocking call on a
dedicated background thread is preferred.
| Returns | ||
|---|---|---|
String | The initial login state as JSON, usually STARTING or PENDING | |