Search in sources :

Example 6 with User

use of kml.auth.user.User in project Krothium-Launcher by DarkLBP.

the class Authentication method fetchUsers.

/**
 * Loads the users from launcher_profile.json
 */
public final void fetchUsers() {
    console.print("Loading user data.");
    JSONObject root = kernel.getLauncherProfiles();
    if (root != null) {
        String selectedUser = null;
        String selectedProfile = null;
        if (root.has("clientToken")) {
            clientToken = root.getString("clientToken");
        }
        if (root.has("selectedUser")) {
            Object selected = root.get("selectedUser");
            if (selected instanceof JSONObject) {
                JSONObject s = (JSONObject) selected;
                if (s.has("account")) {
                    selectedUser = s.getString("account");
                }
                if (s.has("profile")) {
                    selectedProfile = s.getString("profile");
                }
            } else {
                console.print("Legacy launcher_profiles.json found");
            }
        }
        if (root.has("authenticationDatabase")) {
            JSONObject users = root.getJSONObject("authenticationDatabase");
            Set s = users.keySet();
            for (Object value : s) {
                String userID = value.toString();
                JSONObject user = users.getJSONObject(userID);
                if (user.has("accessToken") && user.has("username") && user.has("profiles")) {
                    String username = user.getString("username");
                    UserType userType = username.startsWith("krothium://") ? UserType.KROTHIUM : UserType.MOJANG;
                    JSONObject profiles = user.getJSONObject("profiles");
                    Set profileSet = profiles.keySet();
                    if (profileSet.size() > 0) {
                        ArrayList<UserProfile> userProfiles = new ArrayList<>();
                        for (Object o : profileSet) {
                            String profileUUID = o.toString();
                            JSONObject profile = profiles.getJSONObject(profileUUID);
                            if (profile.has("displayName")) {
                                UserProfile up = new UserProfile(profileUUID, profile.getString("displayName"));
                                userProfiles.add(up);
                            }
                        }
                        User u;
                        if (userID.equalsIgnoreCase(selectedUser)) {
                            u = new User(userID, user.getString("accessToken"), username, userType, userProfiles, selectedProfile);
                            addUser(u);
                            setSelectedUser(u);
                        } else {
                            u = new User(userID, user.getString("accessToken"), username, userType, userProfiles, null);
                            addUser(u);
                        }
                    }
                }
            }
        }
    } else {
        console.print("No users to be loaded.");
    }
}
Also used : User(kml.auth.user.User) JSONObject(org.json.JSONObject) UserProfile(kml.auth.user.UserProfile) JSONObject(org.json.JSONObject) UserType(kml.auth.user.UserType)

Example 7 with User

use of kml.auth.user.User 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)

Example 8 with User

use of kml.auth.user.User in project Krothium-Launcher by DarkLBP.

the class Authentication method toJSON.

/**
 * Converts the user database to JSON
 * @return The user database in json format
 */
public final JSONObject toJSON() {
    JSONObject o = new JSONObject();
    o.put("clientToken", clientToken);
    if (!userDatabase.isEmpty()) {
        JSONObject db = new JSONObject();
        for (User u : userDatabase) {
            JSONObject user = new JSONObject();
            user.put("accessToken", u.getAccessToken());
            user.put("username", u.getUsername());
            JSONObject profile = new JSONObject();
            for (UserProfile up : u.getProfiles()) {
                JSONObject profileInfo = new JSONObject();
                profileInfo.put("displayName", u.getDisplayName());
                profile.put(up.getId(), profileInfo);
            }
            user.put("profiles", profile);
            db.put(u.getUserID(), user);
        }
        o.put("authenticationDatabase", db);
        JSONObject selectedUser = new JSONObject();
        if (selectedAccount != null) {
            selectedUser.put("account", selectedAccount.getUserID());
            selectedUser.put("profile", selectedAccount.getSelectedProfile());
        }
        o.put("selectedUser", selectedUser);
    }
    return o;
}
Also used : User(kml.auth.user.User) JSONObject(org.json.JSONObject) UserProfile(kml.auth.user.UserProfile)

Example 9 with User

use of kml.auth.user.User in project Krothium-Launcher by DarkLBP.

the class MainFX method updateExistingUsers.

/**
 * Updates the existing users list
 */
private void updateExistingUsers() {
    Authentication a = kernel.getAuthentication();
    if (!a.getUsers().isEmpty() && a.getSelectedUser() == null) {
        existingPanel.setVisible(true);
        existingPanel.setManaged(true);
        ObservableList<User> users = FXCollections.observableArrayList();
        Set<User> us = a.getUsers();
        users.addAll(us);
        existingUsers.setItems(users);
        existingUsers.getSelectionModel().select(0);
    } else {
        existingPanel.setVisible(false);
        existingPanel.setManaged(false);
    }
}
Also used : User(kml.auth.user.User) Authentication(kml.auth.Authentication)

Example 10 with User

use of kml.auth.user.User in project Krothium-Launcher by DarkLBP.

the class MainFX method logout.

/**
 * Logs out the selected user from the existing user list
 */
public final void logout() {
    User selected = existingUsers.getSelectionModel().getSelectedItem();
    int result = kernel.showAlert(Alert.AlertType.CONFIRMATION, null, Language.get(8));
    if (result == 1) {
        Authentication auth = kernel.getAuthentication();
        auth.removeUser(selected);
        kernel.saveProfiles();
        updateExistingUsers();
    }
}
Also used : User(kml.auth.user.User) Authentication(kml.auth.Authentication)

Aggregations

User (kml.auth.user.User)11 JSONObject (org.json.JSONObject)6 Authentication (kml.auth.Authentication)5 AuthenticationException (kml.exceptions.AuthenticationException)5 IOException (java.io.IOException)3 UserProfile (kml.auth.user.UserProfile)3 UserType (kml.auth.user.UserType)2 GameLauncherException (kml.exceptions.GameLauncherException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 FXMLLoader (javafx.fxml.FXMLLoader)1 Parent (javafx.scene.Parent)1 Scene (javafx.scene.Scene)1 Image (javafx.scene.image.Image)1 Stage (javafx.stage.Stage)1 DownloaderException (kml.exceptions.DownloaderException)1 Profile (kml.game.profile.Profile)1 Version (kml.game.version.Version)1