Esempio n. 1
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            this.logger.WriteVerbose("Authenticate Core");

            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];
                }

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

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

                var tokenRequestParameters = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("client_id", Options.ClientId),
                    new KeyValuePair <string, string>("client_secret", Options.ClientSecret),
                    new KeyValuePair <string, string>("grant_type", "authorization_code"),
                    new KeyValuePair <string, string>("redirect_uri", GenerateRedirectUri()),
                    new KeyValuePair <string, string>("code", code),
                };

                FormUrlEncodedContent requestContent = new FormUrlEncodedContent(tokenRequestParameters);
                HttpResponseMessage   response       = await this.httpClient.PostAsync(TokenEndpoint, requestContent, Request.CallCancelled);

                response.EnsureSuccessStatusCode();
                string oauthTokenResponse = await response.Content.ReadAsStringAsync();

                JObject oauth2Token = JObject.Parse(oauthTokenResponse);

                string accessToken = oauth2Token["access_token"]["token"].Value <string>();
                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    logger.WriteWarning("Access token was not found");
                    return(new AuthenticationTicket(null, properties));
                }

                var context = new YammerAuthenticatedContext(Context, oauth2Token, accessToken);
                context.Identity = new ClaimsIdentity(
                    new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, context.Id, XmlSchemaString, Options.AuthenticationType),
                    new Claim(ClaimTypes.Name, context.Name, XmlSchemaString, Options.AuthenticationType),
                    new Claim("urn:yammer:id", context.Id, XmlSchemaString, Options.AuthenticationType),
                    new Claim("urn:yammer:name", context.Name, XmlSchemaString, Options.AuthenticationType),
                    new Claim("urn:yammer:networkname", context.NetworkName, XmlSchemaString, Options.AuthenticationType),
                    new Claim("urn:yammer:networkid", context.NetworkId.ToString(), XmlSchemaString, Options.AuthenticationType),
                    new Claim("urn:yammer:jobtitle", context.JobTitle, XmlSchemaString, Options.AuthenticationType)
                },
                    Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);

                if (!string.IsNullOrWhiteSpace(context.Email))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, Options.AuthenticationType));
                }

                await Options.Provider.Authenticated(context);

                context.Properties = properties;
                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                this.logger.WriteWarning("Authentication failed", ex);
                return(new AuthenticationTicket(null, properties));
            }
        }
 public virtual Task Authenticated(YammerAuthenticatedContext context)
 {
     return(this.OnAuthenticated(context));
 }