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]; } 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.ClientId) + "&client_secret=" + Uri.EscapeDataString(Options.ClientSecret); //Set accept header to request details as a JSON Object _httpClient.DefaultRequestHeaders.Add("accept", "application/json"); HttpResponseMessage tokenResponse = await _httpClient.GetAsync(TokenEndpoint + "?" + tokenRequest, Request.CallCancelled); tokenResponse.EnsureSuccessStatusCode(); string text = await tokenResponse.Content.ReadAsStringAsync(); //Parsing the string to a JSON Object JObject jsonText = JObject.Parse(text); //Extracting the access token from the JSON Object JToken accessToken; jsonText.TryGetValue("access_token", out accessToken); //Set the expiration time 60 days (5183999 seconds) string expires = "5183999"; //As Github required to include a User Agent in all requests, set User Agent in the request header _httpClient.DefaultRequestHeaders.Add("user-agent", "OwinContrib Security Github"); HttpResponseMessage API_Response = await _httpClient.GetAsync( ApiEndpoint + Uri.EscapeDataString(accessToken.ToString()), Request.CallCancelled); API_Response.EnsureSuccessStatusCode(); text = await API_Response.Content.ReadAsStringAsync(); JObject user = JObject.Parse(text); var context = new GithubAuthenticatedContext(Context, user, accessToken.ToString(), expires); context.Identity = new ClaimsIdentity( Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); //Add context properties to the Claims Identity if (!string.IsNullOrEmpty(context.Id)) { context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Email)) { context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.UserName)) { context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Url)) { context.Identity.AddClaim(new Claim("urn:github:link", context.Url, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Login)) { context.Identity.AddClaim(new Claim("urn:github:login", context.Login, 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(GithubAuthenticatedContext context) { return(OnAuthenticated(context)); }
public virtual Task Authenticated(GithubAuthenticatedContext context) { return OnAuthenticated(context); }