public void FindApiResourcesByScopeAsync_WhenResourceExists_ExpectResourceAndCollectionsReturned(DbContextOptions <ConfigurationDbContext> options) { var resource = CreateApiTestResource(); using (var context = new ConfigurationDbContext(options, StoreOptions)) { context.ApiResources.Add(resource.ToEntity()); context.SaveChanges(); } IList <ApiResource> resources; using (var context = new ConfigurationDbContext(options, StoreOptions)) { var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create()); resources = store.FindApiResourcesByScopeAsync(new List <string> { resource.Scopes.First().Name }).Result.ToList(); } Assert.NotEmpty(resources); Assert.NotNull(resources); Assert.NotNull(resources.First().UserClaims); Assert.NotEmpty(resources.First().UserClaims); Assert.NotNull(resources.First().ApiSecrets); Assert.NotEmpty(resources.First().ApiSecrets); Assert.NotNull(resources.First().Scopes); Assert.NotEmpty(resources.First().Scopes); Assert.True(resources.First().Scopes.Any(x => x.UserClaims.Any())); }
public void FindApiResourcesByScopeAsync_WhenMultipleResourcesExist_ExpectOnlyRequestedResourcesReturned(DbContextOptions <ConfigurationDbContext> options) { var resource = CreateApiTestResource(); using (var context = new ConfigurationDbContext(options, StoreOptions)) { context.ApiResources.Add(resource.ToEntity()); context.ApiResources.Add(CreateApiTestResource().ToEntity()); context.ApiResources.Add(CreateApiTestResource().ToEntity()); context.SaveChanges(); } IList <ApiResource> resources; using (var context = new ConfigurationDbContext(options, StoreOptions)) { var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create()); resources = store.FindApiResourcesByScopeAsync(new List <string> { resource.Scopes.First().Name }).Result.ToList(); } Assert.NotNull(resources); Assert.NotEmpty(resources); Assert.Equal(1, resources.Count); }
public async Task FindApiResourcesByScopeAsync_WhenResourceExists_ExpectResourceAndCollectionsReturned(ISessionFactory sessionFactory) { var resource = CreateApiTestResource(); using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession)) { await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(resource)); } IEnumerable <ApiResource> resources; using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession)) { var store = new ResourceStore(provider); resources = await store.FindApiResourcesByScopeAsync(new List <string> { resource.Scopes.First().Name }); } Assert.NotEmpty(resources); Assert.NotNull(resources); Assert.NotNull(resources.First().UserClaims); Assert.NotEmpty(resources.First().UserClaims); Assert.NotNull(resources.First().ApiSecrets); Assert.NotEmpty(resources.First().ApiSecrets); Assert.NotNull(resources.First().Scopes); Assert.NotEmpty(resources.First().Scopes); Assert.Contains(resources.First().Scopes, x => x.UserClaims.Any()); }
public async Task FindApiResourcesByScopeAsync_WhenMultipleResourcesExist_ExpectOnlyRequestedResourcesReturned(ISessionFactory sessionFactory) { var resource = CreateApiTestResource(); resource.Scopes.Add(new Scope { Name = Guid.NewGuid().ToString(), Description = Guid.NewGuid().ToString(), DisplayName = Guid.NewGuid().ToString() }); using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession)) { await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(resource)); await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(CreateApiTestResource())); await provider.Session.SaveAsync(_mapper.Map <Entities.ApiResource>(CreateApiTestResource())); } IEnumerable <ApiResource> resources; using (var provider = new ConfigurationSessionProvider(sessionFactory.OpenSession)) { var store = new ResourceStore(provider); resources = await store.FindApiResourcesByScopeAsync(new List <string> { resource.Scopes.First().Name }); } Assert.NotNull(resources); Assert.NotEmpty(resources); Assert.Single(resources); }
public async Task FindApiResourcesByScopeAsync_WhenMultipleResourcesExist_ExpectOnlyRequestedResourcesReturned(DbContextOptions <ConfigurationDbContext> options) { var resource = CreateApiTestResource(); using (var context = new ConfigurationDbContext(options, StoreOptions)) { context.ApiResources.Add(resource.ToEntity()); context.ApiResources.Add(CreateApiTestResource().ToEntity()); context.ApiResources.Add(CreateApiTestResource().ToEntity()); await context.SaveChangesAsync(); } IEnumerable <ApiResource> resources; using (var context = new ConfigurationDbContext(options, StoreOptions)) { var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create()); resources = await store.FindApiResourcesByScopeAsync(new List <string> { resource.Scopes.First().Name }); } Assert.NotNull(resources); Assert.NotEmpty(resources); Assert.Single(resources); }
public void FindApiResourcesByScopeAsync_Success() { var resourceModel = MockModels.ApiResource; var scopeModel = MockModels.Scope; scopeModel.ResourceKey = resourceModel.Key; resourceModel.Scopes.Add(scopeModel); DatabaseHelper.AddEntryIntoDatabase(options, resourceModel); Assert.NotNull(store.FindApiResourcesByScopeAsync(new List <string>() { scopeModel.Name }).Result); }
public async Task ResourceStore_Api_SaveGetTest() { Stopwatch stopwatch = new Stopwatch(); var storageContext = Services.BuildServiceProvider().GetService <ResourceStorageContext>(); Assert.IsNotNull(storageContext); var store = new ResourceStore(storageContext, _logger); Assert.IsNotNull(store); var resource = CreateApiTestObject(); Console.WriteLine(JsonConvert.SerializeObject(resource)); stopwatch.Start(); await store.StoreAsync(resource); stopwatch.Stop(); Console.WriteLine($"ResourceStore.StoreAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var findResource = await store.FindApiResourceAsync(resource.Name); stopwatch.Stop(); Console.WriteLine($"ResourceStore.FindResourceByIdAsync({resource.Name})-api: {stopwatch.ElapsedMilliseconds} ms"); Assert.AreEqual <string>(resource.Name, findResource.Name); stopwatch.Reset(); stopwatch.Start(); string[] findScopes = new string[] { "api1Scope", Guid.NewGuid().ToString() }; var findScopesResources = await store.FindApiResourcesByScopeAsync(findScopes); stopwatch.Stop(); Console.WriteLine($"ResourceStore.FindApiResourcesByScopeAsync({string.Join(",", findScopes)})-api: {stopwatch.ElapsedMilliseconds} ms"); Assert.AreEqual <string>(resource.Name, findScopesResources.Single()?.Name); stopwatch.Reset(); stopwatch.Start(); var resources = await store.GetAllResourcesAsync(); int count = resources.ApiResources.Count(); stopwatch.Stop(); Console.WriteLine($"ResourceStore.GetAllResourcesAsync().ApiResources.Count: {count} : {stopwatch.ElapsedMilliseconds} ms"); Assert.IsTrue(count > 0); }
// Based on https://stackoverflow.com/a/44322425/1249506 public async Task <IdentityServerToken> GetIdentityServerTokenForUserAsync(User user) { var request = new TokenCreationRequest(); var identityPricipal = await PrincipalFactory.CreateAsync(user); var identityUser = new IdentityServerUser(user.Id.ToString()) { AdditionalClaims = identityPricipal.Claims.ToArray(), DisplayName = user.UserName, AuthenticationTime = DateTime.UtcNow, IdentityProvider = IdentityServerConstants.LocalIdentityProvider }; request.Subject = identityUser.CreatePrincipal(); request.IncludeAllIdentityClaims = true; request.ValidatedRequest = new ValidatedRequest { Subject = request.Subject, }; var client = await ClientStore.FindClientByIdAsync(ClientId); request.ValidatedRequest.SetClient(client); request.Resources = new Resources(await ResourceStore.FindEnabledIdentityResourcesByScopeAsync(client.AllowedScopes), await ResourceStore.FindApiResourcesByScopeAsync(client.AllowedScopes)) { OfflineAccess = client.AllowOfflineAccess }; request.ValidatedRequest.Options = Options; request.ValidatedRequest.ClientClaims = identityUser.AdditionalClaims; var token = await TokenService.CreateAccessTokenAsync(request); var accessToken = await TokenService.CreateSecurityTokenAsync(token); var refreshToken = await RefreshTokenService.CreateRefreshTokenAsync(request.Subject, token, client); return(new IdentityServerToken(token, accessToken, refreshToken)); }
public void FindApiResourcesByScopeAsync_ShouldFindByScopes() { A.Configure <ApiResource>().Fill(x => x.UserClaims, A.ListOf <ApiResourceClaim>(2)); A.Configure <ApiResource>().Fill(x => x.Scopes, A.ListOf <ApiScope>()); var apiResources = A.ListOf <ApiResource>(5); using (var session = db.Store.LightweightSession()) { session.StoreObjects(apiResources); session.SaveChanges(); } using (var session = db.Store.LightweightSession()) { var store = new ResourceStore(session); var _scopes = apiResources.Select(y => y.Name); var result = store.FindApiResourcesByScopeAsync(_scopes).Result; Assert.True(apiResources.Count == result.Count()); } }