Beispiel #1
0
        public ActionResult <string> AuthenticateUser([FromBody] Credentials creds)
        {
            if (UserVault.ContainsCredentials(creds.UserName, creds.Password))
            {
                var key = SecurityService.GetSecurityKey();
                var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                var identity           = new ClaimsIdentity(new GenericIdentity(creds.UserName, "username"));
                identity.AddClaims(GetClaims());

                var handler = new JwtSecurityTokenHandler();
                var token   = handler.CreateToken(new SecurityTokenDescriptor()
                {
                    Issuer             = SecurityService.GetIssuer(),
                    Audience           = SecurityService.GetAudience(),
                    SigningCredentials = signingCredentials,
                    Subject            = identity,
                    Expires            = DateTime.Now.AddMinutes(10),
                    NotBefore          = DateTime.Now
                });
                return(handler.WriteToken(token));
            }
            else
            {
                return(StatusCode(401));
            }
        }
Beispiel #2
0
        public ActionResult Create(WebUser user)
        {
            var vault = new UserVault()
            {
                AllowRead   = new List <VaultUser>(),
                AllowCreate = new List <VaultUser>(),
            };

            return(View(vault));
        }
Beispiel #3
0
        //public ICollection<User> GetUsers(int vaultId)
        //{
        //    throw new NotImplementedException();
        //}

        public void AddUserVault(long userId, long vaultId, bool isCreator = false)
        {
            var has = UserHasVault(userId, vaultId);

            if (!has)
            {
                var uv = new UserVault {
                    UserId = userId, VaultId = vaultId, UserIsCreator = isCreator
                };
                _userVaultRepo.Insert(uv);
            }
        }
Beispiel #4
0
        public async Task <ActionResult> Create(WebUser user, UserVault vault)
        {
            vault.VaultAdmin = new VaultUser()
            {
                Id = user.Id, UserName = user.UserName
            };
            if (ModelState.IsValid)
            {
                try
                {
                    await _vaultManager.CreateAsync(vault);

                    return(RedirectToAction("Index"));
                }
                catch (Exception)
                {
                    return(View("Error", new string[] { "Something went wrong. Please try again." }));
                }
            }
            return(View(vault));
        }
Beispiel #5
0
        public async Task DeleteEqualUserIdsRedirectNoSuccess()
        {
            Mock <IVaultManager> mockVaultManager = new Mock <IVaultManager>();
            var vault = new UserVault()
            {
                Id = "1", VaultAdmin = new VaultUser()
                {
                    Id = "1"
                }
            };

            mockVaultManager.Setup(x => x.GetVaultAdmin(vault.Id)).ReturnsAsync(vault.VaultAdmin);
            var target = new VaultController(mockVaultManager.Object, null, null);

            var result = await target.Delete(new WebUser()
            {
                Id = "2"
            }, vault.Id);

            Assert.AreEqual(((RedirectToRouteResult)result).RouteValues["action"], "Index");
        }
Beispiel #6
0
        public async Task DeleteNonequalUserIdsReturnRedirectSuccess()
        {
            Mock <IVaultManager> mockVaultManager = new Mock <IVaultManager>();
            var vault = new UserVault()
            {
                Id = "1", VaultAdmin = new VaultUser()
                {
                    Id = "1"
                }
            };

            mockVaultManager.Setup(x => x.GetVaultAdmin(vault.Id)).ReturnsAsync(vault.VaultAdmin);
            var target = new VaultController(mockVaultManager.Object, null, null);

            var result = await target.Delete(new WebUser()
            {
                Id = "1"
            }, vault.Id);

            mockVaultManager.Verify(x => x.DeleteAsync(It.IsAny <string>()), Times.Once);
            Assert.IsNotNull(target.TempData["message"]);
            Assert.AreEqual(((RedirectToRouteResult)result).RouteValues["action"], "Index");
        }
Beispiel #7
0
 public async Task UpdateAsync(UserVault vault)
 {
     await _userVaultRepository.UpdateAsync(vault);
 }
Beispiel #8
0
 public async Task <UserVault> CreateAsync(UserVault vault)
 {
     return(await _userVaultRepository.CreateAsync(vault));
 }