Advertisement
Guest User

Untitled

a guest
May 24th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package net.minecraft.util.com.mojang.authlib.legacy;
  2.  
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import net.minecraft.util.com.mojang.authlib.GameProfile;
  8. import net.minecraft.util.com.mojang.authlib.HttpAuthenticationService;
  9. import net.minecraft.util.com.mojang.authlib.HttpUserAuthentication;
  10. import net.minecraft.util.com.mojang.authlib.UserType;
  11. import net.minecraft.util.com.mojang.authlib.exceptions.AuthenticationException;
  12. import net.minecraft.util.com.mojang.authlib.exceptions.InvalidCredentialsException;
  13. import net.minecraft.util.com.mojang.util.UUIDTypeAdapter;
  14. import net.minecraft.util.org.apache.commons.lang3.StringUtils;
  15.  
  16. public class LegacyUserAuthentication extends HttpUserAuthentication
  17. {
  18.   private static final URL AUTHENTICATION_URL = HttpAuthenticationService.constantURL("https://login.minecraft.net");
  19.   private static final int AUTHENTICATION_VERSION = 14;
  20.   private static final int RESPONSE_PART_PROFILE_NAME = 2;
  21.   private static final int RESPONSE_PART_SESSION_TOKEN = 3;
  22.   private static final int RESPONSE_PART_PROFILE_ID = 4;
  23.   private String sessionToken;
  24.  
  25.   protected LegacyUserAuthentication(LegacyAuthenticationService authenticationService)
  26.   {
  27.     super(authenticationService);
  28.   }
  29.  
  30.   public void logIn() throws AuthenticationException
  31.   {
  32.     if (StringUtils.isBlank(getUsername())) {
  33.       throw new InvalidCredentialsException("Invalid username");
  34.     }
  35.     if (StringUtils.isBlank(getPassword())) {
  36.       throw new InvalidCredentialsException("Invalid password"); }
  37. Map args = new HashMap();
  38.     args.put("user", getUsername());
  39.     args.put("password", getPassword());
  40.     args.put("version", Integer.valueOf(14));
  41.     String response;
  42.     try { response = getAuthenticationService().performPostRequest(AUTHENTICATION_URL, HttpAuthenticationService.buildQuery(args), "application/x-www-form-urlencoded").trim();
  43.     } catch (IOException e) {
  44.       throw new AuthenticationException("Authentication server is not responding", e);
  45.     }
  46.  
  47.     String[] split = response.split(":");
  48.  
  49.     if (split.length == 5) {
  50.       String profileId = split[4];
  51.       String profileName = split[2];
  52.       String sessionToken = split[3];
  53.  
  54.       if ((StringUtils.isBlank(profileId)) || (StringUtils.isBlank(profileName)) || (StringUtils.isBlank(sessionToken))) {
  55.         throw new AuthenticationException("Unknown response from authentication server: " + response);
  56.       }
  57.  
  58.       setSelectedProfile(new GameProfile(UUIDTypeAdapter.fromString(profileId), profileName));
  59.       this.sessionToken = sessionToken;
  60.       setUserType(UserType.LEGACY);
  61.     } else {
  62.       throw new InvalidCredentialsException(response);
  63.     }
  64.   }
  65.  
  66.   public void logOut()
  67.   {
  68.     super.logOut();
  69.     this.sessionToken = null;
  70.   }
  71.  
  72.   public boolean canPlayOnline()
  73.   {
  74.     return (isLoggedIn()) && (getSelectedProfile() != null) && (getAuthenticatedToken() != null);
  75.   }
  76.  
  77.   public GameProfile[] getAvailableProfiles()
  78.   {
  79.     if (getSelectedProfile() != null) {
  80.       return new GameProfile[] { getSelectedProfile() };
  81.     }
  82.     return new GameProfile[0];
  83.   }
  84.  
  85.   public void selectGameProfile(GameProfile profile)
  86.     throws AuthenticationException
  87.   {
  88.     throw new UnsupportedOperationException("Game profiles cannot be changed in the legacy authentication service");
  89.   }
  90.  
  91.   public String getAuthenticatedToken()
  92.   {
  93.     return this.sessionToken;
  94.   }
  95.  
  96.   public String getUserID()
  97.   {
  98.     return getUsername();
  99.   }
  100.  
  101.   public LegacyAuthenticationService getAuthenticationService()
  102.   {
  103.     return (LegacyAuthenticationService)super.getAuthenticationService();
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement