|
using Microsoft.IdentityModel.Protocols; |
|
using Microsoft.Owin.Security.Notifications; |
|
using Microsoft.Owin.Security.OpenIdConnect; |
|
using Owin; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Security.Claims; |
|
using System.Threading.Tasks; |
|
using Telerik.Sitefinity.Authentication; |
|
using Telerik.Sitefinity.Authentication.Configuration; |
|
using Telerik.Sitefinity.Authentication.Configuration.SecurityTokenService.ExternalProviders; |
|
using Telerik.Sitefinity.Configuration; |
|
using Telerik.Sitefinity.Security.Claims; |
|
|
|
public class AuthenticationProvidersInitializerExtender : AuthenticationProvidersInitializer |
|
{ |
|
public override Dictionary<string, Action<IAppBuilder, string, AuthenticationProviderElement>> GetAdditionalIdentityProviders() |
|
{ |
|
var providers = base.GetAdditionalIdentityProviders(); |
|
|
|
// 'CustomSTS' is the name of the external authentication provider as configured in the Advanced settings |
|
providers.Add("CustomSTS", (IAppBuilder app, string signInAsType, AuthenticationProviderElement providerConfig) => |
|
{ |
|
var clientId = providerConfig.GetParameter("clientId"); |
|
var issuer = providerConfig.GetParameter("issuer").Trim('/'); |
|
var redirectUri = providerConfig.GetParameter("redirectUri"); |
|
var responseType = providerConfig.GetParameter("responseType"); |
|
var scope = providerConfig.GetParameter("scope"); |
|
var caption = providerConfig.GetParameter("caption"); |
|
|
|
var localStsRelativePath = Config.Get<AuthenticationConfig>().SecurityTokenService.ServicePath.Trim('/'); |
|
|
|
var options = new OpenIdConnectAuthenticationOptions() |
|
{ |
|
ClientId = clientId, |
|
Authority = issuer + "/", |
|
AuthenticationType = providerConfig.Name, |
|
SignInAsAuthenticationType = signInAsType, |
|
RedirectUri = redirectUri, |
|
ResponseType = responseType, |
|
Scope = scope, |
|
Caption = caption, |
|
Notifications = new OpenIdConnectAuthenticationNotifications() |
|
{ |
|
SecurityTokenValidated = n => this.SecurityTokenValidatedInternal(n), |
|
} |
|
}; |
|
|
|
app.UseOpenIdConnectAuthentication(options); |
|
}); |
|
|
|
return providers; |
|
} |
|
|
|
private Task SecurityTokenValidatedInternal(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) |
|
{ |
|
var identity = notification.AuthenticationTicket.Identity; |
|
|
|
var externalUserEmail = identity.FindFirst("email"); |
|
if (externalUserEmail != null) |
|
identity.AddClaim(new Claim(SitefinityClaimTypes.ExternalUserEmail, externalUserEmail.Value)); |
|
|
|
var externalUserId = identity.FindFirst("sub"); |
|
if (externalUserId != null) |
|
identity.AddClaim(new Claim(SitefinityClaimTypes.ExternalUserId, externalUserId.Value)); |
|
|
|
var externalUserFirstName = identity.FindFirst("given_name") != null ? identity.FindFirst("given_name").Value : string.Empty; |
|
identity.AddClaim(new Claim("ClaimsMapping:SitefinityProfile.FirstName", externalUserFirstName)); |
|
var externalUserFamilyName = identity.FindFirst("family_name") != null ? identity.FindFirst("family_name").Value : string.Empty; |
|
identity.AddClaim(new Claim("ClaimsMapping:SitefinityProfile.LastName", externalUserFamilyName)); |
|
var externalUserFullName = externalUserFirstName + " " + externalUserFamilyName; |
|
identity.AddClaim(new Claim(SitefinityClaimTypes.ExternalUserName, externalUserFullName)); |
|
|
|
var externalUserNickName = identity.FindFirst("nickname") != null ? identity.FindFirst("nickname").Value : string.Empty; |
|
identity.AddClaim(new Claim(SitefinityClaimTypes.ExternalUserNickName, externalUserNickName)); |
|
|
|
var externalUserPicture = identity.FindFirst("picture"); |
|
if (externalUserPicture != null) |
|
identity.AddClaim(new Claim(SitefinityClaimTypes.ExternalUserPictureUrl, externalUserPicture.Value)); |
|
|
|
return Task.FromResult(0); |
|
} |
|
} |