예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
0
        public UserModel Map(UserRecord user)
        {
            var userModel = new UserModel
            {
                Identifier = user.Identifier,
                Username   = _encryptor.Decrypt(user.Username),
                Picture    = _encryptor.Decrypt(user.Picture)
            };

            if (user.Latitude != null && user.Longitude != null)
            {
                double.TryParse(_encryptor.Decrypt(user.Latitude), out var latitude);
                double.TryParse(_encryptor.Decrypt(user.Longitude), out var longitude);
                userModel.Latitude  = latitude;
                userModel.Longitude = longitude;
            }

            return(userModel);
        }
        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);
        }
예제 #5
0
        public CreateUserResponse CreateUser(CreateUserRequest request)
        {
            var response = new CreateUserResponse();

            using (var context = new DatabaseContext())
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var user = context.Users.FirstOrDefault(x => _encryptor.Decrypt(x.UserToken) == request.User.UserToken);

                        if (user == null)
                        {
                            context.Add(new UserRecord
                            {
                                UserToken = _encryptor.Encrypt(request.User.UserToken),
                                Username  = _encryptor.Encrypt(request.User.Username),
                                Picture   = _encryptor.Encrypt(request.User.Picture),
                                Active    = true
                            });

                            context.SaveChanges();
                            transaction.Commit();
                        }
                        else
                        {
                            if (user.Active)
                            {
                                throw new UserAlreadyExistsException("User has already been registered");
                            }

                            user.UserToken = _encryptor.Encrypt(request.User.UserToken);
                            user.Username  = _encryptor.Encrypt(request.User.Username);
                            user.Picture   = _encryptor.Encrypt(request.User.Picture);
                            user.Active    = true;

                            context.SaveChanges();
                            transaction.Commit();
                        }
                    }
                    catch (UserAlreadyExistsException exception)
                    {
                        transaction.Rollback();
                        response.AddError(new Error
                        {
                            Code             = ErrorCodes.UserAlreadyExists,
                            UserMessage      = "User with that UserToken already exists.",
                            TechnicalMessage = $"The following exception was thrown: {exception.Message}"
                        });
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        response.AddError(new Error
                        {
                            Code             = ErrorCodes.DatabaseError,
                            UserMessage      = "Something went wrong when creating your account. Please try again later.",
                            TechnicalMessage = $"The following exception was thrown: {exception.Message}"
                        });
                    }
                }

            return(response);
        }
예제 #6
0
        private string DecryptPassword(string encryptedPassword)
        {
            var str = _encryptionService.Decrypt(encryptedPassword);

            return(str);
        }