Beispiel #1
0
        public async Task PostKey([FromBody] UpdateKeyRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id);

            var ciphersDict = model.Ciphers?.ToDictionary(c => c.Id.Value);
            var ciphers     = new List <Cipher>();

            if (existingCiphers.Any() && ciphersDict != null)
            {
                foreach (var cipher in existingCiphers.Where(c => ciphersDict.ContainsKey(c.Id)))
                {
                    ciphers.Add(ciphersDict[cipher.Id].ToCipher(cipher));
                }
            }

            var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id);

            var foldersDict = model.Folders?.ToDictionary(f => f.Id);
            var folders     = new List <Folder>();

            if (existingFolders.Any() && foldersDict != null)
            {
                foreach (var folder in existingFolders.Where(f => foldersDict.ContainsKey(f.Id)))
                {
                    folders.Add(foldersDict[folder.Id].ToFolder(folder));
                }
            }

            var result = await _userService.UpdateKeyAsync(
                user,
                model.MasterPasswordHash,
                model.Key,
                model.PrivateKey,
                ciphers,
                folders);

            if (result.Succeeded)
            {
                return;
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            await Task.Delay(2000);

            throw new BadRequestException(ModelState);
        }
Beispiel #2
0
        public async Task PostKey([FromBody] UpdateKeyRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            // NOTE: It is assumed that the eventual repository call will make sure the updated
            // ciphers belong to user making this call. Therefore, no check is done here.

            var ciphers = model.Ciphers.Select(c => c.ToCipher(user.Id));
            var folders = model.Folders.Select(c => c.ToFolder(user.Id));

            var result = await _userService.UpdateKeyAsync(
                user,
                model.MasterPasswordHash,
                model.Key,
                model.PrivateKey,
                ciphers,
                folders);

            if (result.Succeeded)
            {
                return;
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            await Task.Delay(2000);

            throw new BadRequestException(ModelState);
        }
Beispiel #3
0
        public async Task PostKey([FromBody] UpdateKeyRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            var ciphers = new List <Cipher>();

            if (model.Ciphers.Any())
            {
                var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id);

                ciphers.AddRange(existingCiphers
                                 .Join(model.Ciphers, c => c.Id, c => c.Id, (existing, c) => c.ToCipher(existing)));
            }

            var folders = new List <Folder>();

            if (model.Folders.Any())
            {
                var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id);

                folders.AddRange(existingFolders
                                 .Join(model.Folders, f => f.Id, f => f.Id, (existing, f) => f.ToFolder(existing)));
            }

            var sends = new List <Send>();

            if (model.Sends?.Any() == true)
            {
                var existingSends = await _sendRepository.GetManyByUserIdAsync(user.Id);

                sends.AddRange(existingSends
                               .Join(model.Sends, s => s.Id, s => s.Id, (existing, s) => s.ToSend(existing, _sendService)));
            }

            var result = await _userService.UpdateKeyAsync(
                user,
                model.MasterPasswordHash,
                model.Key,
                model.PrivateKey,
                ciphers,
                folders,
                sends);

            if (result.Succeeded)
            {
                return;
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            await Task.Delay(2000);

            throw new BadRequestException(ModelState);
        }