Beispiel #1
0
        public LoginType Login(string email, string password)
        {
            var loginType = LoginType.ERROR;

            Email    = email;
            Password = password;

            try {
                if (_authCodeService != null && _authCodeService.TryGetSid(Email, out var sidCookie))
                {
                    var newCookie = new System.Net.Cookie(sidCookie.Name, sidCookie.Value, sidCookie.Path);
                    Uri uri       = new Uri("https://accounts.ea.com/connect");
                    client.CookieContainer.Add(uri, newCookie);
                    loginType = LoginType.TOKEN;
                }
                else
                {
                    // Initializing
                    client.DownloadString("https://accounts.ea.com/connect/auth?response_type=code&client_id=ORIGIN_SPA_ID&display=originXWeb/login&locale=en_US&release_type=prod");
                    var authUrl = client.ResponseHeaders["Location"];
                    var fid     = authUrl.Split("fid=")[1];
                    client.DownloadString(authUrl);
                    var loginRequestUrl = $"https://signin.ea.com{client.ResponseHeaders["Location"]}";
                    client.DownloadString(loginRequestUrl); // Downloads the login page

                    // Login
                    var reqparm = new System.Collections.Specialized.NameValueCollection();
                    reqparm.Add("email", email);
                    reqparm.Add("password", password);
                    reqparm.Add("_eventId", "submit");
                    reqparm.Add("showAgeUp", "true");
                    reqparm.Add("googleCaptchaResponse", "");
                    reqparm.Add("_rememberMe", "on");
                    reqparm.Add("rememberMe", "on");

                    //reqparm.Add("pn_text", "");
                    //reqparm.Add("passwordForPhone", "");
                    //reqparm.Add("country", "US");
                    //reqparm.Add("phoneNumber", "");
                    //reqparm.Add("_eventId", "submit");
                    //reqparm.Add("gCaptchaResponse", "");
                    //reqparm.Add("isPhoneNumberLogin", "false");
                    //reqparm.Add("isIncompletePhone", "");
                    //reqparm.Add("countryPrefixPhoneNumber", "");
                    byte[] responsebytes = client.UploadValues(loginRequestUrl, "POST", reqparm);
                    string responsebody  = Encoding.UTF8.GetString(responsebytes);

                    if (!string.IsNullOrEmpty(responsebody))
                    {
                        var arr          = responsebody.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
                        var nextLocation = arr.FirstOrDefault(x => x.Contains("window.location"));
                        var startIndex   = nextLocation.IndexOf("\"");
                        loginRequestUrl = nextLocation.Substring(startIndex + 1, nextLocation.Length - (startIndex + 2));

                        // TODO: Check if these are even correct
                        var endOrChallenge = client.DownloadString(loginRequestUrl);
                        if (endOrChallenge.Contains("&_eventId=challenge"))
                        {
                            LoginVerification(loginRequestUrl);
                        }
                        else if (endOrChallenge.Contains("&_eventId=end"))
                        {
                            client.DownloadString(loginRequestUrl + "&_eventId=end");
                        }

                        // Finish login
                        client.DownloadString(client.ResponseHeaders["Location"]);
                    }
                    else
                    {
                        if (client.ResponseHeaders.AllKeys.Contains("Location"))
                        {
                            throw new Exception($"Rate limit exceeded: {email}");
                        }
                        client.DownloadString($"https://accounts.ea.com/connect/auth?response_type=code&client_id=ORIGIN_SPA_ID&display=originXWeb%2Flogin&locale=en_US&release_type=prod&fid={fid}");
                    }

                    var cookies = client.CookieContainer.GetCookies(new Uri("https://accounts.ea.com/connect"));
                    var sid     = cookies["sid"];
                    _authCodeService?.AddOrRefresh(email, sid);

                    loginType = LoginType.CREDENTIALS;
                }

                FetchOriginAccessToken();
                FetchCompanionAccessToken();
                FetchMyself();
            }
            catch (Exception e) {
                Debug.WriteLine($"Error: {e.Message}");
                loginType = LoginType.ERROR;
                throw new ApplicationException($"Incorrect email or password for {email}");
            }

            return(loginType);
        }