public virtual Task Authenticated(MeetupAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
Exemple #2
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string code  = null;
                string state = null;

                IReadableStringCollection query  = Request.Query;
                IList <string>            values = query.GetValues("code");
                if (values != null && values.Count == 1)
                {
                    code = values[0];
                }
                values = query.GetValues("state");
                if (values != null && values.Count == 1)
                {
                    state = values[0];
                }

                //Meetup replaces '_' charactors with spaces in the 'state' parameter when returns.
                //Resetting it back to it's original string
                state = state.Replace(' ', '_');

                properties = Options.StateDataFormat.Unprotect(state);
                if (properties == null)
                {
                    return(null);
                }

                // OAuth2 10.12 CSRF
                if (!ValidateCorrelationId(properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                string requestPrefix = Request.Scheme + "://" + Request.Host;
                string redirectUri   = requestPrefix + Request.PathBase + Options.CallbackPath;

                string tokenRequest = "grant_type=authorization_code" +
                                      "&code=" + Uri.EscapeDataString(code) +
                                      "&redirect_uri=" + Uri.EscapeDataString(redirectUri) +
                                      "&client_id=" + Uri.EscapeDataString(Options.Key) +
                                      "&client_secret=" + Uri.EscapeDataString(Options.Secret);

                //Meetup API requires to send the token request as a POST message.
                //Initiated a new HttpRequestMessage object to use as a parameter for PostAsync method.
                HttpRequestMessage  request       = new HttpRequestMessage();
                HttpResponseMessage tokenResponse = await _httpClient.PostAsync(TokenEndpoint + "?" + tokenRequest, request.Content, Request.CallCancelled);

                tokenResponse.EnsureSuccessStatusCode();
                string text = await tokenResponse.Content.ReadAsStringAsync();

                //Object is returned as a JSON Object.
                //Parsing the string contains JSON text to a JSON Object.
                JObject details = JObject.Parse(text);

                string accessToken = null;
                string expires     = null;

                foreach (var x in details)
                {
                    if (x.Key == "access_token")
                    {
                        accessToken = (string)x.Value;
                    }

                    if (x.Key == "expires_in")
                    {
                        expires = (string)x.Value;
                    }
                }

                HttpResponseMessage apiResponse = await _httpClient.GetAsync(
                    ApiEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken) +
                    "&member_id=self" + "&key=" + Uri.EscapeDataString(Options.Key), Request.CallCancelled);

                apiResponse.EnsureSuccessStatusCode();

                text = await apiResponse.Content.ReadAsStringAsync();

                //Convert JSON string to a JSON Object
                JObject info = JObject.Parse(text);

                //Extract the 'results' JToken from info
                JToken  results  = info["results"];
                JToken  userinfo = results.First;
                JObject user     = userinfo as JObject;

                var context = new MeetupAuthenticatedContext(Context, user, accessToken, expires);
                context.Identity = new ClaimsIdentity(
                    Options.AuthenticationType,
                    ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType);
                if (!string.IsNullOrEmpty(context.Id))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Name))
                {
                    context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.Name, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.City))
                {
                    context.Identity.AddClaim(new Claim("urn:meetup:city", context.City, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Country))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Country, context.Country, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Link))
                {
                    context.Identity.AddClaim(new Claim("urn:meetup:link", context.Link, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Joined))
                {
                    context.Identity.AddClaim(new Claim("urn:meetup:joined", context.Joined, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.PhotoUrl))
                {
                    context.Identity.AddClaim(new Claim("urn:meetup:photourl", context.PhotoUrl, XmlSchemaString, Options.AuthenticationType));
                }
                context.Properties = properties;

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex.Message);
            }
            return(new AuthenticationTicket(null, properties));
        }
 public virtual Task Authenticated(MeetupAuthenticatedContext context)
 {
     return OnAuthenticated(context);
 }