예제 #1
0
파일: Provider.cs 프로젝트: patricker/Pulse
        private void Authenticate(string username, string password)
        {
            //if we have a username/password and we aren't already authenticated then authenticate
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                using(HttpUtility.CookieAwareWebClient _client = new HttpUtility.CookieAwareWebClient(_cookies))
                {
                    //check if the user is already logged in (doh!)
                    try
                    {
                        var loginReg = @"<span class=""name"".*?" + username + "</span>";
                        string homepage = _client.DownloadString("http://wallbase.cc");
                        if (Regex.Match(homepage, loginReg, RegexOptions.IgnoreCase).Success)
                        {
                            return;
                        }
                    }
                    catch(Exception ex)
                    {
                        Log.Logger.Write(string.Format("There was an error trying to check for a pre-existing wallbase auth, ignoring it.  Exception details: {0}", ex.ToString()), Log.LoggerLevels.Errors);
                    }

                    try
                    {
                        //need to extract the cross-site request forgery token from the page
                        //<img.*src=""(?<img>.*(wallpaper.*\.(jpg|png)))""
                        var csrfRegex = new Regex(@"<input type=""hidden"" name=""csrf"" value=""(?<csrf>.*)"">");
                        var refWallbase64Regex = new Regex(@"<input type=""hidden"" name=""ref"" value=""(?<ref>.*)"">");

                        string loginPage = _client.DownloadString("http://wallbase.cc/user/login");
                        Match lpM = csrfRegex.Match(loginPage);
                        Match lpWallbaseInbase64 = refWallbase64Regex.Match(loginPage);

                        if (!lpM.Success) return;

                        var loginData = new NameValueCollection();
                        loginData.Add("csrf", lpM.Groups["csrf"].Value);
                        loginData.Add("ref", lpWallbaseInbase64.Groups["ref"].Value);

                        loginData.Add("username", username);
                        loginData.Add("password", password);

                        _client.Referrer = "http://wallbase.cc/user/login";
                        _client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");

                        byte[] result = _client.UploadValues(@"http://wallbase.cc/user/do_login", "POST", loginData);

                        //we do not need the response, all we need are the cookies
                        string response = System.Text.Encoding.UTF8.GetString(result);
                    }
                    catch (Exception ex)
                    {
                        throw new WallbaseAccessDeniedException("Wallbase authentication failed. Please verify your username and password.", ex);
                    }
                }
            }
        }