コード例 #1
0
        public static void SeedResource(this IApplicationBuilder app)
        {
            using var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope();

            var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDBContext>();

            if (!context.ApiResources.Any())
            {
                var catalogRes = new ApiResource("catalog", "Catalog API");
                context.ApiResources.Add(catalogRes.ToEntity());

                var accountsRes = new ApiResource("accounts", "Account API");
                context.ApiResources.Add(accountsRes.ToEntity());

                var localRes = new ApiResource(IdentityServerConstants.LocalApi.ScopeName, "IS4 Local API");
                context.ApiResources.Add(localRes.ToEntity());

                context.SaveChanges();
            }

            if (!context.ApiScopes.Any())
            {
                var apiResource = new ApiScope("trainers", "Trainers Services");
                context.ApiScopes.Add(apiResource.ToEntity());
                context.SaveChanges();
            }
        }
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var storeHolder = GetConfigurationDocumentStoreHolder();

            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource
            {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var session = storeHolder.OpenAsyncSession())
            {
                await session.StoreAsync(visibleIdentityResource.ToEntity());

                await session.StoreAsync(visibleApiResource.ToEntity());

                await session.StoreAsync(visibleApiScope.ToEntity());

                await session.StoreAsync(hiddenIdentityResource.ToEntity());

                await session.StoreAsync(hiddenApiResource.ToEntity());

                await session.StoreAsync(hiddenApiScope.ToEntity());

                await session.SaveChangesAsync();
            }

            WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore());
            WaitForUserToContinueTheTest(storeHolder.IntegrationTest_GetDocumentStore());

            var store     = new ResourceStore(storeHolder, FakeLogger <ResourceStore> .Create());
            var resources = await store.GetAllResourcesAsync();

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
コード例 #3
0
        public static void Tests() => Describe(nameof(ScopeMappers), () =>
        {
            It("Scope Automapper Configuration is valid", () => ScopeMappers.Mapper.ConfigurationProvider.AssertConfigurationIsValid <ScopeMapperProfile>());

            It("Can map scope", () =>
            {
                using var session = new Session();
                var model         = new ApiScope();
                var mappedEntity  = model.ToEntity(session);
                var mappedModel   = mappedEntity.ToModel();

                mappedModel.Should().NotBeNull();
                mappedEntity.Should().NotBeNull();
            });

            It("Properties map", () =>
            {
                using var session = new Session();

                var model = new ApiScope()
                {
                    Description = "description",
                    DisplayName = "displayname",
                    Name        = "foo",
                    UserClaims  = { "c1", "c2" },
                    Properties  =
                    {
                        { "x", "xx" },
                        { "y", "yy" },
                    },
                    Enabled = false
                };


                var mappedEntity = model.ToEntity(session);
                mappedEntity.Description.Should().Be("description");
                mappedEntity.DisplayName.Should().Be("displayname");
                mappedEntity.Name.Should().Be("foo");

                mappedEntity.UserClaims.Count.Should().Be(2);
                mappedEntity.UserClaims.Select(x => x.Type).Should().BeEquivalentTo(new[] { "c1", "c2" });
                mappedEntity.Properties.Count.Should().Be(2);
                mappedEntity.Properties.Should().Contain(x => x.Key == "x" && x.Value == "xx");
                mappedEntity.Properties.Should().Contain(x => x.Key == "y" && x.Value == "yy");


                var mappedModel = mappedEntity.ToModel();

                mappedModel.Description.Should().Be("description");
                mappedModel.DisplayName.Should().Be("displayname");
                mappedModel.Enabled.Should().BeFalse();
                mappedModel.Name.Should().Be("foo");
                mappedModel.UserClaims.Count.Should().Be(2);
                mappedModel.UserClaims.Should().BeEquivalentTo(new[] { "c1", "c2" });
                mappedModel.Properties.Count.Should().Be(2);
                mappedModel.Properties["x"].Should().Be("xx");
                mappedModel.Properties["y"].Should().Be("yy");
            });
        });
コード例 #4
0
        public void CanMapScope()
        {
            var model        = new ApiScope();
            var mappedEntity = model.ToEntity();
            var mappedModel  = mappedEntity.ToModel();

            Assert.NotNull(mappedModel);
            Assert.NotNull(mappedEntity);
        }
コード例 #5
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options)
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(visibleIdentityResource.ToEntity());
                context.ApiResources.Add(visibleApiResource.ToEntity());
                context.ApiScopes.Add(visibleApiScope.ToEntity());

                context.IdentityResources.Add(hiddenIdentityResource.ToEntity());
                context.ApiResources.Add(hiddenApiResource.ToEntity());
                context.ApiScopes.Add(hiddenApiScope.ToEntity());

                context.SaveChanges();
            }

            Resources resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create());
                resources = await store.GetAllResourcesAsync();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
コード例 #6
0
        public IActionResult AddScope()
        {
            var scope = new ApiScope {
                Name = "api_meteo_scope", Enabled = true
            };

            _confContext.ApiScopes.Add(scope.ToEntity());
            _confContext.SaveChanges();

            return(Ok());
        }
コード例 #7
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            await _context.IdentityResources.AddAsync(visibleIdentityResource.ToEntity());

            await _context.ApiResources.AddAsync(visibleApiResource.ToEntity());

            await _context.ApiScopes.AddAsync(visibleApiScope.ToEntity());

            await _context.IdentityResources.AddAsync(hiddenIdentityResource.ToEntity());

            await _context.ApiResources.AddAsync(hiddenApiResource.ToEntity());

            await _context.ApiScopes.AddAsync(hiddenApiScope.ToEntity());

            Resources resources;
            var       store = new ResourceStore(_context, FakeLogger <ResourceStore> .Create());

            resources = await store.GetAllResourcesAsync();

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
コード例 #8
0
        public void Properties_Map()
        {
            var model = new ApiScope()
            {
                Description = "description",
                DisplayName = "displayname",
                Name        = "foo",
                UserClaims  = { "c1", "c2" },
                Properties  =
                {
                    { "x", "xx" },
                    { "y", "yy" },
                },
                Enabled = false
            };


            var mappedEntity = model.ToEntity();

            mappedEntity.Description.Should().Be("description");
            mappedEntity.DisplayName.Should().Be("displayname");
            mappedEntity.Name.Should().Be("foo");

            mappedEntity.UserClaims.Count.Should().Be(2);
            mappedEntity.UserClaims.Should().BeEquivalentTo(new[] { "c1", "c2" });
            mappedEntity.Properties.Count.Should().Be(2);
            var item1 = new KeyValuePair <string, string>("x", "xx");
            var item2 = new KeyValuePair <string, string>("y", "yy");

            mappedEntity.Properties.Should().Contain(item1);
            mappedEntity.Properties.Should().Contain(item2);


            var mappedModel = mappedEntity.ToModel();

            mappedModel.Description.Should().Be("description");
            mappedModel.DisplayName.Should().Be("displayname");
            mappedModel.Enabled.Should().BeFalse();
            mappedModel.Name.Should().Be("foo");
            mappedModel.UserClaims.Count.Should().Be(2);
            mappedModel.UserClaims.Should().BeEquivalentTo(new[] { "c1", "c2" });
            mappedModel.Properties.Count.Should().Be(2);
            mappedModel.Properties["x"].Should().Be("xx");
            mappedModel.Properties["y"].Should().Be("yy");
        }
コード例 #9
0
        public void Properties_Map()
        {
            ApiScope model = new ApiScope
            {
                Description = "description",
                DisplayName = "displayname",
                Name        = "foo",
                UserClaims  = { "c1", "c2" },
                Properties  = { { "x", "xx" }, { "y", "yy" } },
                Enabled     = false
            };


            Entities.ApiScope mappedEntity = model.ToEntity();
            mappedEntity.Description.Should().Be("description");
            mappedEntity.DisplayName.Should().Be("displayname");
            mappedEntity.Name.Should().Be("foo");

            mappedEntity.UserClaims.Count.Should().Be(2);
            mappedEntity.UserClaims.Should().BeEquivalentTo("c1", "c2");
            mappedEntity.Properties.Count.Should().Be(2);
            mappedEntity.Properties.Should().Contain("x", "xx");
            mappedEntity.Properties.Should().Contain("y", "yy");


            ApiScope mappedModel = mappedEntity.ToModel();

            mappedModel.Description.Should().Be("description");
            mappedModel.DisplayName.Should().Be("displayname");
            mappedModel.Enabled.Should().BeFalse();
            mappedModel.Name.Should().Be("foo");
            mappedModel.UserClaims.Count.Should().Be(2);
            mappedModel.UserClaims.Should().BeEquivalentTo("c1", "c2");
            mappedModel.Properties.Count.Should().Be(2);
            mappedModel.Properties["x"].Should().Be("xx");
            mappedModel.Properties["y"].Should().Be("yy");
        }
コード例 #10
0
        public async Task <String> CreateApiScope(String name,
                                                  String displayName,
                                                  String description,
                                                  CancellationToken cancellationToken)
        {
            ApiScope apiScope = new ApiScope
            {
                Description             = description,
                DisplayName             = displayName,
                Name                    = name,
                Emphasize               = false,
                Enabled                 = true,
                Required                = false,
                ShowInDiscoveryDocument = true
            };

            // Now translate the model to the entity
            await this.ConfigurationDbContext.ApiScopes.AddAsync(apiScope.ToEntity(), cancellationToken);

            // Save the changes
            await this.ConfigurationDbContext.SaveChangesAsync();

            return(name);
        }
コード例 #11
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            using var ravenStore = GetDocumentStore();
            await new ApiResourceIndex().ExecuteAsync(ravenStore);

            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource
            {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            using (var session = ravenStore.OpenSession())
            {
                session.Store(visibleIdentityResource.ToEntity());
                session.Store(visibleApiResource.ToEntity());
                session.Store(visibleApiScope.ToEntity());

                session.Store(hiddenIdentityResource.ToEntity());
                session.Store(hiddenApiResource.ToEntity());
                session.Store(hiddenApiScope.ToEntity());

                session.SaveChanges();
            }

            WaitForIndexing(ravenStore);
            WaitForUserToContinueTheTest(ravenStore);

            Resources resources;

            using (var session = ravenStore.OpenAsyncSession())
            {
                var store = new ResourceStore(session, FakeLogger <ResourceStore> .Create());
                resources = await store.GetAllResourcesAsync();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
コード例 #12
0
        private static void Migrate(ServiceProvider sp, ConfigurationDbContext configurationContext, ApplicationDbContext identityContext)
        {
            if (!configurationContext.IdentityResources.AnyAsync().Result)
            {
                configurationContext.IdentityResources.Add(new IdentityResources.OpenId().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Email().ToEntity());
                configurationContext.IdentityResources.Add(new IdentityResources.Profile().ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.ApiResources.AnyAsync().Result)
            {
                var api = new ApiResource("myAppoiments_api", "Web Api call")
                {
                    ApiSecrets = { new Secret("secret".Sha256()) },
                    Scopes     =
                    {
                        "myAppoiments_api.full",
                        "myAppoiments_api.read",
                        "myAppoiments_api"
                    },
                    UserClaims =
                    {
                        ClaimTypes.NameIdentifier,
                        ClaimTypes.Name,
                        ClaimTypes.Email,
                        ClaimTypes.Role,
                        JwtClaimTypes.Role,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    }
                };

                //adding local API
                var localApi = new ApiResource(IdentityServerConstants.LocalApi.ScopeName);

                configurationContext.ApiResources.Add(api.ToEntity());
                configurationContext.ApiResources.Add(localApi.ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.ApiScopes.AnyAsync().Result)
            {
                var api = new ApiScope("myAppoiments_api", "Web Api call")
                {
                    UserClaims =
                    {
                        ClaimTypes.NameIdentifier,
                        ClaimTypes.Name,
                        ClaimTypes.Email,
                        ClaimTypes.Role,
                        JwtClaimTypes.Role,
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Email
                    }
                };

                var localApi = new ApiScope(IdentityServerConstants.LocalApi.ScopeName);

                configurationContext.ApiScopes.Add(api.ToEntity());
                configurationContext.ApiScopes.Add(localApi.ToEntity());

                configurationContext.SaveChanges();
            }

            if (!configurationContext.Clients.AnyAsync().Result)
            {
                List <Client> clients = new List <Client>()
                {
                    new Client
                    {
                        ClientId      = "mvc",
                        ClientSecrets = { new Secret("secret".Sha256()) },

                        AllowedGrantTypes = GrantTypes.Code,

                        // where to redirect to after login
                        RedirectUris = { "http://*****:*****@myappoiments.com",
                    UserType    = UserType.CompanyUser
                }, "1qaz!QAZ").Wait();

                var user = userManager.Users.FirstOrDefaultAsync(x => x.UserName == "*****@*****.**").Result;

                userManager.AddToRoleAsync(user, Role.CompanyUser).Wait();

                userManager.CreateAsync(new ApplicationUser()
                {
                    DisplayName = "Public_user 1",
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    UserType    = UserType.PublicUser
                }, "1qaz!QAZ").Wait();

                user = userManager.Users.FirstOrDefaultAsync(x => x.UserName == "*****@*****.**").Result;

                userManager.AddToRoleAsync(user, Role.PublicUser).Wait();
            }
        }
コード例 #13
0
        public static void PopulateDatabase(IApplicationBuilder app)
        {
            using var serviceScope         = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope();
            using var configurationContext = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();

            //Seed clients
            bool frontEndClientExists =
                configurationContext.Clients.Any(x => x.ClientId == "7ceea8f0-9ef6-4a41-b0d7-d4ebe99430bb");

            if (!frontEndClientExists)
            {
                Client frontEndClient = new Client
                {
                    ClientName                       = "Microservices.WebUI",
                    ClientId                         = "7ceea8f0-9ef6-4a41-b0d7-d4ebe99430bb",
                    AllowedGrantTypes                = GrantTypes.Implicit,
                    RequireConsent                   = false,
                    AllowAccessTokensViaBrowser      = true,
                    AlwaysIncludeUserClaimsInIdToken = true,
                    PostLogoutRedirectUris           = new List <string>()
                    {
                        "http://localhost:4200"
                    },
                    RedirectUris = new List <string>()
                    {
                        "http://localhost:4200/authentication/signin-oidc",
                        "http://localhost:4200/authentication/redirect-silent"
                    },
                    AllowedScopes = new List <string>()
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "roles", "portal-gateway", "product-service-api"
                    },
                    ClientSecrets = new List <Secret>()
                    {
                        new Secret("2af62f010fab4e558324b841d8006be1".Sha256())
                    },
                    Claims = new List <ClientClaim>()
                    {
                        new ClientClaim(string.Empty, "openid"),
                        new ClientClaim(string.Empty, "profile"),
                        new ClientClaim(string.Empty, "roles")
                    }
                };

                configurationContext.Clients.Add(frontEndClient.ToEntity());
            }

            //Seed identity resources
            IdentityResources.OpenId openId = new IdentityResources.OpenId();
            if (!configurationContext.IdentityResources.Any(x => x.Name == openId.Name))
            {
                configurationContext.IdentityResources.Add(openId.ToEntity());
            }

            IdentityResources.Profile profile = new IdentityResources.Profile();
            if (!configurationContext.IdentityResources.Any(x => x.Name == profile.Name))
            {
                configurationContext.IdentityResources.Add(profile.ToEntity());
            }

            IdentityResource role = new IdentityResource("roles", "Your role(s)", new[] { "role" });

            if (!configurationContext.IdentityResources.Any(x => x.Name == role.Name))
            {
                configurationContext.IdentityResources.Add(role.ToEntity());
            }

            //Seed api resources
            ApiResource portalGateway = new ApiResource("portal-gateway", "Portal.Gateway", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == portalGateway.Name))
            {
                configurationContext.ApiResources.Add(portalGateway.ToEntity());
            }

            ApiResource productService = new ApiResource("product-service-api", "ProductService.API", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == productService.Name))
            {
                configurationContext.ApiResources.Add(productService.ToEntity());
            }


            ApiResource pricingService = new ApiResource("pricing-service-api", "PricingService.API", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == pricingService.Name))
            {
                configurationContext.ApiResources.Add(pricingService.ToEntity());
            }


            ApiResource policyService = new ApiResource("policy-service-api", "PolicyService.API", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == policyService.Name))
            {
                configurationContext.ApiResources.Add(policyService.ToEntity());
            }


            ApiResource policySearchService = new ApiResource("policy-search-service-api", "PolicySearchService.API", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == policySearchService.Name))
            {
                configurationContext.ApiResources.Add(policySearchService.ToEntity());
            }


            ApiResource paymentService = new ApiResource("payment-service-api", "PaymentService.API", new[] { "role" });

            if (!configurationContext.ApiResources.Any(x => x.Name == paymentService.Name))
            {
                configurationContext.ApiResources.Add(paymentService.ToEntity());
            }

            //.AddInMemoryApiScopes(new IdentityServer4.Models.ApiScope[] { new IdentityServer4.Models.ApiScope("portal-gateway", "Portal.Gateway", new[] { "role" })})
            ApiScope portalGatewayScope = new ApiScope("portal-gateway", "Portal.Gateway", new[] { "role" });

            if (!configurationContext.ApiScopes.Any(x => x.Name == portalGatewayScope.Name))
            {
                configurationContext.ApiScopes.Add(portalGatewayScope.ToEntity());
            }

            configurationContext.SaveChanges();
        }
コード例 #14
0
        public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden()
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiResourceTestResource();
            var visibleApiScope         = CreateApiScopeTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope
            {
                Name = Guid.NewGuid().ToString(),
                ShowInDiscoveryDocument = false
            };

            var identityResourcesRepo = g.configurationDb.GetRepository <Storage.Entities.IdentityResource>();
            var apiResourcesRepo      = g.configurationDb.GetRepository <Storage.Entities.ApiResource>();
            var apiScopesRepo         = g.configurationDb.GetRepository <Storage.Entities.ApiScope>();

            var visibleIdentityResourceEntity = visibleIdentityResource.ToEntity();

            identityResourcesRepo.Insert(visibleIdentityResourceEntity);
            identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "UserClaims");
            identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "Properties");

            var visibleApiResourceEntity = visibleApiResource.ToEntity();

            apiResourcesRepo.Insert(visibleApiResourceEntity);
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Secrets");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Scopes");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "UserClaims");
            apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Properties");

            var visibleApiScopeEntity = visibleApiScope.ToEntity();

            apiScopesRepo.Insert(visibleApiScopeEntity);
            apiScopesRepo.SaveMany(visibleApiScopeEntity, "UserClaims");
            apiScopesRepo.SaveMany(visibleApiScopeEntity, "Properties");

            var hiddenIdentityResourceEntity = hiddenIdentityResource.ToEntity();

            identityResourcesRepo.Insert(hiddenIdentityResourceEntity);
            identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "UserClaims");
            identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "Properties");

            var hiddenApiResourceEntity = hiddenApiResource.ToEntity();

            apiResourcesRepo.Insert(hiddenApiResourceEntity);
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Secrets");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Scopes");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "UserClaims");
            apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Properties");

            var hiddenApiScopeEntity = hiddenApiScope.ToEntity();

            apiScopesRepo.Insert(hiddenApiScopeEntity);
            apiScopesRepo.SaveMany(hiddenApiScopeEntity, "UserClaims");
            apiScopesRepo.SaveMany(hiddenApiScopeEntity, "Properties");


            Resources resources;

            var store = new ResourceStore(g.configurationDb, FakeLogger <ResourceStore> .Create());

            resources = await store.GetAllResourcesAsync();


            Assert.NotNull(resources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotEmpty(resources.ApiScopes);

            Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name);
            Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name);

            Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name);
            Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name);

            Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name);
            Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name);
        }
コード例 #15
0
        public async Task Should_Retrieve_All_Resources_Including_Hidden_Ones(TestDatabase testDb)
        {
            var visibleIdentityResource = CreateTestIdentityResource("identity_visible");
            var visibleApiResource      = CreateTestApiResource("api_visible", new[] { Guid.NewGuid().ToString() });
            var visibleApiScope         = CreateTestApiScopeForResource("api_visible_scope", new[] { Guid.NewGuid().ToString() });

            visibleApiResource.Scopes.Add(visibleApiScope.Name);
            var hiddenIdentityResource = new IdentityResource()
            {
                Name = "identity_hidden",
                ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource()
            {
                Name   = "api_hidden",
                Scopes = { Guid.NewGuid().ToString() },
                ShowInDiscoveryDocument = false
            };
            var hiddenApiScope = new ApiScope()
            {
                Name = "api_scope_hidden",
                ShowInDiscoveryDocument = false
            };

            using (var session = testDb.OpenSession())
            {
                await session.SaveAsync(visibleIdentityResource.ToEntity());

                await session.SaveAsync(visibleApiResource.ToEntity());

                await session.SaveAsync(visibleApiScope.ToEntity());

                await session.SaveAsync(hiddenIdentityResource.ToEntity());

                await session.SaveAsync(hiddenApiResource.ToEntity());

                await session.SaveAsync(hiddenApiScope.ToEntity());

                await session.FlushAsync();
            }

            var       loggerMock = new Mock <ILogger <ResourceStore> >();
            Resources resources;

            using (var session = testDb.OpenStatelessSession())
            {
                var store = new ResourceStore(session, loggerMock.Object);
                resources = await store.GetAllResourcesAsync();
            }

            resources.Should().NotBeNull();
            resources.IdentityResources.Should().NotBeEmpty();
            resources.IdentityResources.Count.Should().Be(2);
            resources.ApiResources.Should().NotBeEmpty();
            resources.ApiResources.Count.Should().Be(2);
            resources.ApiScopes.Should().NotBeEmpty();
            resources.ApiScopes.Count.Should().Be(2);

            resources.IdentityResources.Any(ir => ir.Name == visibleIdentityResource.Name).Should().BeTrue();
            resources.IdentityResources.Any(ir => ir.Name == hiddenIdentityResource.Name).Should().BeTrue();

            resources.ApiResources.Any(ar => ar.Name == visibleApiResource.Name).Should().BeTrue();
            resources.ApiResources.Any(ar => ar.Name == hiddenApiResource.Name).Should().BeTrue();

            resources.ApiScopes.Any(s => s.Name == visibleApiScope.Name).Should().BeTrue();
            resources.ApiScopes.Any(s => s.Name == hiddenApiScope.Name).Should().BeTrue();

            await CleanupTestDataAsync(testDb);
        }