protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { string state = null; string code = null; IReadableStringCollection query = Request.Query; IList <string> values; values = query.GetValues("state"); if (values != null && values.Count == 1) { state = values[0]; } properties = Options.StateDataFormat.Unprotect(state); if (properties == null) { return(null); } values = query.GetValues("error"); if (values != null && values.Count == 1) { return(new AuthenticationTicket(null, properties)); } values = query.GetValues("code"); if (values != null && values.Count == 1) { code = values[0]; } // 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; _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ToBase64($"{Options.ClientId}:{Options.ClientSecret}")); // Build up the body for the token request var body = new List <KeyValuePair <string, string> >(); body.Add(new KeyValuePair <string, string>("grant_type", "authorization_code")); body.Add(new KeyValuePair <string, string>("code", code)); body.Add(new KeyValuePair <string, string>("redirect_uri", redirectUri)); // Request the token var tokenResponse = await _httpClient.PostAsync(TokenEndpoint, new FormUrlEncodedContent(body)); tokenResponse.EnsureSuccessStatusCode(); string content = await tokenResponse.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <JObject>(content); string accessToken = response.Value <string>("access_token"); string expires = response.Value <string>("expires_in"); string refreshToken = response.Value <string>("refresh_token"); // Request user info var userRequest = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint); userRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var userResponse = await _httpClient.SendAsync(userRequest); var userContent = await userResponse.Content.ReadAsStringAsync(); JObject userJson = null; if (userResponse.IsSuccessStatusCode) { userJson = JObject.Parse(userContent); } var context = new ZoomAuthenticatedContext(Context, accessToken, expires, refreshToken, userJson); context.Identity = new ClaimsIdentity( Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); if (!String.IsNullOrEmpty(context.UserId)) { context.Identity.AddClaim( new Claim(ClaimTypes.NameIdentifier, context.UserId, XmlSchemaString, Options.AuthenticationType)); } if (!String.IsNullOrEmpty(context.GivenName) || !String.IsNullOrEmpty(context.Surname)) { var name = $"{context.GivenName ?? String.Empty} {context.Surname ?? String.Empty}".Trim(); context.Identity.AddClaim( new Claim(ClaimsIdentity.DefaultNameClaimType, name, XmlSchemaString, Options.AuthenticationType)); } if (!String.IsNullOrEmpty(context.Email)) { context.Identity.AddClaim( new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.GivenName)) { context.Identity.AddClaim( new Claim(ClaimTypes.GivenName, context.GivenName, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Surname)) { context.Identity.AddClaim( new Claim(ClaimTypes.Surname, context.Surname, XmlSchemaString, Options.AuthenticationType)); } context.Properties = properties; await Options.Provider.Authenticated(context); return(new AuthenticationTicket(context.Identity, context.Properties)); } catch (Exception ex) { _logger.WriteError("Authentication failed", ex); return(new AuthenticationTicket(null, properties)); } }
/// <summary> /// Invoked whenever Zoom successfully authenticates a user /// </summary> /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param> /// <returns>A <see cref="Task"/> representing the completed operation.</returns> public virtual Task Authenticated(ZoomAuthenticatedContext context) { return(OnAuthenticated(context)); }