public static String toString(YggdrasilUserAuthentication y) {
   return "YggdrasilAuthenticationService{profiles="
       + Arrays.toString(y.getAvailableProfiles())
       + ", selectedProfile="
       + y.getSelectedProfile()
       + ", isLoggedIn="
       + y.isLoggedIn()
       + ", userType="
       + y.getUserType()
       + ", canPlayOnline="
       + y.canPlayOnline()
       + "}";
 }
 private static boolean isValid(YggdrasilUserAuthentication authentication) {
   boolean ret = true;
   if (!authentication.isLoggedIn()) {
     Logger.logDebug("authentication not valid");
     ret = false;
   }
   if (authentication.getAuthenticatedToken() == null) {
     Logger.logDebug("authentication not valid");
     ret = false;
   }
   if (authentication.getSelectedProfile() == null) {
     Logger.logDebug("authentication not valid");
     ret = false;
   }
   return ret;
 }
  protected static LoginResponse authenticateWithAuthlib(
      String user, String pass, String mojangData, String selectedProfileName) {
    String displayName;
    boolean hasMojangData = false;
    boolean hasPassword = false;
    GameProfile selectedProfile = null;
    YggdrasilUserAuthentication authentication =
        (YggdrasilUserAuthentication)
            new YggdrasilAuthenticationService(Proxy.NO_PROXY, "1")
                .createUserAuthentication(Agent.MINECRAFT);
    if (user != null) {
      Logger.logDebug(
          user.contains("@")
              ? "Email address given"
              : "Username given" + " Not 100% sure, mojangdata might contain different username");
      Logger.logInfo("Beginning authlib authentication attempt");
      Logger.logInfo("successfully created YggdrasilAuthenticationService");
      authentication.setUsername(user);
      if (pass != null && !pass.isEmpty()) {
        authentication.setPassword(pass);
        hasPassword = true;
      }
      if (mojangData != null && !mojangData.isEmpty()) {
        Logger.logDebug("mojangData was passed to current method");
        Map<String, Object> m = decode(mojangData);
        if (m != null) {
          Logger.logDebug("Loading mojangData into authlib");
          authentication.loadFromStorage(m);
          hasMojangData = true;
        }
      } else {
        Logger.logDebug("mojangData is null or empty");
      }
      if (authentication.canLogIn()) {
        try {
          authentication.logIn();
        } catch (UserMigratedException e) {
          Logger.logError(e.toString());
          ErrorUtils.tossError(
              "Invalid credentials, please make sure to login with your Mojang account.");
          return null;
        } catch (InvalidCredentialsException e) {
          Logger.logError("Invalid credentials recieved for user: "******"Invalid username or password.");
            return null;
          }
        } catch (AuthenticationUnavailableException e) {
          Logger.logDebug("Error while authenticating, trying offline mode");
          if (hasMojangData) {
            // if the UUID is valid we can proceed to offline mode later
            uniqueID = authentication.getSelectedProfile().getId().toString();
            if (uniqueID != null && !uniqueID.isEmpty()) Logger.logDebug("Setting UUID");
            UserManager.setUUID(user, uniqueID);
          }
          if (uniqueID != null && !uniqueID.isEmpty()) {
            UserManager.setUUID(user, uniqueID);
            Logger.logDebug("Setting UUID and creating and returning new LoginResponse");
            return new LoginResponse(
                Integer.toString(authentication.getAgent().getVersion()),
                "token",
                user,
                null,
                uniqueID,
                authentication);
          }
          ErrorUtils.tossError(
              "Exception occurred, minecraft servers might be down. Check @ help.mojang.com");
          Logger.logDebug("failed", e);
          Logger.logDebug("AuthenticationUnavailableException caused by", e.getCause());
          return null;
        } catch (AuthenticationException e) {
          Logger.logError("Unknown error from authlib:", e);
        } catch (Exception e) {
          Logger.logError("Unknown authentication error occurred", e);
        }
      } else {
        Logger.logDebug("authentication.canLogIn() returned false");
      }

      if (isValid(authentication)) {
        Logger.logDebug("Authentication is valid ");
        displayName = authentication.getSelectedProfile().getName();
        if ((authentication.isLoggedIn()) && (authentication.canPlayOnline())) {
          Logger.logDebug("loggedIn() && CanPlayOnline()");
          if ((authentication instanceof YggdrasilUserAuthentication)) {
            UserManager.setStore(user, encode(authentication.saveForStorage()));
            UserManager.setUUID(
                user,
                authentication
                    .getSelectedProfile()
                    .getId()
                    .toString()); // enables use of offline mode later if needed on newer MC
            // Versions
            Logger.logDebug("Authentication done, returning LoginResponse");
            return new LoginResponse(
                Integer.toString(authentication.getAgent().getVersion()),
                "token",
                displayName,
                authentication.getAuthenticatedToken(),
                authentication.getSelectedProfile().getId().toString(),
                authentication);
          }
        }
        Logger.logDebug(
            "this should never happen: isLoggedIn: "
                + authentication.isLoggedIn()
                + " canPlayOnline(): "
                + authentication.canPlayOnline());
      } else if (authentication.getSelectedProfile() == null
          && (authentication.getAvailableProfiles() != null
              && authentication.getAvailableProfiles().length != 0)) {
        // user has more than one profile
        Logger.logDebug("User has more than one profile: " + toString(authentication));
        for (GameProfile profile : authentication.getAvailableProfiles()) {
          if (selectedProfileName.equals(profile.getName())) {
            Logger.logInfo("Selected profile: " + profile.getName());
            selectedProfile = profile;
          }
        }
        if (selectedProfile == null) {
          Logger.logInfo("Profile not found, defaulting to first");
          selectedProfile = authentication.getAvailableProfiles()[0];
        }
        Logger.logDebug("Authentication done, returning LoginResponse");
        return new LoginResponse(
            Integer.toString(authentication.getAgent().getVersion()),
            "token",
            selectedProfile.getName(),
            authentication.getAuthenticatedToken(),
            selectedProfile.getId().toString(),
            authentication);
      } else if (authentication.getSelectedProfile() == null
          && (authentication.getAvailableProfiles() != null
              && authentication.getAvailableProfiles().length == 0)) {
        Logger.logDebug("No profiles in mojang account: " + toString(authentication));
        ErrorUtils.showClickableMessage(
            "You need to own minecraft to play FTB Modpacks",
            "https://help.mojang.com/customer/portal/articles/1218766-can-only-play-minecraft-demo");
        return null;
      } else {
        Logger.logDebug("this should never happen: " + toString(authentication));
      }

    } else {
      Logger.logDebug("this should never happen");
    }

    if (hasMojangData) {
      Logger.logError(
          "Failed to authenticate with mojang data, attempting to use username & password");
      if (!hasPassword) {
        new PasswordDialog(LaunchFrame.getInstance(), true).setVisible(true);
        if (LaunchFrame.tempPass.isEmpty()) return null;
        pass = LaunchFrame.tempPass;
      }

      LoginResponse l = authenticateWithAuthlib(user, pass, null, selectedProfileName);
      if (l == null) {
        Logger.logError("Failed to login with username & password");
        return null;
      } else {
        Logger.logDebug("authentication ready, returning LoginResponse from authlib");
        return l;
      }
    }
    Logger.logError("Failed to authenticate");
    return null;
  }