Ejemplo n.º 1
0
        private void ParseClaims(JObject user, OAuthAuthenticationParameters parameters)
        {
            var claims = new UserClaims();

            claims.Contact = new ContactClaims();
            IDictionary <string, JToken> userAsDictionary = user;
            string name = PropertyValueIfExists("nickname", userAsDictionary);

            claims.Name = new NameClaims();

            if (!name.IsEmpty())
            {
                var nameSplit = name.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (nameSplit.Length >= 2)
                {
                    claims.Name.First = nameSplit[0];
                    claims.Name.Last  = nameSplit[1];
                }
                else
                {
                    claims.Name.Last = nameSplit[0];
                }
            }
            parameters.AddClaim(claims);
        }
Ejemplo n.º 2
0
        private AuthorizeState VerifyAuthentication(string returnUrl)
        {
            var settings = _services.Settings.LoadSetting <QQExternalAuthSettings>(_services.SiteContext.CurrentSite.Id);

            string accessTokenUri = TokenEndpoint +
                                    "?client_id=" + Uri.EscapeDataString(settings.ClientKeyIdentifier) +
                                    "&client_secret=" + Uri.EscapeDataString(settings.ClientSecret) +
                                    "&redirect_uri=" + Uri.EscapeDataString(returnUrl) +
                                    "&code=test" +
                                    "&grant_type=authorization_code";
            string oauthTokenResponse = Get_Http(accessTokenUri, 120000);
            var    tokenDict          = QueryStringToDict(oauthTokenResponse);
            string accessToken        = null;

            if (tokenDict.ContainsKey("access_token"))
            {
                accessToken = tokenDict["access_token"];
            }
            else
            {
                throw new Exception("Authentication result does not contain accesstoken data");
            }
            if (!accessToken.IsEmail())
            {
                string openIDUri    = OpenIDEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken);
                string openIDString = Get_Http(openIDUri, 120000);
                openIDString = ExtractOpenIDCallbackBody(openIDString);
                JObject openIDInfo = JObject.Parse(openIDString);

                var clientId = openIDInfo["client_id"].Value <string>();
                var openId   = openIDInfo["openid"].Value <string>();

                string userInfoUri = UserInfoEndpoint +
                                     "?access_token=" + Uri.EscapeDataString(accessToken) +
                                     "&oauth_consumer_key=" + Uri.EscapeDataString(clientId) +
                                     "&openid=" + Uri.EscapeDataString(openId);

                string  userInfoString = Get_Http(userInfoUri, 120000);
                JObject userInfo       = JObject.Parse(userInfoString);

                var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier = settings.ClientKeyIdentifier,
                    OAuthToken         = accessToken,
                    OAuthAccessToken   = openId,
                };
                // userInfo["nickname"].Value<string>()
                if (_externalAuthenticationSettings.AutoRegisterEnabled)
                {
                    ParseClaims(userInfo, parameters);
                }

                var result = _authorizer.Authorize(parameters);

                return(new AuthorizeState(returnUrl, result));
            }

            var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);
            var error = "Unknown error";

            state.AddError(error);
            return(state);
        }