Search in sources :

Example 1 with UserProfile

use of kml.auth.user.UserProfile 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 2 with UserProfile

use of kml.auth.user.UserProfile 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 3 with UserProfile

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

Aggregations

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