public void Parse()
        {
            var item = GitHubResponse.Parse("access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer");

            Assert.AreEqual <string>("e72e16c7e42f292c6912e7710c838347ae178b4a", item.AccessToken);
            Assert.AreEqual <string>("bearer", item.TokenType);
        }
 public void ParseDataInvalid()
 {
     GitHubResponse.Parse(StringHelper.NullEmptyWhiteSpace());
 }
        /// <summary>
        /// GitHub Auth API
        /// </summary>
        /// <remarks>
        /// GET: /Auth/GitHub
        /// </remarks>
        /// <returns></returns>
        public ActionResult GitHub()
        {
            var code = Request.Params["code"];

            if (!string.IsNullOrWhiteSpace(code))
            {
                try
                {
                    string responseData = null;
                    using (var client = new WebClient())
                    {
                        var nameValuePairs = new NameValueCollection();
                        nameValuePairs.Add("code", code);
                        nameValuePairs.Add("client_secret", ServerConfiguration.GitHubSecret);
                        nameValuePairs.Add("client_id", ServerConfiguration.GitHubClientId);
                        client.Headers.Add("content-type", "application/x-www-form-urlencoded");
                        var response = client.UploadValues(GitHubAuthorizationPost, nameValuePairs);
                        responseData = client.Encoding.GetString(response);
                    }

                    if (!string.IsNullOrWhiteSpace(responseData))
                    {
                        var    gitHubResponse = GitHubResponse.Parse(responseData);
                        string profileJson    = null;
                        using (var client = new WebClient())
                        {
                            profileJson = client.DownloadString(string.Format(GitHubApi, "user", gitHubResponse.AccessToken));
                        }

                        if (!string.IsNullOrWhiteSpace(profileJson))
                        {
                            var serializer = new JavaScriptSerializer();

                            var profile = serializer.Deserialize <GitHubProfile>(profileJson);
                            if (this.Login(profile)) //New User
                            {
                                var source     = new DomainSource();
                                var user       = source.GetUserByNameIdentifier(Application.Default.Identifier, string.Format("github{0}", profile.Id));
                                var preference = profile.Convert();
                                preference.Application = Application.Default;
                                preference.User        = user.Convert();
                                var core = new UserCore();
                                core.Save(preference);
                                var profilePage = ((IConvert <ProfilePage>)profile).Convert();
                                profilePage.ApplicationIdentifier = Application.Default.Identifier;
                                profilePage.OwnerIdentifier       = user.Id;
                                profilePage.GitCode        = code;
                                profilePage.GitAccessToken = gitHubResponse.AccessToken;
                                core.Save(profilePage);
                                return(this.RedirectToAction("Welcome", "Home"));
                            }
                            else
                            {
                                return(this.RedirectToAction("Index", "Profile", new { username = profile.Login }));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Log(ex, Abc.Services.Contracts.EventTypes.Critical, (int)ServiceFault.Unknown);
                }
            }

            return(this.RedirectToAction("Index", "Auth"));
        }