Ejemplo n.º 1
0
        public async Task Test_TruncateTablesAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            await dao.TruncateTablesAsync();
        }
Ejemplo n.º 2
0
        public async Task Test_Create_Page_ByClientIdAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            await dao.TruncateTablesAsync();

            int nNumber    = 100;
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(nNumber);

            foreach (var item in insert)
            {
                var result = await adminStore.FindClientByIdAsync(item.ClientId);

                Assert.IsNotNull(result);
                Assert.AreEqual(item.ClientName, result.ClientName);
            }


            var pageSize = 9;

            byte[] pagingState  = null;
            int    runningCount = 0;

            do
            {
                var items = await adminStore.PageClientsAsync(pageSize, pagingState);

                pagingState   = items.PagingState;
                runningCount += items.Count();
            } while (pagingState != null);
            Assert.AreEqual(100, runningCount);
        }
Ejemplo n.º 3
0
        public async Task Test_Create_Find_Delete_Scope_Async()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insertResult = await CassandraTestHelper.InsertTestData_Scopes(1);

            var queryNames = from item in insertResult
                             select item.Record.Name;
            var nameList = queryNames.ToList();
            var result   = await dao.FindScopesByNamesAsync(nameList);

            Assert.AreEqual(result.Count(), insertResult.Count);

            var scope = await dao.FindScopeByNameAsync(nameList[0]);

            Assert.IsNotNull(scope);
            Assert.AreEqual(scope.Name, nameList[0]);
            FlattenedScopeRecord fsr = new FlattenedScopeRecord(new FlattenedScopeHandle(scope));

            scope = await dao.FindScopeByIdAsync(fsr.Id);

            Assert.IsNotNull(scope);
            Assert.AreEqual(scope.Name, nameList[0]);

            await dao.DeleteScopeAsync(scope);

            scope = await dao.FindScopeByNameAsync(scope.Name);

            Assert.IsNull(scope);

            scope = await dao.FindScopeByIdAsync(fsr.Id);

            Assert.IsNull(scope);
        }
Ejemplo n.º 4
0
        public async Task Test_Create_Page_ByScopeIdAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            await dao.TruncateTablesAsync();

            List <Scope> addedScopes = new List <Scope>();
            int          nNumber     = 100;
            var          adminStore  = new IdentityServer3AdminStore();

            for (int i = 0; i < nNumber; ++i)
            {
                addedScopes.Add(await CreateAsync(nNumber));
            }

            var pageSize = 9;

            byte[] pagingState  = null;
            int    runningCount = 0;

            do
            {
                var items = await adminStore.PageScopesAsync(pageSize, pagingState);

                pagingState   = items.PagingState;
                runningCount += items.Count();
            } while (pagingState != null);
            Assert.AreEqual(100, runningCount);
        }
        public static async Task <List <FlattenedConsentHandle> > InsertTestData_Consents(string clientId, string subject, int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            List <FlattenedConsentHandle> result = new List <FlattenedConsentHandle>();

            for (int i = 0; i < count; ++i)
            {
                var flat = new FlattenedConsentHandle(new Consent()
                {
                    ClientId = clientId,
                    Scopes   = new List <string>()
                    {
                        "Scope 0:",
                        "Scope 1:"
                    },
                    Subject = subject
                });
                result.Add(flat);
            }
            await dao.CreateManyConsentHandleAsync(result);

            return(result);
        }
Ejemplo n.º 6
0
        public async Task Test_Find_Scope_by_Name_Async()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var actualScope1 = new Scope()
            {
                Name = Guid.NewGuid().ToString(),
                Type = ScopeType.Identity
            };
            await dao.UpsertScopeAsync(new FlattenedScopeRecord(new FlattenedScopeHandle(actualScope1)));

            var actualScope2 = new Scope()
            {
                Name = Guid.NewGuid().ToString(),
                Type = ScopeType.Resource
            };
            await dao.UpsertScopeAsync(new FlattenedScopeRecord(new FlattenedScopeHandle(actualScope2)));

            var scope = await dao.FindScopeByNameAsync(actualScope1.Name);

            Assert.AreEqual(scope.Type, actualScope1.Type);
            Assert.AreEqual(scope.Name, actualScope1.Name);

            scope = await dao.FindScopeByNameAsync(actualScope2.Name);

            Assert.AreEqual(scope.Type, actualScope2.Type);
            Assert.AreEqual(scope.Name, actualScope2.Name);
        }
        public static async Task <List <FlattenedRefreshTokenHandle> > InsertTestData_RefreshTokens(IClientStore store, int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var tokenInsert = InsertTestData_Tokens(count);
            var result      = new List <FlattenedRefreshTokenHandle>();

            foreach (var token in tokenInsert.Result)
            {
                var accessToken = await token.MakeIdentityServerTokenAsync(store);

                var rt = new RefreshToken
                {
                    AccessToken  = accessToken,
                    CreationTime = DateTimeOffset.UtcNow,
                    LifeTime     = 5,
                    Version      = 1
                };
                var rth = new FlattenedRefreshTokenHandle(token.Key, rt);
                result.Add(rth);
            }
            await dao.CreateManyRefreshTokenHandleAsync(result);

            return(result);
        }
Ejemplo n.º 8
0
        public async Task EstablishSessionAsync()
        {
            if (ResilientSession == null)
            {
                var rs = new IdentityServer3CassandraDao();
                await rs.EstablishConnectionAsync();

                ResilientSession = rs;
            }
        }
        public static async Task <List <FlattenedAuthorizationCodeHandle> > InsertTestData_AuthorizationCode(int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            IClientStore clientStore  = new ClientStore();
            var          insertTokens = await CassandraTestHelper.InsertTestData_Tokens(count); // only add one client

            var clientId     = insertTokens[0].ClientId;
            var clientRecord = await clientStore.FindClientByIdAsync(clientId);

            List <FlattenedAuthorizationCodeHandle> result = new List <FlattenedAuthorizationCodeHandle>();
            int i = 0;

            foreach (var insertToken in insertTokens)
            {
                var claimIdentityRecords = new List <ClaimIdentityRecord>()
                {
                    new ClaimIdentityRecord()
                    {
                        AuthenticationType = Constants.PrimaryAuthenticationType,
                        ClaimTypeRecords   = new List <ClaimTypeRecord>()
                        {
                            new ClaimTypeRecord()
                            {
                                Type      = Constants.ClaimTypes.Subject,
                                Value     = "Value:" + i,
                                ValueType = "VALUETYPE:" + i
                            }
                        }
                    }
                };
                FlattenedAuthorizationCodeHandle handle = new FlattenedAuthorizationCodeHandle
                {
                    ClientId        = clientId,
                    SubjectId       = insertToken.SubjectId,
                    Expires         = DateTimeOffset.UtcNow.AddMinutes(5),
                    CreationTime    = DateTimeOffset.UtcNow,
                    IsOpenId        = true,
                    RedirectUri     = "REDIRECTURI/" + i,
                    WasConsentShown = true,
                    Nonce           = "NONCE:" + i,

                    ClaimIdentityRecords = new FlattenedAuthorizationCodeHandle().SerializeClaimsIdentityRecords(claimIdentityRecords),
                    RequestedScopes      = new FlattenedAuthorizationCodeHandle().SerializeRequestScopes(clientRecord.AllowedScopes),
                    Key = Guid.NewGuid().ToString()
                };
                ++i;
                result.Add(handle);
            }
            await dao.CreateManyAuthorizationCodeHandleAsync(result);

            return(result);
        }
        public async Task TestCreateTokenHandleAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            int i      = 0;
            var claims = new List <Claim>()
            {
                new Claim("Type 0:" + i, "Value 0:" + i),
                new Claim("Type 1:" + i, "Value 1:" + i)
            };
            var json = JsonConvert.SerializeObject(claims);

            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var flat = new FlattenedTokenHandle
            {
                Key          = Guid.NewGuid().ToString(),
                Audience     = "Audience:" + i,
                Claims       = JsonConvert.SerializeObject(claims),
                ClientId     = insert[0].ClientId,
                CreationTime = DateTimeOffset.UtcNow,
                Expires      = DateTimeOffset.UtcNow,
                Issuer       = "Issuer:" + i,
                Lifetime     = 1,
                SubjectId    = "SubjectId:" + i,
                Type         = "Type:" + i,
                Version      = 1
            };
            IClientStore      cs  = new ClientStore();
            ITokenHandleStore ths = new TokenHandleStore();
            var result            = await dao.CreateTokenHandleAsync(flat);

            var result_of_find = await dao.FindTokenByKey(flat.Key, cs);

            Token tt = result_of_find;

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            var newKey = Guid.NewGuid().ToString();
            await ths.StoreAsync(newKey, tt);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(flat.ClientId, result_of_find.ClientId);

            await ths.RemoveAsync(newKey);

            result_of_find = await ths.GetAsync(newKey);

            Assert.AreEqual(result_of_find, null);
        }
Ejemplo n.º 11
0
        public async Task Test_FindScopesAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insertResult = await CassandraTestHelper.InsertTestData_Scopes(1);

            var queryNames = from item in insertResult
                             select item.Record.Name;
            var nameList = queryNames.ToList();
            var result   = await dao.FindScopesByNamesAsync(nameList);

            Assert.AreEqual(result.Count(), insertResult.Count);
        }
Ejemplo n.º 12
0
        public async Task TestCreateRefreshTokenHandleAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            IClientStore cs     = new ClientStore();
            var          insert = await CassandraTestHelper.InsertTestData_RefreshTokens(cs);

            foreach (var rth in insert)
            {
                var result = await dao.FindRefreshTokenByKey(rth.Key, cs);

                Assert.AreEqual(result.ClientId, rth.ClientId);
            }
        }
Ejemplo n.º 13
0
        public async Task Test_Find_Scope_by_ID_Async()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var actualScope1 = new Scope()
            {
                Name = Guid.NewGuid().ToString(),
                Type = ScopeType.Identity,
                AllowUnrestrictedIntrospection = true,
                ClaimsRule              = Guid.NewGuid().ToString(),
                Description             = Guid.NewGuid().ToString(),
                DisplayName             = Guid.NewGuid().ToString(),
                Emphasize               = true,
                Enabled                 = true,
                IncludeAllClaimsForUser = true,
                Required                = true,
                ShowInDiscoveryDocument = true
            };
            var rec1 = new FlattenedScopeRecord(new FlattenedScopeHandle(actualScope1));
            await dao.UpsertScopeAsync(rec1);

            var actualScope2 = new Scope()
            {
                Name = Guid.NewGuid().ToString(),
                Type = ScopeType.Resource,
                AllowUnrestrictedIntrospection = true,
                ClaimsRule              = Guid.NewGuid().ToString(),
                Description             = Guid.NewGuid().ToString(),
                DisplayName             = Guid.NewGuid().ToString(),
                Emphasize               = true,
                Enabled                 = true,
                IncludeAllClaimsForUser = true,
                Required                = true,
                ShowInDiscoveryDocument = true
            };
            var rec2 = new FlattenedScopeRecord(new FlattenedScopeHandle(actualScope2));
            await dao.UpsertScopeAsync(rec2);

            var scope = await dao.FindScopeByIdAsync(rec1.Id);

            Assert.IsTrue(ScopeComparer.OrdinalIgnoreCase.Equals(scope, actualScope1));

            scope = await dao.FindScopeByIdAsync(rec2.Id);

            Assert.IsTrue(ScopeComparer.OrdinalIgnoreCase.Equals(scope, actualScope2));
        }
Ejemplo n.º 14
0
        public async Task TestUpdateAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var result = await dao.FindClientByClientIdAsync(insert[0].ClientId);

            Assert.AreEqual(insert[0].ClientName, result.ClientName);

            await dao.TruncateTablesAsync();

            result = await dao.FindClientByClientIdAsync(insert[0].ClientId);

            Assert.IsNull(result);
        }
        public static async Task <List <FlattenedTokenHandle> > InsertTestData_Tokens(int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insertClients = await CassandraTestHelper.InsertTestData_Clients(1); // only add one client

            // we are going to associate a bunch of tokens to this one client

            var client    = insertClients[0];
            var subjectId = Guid.NewGuid().ToString();
            List <FlattenedTokenHandle> result = new List <FlattenedTokenHandle>();

            for (int i = 0; i < count; ++i)
            {
                var claims = new List <Claim>()
                {
                    new Claim(Constants.ClaimTypes.Subject, subjectId),
                    new Claim(Constants.ClaimTypes.Name, "Name:" + i)
                };
                var json = JsonConvert.SerializeObject(claims);

                var flat = new FlattenedTokenHandle
                {
                    Key          = Guid.NewGuid().ToString(),
                    Audience     = "Audience:" + i,
                    Claims       = JsonConvert.SerializeObject(claims),
                    ClientId     = client.ClientId,
                    CreationTime = DateTimeOffset.UtcNow,
                    Expires      = DateTimeOffset.UtcNow,
                    Issuer       = "Issuer:" + i,
                    Lifetime     = 1,
                    SubjectId    = subjectId,
                    Type         = "Type:" + i,
                    Version      = 1
                };

                result.Add(flat);
            }
            await dao.CreateManyTokenHandleAsync(result);

            return(result);
        }
Ejemplo n.º 16
0
        public async Task TestCreateTokenHandleAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var store = new ConsentStore();

            var insert = await CassandraTestHelper.InsertTestData_Consents(1);

            var flat = insert[0];
            FlattenedConsentRecord fcr = new FlattenedConsentRecord(flat);
            var result = await dao.FindConsentByIdAsync(fcr.Id);

            Assert.AreEqual(result.ClientId, flat.ClientId);
            Assert.AreEqual(result.Subject, flat.Subject);

            result = await store.LoadAsync(flat.Subject, flat.ClientId);

            Assert.AreEqual(result.ClientId, flat.ClientId);
            Assert.AreEqual(result.Subject, flat.Subject);
        }
Ejemplo n.º 17
0
        public async Task Test_Add_Protected_Secret_Async()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var value               = Guid.NewGuid().ToString();
            var valueProtected      = Guid.NewGuid().ToString();
            TripleDesEncryption tde = new TripleDesEncryption("test");
            var eValueProtected     = tde.Encrypt(valueProtected);

            ProtectedSecretHandle handle = new ProtectedSecretHandle()
            {
                ClientId       = Guid.NewGuid().ToString(),
                Value          = value,
                ProtectedValue = eValueProtected
            };

            await dao.AddSecretProtectedValue(handle);

            ProtectedSecretQueryValues queryValues = new ProtectedSecretQueryValues()
            {
                ClientId = handle.ClientId,
                Value    = handle.Value
            };

            var record = await dao.FindSecretProtectedValue(queryValues);

            var fetchedValueProtected = tde.Decrypt(record.ProtectedValue);

            Assert.AreEqual(valueProtected, fetchedValueProtected);

            await dao.DeleteSecretProtectedValue(queryValues);

            record = await dao.FindSecretProtectedValue(queryValues);

            Assert.IsNull(record);
        }
        public static async Task <List <FlattenedScopeRecord> > InsertTestData_Scopes(int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var result = new List <FlattenedScopeRecord>();

            for (int i = 0; i < count; ++i)
            {
                var name = "ScopeName:" + Guid.NewGuid();
                global::IdentityServer3.Core.Models.Scope record = new global::IdentityServer3.Core.Models.Scope()
                {
                    AllowUnrestrictedIntrospection = true,
                    Name                    = name,
                    ClaimsRule              = "ClaimRule:" + i,
                    Description             = "Description:" + i,
                    DisplayName             = "DisplayName:" + i,
                    Enabled                 = true,
                    Emphasize               = true,
                    IncludeAllClaimsForUser = true,
                    Required                = true,
                    Type                    = ScopeType.Identity,
                    ScopeSecrets            = new List <Secret>()
                    {
                        new Secret
                        {
                            Type        = "Type1:" + i,
                            Description = "Decription1:" + i,
                            Expiration  = DateTimeOffset.UtcNow,
                            Value       = "Value1:" + i
                        },
                        new Secret
                        {
                            Type        = "Type2:" + i,
                            Description = "Decription2:" + i,
                            Expiration  = DateTimeOffset.UtcNow,
                            Value       = "Value2:" + i
                        }
                    },
                    ShowInDiscoveryDocument = true,
                    Claims = new List <ScopeClaim>()
                    {
                        new ScopeClaim
                        {
                            AlwaysIncludeInIdToken = true,
                            Description            = "Decription1:" + i,
                            Name = "Name1:" + i
                        },
                        new ScopeClaim
                        {
                            AlwaysIncludeInIdToken = true,
                            Description            = "Decription2:" + i,
                            Name = "Name2:" + i
                        }
                    }
                };
                var scopeRecord = new FlattenedScopeRecord(new FlattenedScopeHandle(record));
                await dao.UpsertScopeAsync(scopeRecord);

                result.Add(scopeRecord);
            }
            //   await dao.UpsertManyScopeAsync(result);
            return(result);
        }
        public static async Task <List <FlattenedClientHandle> > InsertTestData_Clients(int count = 1)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insertScope = await InsertTestData_Scopes(3);

            var scopeNames = (from item in insertScope
                              let c = item.Record.Name
                                      select c).ToList();

            List <FlattenedClientHandle> result = new List <FlattenedClientHandle>();

            for (int i = 0; i < count; ++i)
            {
                var name = "ClientName:" + Guid.NewGuid();

                global::IdentityServer3.Core.Models.Client client = new global::IdentityServer3.Core.Models.Client()
                {
                    AbsoluteRefreshTokenLifetime = 1,
                    AccessTokenLifetime          = 1,
                    AccessTokenType                  = AccessTokenType.Jwt,
                    AllowAccessToAllScopes           = true,
                    AllowAccessToAllCustomGrantTypes = true,
                    AllowClientCredentialsOnly       = true,
                    AllowAccessTokensViaBrowser      = true,
                    AllowRememberConsent             = true,
                    AlwaysSendClientClaims           = true,
                    AuthorizationCodeLifetime        = 1,
                    AllowedCorsOrigins               = new List <string>()
                    {
                        "AllowedCorsOrigins 1:" + i,
                        "AllowedCorsOrigins 2:" + i
                    },
                    AllowedCustomGrantTypes = new List <string>()
                    {
                        "AllowedCustomGrantTypes 1:" + i,
                        "AllowedCustomGrantTypes 2:" + i
                    },
                    AllowedScopes = scopeNames,
                    ClientId      = Guid.NewGuid().ToString(),
                    ClientName    = name,
                    ClientUri     = "ClientUri:" + i,
                    Claims        = new List <Claim>()
                    {
                        new Claim("Type:" + i, "Value:" + i, "ValueType:" + i, "Issuer:" + i, "OriginalIssuer:" + i,
                                  new ClaimsIdentity(new List <Claim>()
                        {
                            new Claim("Type:" + i, "Value:" + i)
                        }))
                    },
                    ClientSecrets = new List <Secret>()
                    {
                        new Secret("Value:" + i, "Description:" + i, DateTimeOffset.UtcNow)
                    },
                    EnableLocalLogin = true,
                    UpdateAccessTokenClaimsOnRefresh = true,
                    Enabled = true,
                    Flow    = Flows.AuthorizationCode,
                    RefreshTokenExpiration      = TokenExpiration.Absolute,
                    IncludeJwtId                = true,
                    LogoutSessionRequired       = true,
                    SlidingRefreshTokenLifetime = 1,
                    IdentityTokenLifetime       = 1,
                    LogoUri                      = "LogoUri:" + i,
                    LogoutUri                    = "LogoutUri:" + i,
                    PrefixClientClaims           = true,
                    RequireConsent               = true,
                    RequireSignOutPrompt         = true,
                    RefreshTokenUsage            = TokenUsage.OneTimeOnly,
                    IdentityProviderRestrictions = new List <string>()
                    {
                        "IdentityProviderRestrictions 1:" + i,
                        "IdentityProviderRestrictions 2:" + i
                    },
                    RedirectUris = new List <string>()
                    {
                        "RedirectUris 1:" + i,
                        "RedirectUris 2:" + i
                    },
                    PostLogoutRedirectUris = new List <string>()
                    {
                        "PostLogoutRedirectUris 1:" + i,
                        "PostLogoutRedirectUris 2:" + i
                    }
                };
                var clientrecord = new FlattenedClientHandle(client);
                await dao.UpsertClientAsync(clientrecord);

                result.Add(clientrecord);
            }

            return(result);
        }
Ejemplo n.º 20
0
        public async Task Test_Create_Add_Delete_ScopesSecretsAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var insertResult = await CassandraTestHelper.InsertTestData_Scopes(1);

            var queryNames = from item in insertResult
                             select item.Record.Name;
            var nameList = queryNames.ToList();

            var adminStore = new IdentityServer3AdminStore();
            var stored     = await adminStore.FindScopesAsync(nameList);

            Assert.AreEqual(stored.Count(), insertResult.Count);
            var secretComparer = SecretComparer.OrdinalIgnoreCase;
            var scopeComparer  = ScopeComparer.OrdinalIgnoreCase;
            var scope          = await insertResult[0].Record.MakeIdentityServerScopeAsync();
            var storedScope    = stored.FirstOrDefault();

            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));

            List <Secret> secrets = new List <Secret>();

            for (int i = 0; i < 2; ++i)
            {
                secrets.Add(new Secret()
                {
                    Value       = Guid.NewGuid().ToString(),
                    Description = Guid.NewGuid().ToString(),
                    Expiration  = DateTimeOffset.UtcNow.AddHours(1),
                    Type        = Guid.NewGuid().ToString()
                });
            }
            List <Secret> original = storedScope.ScopeSecrets;

            List <Secret> expected = storedScope.ScopeSecrets.Union(secrets, SecretComparer.OrdinalIgnoreCase).ToList();

            await adminStore.AddScopeSecretsAsync(insertResult[0].Record.Name, secrets);

            stored = await adminStore.FindScopesAsync(nameList);

            storedScope = stored.FirstOrDefault();
            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));

            var query = from item in storedScope.ScopeSecrets
                        where !expected.Contains(item, secretComparer)
                        select item;
            var finalList = query.ToList();

            Assert.IsTrue(finalList.Count == 0);


            //DELETE IT
            await adminStore.DeleteScopeSecretsAsync(insertResult[0].Record.Name, secrets);

            stored = await adminStore.FindScopesAsync(nameList);

            storedScope = stored.FirstOrDefault();
            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));

            query = from item in storedScope.ScopeSecrets
                    where !original.Contains(item, secretComparer)
                    select item;

            finalList = query.ToList();
            Assert.IsTrue(finalList.Count == 0);
        }
Ejemplo n.º 21
0
        public async Task <Scope> CreateAsync(int i)
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var name = Guid.NewGuid().ToString();

            global::IdentityServer3.Core.Models.Scope record = new global::IdentityServer3.Core.Models.Scope()
            {
                AllowUnrestrictedIntrospection = true,
                Name                    = name,
                ClaimsRule              = "ClaimRule:" + i,
                Description             = "Description:" + i,
                DisplayName             = "DisplayName:" + i,
                Enabled                 = true,
                Emphasize               = true,
                IncludeAllClaimsForUser = true,
                Required                = true,
                Type                    = ScopeType.Identity,
                ScopeSecrets            = new List <Secret>()
                {
                    new Secret
                    {
                        Type        = "Type1:" + i,
                        Description = "Decription1:" + i,
                        Expiration  = DateTimeOffset.UtcNow,
                        Value       = "Value1:" + i
                    },
                    new Secret
                    {
                        Type        = "Type2:" + i,
                        Description = "Decription2:" + i,
                        Expiration  = DateTimeOffset.UtcNow,
                        Value       = "Value2:" + i
                    }
                },
                ShowInDiscoveryDocument = true,
                Claims = new List <ScopeClaim>()
                {
                    new ScopeClaim
                    {
                        AlwaysIncludeInIdToken = true, Description = "Decription1:" + i,
                        Name = "Name1:" + i
                    },
                    new ScopeClaim
                    {
                        AlwaysIncludeInIdToken = true, Description = "Decription2:" + i,
                        Name = "Name2:" + i
                    }
                }
            };
            var  scopeRecord = new FlattenedScopeRecord(new FlattenedScopeHandle(record));
            Guid id          = scopeRecord.Id;

            var result = await dao.UpsertScopeAsync(scopeRecord);

            Assert.IsTrue(result);

            var result2 = await dao.FindScopeByIdAsync(id);

            Assert.IsNotNull(result2);

            scopeRecord = new FlattenedScopeRecord(new FlattenedScopeHandle(result2));

            Assert.AreEqual(scopeRecord.Record.Name, name);
            Assert.AreEqual(scopeRecord.Id, id);

            var scope = await scopeRecord.Record.GetScopeAsync();

            Assert.AreEqual(record.Claims.Count, scope.Claims.Count);

            var differences = record.Claims.Except(scope.Claims, ScopeClaimComparer.MinimalScopeClaimComparer);

            Assert.IsTrue(!differences.Any());
            return(scope);
        }
Ejemplo n.º 22
0
        public async Task Test_Create_Add_Update_ScopeClaimsAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();



            await dao.CreateTablesAsync();

            await dao.TruncateTablesAsync();

            var insertResult = await CassandraTestHelper.InsertTestData_Scopes(1);

            var queryNames = from item in insertResult
                             select item.Record.Name;
            var nameList = queryNames.ToList();

            var adminStore = new IdentityServer3AdminStore();
            var stored     = await adminStore.FindScopesAsync(nameList);

            Assert.AreEqual(stored.Count(), insertResult.Count);
            var scopeClaimComparer = ScopeClaimComparer.MinimalScopeClaimComparer;
            var scopeComparer      = ScopeComparer.OrdinalIgnoreCase;
            var scope       = await insertResult[0].Record.MakeIdentityServerScopeAsync();
            var storedScope = stored.FirstOrDefault();

            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));

            List <ScopeClaim> claims = new List <ScopeClaim>();

            for (int i = 0; i < 2; ++i)
            {
                claims.Add(new ScopeClaim
                {
                    Name = Guid.NewGuid().ToString(),
                    AlwaysIncludeInIdToken = true,
                    Description            = Guid.NewGuid().ToString()
                });
            }
            await adminStore.UpdateScopeByNameAsync(storedScope.Name, new List <PropertyValue>()
            {
                new PropertyValue()
                {
                    Name  = "Claims",
                    Value = claims
                }
            });


            stored = await adminStore.FindScopesAsync(nameList);

            storedScope = stored.FirstOrDefault();
            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));


            var query = from item in storedScope.Claims
                        where !claims.Contains(item, ScopeClaimComparer.DeepScopeClaimComparer)
                        select item;
            var finalList = query.ToList();

            Assert.IsTrue(finalList.Count == 0);

            // Do NOT update name
            foreach (var claim in claims)
            {
                claim.Description            = Guid.NewGuid().ToString();
                claim.AlwaysIncludeInIdToken = !claim.AlwaysIncludeInIdToken;
            }

            await adminStore.UpdateScopeClaimsAsync(storedScope.Name, claims);

            stored = await adminStore.FindScopesAsync(nameList);

            storedScope = stored.FirstOrDefault();
            Assert.IsTrue(scopeComparer.Equals(scope, storedScope));


            query = from item in storedScope.Claims
                    where !claims.Contains(item, ScopeClaimComparer.DeepScopeClaimComparer)
                    select item;

            finalList = query.ToList();
            Assert.IsTrue(finalList.Count == 0);
        }
Ejemplo n.º 23
0
        public async Task Test_Create_Modify_ClientAsync()
        {
            var dao = new IdentityServer3CassandraDao();
            await dao.EstablishConnectionAsync();

            var adminStore = new IdentityServer3AdminStore();

            global::IdentityServer3.Core.Models.Client dd = new global::IdentityServer3.Core.Models.Client();
            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var result = await adminStore.FindClientByIdAsync(insert[0].ClientId);

            Assert.AreEqual(insert[0].ClientName, result.ClientName);
            int           expectedInt    = 1961;
            string        expectedString = Guid.NewGuid().ToString();
            List <string> expectedList   = new List <string>()
            {
                expectedString
            };

            #region PROPERTY_VALUES

            List <PropertyValue> propertyList = new List <PropertyValue>
            {
                new PropertyValue()
                {
                    Name  = "Claims",
                    Value = new List <Claim>()
                    {
                        new Claim("Type:" + expectedString, "Value:" + expectedString, "ValueType:" + expectedString, "Issuer:" + expectedString, "OriginalIssuer:" + expectedString,
                                  new ClaimsIdentity(new List <Claim>()
                        {
                            new Claim("Type:" + expectedString, "Value:" + expectedString)
                        }))
                    }
                },
                new PropertyValue()
                {
                    Name  = "ClientSecrets",
                    Value = new List <Secret>
                    {
                        new Secret(expectedString.Sha256()),
                    }
                },
                new PropertyValue()
                {
                    Name  = "ClientUri",
                    Value = expectedString
                },
                new PropertyValue()
                {
                    Name  = "ClientName",
                    Value = expectedString
                },
                new PropertyValue()
                {
                    Name  = "AbsoluteRefreshTokenLifetime",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "AccessTokenLifetime",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "AccessTokenType",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "AllowAccessToAllCustomGrantTypes",
                    Value = !result.AllowAccessToAllCustomGrantTypes
                },
                new PropertyValue()
                {
                    Name  = "AllowAccessToAllScopes",
                    Value = !result.AllowAccessToAllScopes
                },
                new PropertyValue()
                {
                    Name  = "AllowAccessTokensViaBrowser",
                    Value = !result.AllowAccessTokensViaBrowser
                },
                new PropertyValue()
                {
                    Name  = "AllowClientCredentialsOnly",
                    Value = !result.AllowClientCredentialsOnly
                },
                new PropertyValue()
                {
                    Name  = "AllowRememberConsent",
                    Value = !result.AllowRememberConsent
                },
                new PropertyValue()
                {
                    Name  = "AlwaysSendClientClaims",
                    Value = !result.AlwaysSendClientClaims
                },
                new PropertyValue()
                {
                    Name  = "AuthorizationCodeLifetime",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "Enabled",
                    Value = !result.Enabled
                },
                new PropertyValue()
                {
                    Name  = "EnableLocalLogin",
                    Value = !result.EnableLocalLogin
                },
                new PropertyValue()
                {
                    Name  = "Flow",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "IdentityTokenLifetime",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "IncludeJwtId",
                    Value = !result.IncludeJwtId
                },
                new PropertyValue()
                {
                    Name  = "LogoutSessionRequired",
                    Value = !result.LogoutSessionRequired
                },
                new PropertyValue()
                {
                    Name  = "LogoutUri",
                    Value = expectedString
                },
                new PropertyValue()
                {
                    Name  = "PrefixClientClaims",
                    Value = !result.PrefixClientClaims
                },
                new PropertyValue()
                {
                    Name  = "RefreshTokenExpiration",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "RefreshTokenUsage",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "RequireConsent",
                    Value = !result.RequireConsent
                },
                new PropertyValue()
                {
                    Name  = "RequireSignOutPrompt",
                    Value = !result.RequireSignOutPrompt
                },
                new PropertyValue()
                {
                    Name  = "SlidingRefreshTokenLifetime",
                    Value = expectedInt
                },
                new PropertyValue()
                {
                    Name  = "UpdateAccessTokenClaimsOnRefresh",
                    Value = !result.UpdateAccessTokenClaimsOnRefresh
                },
                new PropertyValue()
                {
                    Name  = "AllowedCorsOrigins",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "AllowedCustomGrantTypes",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "AllowedScopes",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "IdentityProviderRestrictions",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "PostLogoutRedirectUris",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "RedirectUris",
                    Value = expectedList
                },
                new PropertyValue()
                {
                    Name  = "LogoUri",
                    Value = expectedString
                },
            };
            #endregion

            await dao.UpdateClientByIdAsync(result.ClientId, propertyList);

            var result2 = await dao.FindClientByClientIdAsync(insert[0].ClientId);

            foreach (var property in propertyList)
            {
                var castTo      = property.Value.GetType();
                var valueRaw    = result2.GetPropertyValue(property.Name);
                var valueActual = Cast(valueRaw, castTo);

                var valueExpected = property.Value;
                if (castTo == typeof(List <string>))
                {
                    var propertyValue = (List <string>)Convert.ChangeType(property.Value, castTo);
                    var resultValue   = (List <string>)Convert.ChangeType(valueRaw, castTo);
                    Assert.AreEqual(propertyValue.Count, resultValue.Count);
                    var v = from x in propertyValue
                            where !resultValue.Contains(x)
                            select x;
                    Assert.IsFalse(v.Any());
                }
                else if (castTo == typeof(List <Secret>))
                {
                    var propertyValue = (List <Secret>)Convert.ChangeType(property.Value, castTo);
                    var resultValue   = (List <Secret>)Convert.ChangeType(valueRaw, castTo);
                    Assert.AreEqual(propertyValue.Count, resultValue.Count);

                    IEnumerable <Secret> except =
                        propertyValue.Except(resultValue, SecretComparer.OrdinalIgnoreCase);
                    Assert.IsFalse(except.Any());
                }
                else if (castTo == typeof(List <Claim>))
                {
                    var propertyValue = (List <Claim>)Convert.ChangeType(property.Value, castTo);
                    var resultValue   = (List <Claim>)Convert.ChangeType(valueRaw, castTo);
                    Assert.AreEqual(propertyValue.Count, resultValue.Count);

                    IEnumerable <Claim> except =
                        propertyValue.Except(resultValue, ClaimComparer.MinimalComparer);
                    Assert.IsFalse(except.Any());
                }

                else
                {
                    Assert.AreEqual(valueActual, valueExpected);
                }
            }
        }