// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Collections.Generic; using IdentityModel; using IdentityServer4.Models; using IdentityServer4.Stores; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.AspNetCore.DataProtection.Repositories; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Squidex.Domain.Users; using Squidex.Shared.Identity; using Squidex.Web; using Squidex.Web.Pipeline; namespace Squidex.Areas.IdentityServer.Config { public static class IdentityServerServices { public static void AddSquidexIdentityServer(this IServiceCollection services) { services.AddSingleton>(s => { return new ConfigureOptions(options => { options.XmlRepository = s.GetRequiredService(); }); }); services.AddDataProtection().SetApplicationName("Squidex"); services.AddSingleton(GetApiResources()); services.AddSingleton(GetIdentityResources()); services.AddIdentity() .AddDefaultTokenProviders(); services.AddSingleton, PwnedPasswordValidator>(); services.AddScoped, UserClaimsPrincipalFactoryWithEmail>(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddIdentityServer(options => { options.UserInteraction.ErrorUrl = "/error/"; }) .AddAspNetIdentity() .AddInMemoryApiResources(GetApiResources()) .AddInMemoryIdentityResources(GetIdentityResources()); } private static IEnumerable GetApiResources() { yield return new ApiResource(Constants.ApiScope) { UserClaims = new List { JwtClaimTypes.Email, JwtClaimTypes.Role, SquidexClaimTypes.Permissions } }; } private static IEnumerable GetIdentityResources() { yield return new IdentityResources.OpenId(); yield return new IdentityResources.Profile(); yield return new IdentityResources.Email(); yield return new IdentityResource(Constants.RoleScope, new[] { JwtClaimTypes.Role }); yield return new IdentityResource(Constants.PermissionsScope, new[] { SquidexClaimTypes.Permissions }); yield return new IdentityResource(Constants.ProfileScope, new[] { SquidexClaimTypes.DisplayName, SquidexClaimTypes.PictureUrl, SquidexClaimTypes.NotifoKey }); } } }