示例#1
0
        public void FindIdentityResourcesByScopeAsync_WhenResourceExists_ExpectResourceAndCollectionsReturned(DbContextOptions <ConfigurationDbContext> options)
        {
            var resource = CreateIdentityTestResource();

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(resource.ToEntity());
                context.SaveChanges();
            }

            IList <IdentityResource> resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, NullLogger <ResourceStore> .Create());
                resources = store.FindIdentityResourcesByScopeAsync(new List <string>
                {
                    resource.Name
                }).Result.ToList();
            }

            Assert.NotNull(resources);
            Assert.NotEmpty(resources);
            var foundScope = resources.Single();

            Assert.Equal(resource.Name, foundScope.Name);
            Assert.NotNull(foundScope.UserClaims);
            Assert.NotEmpty(foundScope.UserClaims);
        }
示例#2
0
        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,
                    NullLogger <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 void Store_should_update_record_if_key_already_exists(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                context.PersistedGrants.Add(persistedGrant.ToEntity());
                context.SaveChanges();
            }

            var newDate = persistedGrant.Expiration.Value.AddHours(1);

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                persistedGrant.Expiration = newDate;
                store.StoreAsync(persistedGrant).Wait();
            }

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants
                                 .FirstOrDefault(x => x.Key == persistedGrant.Key);

                Assert.NotNull(foundGrant);
                Assert.Equal(newDate, persistedGrant.Expiration);
            }
        }
        public void StoreAsync_WhenPersistedGrantStored_ExpectSuccess(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                store.StoreAsync(persistedGrant).Wait();
            }

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants
                                 .FirstOrDefault(x => x.Key == persistedGrant.Key);

                Assert.NotNull(foundGrant);
            }
        }
        public void Store_should_create_new_record_if_key_does_not_exist(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants
                                 .FirstOrDefault(x => x.Key == persistedGrant.Key);

                Assert.Null(foundGrant);
            }

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                store.StoreAsync(persistedGrant).Wait();
            }

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants
                                 .FirstOrDefault(x => x.Key == persistedGrant.Key);

                Assert.NotNull(foundGrant);
            }
        }
        public void RemoveAsync_WhenSubIdAndClientIdOfExistingReceived_ExpectGrantDeleted(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                context.PersistedGrants.Add(persistedGrant.ToEntity());
                context.SaveChanges();
            }

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                store.RemoveAllAsync(
                    persistedGrant.SubjectId,
                    persistedGrant.ClientId
                    ).Wait();
            }

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var foundGrant = context.PersistedGrants
                                 .FirstOrDefault(x => x.Key == persistedGrant.Key);

                Assert.Null(foundGrant);
            }
        }
        public void GetAsync_WithSubAndTypeAndPersistedGrantExists_ExpectPersistedGrantReturned(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                context.PersistedGrants.Add(persistedGrant.ToEntity());
                context.SaveChanges();
            }

            IList <PersistedGrant> foundPersistedGrants;

            using (var context = new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                foundPersistedGrants = store
                                       .GetAllAsync(persistedGrant.SubjectId).Result
                                       .ToList();
            }

            Assert.NotNull(foundPersistedGrants);
            Assert.NotEmpty(foundPersistedGrants);
        }
        public void GetAsync_WithKeyAndPersistedGrantExists_ExpectPersistedGrantReturned(
            DbContextOptions <PersistedGrantDbContext> options)
        {
            var persistedGrant = CreateTestObject();

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                context.PersistedGrants.Add(persistedGrant.ToEntity());
                context.SaveChanges();
            }

            PersistedGrant foundPersistedGrant;

            using (var context =
                       new PersistedGrantDbContext(options, StoreOptions))
            {
                var store = new PersistedGrantStore(
                    context,
                    NullLogger <PersistedGrantStore> .Create()
                    );

                foundPersistedGrant = store.GetAsync(persistedGrant.Key).Result;
            }

            Assert.NotNull(foundPersistedGrant);
        }
示例#9
0
        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, NullLogger <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()));
        }
示例#10
0
        public void IsOriginAllowedAsync_WhenOriginIsNotAllowed_ExpectFalse(DbContextOptions <ConfigurationDbContext> options)
        {
            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.Clients.Add(new Client
                {
                    ClientId           = Guid.NewGuid().ToString(),
                    ClientName         = Guid.NewGuid().ToString(),
                    AllowedCorsOrigins = new List <string> {
                        "https://www.identityserver.com"
                    }
                }.ToEntity());
                context.SaveChanges();
            }

            bool result;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var service = new CorsPolicyService(context, NullLogger <CorsPolicyService> .Create());
                result = service.IsOriginAllowedAsync("InvalidOrigin").Result;
            }

            Assert.False(result);
        }
示例#11
0
        public void FindApiResourceAsync_WhenResourceExists_ExpectResourceAndCollectionsReturned(
            DbContextOptions <ConfigurationDbContext> options)
        {
            var resource = CreateApiTestResource();

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.ApiResources.Add(resource.ToEntity());
                context.SaveChanges();
            }

            ApiResource foundResource;

            using (var context =
                       new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(
                    context,
                    NullLogger <ResourceStore> .Create()
                    );

                foundResource = store
                                .FindApiResourceAsync(resource.Name).Result;
            }

            Assert.NotNull(foundResource);

            Assert.NotNull(foundResource.UserClaims);
            Assert.NotEmpty(foundResource.UserClaims);
            Assert.NotNull(foundResource.ApiSecrets);
            Assert.NotEmpty(foundResource.ApiSecrets);
            Assert.NotNull(foundResource.Scopes);
            Assert.NotEmpty(foundResource.Scopes);
            Assert.Contains(foundResource.Scopes, x => x.UserClaims.Any());
        }
示例#12
0
        public void LoadByIdAsync_WhenUserAccountExists_ExpectUserAccountRetured(DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount = new UserAccount
            {
                Id    = Guid.NewGuid(),
                Email = "*****@*****.**"
            };

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(context, NullLogger <UserAccountStore> .Create());

                userAccount = store.LoadByIdAsync(testUserAccount.Id).Result;
            }

            Assert.NotNull(userAccount);
        }
示例#13
0
        public void FindClientByIdAsync_WhenClientExists_ExpectClientRetured(
            DbContextOptions <ConfigurationDbContext> options)
        {
            var testClient = new Client
            {
                ClientId   = "test_client",
                ClientName = "Test Client"
            };

            using (var context =
                       new ConfigurationDbContext(options, StoreOptions))
            {
                context.Clients.Add(testClient.ToEntity());
                context.SaveChanges();
            }

            Client client;

            using (var context =
                       new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ClientStore(
                    context,
                    NullLogger <ClientStore> .Create()
                    );

                client = store.FindClientByIdAsync(testClient.ClientId).Result;
            }

            Assert.NotNull(client);
        }
示例#14
0
        public void FindResourcesAsync_WhenResourcesExist_ExpectResourcesReturned(DbContextOptions <ConfigurationDbContext> options)
        {
            var testIdentityResource = CreateIdentityTestResource();
            var testApiResource      = CreateApiTestResource();

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(testIdentityResource.ToEntity());
                context.ApiResources.Add(testApiResource.ToEntity());
                context.SaveChanges();
            }

            Resources resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, NullLogger <ResourceStore> .Create());
                resources = store.FindResourcesByScopeAsync(new List <string>
                {
                    testIdentityResource.Name,
                    testApiResource.Scopes.First().Name
                }).Result;
            }

            Assert.NotNull(resources);
            Assert.NotNull(resources.IdentityResources);
            Assert.NotEmpty(resources.IdentityResources);
            Assert.NotNull(resources.ApiResources);
            Assert.NotEmpty(resources.ApiResources);
            Assert.NotNull(resources.IdentityResources.FirstOrDefault(x => x.Name == testIdentityResource.Name));
            Assert.NotNull(resources.ApiResources.FirstOrDefault(x => x.Name == testApiResource.Name));
        }
示例#15
0
        public void FindClientByIdAsync_WhenClientExists_ExpectClientPropertiesRetured(DbContextOptions <ConfigurationDbContext> options)
        {
            var testClient = new Client
            {
                ClientId   = "properties_test_client",
                ClientName = "Properties Test Client",
                Properties =
                {
                    { "foo1", "bar1" },
                    { "foo2", "bar2" },
                }
            };

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.Clients.Add(testClient.ToEntity());
                context.SaveChanges();
            }

            Client client;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ClientStore(context, NullLogger <ClientStore> .Create());
                client = store.FindClientByIdAsync(testClient.ClientId).Result;
            }

            client.Properties.Should().NotBeNull();
            client.Properties.Count.Should().Be(2);
            client.Properties.ContainsKey("foo1").Should().BeTrue();
            client.Properties.ContainsKey("foo2").Should().BeTrue();
            client.Properties["foo1"].Should().Be("bar1");
            client.Properties["foo2"].Should().Be("bar2");
        }
        public void IsOriginAllowedAsync_WhenOriginIsNotAllowed_ExpectFalse(DbContextOptions <ConfigurationDbContext> options)
        {
            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.Clients.Add(new Client
                {
                    ClientId           = Guid.NewGuid().ToString(),
                    ClientName         = Guid.NewGuid().ToString(),
                    AllowedCorsOrigins = new List <string> {
                        "https://www.identityserver.com"
                    }
                }.ToEntity());
                context.SaveChanges();
            }

            bool result;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var ctx  = new DefaultHttpContext();
                var svcs = new ServiceCollection();
                svcs.AddSingleton <IConfigurationDbContext>(context);
                ctx.RequestServices = svcs.BuildServiceProvider();
                var ctxAccessor = new HttpContextAccessor();
                ctxAccessor.HttpContext = ctx;

                var service = new CorsPolicyService(ctxAccessor, NullLogger <CorsPolicyService> .Create());
                result = service.IsOriginAllowedAsync("InvalidOrigin").Result;
            }

            Assert.False(result);
        }
示例#17
0
 public CoroutinesManager()
 {
     ThreadsCount     = 1;
     Log              = NullLogger.Create();
     MessageBus       = new MessageBus();
     _coroutinesQueue = new ConcurrentQueue <CoroutineQueueContent>();
     _running         = new List <CoroutineInstance>();
     _thread          = new Thread(Run);
 }
 public CoroutineResultEnumerator(string instanceName, IEnumerator <ICoroutineResult> baseEnumerator, TimeSpan?expireIn = null)
 {
     Log           = NullLogger.Create();
     _base         = baseEnumerator;
     _instanceName = instanceName;
     if (expireIn == null || _expireIn.TotalMilliseconds < 0.01)
     {
         _expireIn = TimeSpan.FromDays(4096);
     }
     _expiration = DateTime.UtcNow + _expireIn;
 }
        public void LoadByEmailWithExternalAsync_WhenUserAccountExists_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Provider = "provider",
                        Email    = "*****@*****.**",
                        Subject  = "123456789"
                    },
                    new ExternalAccount
                    {
                        Provider = "provider",
                        Email    = "*****@*****.**",
                        Subject  = "asda5sd4a564da6"
                    }
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                userAccount = store
                              .LoadByEmailWithExternalAsync(testUserAccount.Email)
                              .Result;
            }

            Assert.NotNull(userAccount);
        }
        public void LoadByExternalProviderAsync_WhenUserAccountExists_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testExternalAccount = new ExternalAccount
            {
                Email    = "*****@*****.**",
                Provider = "yahoo",
                Subject  = "123456789"
            };

            var testUserAccount = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                Accounts = new List <ExternalAccount>
                {
                    testExternalAccount
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                userAccount = store.LoadByExternalProviderAsync(
                    testExternalAccount.Provider,
                    testExternalAccount.Subject).Result;
            }

            Assert.NotNull(userAccount);
        }
        public void WriteAsync_NewUserAccount_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount1 = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "foo2@localhost",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Email    = "foo2@provider",
                        Provider = "facebook",
                        Subject  = "123456712",
                    },
                    new ExternalAccount
                    {
                        Email    = "bar2@provider",
                        Provider = "google",
                        Subject  = "789456111",
                    }
                },
                Claims = new List <UserAccountClaim>
                {
                    new UserAccountClaim("name", "foo2"),
                    new UserAccountClaim("email", "foo2@localhost"),
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                var userAccount = store.WriteAsync(testUserAccount1).Result;
                Assert.NotNull(userAccount);
            }
        }
示例#22
0
        public void GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options)
        {
            var visibleIdentityResource = CreateIdentityTestResource();
            var visibleApiResource      = CreateApiTestResource();
            var hiddenIdentityResource  = new IdentityResource {
                Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
            };
            var hiddenApiResource = new ApiResource
            {
                Name   = Guid.NewGuid().ToString(),
                Scopes = new List <Scope> {
                    new Scope {
                        Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false
                    }
                }
            };

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                context.IdentityResources.Add(visibleIdentityResource.ToEntity());
                context.ApiResources.Add(visibleApiResource.ToEntity());
                context.IdentityResources.Add(hiddenIdentityResource.ToEntity());
                context.ApiResources.Add(hiddenApiResource.ToEntity());
                context.SaveChanges();
            }

            Resources resources;

            using (var context = new ConfigurationDbContext(options, StoreOptions))
            {
                var store = new ResourceStore(context, NullLogger <ResourceStore> .Create());
                resources = store.GetAllResources().Result;
            }

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

            Assert.True(resources.IdentityResources.Any(x => !x.ShowInDiscoveryDocument));
            Assert.True(resources.ApiResources.Any(x => !x.Scopes.Any(y => y.ShowInDiscoveryDocument)));
        }
示例#23
0
        public void WriteAsync_UpdateUserAccount_ExpectUserAccountRetured(DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount1 = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "foox@localhost",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Email    = "foox@provider",
                        Provider = "facebook",
                        Subject  = "123456789",
                    },
                    new ExternalAccount
                    {
                        Email    = "bar@provider",
                        Provider = "google",
                        Subject  = "789456123",
                    }
                },
                Claims = new List <UserAccountClaim>
                {
                    new UserAccountClaim("name", "foo"),
                    new UserAccountClaim("email", "foo@localhost"),
                    new UserAccountClaim("w00t", "some junk"),
                }
            };

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount1.ToEntity());
                context.SaveChanges();
                testUserAccount1 = context.UserAccounts.AsNoTracking()
                                   .FirstOrDefault(c => c.Id == testUserAccount1.Id).ToModel();
            }

            UserAccount userAccount;

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(context, NullLogger <UserAccountStore> .Create());

                testUserAccount1.VerificationKeySentAt = DateTime.Now;
                testUserAccount1.VerificationPurpose   = 1;
                testUserAccount1.VerificationStorage   = "hallo welt";

                userAccount = store.WriteAsync(testUserAccount1).Result;

                Assert.NotNull(userAccount);
            }

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var updatedAccount = testUserAccount1 = context.UserAccounts
                                                        .Include(c => c.Accounts)
                                                        .Include(c => c.Claims)
                                                        .FirstOrDefault(c => c.Id == testUserAccount1.Id).ToModel();

                Assert.NotNull(updatedAccount);
                Assert.Equal(updatedAccount.VerificationStorage, userAccount.VerificationStorage);
                Assert.Equal(2, updatedAccount.Accounts.Count());
                Assert.Equal(3, updatedAccount.Claims.Count());
            }
        }