Search in sources :

Example 1 with AuthenticationException

use of kml.exceptions.AuthenticationException in project Krothium-Launcher by DarkLBP.

the class MainFX method refresh.

/**
 * Refreshes user selected from the existing user list
 */
public final void refresh() {
    User selected = existingUsers.getSelectionModel().getSelectedItem();
    Authentication auth = kernel.getAuthentication();
    try {
        auth.setSelectedUser(selected);
        auth.refresh();
        kernel.saveProfiles();
        texturesLoaded = false;
        showLoginPrompt(false);
        fetchAds();
    } catch (AuthenticationException ex) {
        kernel.showAlert(Alert.AlertType.ERROR, Language.get(62), ex.getMessage());
        updateExistingUsers();
    }
}
Also used : User(kml.auth.user.User) AuthenticationException(kml.exceptions.AuthenticationException) Authentication(kml.auth.Authentication)

Example 2 with AuthenticationException

use of kml.exceptions.AuthenticationException in project Krothium-Launcher by DarkLBP.

the class MainFX method refreshSession.

/**
 * Refreshes latest session
 */
private void refreshSession() {
    console.print("Refreshing session...");
    Authentication a = kernel.getAuthentication();
    User u = a.getSelectedUser();
    try {
        if (u != null) {
            a.refresh();
            texturesLoaded = false;
            kernel.saveProfiles();
            console.print("Session refreshed.");
        } else {
            console.print("No user is selected.");
        }
    } catch (AuthenticationException ex) {
        if (u.getType() == UserType.KROTHIUM) {
            authKrothium.setSelected(true);
            username.setText(u.getUsername().replace("krothium://", ""));
        } else {
            authMojang.setSelected(true);
            username.setText(u.getUsername());
        }
        console.print("Couldn't refresh your session.");
    } finally {
        if (a.isAuthenticated()) {
            showLoginPrompt(false);
            fetchAds();
        } else {
            showLoginPrompt(true);
        }
    }
}
Also used : User(kml.auth.user.User) AuthenticationException(kml.exceptions.AuthenticationException) Authentication(kml.auth.Authentication)

Example 3 with AuthenticationException

use of kml.exceptions.AuthenticationException in project Krothium-Launcher by DarkLBP.

the class Authentication method authenticate.

/**
 * Performs an authenticate request to the server
 * @param username The username
 * @param password The password
 * @throws AuthenticationException If authentication failed
 */
public final void authenticate(String username, String password) throws AuthenticationException {
    JSONObject request = new JSONObject();
    JSONObject agent = new JSONObject();
    UserType type;
    agent.put("name", "Minecraft");
    agent.put("version", 1);
    request.put("agent", agent);
    String tmpUser;
    if (username.startsWith("krothium://")) {
        type = UserType.KROTHIUM;
        tmpUser = username.replace("krothium://", "");
    } else {
        type = UserType.MOJANG;
        tmpUser = username;
    }
    request.put("username", tmpUser);
    request.put("password", password);
    if (clientToken != null) {
        request.put("clientToken", clientToken);
    }
    request.put("requestUser", true);
    Map<String, String> postParams = new HashMap<>();
    postParams.put("Content-Type", "application/json; charset=utf-8");
    postParams.put("Content-Length", String.valueOf(request.toString().length()));
    String response;
    String authURL;
    if (type == UserType.MOJANG) {
        authURL = "https://" + mojangDomain + authenticatePath;
    } else {
        authURL = "https://" + krothiumDomain + authenticatePath;
    }
    try {
        response = Utils.sendPost(authURL, request.toString().getBytes(Charset.forName("UTF-8")), postParams);
    } catch (IOException ex) {
        console.print("Failed to send request to authentication server");
        ex.printStackTrace(console.getWriter());
        throw new AuthenticationException("Failed to send request to authentication server");
    }
    if (response.isEmpty()) {
        throw new AuthenticationException("Authentication server does not respond.");
    }
    JSONObject r;
    try {
        r = new JSONObject(response);
    } catch (JSONException ex) {
        throw new AuthenticationException("Failed to read authentication response.");
    }
    if (!r.has("error")) {
        try {
            String accessToken = r.getString("accessToken");
            String selectedProfile = r.getJSONObject("selectedProfile").getString("id");
            String userID = r.getJSONObject("user").getString("id");
            clientToken = r.getString("clientToken");
            ArrayList<UserProfile> userProfiles = new ArrayList<>();
            JSONArray uprofs = r.getJSONArray("availableProfiles");
            for (int i = 0; i < uprofs.length(); i++) {
                JSONObject prof = uprofs.getJSONObject(i);
                UserProfile up = new UserProfile(prof.getString("id"), prof.getString("name"));
                userProfiles.add(up);
            }
            User u = new User(userID, accessToken, username, type, userProfiles, selectedProfile);
            selectedAccount = u;
            authenticated = true;
            addUser(u);
        } catch (JSONException ex) {
            ex.printStackTrace(console.getWriter());
            throw new AuthenticationException("Authentication server replied wrongly.");
        }
    } else {
        authenticated = false;
        throwError(r);
    }
}
Also used : User(kml.auth.user.User) UserProfile(kml.auth.user.UserProfile) AuthenticationException(kml.exceptions.AuthenticationException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) UserType(kml.auth.user.UserType)

Example 4 with AuthenticationException

use of kml.exceptions.AuthenticationException in project Krothium-Launcher by DarkLBP.

the class Authentication method refresh.

/**
 * Performs a refresh request to the server
 * @throws AuthenticationException If the refresh failed
 */
public final void refresh() throws AuthenticationException, JSONException {
    if (selectedAccount == null) {
        throw new AuthenticationException("No user is selected.");
    }
    JSONObject request = new JSONObject();
    JSONObject agent = new JSONObject();
    User u = selectedAccount;
    agent.put("name", "Minecraft");
    agent.put("version", 1);
    request.put("agent", agent);
    request.put("accessToken", u.getAccessToken());
    request.put("clientToken", clientToken);
    request.put("requestUser", true);
    Map<String, String> postParams = new HashMap<>();
    postParams.put("Content-Type", "application/json; charset=utf-8");
    postParams.put("Content-Length", String.valueOf(request.toString().length()));
    String response;
    String refreshURL;
    if (u.getType() == UserType.MOJANG) {
        refreshURL = "https://" + mojangDomain + refreshPath;
    } else {
        refreshURL = "https://" + krothiumDomain + refreshPath;
    }
    try {
        response = Utils.sendPost(refreshURL, request.toString().getBytes(Charset.forName("UTF-8")), postParams);
    } catch (IOException ex) {
        Kernel.USE_LOCAL = true;
        authenticated = true;
        console.print("Authenticated locally.");
        return;
    }
    if (response.isEmpty()) {
        throw new AuthenticationException("Authentication server does not respond.");
    }
    JSONObject r;
    try {
        r = new JSONObject(response);
    } catch (JSONException ex) {
        throw new AuthenticationException("Failed to read authentication response.");
    }
    if (!r.has("error")) {
        try {
            clientToken = r.getString("clientToken");
            u.setAccessToken(r.getString("accessToken"));
            String selectedProfile = r.getJSONObject("selectedProfile").getString("id");
            u.setSelectedProfile(selectedProfile);
            authenticated = true;
        } catch (JSONException ex) {
            ex.printStackTrace(console.getWriter());
            throw new AuthenticationException("Authentication server replied wrongly.");
        }
    } else {
        authenticated = false;
        removeUser(selectedAccount);
        throwError(r);
    }
}
Also used : User(kml.auth.user.User) JSONObject(org.json.JSONObject) AuthenticationException(kml.exceptions.AuthenticationException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

User (kml.auth.user.User)4 AuthenticationException (kml.exceptions.AuthenticationException)4 IOException (java.io.IOException)2 Authentication (kml.auth.Authentication)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 UserProfile (kml.auth.user.UserProfile)1 UserType (kml.auth.user.UserType)1 JSONArray (org.json.JSONArray)1