Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.skinseller.bot;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.CookieStore;
- import org.apache.http.cookie.Cookie;
- import org.apache.http.impl.client.BasicCookieStore;
- import org.apache.http.impl.cookie.BasicClientCookie;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.nio.charset.StandardCharsets;
- import java.security.InvalidKeyException;
- import java.security.KeyFactory;
- import java.security.NoSuchAlgorithmException;
- import java.security.PublicKey;
- import java.security.spec.InvalidKeySpecException;
- import java.security.spec.RSAPublicKeySpec;
- import java.time.Instant;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- * Created by Denis on 02.01.2016.
- */
- public class UserLogin {
- private String userName;
- private String password;
- private long steamID;
- private boolean requiresCaptcha;
- private String captchaGID = null;
- private String captchaText = null;
- private boolean requiresEmail;
- private String emailDomain = null;
- private String emailCode = null;
- private boolean requires2FA;
- private String twoFactorCode = null;
- private SessionData session = null;
- private boolean loggedIn = false;
- private CookieStore cookies = new BasicCookieStore();
- public UserLogin(String userName, String password) {
- this.userName = userName;
- this.password = password;
- }
- public LoginResult doLogin() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException {
- if (cookies.getCookies().size() == 0) {
- cookies.addCookie(createCookie("mobileClientVersion", "0 (2.1.3)", "/", ".steamcommunity.com"));
- cookies.addCookie(createCookie("mobileClient", "android", "/", ".steamcommunity.com"));
- cookies.addCookie(createCookie("Steam_Language", "english", "/", ".steamcommunity.com"));
- List<NameValuePair> headers = new ArrayList<>();
- headers.add(new BasicNameValuePair("X-Requested-With", "com.valvesoftware.android.steam.community"));
- SteamWeb.mobileLoginRequest("https://steamcommunity.com/login?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client", "GET", Collections.emptyList(), cookies, headers);
- }
- List<NameValuePair> postData = new ArrayList<>();
- postData.add(new BasicNameValuePair("username", this.userName));
- String response = SteamWeb.mobileLoginRequest(APIEndpoints.COMMUNITY_BASE + "/login/getrsakey", "POST", postData, cookies, Collections.emptyList());
- if (response == null || response.contains("<BODY>\nAn error occurred while processing your request.")) {
- return LoginResult.GeneralFailure;
- }
- RSAResponse rsaResponse = RSAResponse.parseRSA(response);
- if (rsaResponse == null || !rsaResponse.success) {
- return LoginResult.BadRSA;
- }
- String encryptedPassword = encryptPassword(rsaResponse);
- postData.clear();
- postData.add(new BasicNameValuePair("username", userName));
- postData.add(new BasicNameValuePair("password", encryptedPassword));
- postData.add(new BasicNameValuePair("twofactorcode", twoFactorCode != null ? twoFactorCode : ""));
- postData.add(new BasicNameValuePair("captchagid", requiresCaptcha ? captchaGID : "-1"));
- postData.add(new BasicNameValuePair("captcha_text", requiresCaptcha ? captchaText : ""));
- postData.add(new BasicNameValuePair("emailsteamid", (requires2FA || requiresEmail) ? Long.toString(steamID) : ""));
- postData.add(new BasicNameValuePair("emailauth", requiresEmail ? emailCode : ""));
- postData.add(new BasicNameValuePair("rsatimestamp", rsaResponse.timestamp));
- postData.add(new BasicNameValuePair("remember_login", "false"));
- postData.add(new BasicNameValuePair("oauth_client_id", "DE45CD61"));
- postData.add(new BasicNameValuePair("oauth_scope", "read_profile write_profile read_client write_client"));
- postData.add(new BasicNameValuePair("loginfriendlyname", "#login_emailauth_friendlyname_mobile"));
- postData.add(new BasicNameValuePair("donotcache", Long.toString(Instant.now().getEpochSecond())));
- response = SteamWeb.mobileLoginRequest(APIEndpoints.COMMUNITY_BASE + "/login/dologin", "POST", postData, cookies, Collections.emptyList());
- if (response == null) {
- return LoginResult.GeneralFailure;
- }
- LoginResponse loginResponse = LoginResponse.parseLoginResponse(response);
- if (loginResponse != null && loginResponse.message != null && loginResponse.message.contains("Incorrect login")) {
- return LoginResult.BadCredentials;
- }
- if (loginResponse.captchaNeeded) {
- this.requiresCaptcha = true;
- this.captchaGID = loginResponse.captchaGID;
- return LoginResult.NeedCaptcha;
- }
- if (loginResponse.emailAuthNeeded) {
- this.requiresEmail = true;
- this.steamID = loginResponse.emailSteamID;
- return LoginResult.NeedEmail;
- }
- if (loginResponse.twoFactorNeeded && !loginResponse.success) {
- this.requires2FA = true;
- return LoginResult.Need2FA;
- }
- if (loginResponse.message != null && loginResponse.message.contains("too many login failures")) {
- return LoginResult.TooManyFailedLogins;
- }
- if (loginResponse.getOAuthData() == null || loginResponse.getOAuthData().oAuthToken == null || loginResponse.getOAuthData().oAuthToken.length() == 0) {
- return LoginResult.GeneralFailure;
- }
- if (!loginResponse.loginComplete) {
- return LoginResult.BadCredentials;
- } else {
- OAuth oAuthData = loginResponse.getOAuthData();
- SessionData session = new SessionData();
- session.setoAuthToken(oAuthData.oAuthToken);
- session.setSteamID(oAuthData.steamID);
- session.setSteamLogin(session.getSteamID() + "%7C%7C" + oAuthData.steamLogin);
- session.setSteamLoginSecure(session.getSteamID() + "%7C%7C" + oAuthData.steamLoginSecure);
- session.setWebCookie(oAuthData.webCookie);
- Cookie httpCookie = cookies
- .getCookies()
- .stream()
- .filter(cookie -> cookie.getName().equals("sessionId"))
- .findFirst().orElse(null);
- session.setSessionID(httpCookie.getValue());
- this.session = session;
- loggedIn = true;
- return LoginResult.LoginOkay;
- }
- }
- private String encryptPassword(RSAResponse rsaResponse) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
- byte[] passwordBytes = password.getBytes(StandardCharsets.US_ASCII);
- BigInteger modulus = new BigInteger(1, hexStringToByteArray(rsaResponse.modulus));
- BigInteger publicExponent = new BigInteger(1, hexStringToByteArray(rsaResponse.exponent));
- Cipher cipher;
- //RSA config
- RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, publicExponent);
- KeyFactory fact = KeyFactory.getInstance("RSA");
- PublicKey pubKey = fact.generatePublic(rsaPubKey);
- cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
- cipher.init(Cipher.ENCRYPT_MODE, pubKey);
- byte[] cipherData = cipher.doFinal(passwordBytes);
- return Base64.encodeBase64String(cipherData);
- }
- private byte[] hexStringToByteArray(String s) {
- int len = s.length();
- byte[] data = new byte[len / 2];
- for (int i = 0; i < len; i += 2) {
- data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
- + Character.digit(s.charAt(i + 1), 16));
- }
- return data;
- }
- private BasicClientCookie createCookie(String name, String value, String path, String domain) {
- BasicClientCookie cookie = new BasicClientCookie(name, value);
- cookie.setPath(path);
- cookie.setDomain(domain);
- return cookie;
- }
- public enum LoginResult {
- LoginOkay,
- GeneralFailure,
- BadRSA,
- BadCredentials,
- NeedCaptcha,
- Need2FA,
- NeedEmail,
- TooManyFailedLogins
- }
- static class RSAResponse {
- public boolean success;
- public String exponent;
- public String modulus;
- public String timestamp;
- public String steamID;
- private RSAResponse() {
- }
- public static RSAResponse parseRSA(String rsa) {
- RSAResponse rsaResponse = new RSAResponse();
- JSONParser jsonParser = new JSONParser();
- JSONObject jsonObject = null;
- try {
- jsonObject = (JSONObject) jsonParser.parse(rsa);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if (jsonObject == null) {
- return null;
- }
- rsaResponse.success = (Boolean) jsonObject.get("success");
- rsaResponse.exponent = (String) jsonObject.get("publickey_exp");
- rsaResponse.modulus = (String) jsonObject.get("publickey_mod");
- rsaResponse.timestamp = (String) jsonObject.get("timestamp");
- rsaResponse.steamID = (String) jsonObject.get("token_gid"); //TODO can be mistake
- return rsaResponse;
- }
- }
- static class LoginResponse {
- public boolean success;
- public boolean loginComplete;
- private String oAuthDataString;
- public boolean captchaNeeded;
- public String captchaGID;
- public long emailSteamID;
- public boolean emailAuthNeeded;
- public boolean twoFactorNeeded;
- public String message;
- private LoginResponse() {
- }
- public static LoginResponse parseLoginResponse(String loginResponseStr) {
- LoginResponse loginResponse = new LoginResponse();
- JSONParser jsonParser = new JSONParser();
- JSONObject jsonObject = null;
- try {
- jsonObject = (JSONObject) jsonParser.parse(loginResponseStr);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if (jsonObject == null) {
- return null;
- }
- loginResponse.success = (Boolean) jsonObject.get("success");
- if (jsonObject.get("login_complete") != null) {
- loginResponse.loginComplete = (Boolean) jsonObject.get("login_complete");
- }
- loginResponse.oAuthDataString = (String) jsonObject.get("oauth");
- if (jsonObject.get("captcha_needed") != null) {
- loginResponse.captchaNeeded = (Boolean) jsonObject.get("captcha_needed");
- }
- loginResponse.captchaGID = jsonObject.get("captcha_gid").toString();
- if (jsonObject.get("emailsteamid") != null) {
- loginResponse.emailSteamID = Long.parseLong((String) jsonObject.get("emailsteamid"));
- }
- if (jsonObject.get("emailauth_needed") != null) {
- loginResponse.emailAuthNeeded = (Boolean) jsonObject.get("emailauth_needed");
- }
- loginResponse.twoFactorNeeded = (Boolean) jsonObject.get("requires_twofactor");
- loginResponse.message = (String) jsonObject.get("message");
- return loginResponse;
- }
- public OAuth getOAuthData() {
- return oAuthDataString != null ? OAuth.parseOAuth(oAuthDataString) : null;
- }
- }
- static class OAuth {
- public long steamID;
- public String oAuthToken;
- public String steamLogin;
- public String steamLoginSecure;
- public String webCookie;
- private OAuth() {
- }
- public static OAuth parseOAuth(String oAuthStr) {
- OAuth oAuth = new OAuth();
- JSONParser jsonParser = new JSONParser();
- JSONObject jsonObject = null;
- try {
- jsonObject = (JSONObject) jsonParser.parse(oAuthStr);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- if (jsonObject == null) {
- return null;
- }
- oAuth.steamID = Long.parseLong(jsonObject.get("steamid").toString());
- oAuth.oAuthToken = (String) jsonObject.get("oauth_token");
- oAuth.steamLogin = (String) jsonObject.get("wgtoken");
- oAuth.steamLogin = (String) jsonObject.get("wgtoken");
- oAuth.steamLoginSecure = (String) jsonObject.get("wgtoken_secure");
- oAuth.webCookie = (String) jsonObject.get("webcookie");
- return oAuth;
- }
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public long getSteamID() {
- return steamID;
- }
- public void setSteamID(long steamID) {
- this.steamID = steamID;
- }
- public boolean isRequiresCaptcha() {
- return requiresCaptcha;
- }
- public void setRequiresCaptcha(boolean requiresCaptcha) {
- this.requiresCaptcha = requiresCaptcha;
- }
- public String getCaptchaGID() {
- return captchaGID;
- }
- public void setCaptchaGID(String captchaGID) {
- this.captchaGID = captchaGID;
- }
- public String getCaptchaText() {
- return captchaText;
- }
- public void setCaptchaText(String captchaText) {
- this.captchaText = captchaText;
- }
- public boolean isRequiresEmail() {
- return requiresEmail;
- }
- public void setRequiresEmail(boolean requiresEmail) {
- this.requiresEmail = requiresEmail;
- }
- public String getEmailDomain() {
- return emailDomain;
- }
- public void setEmailDomain(String emailDomain) {
- this.emailDomain = emailDomain;
- }
- public String getEmailCode() {
- return emailCode;
- }
- public void setEmailCode(String emailCode) {
- this.emailCode = emailCode;
- }
- public boolean isRequires2FA() {
- return requires2FA;
- }
- public void setRequires2FA(boolean requires2FA) {
- this.requires2FA = requires2FA;
- }
- public String getTwoFactorCode() {
- return twoFactorCode;
- }
- public void setTwoFactorCode(String twoFactorCode) {
- this.twoFactorCode = twoFactorCode;
- }
- public SessionData getSession() {
- return session;
- }
- public void setSession(SessionData session) {
- this.session = session;
- }
- public boolean isLoggedIn() {
- return loggedIn;
- }
- public void setLoggedIn(boolean loggedIn) {
- this.loggedIn = loggedIn;
- }
- public CookieStore getCookies() {
- return cookies;
- }
- public void setCookies(CookieStore cookies) {
- this.cookies = cookies;
- }
- }
Add Comment
Please, Sign In to add comment