com.github.steveice10.mc.auth.exception.request.RequestException Java Examples

The following examples show how to use com.github.steveice10.mc.auth.exception.request.RequestException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HTTP.java    From MCAuthLib with MIT License 6 votes vote down vote up
private static void checkForError(JsonElement response) throws RequestException {
    if(response.isJsonObject()) {
        JsonObject object = response.getAsJsonObject();
        if(object.has("error")) {
            String error = object.get("error").getAsString();
            String cause = object.has("cause") ? object.get("cause").getAsString() : "";
            String errorMessage = object.has("errorMessage") ? object.get("errorMessage").getAsString() : "";
            if(!error.equals("")) {
                if(error.equals("ForbiddenOperationException")) {
                    if(cause != null && cause.equals("UserMigratedException")) {
                        throw new UserMigratedException(errorMessage);
                    } else {
                        throw new InvalidCredentialsException(errorMessage);
                    }
                } else {
                    throw new RequestException(errorMessage);
                }
            }
        }
    }
}
 
Example #2
Source File: HTTP.java    From MCAuthLib with MIT License 6 votes vote down vote up
/**
 * Makes an HTTP request.
 *
 * @param proxy        Proxy to use when making the request.
 * @param uri          URI to make the request to.
 * @param input        Input to provide in the request.
 * @param responseType Class to provide the response as.
 * @param <T>          Type to provide the response as.
 * @return The response of the request.
 * @throws IllegalArgumentException If the given proxy or URI is null.
 * @throws RequestException If an error occurs while making the request.
 */
public static <T> T makeRequest(Proxy proxy, URI uri, Object input, Class<T> responseType) throws RequestException {
    if(proxy == null) {
        throw new IllegalArgumentException("Proxy cannot be null.");
    } else if(uri == null) {
        throw new IllegalArgumentException("URI cannot be null.");
    }

    JsonElement response;
    try {
        response = input == null ? performGetRequest(proxy, uri) : performPostRequest(proxy, uri, GSON.toJson(input), "application/json");
    } catch(IOException e) {
        throw new ServiceUnavailableException("Could not make request to '" + uri + "'.", e);
    }

    if(response != null) {
        checkForError(response);

        if(responseType != null) {
            return GSON.fromJson(response, responseType);
        }
    }

    return null;
}
 
Example #3
Source File: AuthenticationService.java    From MCAuthLib with MIT License 6 votes vote down vote up
/**
 * Selects a game profile.
 *
 * @param profile Profile to select.
 * @throws RequestException If an error occurs while making the request.
 */
public void selectGameProfile(GameProfile profile) throws RequestException {
    if(!this.loggedIn) {
        throw new RequestException("Cannot change game profile while not logged in.");
    } else if(this.selectedProfile != null) {
        throw new RequestException("Cannot change game profile when it is already selected.");
    } else if(profile == null || !this.profiles.contains(profile)) {
        throw new IllegalArgumentException("Invalid profile '" + profile + "'.");
    }

    RefreshRequest request = new RefreshRequest(this.clientToken, this.accessToken, profile);
    AuthenticateRefreshResponse response = HTTP.makeRequest(this.getProxy(), this.getEndpointUri(REFRESH_ENDPOINT), request, AuthenticateRefreshResponse.class);
    if(response == null) {
        throw new RequestException("Server returned invalid response.");
    } else if(!response.clientToken.equals(this.clientToken)) {
        throw new RequestException("Server responded with incorrect client token.");
    }

    this.accessToken = response.accessToken;
    this.selectedProfile = response.selectedProfile;
}
 
Example #4
Source File: AuthenticationService.java    From MCAuthLib with MIT License 6 votes vote down vote up
/**
 * Logs the service out.
 *
 * @throws RequestException If an error occurs while making the request.
 */
public void logout() throws RequestException {
    if(!this.loggedIn) {
        throw new IllegalStateException("Cannot log out while not logged in.");
    }

    InvalidateRequest request = new InvalidateRequest(this.clientToken, this.accessToken);
    HTTP.makeRequest(this.getProxy(), this.getEndpointUri(INVALIDATE_ENDPOINT), request);

    this.accessToken = null;
    this.loggedIn = false;
    this.id = null;
    this.properties.clear();
    this.profiles.clear();
    this.selectedProfile = null;
}
 
Example #5
Source File: SessionService.java    From MCAuthLib with MIT License 6 votes vote down vote up
/**
 * Fills in the properties of a profile.
 *
 * @param profile Profile to fill in the properties of.
 * @return The given profile, after filling in its properties.
 * @throws ProfileException If the property lookup fails.
 */
public GameProfile fillProfileProperties(GameProfile profile) throws ProfileException {
    if(profile.getId() == null) {
        return profile;
    }

    try {
        MinecraftProfileResponse response = HTTP.makeRequest(this.getProxy(), this.getEndpointUri(PROFILE_ENDPOINT + "/" + UUIDSerializer.fromUUID(profile.getId()), "unsigned=false"), null, MinecraftProfileResponse.class);
        if(response == null) {
            throw new ProfileNotFoundException("Couldn't fetch profile properties for " + profile + " as the profile does not exist.");
        }

        profile.setProperties(response.properties);
        return profile;
    } catch(RequestException e) {
        throw new ProfileLookupException("Couldn't look up profile properties for " + profile + ".", e);
    }
}
 
Example #6
Source File: MinecraftAuthTest.java    From MCAuthLib with MIT License 6 votes vote down vote up
private static AuthenticationService login(String clientToken, String with, boolean token) {
    AuthenticationService auth = new AuthenticationService(clientToken);
    auth.setProxy(PROXY);
    auth.setUsername(EMAIL);
    if(token) {
        auth.setAccessToken(with);
    } else {
        auth.setPassword(with);
    }

    try {
        auth.login();
    } catch(RequestException e) {
        System.err.println("Failed to log in with " + (token ? "token" : "password") + "!");
        e.printStackTrace();
    }

    return auth;
}
 
Example #7
Source File: SessionService.java    From Visage with MIT License 6 votes vote down vote up
/**
 * Fills in the properties of a profile.
 *
 * @param profile Profile to fill in the properties of.
 * @return The given profile, after filling in its properties.
 * @throws ProfileException If the property lookup fails.
 */
public GameProfile fillProfileProperties(GameProfile profile) throws ProfileException {
    if(profile.getId() == null) {
        return profile;
    }

    try {
        MinecraftProfileResponse response = HTTP.makeRequest(this.proxy, BASE_URL+"/profile" + "/" + UUIDSerializer.fromUUID(profile.getId()) + "?unsigned=false", null, MinecraftProfileResponse.class);
        if(response == null) {
            throw new ProfileNotFoundException("Couldn't fetch profile properties for " + profile + " as the profile does not exist.");
        }

        if(response.properties != null) {
            profile.getProperties().addAll(response.properties);
        }

        return profile;
    } catch(RequestException e) {
        throw new ProfileLookupException("Couldn't look up profile properties for " + profile + ".", e);
    }
}
 
Example #8
Source File: AuthenticationService.java    From Visage with MIT License 6 votes vote down vote up
/**
 * Logs the service in.
 *
 * @throws RequestException If an error occurs while making the request.
 */
public void login() throws RequestException {
    if(this.username == null || this.username.equals("")) {
        throw new InvalidCredentialsException("Invalid username.");
    } else {
        if(this.accessToken != null && !this.accessToken.equals("")) {
            this.loginWithToken();
        } else {
            if(this.password == null || this.password.equals("")) {
                throw new InvalidCredentialsException("Invalid password.");
            }

            this.loginWithPassword();
        }
    }
}
 
Example #9
Source File: AuthenticationService.java    From Visage with MIT License 6 votes vote down vote up
/**
 * Logs the service out.
 *
 * @throws RequestException If an error occurs while making the request.
 */
public void logout() throws RequestException {
    if(!this.loggedIn) {
        throw new IllegalStateException("Cannot log out while not logged in.");
    }

    InvalidateRequest request = new InvalidateRequest(this.clientToken, this.accessToken);
    HTTP.makeRequest(this.proxy, INVALIDATE_URL, request);

    this.accessToken = null;
    this.loggedIn = false;
    this.id = null;
    this.properties.clear();
    this.profiles.clear();
    this.selectedProfile = null;
}
 
Example #10
Source File: AuthenticationService.java    From Visage with MIT License 6 votes vote down vote up
/**
 * Selects a game profile.
 *
 * @param profile Profile to select.
 * @throws RequestException If an error occurs while making the request.
 */
public void selectGameProfile(GameProfile profile) throws RequestException {
    if(!this.loggedIn) {
        throw new RequestException("Cannot change game profile while not logged in.");
    } else if(this.selectedProfile != null) {
        throw new RequestException("Cannot change game profile when it is already selected.");
    } else if(profile != null && this.profiles.contains(profile)) {
        RefreshRequest request = new RefreshRequest(this.clientToken, this.accessToken, profile);
        RefreshResponse response = HTTP.makeRequest(this.proxy, REFRESH_URL, request, RefreshResponse.class);
        if(response.clientToken.equals(this.clientToken)) {
            this.accessToken = response.accessToken;
            this.selectedProfile = response.selectedProfile;
        } else {
            throw new RequestException("Server requested we change our client token. Don't know how to handle this!");
        }
    } else {
        throw new IllegalArgumentException("Invalid profile '" + profile + "'.");
    }
}
 
Example #11
Source File: AuthenticationService.java    From Visage with MIT License 5 votes vote down vote up
private void loginWithPassword() throws RequestException {
    if(this.username == null || this.username.isEmpty()) {
        throw new InvalidCredentialsException("Invalid username.");
    } else if(this.password == null || this.password.isEmpty()) {
        throw new InvalidCredentialsException("Invalid password.");
    } else {
        AuthenticationRequest request = new AuthenticationRequest(this.username, this.password, this.clientToken);
        AuthenticationResponse response = HTTP.makeRequest(this.proxy, AUTHENTICATE_URL, request, AuthenticationResponse.class);
        if(response.clientToken.equals(this.clientToken)) {
            if(response.user != null && response.user.id != null) {
                this.id = response.user.id;
            } else {
                this.id = this.username;
            }

            this.loggedIn = true;
            this.accessToken = response.accessToken;
            this.profiles = response.availableProfiles != null ? Arrays.asList(response.availableProfiles) : Collections.<GameProfile>emptyList();
            this.selectedProfile = response.selectedProfile;
            this.properties.clear();
            if(response.user != null && response.user.properties != null) {
                this.properties.addAll(response.user.properties);
            }
        } else {
            throw new RequestException("Server requested we change our client token. Don't know how to handle this!");
        }
    }
}
 
Example #12
Source File: SessionService.java    From MCAuthLib with MIT License 5 votes vote down vote up
/**
 * Gets the profile of the given user if they are currently logged in to the given server.
 *
 * @param name     Name of the user to get the profile of.
 * @param serverId ID of the server to check if they're logged in to.
 * @return The profile of the given user, or null if they are not logged in to the given server.
 * @throws RequestException If an error occurs while making the request.
 */
public GameProfile getProfileByServer(String name, String serverId) throws RequestException {
    HasJoinedResponse response = HTTP.makeRequest(this.getProxy(), this.getEndpointUri(HAS_JOINED_ENDPOINT, "username=" + name + "&serverId=" + serverId), null, HasJoinedResponse.class);
    if(response != null && response.id != null) {
        GameProfile result = new GameProfile(response.id, name);
        result.setProperties(response.properties);
        return result;
    } else {
        return null;
    }
}
 
Example #13
Source File: HTTP.java    From Visage with MIT License 5 votes vote down vote up
/**
 * Makes an HTTP request.
 *
 * @param proxy Proxy to use when making the request.
 * @param url   URL to make the request to.
 * @param input Input to provide in the request.
 * @param clazz Class to provide the response as.
 * @param <T> Type to provide the response as.
 * @return The response of the request.
 * @throws RequestException If an error occurs while making the request.
 */
public static <T> T makeRequest(Proxy proxy, String url, Object input, Class<T> clazz) throws RequestException {
    JsonElement response = null;
    try {
        String jsonString = input == null ? performGetRequest(proxy, url) : performPostRequest(proxy, url, GSON.toJson(input), "application/json");
        response = GSON.fromJson(jsonString, JsonElement.class);
    } catch(Exception e) {
        throw new ServiceUnavailableException("Could not make request to '" + url + "'.", e);
    }

    if(response != null) {
        if(response.isJsonObject()) {
            JsonObject object = response.getAsJsonObject();
            if(object.has("error")) {
                String error = object.get("error").getAsString();
                String cause = object.has("cause") ? object.get("cause").getAsString() : "";
                String errorMessage = object.has("errorMessage") ? object.get("errorMessage").getAsString() : "";
                if(!error.equals("")) {
                    if(error.equals("ForbiddenOperationException")) {
                        if(cause != null && cause.equals("UserMigratedException")) {
                            throw new UserMigratedException(errorMessage);
                        } else {
                            throw new InvalidCredentialsException(errorMessage);
                        }
                    } else {
                        throw new RequestException(errorMessage);
                    }
                }
            }
        }

        if(clazz != null) {
            return GSON.fromJson(response, clazz);
        }
    }

    return null;
}
 
Example #14
Source File: AuthenticationService.java    From Visage with MIT License 5 votes vote down vote up
private void loginWithToken() throws RequestException {
    if(this.id == null || this.id.isEmpty()) {
        if(this.username == null || this.username.isEmpty()) {
            throw new InvalidCredentialsException("Invalid uuid and username.");
        }

        this.id = this.username;
    }

    if(this.accessToken == null || this.accessToken.equals("")) {
        throw new InvalidCredentialsException("Invalid access token.");
    } else {
        RefreshRequest request = new RefreshRequest(this.clientToken, this.accessToken, null);
        RefreshResponse response = HTTP.makeRequest(this.proxy, REFRESH_URL, request, RefreshResponse.class);
        if(response.clientToken.equals(this.clientToken)) {
            if(response.user != null && response.user.id != null) {
                this.id = response.user.id;
            } else {
                this.id = this.username;
            }

            this.loggedIn = true;
            this.accessToken = response.accessToken;
            this.profiles = response.availableProfiles != null ? Arrays.asList(response.availableProfiles) : Collections.<GameProfile>emptyList();
            this.selectedProfile = response.selectedProfile;
            this.properties.clear();
            if(response.user != null && response.user.properties != null) {
                this.properties.addAll(response.user.properties);
            }
        } else {
            throw new RequestException("Server requested we change our client token. Don't know how to handle this!");
        }
    }
}
 
Example #15
Source File: SessionService.java    From Visage with MIT License 5 votes vote down vote up
/**
 * Gets the profile of the given user if they are currently logged in to the given server.
 *
 * @param name     Name of the user to get the profile of.
 * @param serverId ID of the server to check if they're logged in to.
 * @return The profile of the given user, or null if they are not logged in to the given server.
 * @throws RequestException If an error occurs while making the request.
 */
public GameProfile getProfileByServer(String name, String serverId) throws RequestException {
    HasJoinedResponse response = HTTP.makeRequest(this.proxy, BASE_URL+"/hasJoined" + "?username=" + name + "&serverId=" + serverId, null, HasJoinedResponse.class);
    if(response != null && response.id != null) {
        GameProfile result = new GameProfile(response.id, name);
        if(response.properties != null) {
            result.getProperties().addAll(response.properties);
        }

        return result;
    } else {
        return null;
    }
}
 
Example #16
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String password) throws RequestException {
    super(username, password);
}
 
Example #17
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String using, boolean token, Proxy authProxy) throws RequestException {
    super(username, using, token, authProxy);
}
 
Example #18
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String using, boolean token) throws RequestException {
    super(username, using, token);
}
 
Example #19
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String password) throws RequestException {
    super(username, password);
}
 
Example #20
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String using, boolean token, Proxy authProxy) throws RequestException {
    super(username, using, token, authProxy);
}
 
Example #21
Source File: ProtocolWrapper.java    From LambdaAttack with MIT License 4 votes vote down vote up
public ProtocolWrapper(String username, String using, boolean token) throws RequestException {
    super(username, using, token);
}
 
Example #22
Source File: HTTP.java    From Visage with MIT License 2 votes vote down vote up
/**
 * Makes an HTTP request.
 *
 * @param proxy Proxy to use when making the request.
 * @param url   URL to make the request to.
 * @param input Input to provide in the request.
 * @throws RequestException If an error occurs while making the request.
 */
public static void makeRequest(Proxy proxy, String url, Object input) throws RequestException {
    makeRequest(proxy, url, input, null);
}
 
Example #23
Source File: SessionService.java    From MCAuthLib with MIT License 2 votes vote down vote up
/**
 * Joins a server.
 *
 * @param profile             Profile to join the server with.
 * @param authenticationToken Authentication token to join the server with.
 * @param serverId            ID of the server to join.
 * @throws RequestException If an error occurs while making the request.
 */
public void joinServer(GameProfile profile, String authenticationToken, String serverId) throws RequestException {
    JoinServerRequest request = new JoinServerRequest(authenticationToken, profile.getId(), serverId);
    HTTP.makeRequest(this.getProxy(), this.getEndpointUri(JOIN_ENDPOINT), request, null);
}
 
Example #24
Source File: SessionService.java    From Visage with MIT License 2 votes vote down vote up
/**
 * Joins a server.
 *
 * @param profile             Profile to join the server with.
 * @param authenticationToken Authentication token to join the server with.
 * @param serverId            ID of the server to join.
 * @throws RequestException If an error occurs while making the request.
 */
public void joinServer(GameProfile profile, String authenticationToken, String serverId) throws RequestException {
    JoinServerRequest request = new JoinServerRequest(authenticationToken, profile.getId(), serverId);
    HTTP.makeRequest(this.proxy, BASE_URL+"/join", request, null);
}
 
Example #25
Source File: HTTP.java    From MCAuthLib with MIT License 2 votes vote down vote up
/**
 * Makes an HTTP request.
 *
 * @param proxy Proxy to use when making the request.
 * @param uri   URI to make the request to.
 * @param input Input to provide in the request.
 * @throws IllegalArgumentException If the given proxy or URI is null.
 * @throws RequestException If an error occurs while making the request.
 */
public static void makeRequest(Proxy proxy, URI uri, Object input) throws RequestException {
    makeRequest(proxy, uri, input, null);
}