Esempio n. 1
0
        public async Task <ActionResult> ManageScopes(string clientId)
        {
            var adminStore = new IdentityServer3AdminStore();
            var userScopes = await adminStore.FindScopesByUserAsync(User.Identity.GetUserId());

            var queryUserScopes = from item in userScopes
                                  let c = new ScopeEnabledRecord()
            {
                Enabled = false,
                Name    = item
            }
            select c;

            var currentClient = await adminStore.FindClientByIdAsync(clientId);

            var queryAllowedScopes = from item in currentClient.AllowedScopes
                                     let c = new ScopeEnabledRecord()
            {
                Enabled = true,
                Name    = item
            }
            select c;

            var model = new ClientScopeModel()
            {
                ClientId      = clientId,
                UserScopes    = queryUserScopes.ToList(),
                AllowedScopes = queryAllowedScopes.ToList()
            };

            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> ManageScopes(ClientScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var adminStore = new IdentityServer3AdminStore();

            if (model.AllowedScopes != null)
            {
                // remove the ones that need to be removed
                var queryToBeDeleted = (from item in model.AllowedScopes
                                        where item.Enabled == false
                                        select item.Name).ToList();
                if (queryToBeDeleted.Any())
                {
                    await adminStore.DeleteScopesFromClientAsync(model.ClientId, queryToBeDeleted);
                }
            }
            if (model.UserScopes != null)
            {
                var queryToBeAdded = (from item in model.UserScopes
                                      where item.Enabled
                                      select item.Name).ToList();
                if (queryToBeAdded.Any())
                {
                    await adminStore.AddScopesToClientAsync(model.ClientId, queryToBeAdded);
                }
            }


            return(RedirectToAction("ManageScopes", new { clientId = model.ClientId }));
        }
Esempio n. 3
0
        public async Task <ActionResult> New(ClientViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Client client = new Client()
            {
                AccessTokenType = model.AccessTokenType,
                Enabled         = model.Enabled,
                AllowedScopes   = model.AllowedScopes,
                ClientId        = model.ClientId,
                ClientName      = model.ClientName,
                ClientSecrets   = model.ClientSecrets,
                Flow            = model.Flow
            };
            var adminStore = new IdentityServer3AdminStore();
            await adminStore.CreateClientAsync(client);

            var clients = new List <string> {
                client.ClientId
            };
            await adminStore.AddClientIdToIdentityServerUserAsync(User.Identity.GetUserId(), clients);

            return(RedirectToAction("Index"));
        }
Esempio n. 4
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());
        }
        public async Task Test_Create_IdentityServerUser_Add_AllowedScopes_Async()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var user       = new IdentityServerUser {
                Enabled = true, UserId = userId, UserName = "******" + userId
            };

            await adminStore.CreateIdentityServerUserAsync(user);

            var result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.AreEqual(user.UserId, result.UserId);
            List <string> scopesToAdd = new List <string> {
                "scope1", "scope2"
            };
            await adminStore.AddScopesToIdentityServerUserAsync(userId, scopesToAdd);

            var scopes = await adminStore.FindScopesByUserAsync(userId);

            Assert.AreEqual(scopes.Count(), scopesToAdd.Count);
            var finalList = scopes.ToList().Except(scopesToAdd);

            Assert.IsFalse(finalList.Any());
        }
Esempio n. 6
0
        public async Task <ActionResult> New(ScopeNewModel model)
        {
            try
            {
                Scope scope = new Scope {
                    Name = model.Name, Type = model.SelectedScopeType, Enabled = true
                };

                var adminStore = new IdentityServer3AdminStore();
                var scopeCheck = await adminStore.FindScopeByNameAsync(scope.Name);

                if (scopeCheck == null)
                {
                    await adminStore.CreateScopeAsync(scope);

                    // Good doesn't exist
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError(string.Empty, string.Format("The scope, {0}, already exists.", scope.Name));
                return(View(model));
            }
            catch
            {
                return(View());
            }
        }
        public async Task <ActionResult> Scopes(UserScopeModel model)
        {
            var fullUserStore = UserManager.FullUserStore;
            var adminStore    = new IdentityServer3AdminStore();

            if (model.UserScopeRecords != null)
            {
                // remove the ones that need to be removed
                var queryToBeDeleted = (from item in model.UserScopeRecords
                                        where item.Enabled == false
                                        select item.Name).ToList();
                await adminStore.DeleteScopesByUserIdAsync(model.UserId, queryToBeDeleted);
            }

            var queryToBeAdded = (from item in model.AllowedScopes
                                  where item.Enabled
                                  select item.Name).ToList();

            // add the ones that need to be added.
            if (queryToBeAdded.Any())
            {
                await adminStore.AddScopesToIdentityServerUserAsync(model.UserId, queryToBeAdded);
            }

            return(RedirectToAction("Index"));
        }
Esempio n. 8
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());
        }
Esempio n. 9
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 async Task CreateIdentityServerUser(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException("userId");
            }

            var fullUserStore = UserManager.FullUserStore;
            var user          = await fullUserStore.FindByIdAsync(Guid.Parse(userId));

            if (user == null)
            {
                throw new UserDoesNotExitException();
            }
            var adminStore    = new IdentityServer3AdminStore();
            var idsUserExists = await adminStore.FindDoesUserExistByUserIdAsync(userId);

            if (!idsUserExists)
            {
                var claim = new Claim(ClaimTypes.Role, "Developer");
                await fullUserStore.AddClaimAsync(user, claim);

                var idsUser = new IdentityServerUser()
                {
                    Enabled = true, UserId = userId
                };
                await adminStore.CreateIdentityServerUserAsync(idsUser);
            }
        }
        public async Task Test_Create_IdentityServerUser_Add_ClientIds_Async()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var user       = new IdentityServerUser {
                Enabled = true, UserId = userId, UserName = "******" + userId
            };

            await adminStore.CreateIdentityServerUserAsync(user);

            var result = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.AreEqual(user.UserId, result.UserId);

            List <string> clientIdsToAdd = new List <string> {
                "clientid1", "clientid2"
            };
            await adminStore.AddClientIdToIdentityServerUserAsync(userId, clientIdsToAdd);

            var clientIds = await adminStore.FindClientIdsByUserAsync(userId);

            Assert.AreEqual(clientIds.Count(), clientIdsToAdd.Count);
            var finalList = clientIds.ToList().Except(clientIdsToAdd);

            Assert.IsFalse(finalList.Any());
        }
Esempio n. 12
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());
        }
Esempio n. 13
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);
        }
        // GET: Admin/Home/Manage/5
        public async Task <ActionResult> Manage(string id, string email)
        {
            var fullUserStore = UserManager.FullUserStore;
            var claims        = await fullUserStore.GetClaimsAsync(Guid.Parse(id));

            var query = from item in claims
                        where item.Type == ClaimTypes.Role && item.Value == "Developer"
                        select item;

            var adminStore = new IdentityServer3AdminStore();

            ViewBag.Email = email;
            var exists = await adminStore.FindDoesUserExistByUserIdAsync(id);

            if (exists)
            {
                var scopes = await adminStore.FindScopesByUserAsync(id);
            }
            IdentityServerUserModel isum = new IdentityServerUserModel()
            {
                UserId        = id,
                Exists        = exists,
                AllowedScopes = new List <string>(),
                Developer     = query.Any()
            };

            return(View(isum));
        }
        public async Task <IEnumerable <string> > GetUserScopesAsync( )
        {
            var adminStore = new IdentityServer3AdminStore();
            var userId     = User.Identity.GetUserId();
            var scopes     = await adminStore.FindScopesByUserAsync(userId);

            return(scopes);
        }
        public async Task Test_Find_No_IdentityServerUserAsync()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();
            var result     = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            Assert.IsNull(result);
        }
        public async Task <Scope> GetScopeAsync(string scope)
        {
            var adminStore = new IdentityServer3AdminStore();
            var userId     = User.Identity.GetUserId();
            var res        = await adminStore.FindScopeByNameAsync(scope);

            return(res);
        }
Esempio n. 18
0
        // GET: NortonDeveloper/ClientCredentials
        public async Task <ActionResult> Index()
        {
            var adminStore = new IdentityServer3AdminStore();
            var userId     = User.Identity.GetUserId();
            var clients    = await adminStore.FindClientIdsByUserAsync(userId);

            return(View(clients));
        }
Esempio n. 19
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());
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
 public async Task UpsertScopeAsync(Scope scope)
 {
     if (scope == null)
     {
         throw new ArgumentNullException("scope");
     }
     var adminStore = new IdentityServer3AdminStore();
     await adminStore.CreateScopeAsync(scope);
 }
 public async Task DeleteIdentityServerUser(string userId)
 {
     if (string.IsNullOrEmpty(userId))
     {
         throw new ArgumentNullException("userId");
     }
     var adminStore = new IdentityServer3AdminStore();
     await adminStore.DeleteIdentityServerUserAsync(userId);
 }
Esempio n. 23
0
        public async Task <ActionResult> Delete(ClientViewModel model)
        {
            var adminStore = new IdentityServer3AdminStore();
            await
            adminStore.DeleteClientIdsByUserIdAsync(User.Identity.GetUserId(), new List <string>() { model.ClientId });

            await adminStore.DeleteClientAsync(model.ClientId);

            return(RedirectToAction("Index"));
        }
        public async Task <IdentityServerUser> GetIdentityServerUser(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException("userId");
            }
            var adminStore = new IdentityServer3AdminStore();
            var user       = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            return(user);
        }
Esempio n. 25
0
        public async Task <Scope> FindScopeAsync(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            var adminStore = new IdentityServer3AdminStore();
            var scope      = await adminStore.FindScopeByNameAsync(name);

            return(scope);
        }
Esempio n. 26
0
 public async Task DeleteScopeAsync(string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentNullException("name");
     }
     var adminStore = new IdentityServer3AdminStore();
     await adminStore.DeleteScopeAsync(new Scope()
     {
         Name = name
     });
 }
Esempio n. 27
0
        public async Task <ActionResult> Edit(string name)
        {
            var adminStore = new IdentityServer3AdminStore();
            var scope      = await adminStore.FindScopeByNameAsync(name);

            if (scope == null)
            {
                return(RedirectToAction("Index"));
            }
            ScopeNewModel snm = scope.ToScopeNewModel();

            return(View(snm));
        }
Esempio n. 28
0
        /*
         * new Client
         *      {
         *          ClientName = "Silicon-only Client",
         *          ClientId = "silicon",
         *          Enabled = true,
         *          Flow = Flows.ClientCredentials,
         *
         *          ClientSecrets = new List<Secret>
         *          {
         *              new Secret("secret".Sha256())
         *          },
         *
         *          AllowedScopes = new List<string>
         *          {
         *              "api1"
         *          },
         *          AccessTokenType = AccessTokenType.Reference
         *      }
         * */
        public async Task <ActionResult> Delete(string clientId)
        {
            var adminStore = new IdentityServer3AdminStore();
            var client     = await adminStore.FindClientByIdAsync(clientId);

            var clientVM = new ClientViewModel
            {
                ClientId   = client.ClientId,
                ClientName = client.ClientName
            };

            return(View(clientVM));
        }
        public async Task Test_Add_ClientIds_To_NonExisting_User_Async()
        {
            var userId     = Guid.NewGuid().ToString();
            var adminStore = new IdentityServer3AdminStore();

            List <string> clientIdsToAdd = new List <string> {
                "clientid1", "clientid2"
            };
            var appliedInfo = await adminStore.AddClientIdToIdentityServerUserAsync(userId, clientIdsToAdd);

            Assert.IsFalse(appliedInfo.Applied);
            Assert.IsNotNull(appliedInfo.Exception);
            Assert.IsNotNull(appliedInfo.Exception as UserDoesNotExitException);
        }
        public async Task UpdateIdentityServerUser(string userId, bool enabled)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException("userId");
            }
            var adminStore = new IdentityServer3AdminStore();
            var user       = await adminStore.FindIdentityServerUserByUserIdAsync(userId);

            if (user != null)
            {
                user.Enabled = enabled;
                await adminStore.UpdateIdentityServerUserAsync(user);
            }
        }