예제 #1
0
        public override void Login(string password, bool rememberSession)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            // todo: remember sessions
            MainForm.SetSignInStatus("Connecting to ClassiCube.net...");

            // download the login page
            string loginPage = DownloadString(LoginUri, ClassiCubeNet);

            Debug.WriteLine(loginPage);

            // See if we're already logged in
            if (LoggedInAs.IsMatch(loginPage))
            {
                // todo
            }

            Match csrfTokenMatch = CsrfToken.Match(loginPage);

            if (!csrfTokenMatch.Success)
            {
                // todo: handle ClassiCube derping
            }

            string csrfToken   = csrfTokenMatch.Groups[1].Value;
            string loginString = String.Format("csrf_token={0}&username={1}&password={2}",
                                               Uri.EscapeDataString(csrfToken),
                                               Uri.EscapeDataString(LoginUsername),
                                               Uri.EscapeDataString(password));

            if (rememberSession)
            {
                loginString += "&remember_me=y";
            }

            string loginResponse = UploadString(LoginUri, LoginUri, loginString);


            // See if now we're logged in
            if (loginResponse.Contains(LoginFailedMessage))
            {
                Status = LoginResult.WrongUsernameOrPass;
            }
            else if (LoggedInAs.IsMatch(loginResponse))
            {
                MinecraftUsername = LoggedInAs.Match(loginResponse).Groups[1].Value;
                // todo: get play session ID
                Status = LoginResult.Success;
                // todo: save cookie
            }
            else
            {
                MainForm.Log("CC.Login: Something went wrong: " + loginResponse);
                Status = LoginResult.UnrecognizedResponse;
            }
        }
        public override void Login(string password, bool rememberSession)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            bool restoredSession = LoadCookie(rememberSession);

            MainForm.SetSignInStatus("Connecting to Minecraft.net...");

            // check if cancel is needed
            if (cancel)
            {
                Status = LoginResult.Canceled;
                cancel = false;
                return;
            }

            // download the login page
            string loginPage = DownloadString(LoginSecureUri, MinecraftNet);

            // See if we're already logged in
            if (LoggedInAs.IsMatch(loginPage))
            {
                string loggedInUsername = LoggedInAs.Match(loginPage).Groups[1].Value;
                if (rememberSession && PlaySessionCookie != null &&
                    MinecraftUsername.Equals(loggedInUsername, StringComparison.OrdinalIgnoreCase))
                {
                    // If player is already logged in with the right account: reuse a previous session
                    MinecraftUsername = loggedInUsername;
                    MainForm.Log("MC.Login: Restored session for " + MinecraftUsername);
                    Status = LoginResult.Success;
                    SaveCookie();
                    return;
                }
                else
                {
                    // If we're not supposed to reuse session, if old username is different,
                    // or if there is no play session cookie set - relog
                    MainForm.SetSignInStatus("Switching accounts...");
                    DownloadString(LogoutUri, MinecraftNet);
                    loginPage = DownloadString(LoginSecureUri, LogoutUri);
                }
            }

            // Extract authenticityToken from the login page
            Match authTokenMatch = LoginAuthToken.Match(loginPage);

            if (!authTokenMatch.Success)
            {
                if (restoredSession)
                {
                    // restoring session failed; log out and retry
                    DownloadString(LogoutUri, MinecraftNet);
                    ResetCookies();
                    MainForm.Log("MC.Login: Unrecognized page; retrying");
                    Login(password, rememberSession);
                    return;
                }
                else
                {
                    // something unexpected happened, panic!
                    MainForm.Log("MC.Login: Unrecognized page: " + loginPage);
                    Status = LoginResult.UnrecognizedResponse;
                }
                return;
            }
            string authToken = authTokenMatch.Groups[1].Value;

            // Build up form data
            string loginString = String.Format("username={0}&password={1}&authenticityToken={2}",
                                               Uri.EscapeDataString(LoginUsername),
                                               Uri.EscapeDataString(password),
                                               Uri.EscapeDataString(authToken));

            if (rememberSession)
            {
                loginString += "&remember=true";
            }

            // check if cancel is needed
            if (cancel)
            {
                Status = LoginResult.Canceled;
                cancel = false;
                return;
            }

            // POST to the login form
            MainForm.SetSignInStatus("Sending login information...");
            string loginResponse = UploadString(LoginSecureUri, LoginSecureUri, loginString);

            // check if cancel is needed
            if (cancel)
            {
                Status = LoginResult.Canceled;
                cancel = false;
                return;
            }

            // Check the response
            if (loginResponse.Contains(WrongUsernameOrPasswordMessage))
            {
                Status = LoginResult.WrongUsernameOrPass;
            }
            else if (LoggedInAs.IsMatch(loginResponse))
            {
                MinecraftUsername = LoggedInAs.Match(loginResponse).Groups[1].Value;
                if (PlaySessionCookie == null)
                {
                    CookieCollection cookies = CookieJar.GetCookies(new Uri(MinecraftNet));
                    MainForm.Log("MC.Login: No play session. There were " + cookies.Count + " cookies served:");
                    foreach (Cookie cookie in cookies)
                    {
                        MainForm.Log("  " + cookie);
                    }
                    Status = LoginResult.NoPlaySession;
                    return;
                }
                Status = LoginResult.Success;
                SaveCookie();
            }
            else if (loginResponse.Contains(MigratedAccountMessage))
            {
                Status = LoginResult.MigratedAccount;
            }
            else
            {
                MainForm.Log("MC.Login: Unrecognized response: " + loginResponse);
                Status = LoginResult.UnrecognizedResponse;
            }
        }