public async Task <UserDto> Register(UserRegisterModel userModel) { UserDto foundUser = await userService.GetUserByNameAsync(userModel.Name); if (foundUser is not null) { throw new InvalidCredentialException("user with this name already exist"); } HashedPassword passwordHash = await encryptionService.EncryptAsync( Encoding.UTF8.GetBytes(userModel.Password)); User newUser = mapper.Map <UserRegisterModel, User>(userModel); newUser.PasswordHash = passwordHash.Hash; newUser.Salt = passwordHash.Salt; newUser.Jwt = jwtService.GenerateToken(newUser); User addedUser = await userRepository.AddAsync(newUser); await userRepository.SaveChangesAsync(); return(mapper.Map <User, UserDto>(addedUser)); }
public async Task <IActionResult> Encrypt(EncryptionModel model) { var result = new ExpandoObject(); result.TryAdd("data", await _encryptionService.EncryptAsync(model)); return(Ok(result)); }
private static async Task <string> _ResolveEncryptedQueryStringParameterValueAsync(this HttpContext context, object routeValues, IEncryptionService encryptionService) { var sb = new StringBuilder(); if (routeValues != null) { var d = new RouteValueDictionary(routeValues); for (var i = 0; i < d.Keys.Count; i++) { if (i > 0) { sb.Append("&"); } sb.Append($"{d.Keys.ElementAt(i)}={d.Values.ElementAt(i)}"); } } var result = string.Empty; if (sb.Length > 0 && (ResolveEncryptionSettings(context)?.EncryptUrls ?? false)) { result = Uri.EscapeDataString(await encryptionService.EncryptAsync(sb.ToString(), true)); } else { result = sb.ToString(); } return(result); }
public async Task ConfigureLoginAsync(CommandContext context, string username, string password) { await context.Message.DeleteAsync(); GuildConfig config = await guildConfig.FindOrCreateConfigAsync(context.Guild.Id); config.ApiLogin = new() { Username = username, Password = await _encryption.EncryptAsync(password) }; await guildConfig.SetLoginAsync(config); await context.Channel.SendMessageAsync("API credentials has been set."); }
public async Task AddEventsAsync(IEnumerable <IEvent <Guid> > events, CancellationToken token) { var encryptedEvents = new List <EncryptedEvent <Guid> >(); foreach (var @event in events) { var encrypted = await _encryptionService.EncryptAsync(@event, token); encryptedEvents.Add(encrypted); } await _mongoCollection.InsertManyAsync(encryptedEvents, cancellationToken : token); foreach (var @event in events.OrderByDescending(x => x.Timestamp)) { await _mediator.Publish(@event, token); } }
public async Task <bool> UpdatePasswordAsync(PasswordUpdateModel updatePasswordModel, int userId) { User user = await userRepository.GetByIdAsync(userId); if (user is null || !await encryptionService.PasswordEqualsHashAsync( updatePasswordModel.CurrentPassword, user.PasswordHash, user.Salt)) { return(false); } HashedPassword newHashedPassword = await encryptionService.EncryptAsync( Encoding.UTF8.GetBytes(updatePasswordModel.NewPassword)); user.PasswordHash = newHashedPassword.Hash; user.Salt = newHashedPassword.Salt; await userRepository.UpdateAsync(user); await userRepository.SaveChangesAsync(); return(true); }
public async Task <ApiResponse <CryptographyResponseModel> > Get() { Stopwatch stopwatch = null; ApiResponse <CryptographyResponseModel> response = new ApiResponse <CryptographyResponseModel>(); try { stopwatch = new Stopwatch(); stopwatch.Start(); response = new ApiResponse <CryptographyResponseModel>(); var cryptoSet = _encryptionService.GenerateCryptoKeySet(); string encrypted = await _encryptionService.EncryptAsync(cryptoSet, Guid.NewGuid().ToString()); string decrypted = await _encryptionService.DecryptAsync(cryptoSet, encrypted); response.Result = new CryptographyResponseModel() { Key = cryptoSet.Key, IV = cryptoSet.IV, Encrypted = encrypted, Decrypted = decrypted }; response.StatusCode = HttpStatusCode.OK; response.Timestamp = DateTime.UtcNow; stopwatch.Stop(); response.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds; } catch (Exception ex) { response = ApiResponse <CryptographyResponseModel> .CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } return(response); }
public async Task <IActionResult> Encrypt([FromBody] EncryptionModel model) { return(Ok(await _encryptionService.EncryptAsync(model))); }
public ActionResult <DecryptMessageModel> Encrypt([FromBody] EncryptMessageModel input) { return(Ok(_encryptionService.EncryptAsync(input))); }