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.");
}
}
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);
}
}
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;
}
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);
}
}
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();
}
}
Aggregations