Пример #1
0
        public async Task <Unit> Handle(UpdateFileCommand request, CancellationToken cancellationToken)
        {
            var user = await _userRepository.FirstOrDefaultAsync(request.Username, cancellationToken);

            if (user.PrivateKeyPassword != request.PrivateKeyPassword.Sha512())
            {
                throw new Exception("Invalid private key password");
            }

            var decryptionResponse = _encryptor.Decrypt(user.PrivateKey, request.PrivateKeyPassword.Sha256AsBytes());

            if (decryptionResponse.Status == Status.Failed)
            {
                throw new Exception("Error while trying to decrypt private key");
            }

            await _fileRepository.UpdateAsync(request.FileId, request.Content.ToArray(), cancellationToken);

            var file = await _fileRepository.FirstOrDefaultAsync(request.FileId, cancellationToken);

            var signResponse = await _ecdsa.SignFileAsync(file.Name, request.Content.ToArray(), decryptionResponse.DecryptedText);

            await _shareRepository.AddShareFileUserAsync(request.ShareId, file.Id, request.Username,
                                                         signResponse.Signature, cancellationToken);

            return(Unit.Value);
        }
Пример #2
0
        public async Task <Unit> Handle(SignShareFileCommand request, CancellationToken cancellationToken)
        {
            var user = await _userRepository.FirstOrDefaultAsync(request.Username, cancellationToken);

            if (user.PrivateKeyPassword != request.PrivateKeyPassword.Sha512())
            {
                throw new Exception();
            }

            var decryptionResponse = _encryptor.Decrypt(user.PrivateKey, request.PrivateKeyPassword.Sha256AsBytes());

            if (decryptionResponse.Status == Status.Failed)
            {
                throw new Exception();
            }

            var file = await _fileRepository.GetFileContentAsync(request.FileId, null, cancellationToken);

            var signResponse = await _ecdsa.SignFileAsync(file.Name, file.Content, decryptionResponse.DecryptedText);

            await _shareRepository.AddShareFileUserAsync(request.ShareId, request.FileId, request.Username,
                                                         signResponse.Signature, cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(AddShareCommand request, CancellationToken cancellationToken)
        {
            var user = await _userRepository.FirstOrDefaultAsync(request.Sender, cancellationToken);

            if (user.PrivateKeyPassword != request.PrivateKeyPassword.Sha512())
            {
                throw new Exception("Invalid private key password");
            }

            var decryptionResponse = _encryptor.Decrypt(user.PrivateKey, request.PrivateKeyPassword.Sha256AsBytes());

            if (decryptionResponse.Status == Status.Failed)
            {
                throw new Exception();
            }

            request.Id = await _shareRepository.AddShareAsync(request.Title, request.Description, cancellationToken);

            await _shareRepository.AddShareUserAsync(request.Id, request.Sender, ShareUserOwnershipStatus.Sender, cancellationToken);

            foreach (var receiver in request.Receivers)
            {
                await _shareRepository.AddShareUserAsync(request.Id, receiver, ShareUserOwnershipStatus.Receiver, cancellationToken);
            }

            foreach (var file in request.Files)
            {
                var fileId = await _fileRepository.AddAsync(file.Name, file.Content, cancellationToken);

                await _userRepository.AssignFileAsync(request.Sender, fileId, UserFileStatus.Owner, cancellationToken);

                await _shareRepository.AddShareFileAsync(request.Id, fileId, cancellationToken);

                var signResponse = await _ecdsa.SignFileAsync(file.Name, file.Content, decryptionResponse.DecryptedText);

                await _shareRepository.AddShareFileUserAsync(request.Id, fileId, request.Sender,
                                                             signResponse.Signature, cancellationToken);
            }

            await _shareRepository.AcceptAsync(request.Id, request.Sender);

            return(Unit.Value);
        }