Skip to content

Commit

Permalink
More consistency in exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Nov 20, 2023
1 parent c684b37 commit 7aea764
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@

public class InvalidRequestException extends Exception {

public InvalidRequestException(final String message) {
super(message);
public final String error;
public final String errorMessage;
public final String cause;

public InvalidRequestException(String error) {
super(error);

this.error = error;
this.errorMessage = null;
this.cause = null;
}

public InvalidRequestException(String error, String errorMessage, String cause) {
super(error + ": " + errorMessage + " (" + cause + ")");

this.error = error;
this.errorMessage = errorMessage;
this.cause = cause;
}
}
35 changes: 23 additions & 12 deletions src/main/java/de/florianmichael/waybackauthlib/WaybackAuthLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ public WaybackAuthLib(String authHost, final String clientToken, final Proxy pro
* @throws Exception If the server didn't send a response or the client token doesn't match.
*/
public void logIn() throws Exception {
if (this.username == null || this.username.isEmpty())
if (this.username == null || this.username.isEmpty()) {
throw new InvalidCredentialsException("Invalid username.");
}

final var refreshAccessToken = this.accessToken != null && !this.accessToken.isEmpty();
final var newAuthentication = this.password != null && !this.password.isEmpty();

if (!refreshAccessToken && !newAuthentication)
if (!refreshAccessToken && !newAuthentication) {
throw new InvalidCredentialsException("Invalid password or access token.");
}

AuthenticateRefreshResponse response;
if (refreshAccessToken) {
Expand All @@ -118,10 +120,12 @@ public void logIn() throws Exception {
response = client.post(this.baseURI.resolve(ROUTE_AUTHENTICATE).toURL(), new AuthenticationRequest(Agent.MINECRAFT, this.username, this.password, this.clientToken), AuthenticateRefreshResponse.class);
}

if (response == null)
if (response == null) {
throw new InvalidRequestException("Server didn't sent a response.");
if (!response.clientToken.equals(this.clientToken))
}
if (!response.clientToken.equals(this.clientToken)) {
throw new InvalidRequestException("Server token and provided token doesn't match.");
}

// AuthLib tracks this field, so we do it too in case someone wants to use this library as a drop-in replacement.
if (response.user != null && response.user.id != null) {
Expand Down Expand Up @@ -162,10 +166,13 @@ public void logOut() throws Exception {
final var request = new InvalidateRequest(this.clientToken, this.accessToken);
final var response = client.post(this.baseURI.resolve(ROUTE_INVALIDATE).toURL(), request, Response.class); // Mojang doesn't send this request, but it seems useful to invalidate the token.

if (!this.loggedIn) throw new IllegalStateException("Cannot log out while not logged in.");
if (!this.loggedIn) {
throw new IllegalStateException("Cannot log out while not logged in.");
}

if (response != null && response.error != null && !response.error.isEmpty())
throw new InvalidRequestException(response.error + " - " + response.errorMessage + " - " + response.cause);
if (response != null && response.error != null && !response.error.isEmpty()) {
throw new InvalidRequestException(response.error, response.errorMessage, response.cause);
}

this.accessToken = null;

Expand All @@ -186,8 +193,9 @@ public String getUsername() {
* @param username The username.
*/
public void setUsername(String username) {
if (this.loggedIn && this.currentProfile != null)
if (this.loggedIn && this.currentProfile != null) {
throw new IllegalStateException("Cannot change username whilst logged in & online");
}

this.username = username;
}
Expand All @@ -202,8 +210,9 @@ public String getPassword() {
* @param password The password.
*/
public void setPassword(String password) {
if (this.loggedIn && this.currentProfile != null)
if (this.loggedIn && this.currentProfile != null) {
throw new IllegalStateException("Cannot set password whilst logged in & online");
}

this.password = password;
}
Expand All @@ -218,8 +227,9 @@ public String getAccessToken() {
* @param accessToken The access token.
*/
public void setAccessToken(String accessToken) {
if (this.loggedIn && this.currentProfile != null)
if (this.loggedIn && this.currentProfile != null) {
throw new IllegalStateException("Cannot set access token whilst logged in & online");
}

this.accessToken = accessToken;
}
Expand Down Expand Up @@ -316,8 +326,9 @@ protected RefreshRequest(String clientToken, String accessToken, GameProfile sel
}

private static class ValidateRequest {
private String clientToken;
private String accessToken;

public String clientToken;
public String accessToken;

public ValidateRequest(final String accessToken, final String clientToken) {
this.clientToken = clientToken;
Expand Down

0 comments on commit 7aea764

Please sign in to comment.