Пример #1
0
        public async Task Test_Create_Add_ClientClaims_ByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

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

            var original = result.Claims;

            var newClaims = new List <Claim> {
                new Claim(Constants.ClaimTypes.Subject, Guid.NewGuid().ToString()),
                new Claim(Constants.ClaimTypes.PreferredUserName, Guid.NewGuid().ToString()),
            };

            var finalList = new List <Claim>();

            finalList.AddRange(original);
            finalList.AddRange(newClaims);


            await adminStore.AddClaimsToClientAsync(insert[0].ClientId, newClaims);

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

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Claims.Count(), finalList.Count);

            var ff = result.Claims.Except(finalList, ClaimComparer.DeepComparer);

            Assert.IsFalse(ff.Any());
        }
Пример #2
0
        public async Task TestRevokeAsync()
        {
            var store         = new ConsentStore();
            var insertClients = await CassandraTestHelper.InsertTestData_Clients(2);

            // we are going to associate a bunch of tokens to this one client
            var subject = Guid.NewGuid().ToString();

            foreach (var client in insertClients)
            {
                await CassandraTestHelper.InsertTestData_Consents(client.ClientId, subject, 1);
            }

            var result = await store.LoadAllAsync(subject);

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

            foreach (var client in insertClients)
            {
                await store.RevokeAsync(subject, client.ClientId);
            }
            result = await store.LoadAllAsync(subject);

            Assert.AreEqual(0, result.Count());
        }
Пример #3
0
        public async Task Test_Create_Add_RedirectUris_ByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

            Assert.IsNotNull(result);
            var original = result.RedirectUris;

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

            List <string> newData = new List <string>()
            {
                Guid.NewGuid().ToString()
            };

            await adminStore.AddRedirectUrisToClientAsync(insert[0].ClientId, newData);

            var finalList = new List <string>();

            finalList.AddRange(original);
            finalList.AddRange(newData);

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

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RedirectUris.Count(), finalList.Count);


            var ff = result.RedirectUris.Except(finalList);

            Assert.IsFalse(ff.Any());
        }
Пример #4
0
        public async Task Test_Create_Add_AllowedCorsOrigins_ByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

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

            List <string> allowedCorsOrigins = new List <string>()
            {
                Guid.NewGuid().ToString()
            };

            await adminStore.AddAllowedCorsOriginsToClientAsync(insert[0].ClientId, allowedCorsOrigins);

            var finalList = new List <string>();

            finalList.AddRange(allowedCorsOrigins);
            finalList.AddRange(result.AllowedCorsOrigins);

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

            Assert.AreEqual(result.AllowedCorsOrigins.Count(), finalList.Count);


            var ff = result.AllowedCorsOrigins.Except(finalList);

            Assert.IsFalse(ff.Any());
        }
Пример #5
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);
        }
Пример #6
0
        public async Task Test_Create_Add_ScopeByClientIdAsync()
        {
            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            var adminStore = new IdentityServer3AdminStore();
            // await adminStore.CleanupClientByIdAsync(insert[0].ClientId);
            var result = await adminStore.FindClientByIdAsync(insert[0].ClientId);

            var originalAllowedScopes = result.AllowedScopes;

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

            List <Scope> scopes = new List <Scope>()
            {
                new Scope()
                {
                    AllowUnrestrictedIntrospection = true,
                    Name    = Guid.NewGuid().ToString(),
                    Enabled = true
                },
                new Scope()
                {
                    AllowUnrestrictedIntrospection = true,
                    Name    = Guid.NewGuid().ToString(),
                    Enabled = true
                }
            };

            foreach (var scope in scopes)
            {
                await adminStore.CreateScopeAsync(scope);
            }


            var query = from item in scopes
                        let c = item.Name
                                select c;

            List <string> addedScopeNames = query.ToList();
            List <string> finalExpected   = new List <string>();

            finalExpected.AddRange(addedScopeNames);
            finalExpected.AddRange(result.AllowedScopes);

            await adminStore.AddScopesToClientAsync(insert[0].ClientId, addedScopeNames);



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

            Assert.IsNotNull(result);
            Assert.AreEqual(result.AllowedScopes.Count(), finalExpected.Count);


            var ff = result.AllowedScopes.Except(finalExpected);

            Assert.IsFalse(ff.Any());
        }
Пример #7
0
        public async Task Test_CreateClientAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

            Assert.AreEqual(insert[0].ClientName, result.ClientName);
        }
        public static async Task <List <FlattenedConsentHandle> > InsertTestData_Consents(int count = 1)
        {
            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 subject = Guid.NewGuid().ToString();

            return(await InsertTestData_Consents(client.ClientId, subject, count));
        }
        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);
        }
Пример #10
0
        public async Task Test_Create_Add_Delete_ClientSecrets_ByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

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

            var original = result.ClientSecrets;

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

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

            finalList.AddRange(original);
            finalList.AddRange(newSecrets);

            await adminStore.AddClientSecretsToClientAsync(insert[0].ClientId, newSecrets);

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

            Assert.IsNotNull(result);
            Assert.AreEqual(result.ClientSecrets.Count(), finalList.Count);

            var ff = result.ClientSecrets.Except(finalList, SecretComparer.OrdinalIgnoreCase);

            Assert.IsFalse(ff.Any());

            await adminStore.DeleteClientSecretsFromClientAsync(insert[0].ClientId, result.ClientSecrets);

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

            Assert.IsNotNull(result);

            Assert.IsFalse(result.ClientSecrets.Any());
        }
Пример #11
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);
        }
Пример #12
0
        public async Task Test_Create_Add_Delete_AllowedCustomGrantTypesByClientIdAsync()
        {
            var adminStore = new IdentityServer3AdminStore();
            var insert     = await CassandraTestHelper.InsertTestData_Clients(1);

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

            Assert.IsNotNull(result);

            var originalAllowedCustomGrantTypes = result.AllowedCustomGrantTypes;

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

            List <string> newAllowedCustomGrantTypes = new List <string>()
            {
                Guid.NewGuid().ToString()
            };
            var finalList = new List <string>();

            finalList.AddRange(originalAllowedCustomGrantTypes);
            finalList.AddRange(newAllowedCustomGrantTypes);

            await adminStore.AddAllowedCustomGrantTypesToClientAsync(insert[0].ClientId, newAllowedCustomGrantTypes);


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

            Assert.IsNotNull(result);
            Assert.AreEqual(result.AllowedCustomGrantTypes.Count(), finalList.Count);


            var ff = result.AllowedCustomGrantTypes.Except(finalList);

            Assert.IsFalse(ff.Any());

            await
            adminStore.DeleteAllowedCustomGrantTypesFromClientAsync(insert[0].ClientId,
                                                                    result.AllowedCustomGrantTypes);

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

            Assert.IsNotNull(result);
            Assert.IsFalse(result.AllowedCustomGrantTypes.Any());
        }
        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);
        }
Пример #14
0
        public async Task TestUpdateAsync()
        {
            var store         = new ConsentStore();
            var insertClients = await CassandraTestHelper.InsertTestData_Clients(1);

            var     client  = insertClients[0];
            Consent consent = new Consent()
            {
                ClientId = client.ClientId,
                Scopes   = new List <string>()
                {
                    "Scope 0:",
                    "Scope 1:"
                },
                Subject = Guid.NewGuid().ToString()
            };
            await store.UpdateAsync(consent);

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

            Assert.AreEqual(consent.ClientId, result.ClientId);
            Assert.AreEqual(consent.Subject, result.Subject);
        }
Пример #15
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);
                }
            }
        }
        async Task TestCreateAsync()
        {
            var insert = await CassandraTestHelper.InsertTestData_Clients(1);

            Assert.IsFalse(true);
        }