public async Task CheckVCode() { string key = RandomExtensions.NextString(10); var code = await verificationCodesService.CreateVerificationCodeAsync(DateTime.UtcNow.ToUnixTime(), key); Assert.True(await verificationCodesService.IsVerificationCodeValidAsync(key, null, code)); Assert.False(await verificationCodesService.IsVerificationCodeValidAsync(key, null, RandomExtensions.NextInt16(1000, 9999))); }
public async Task <TokenVm> UserIdVCodeCreateTokenPairAsync(long userId, short vCode, string deviceTokenId) { using (MessengerDbContext context = contextFactory.Create()) { User targetUser = await context.Users .Include(opt => opt.Phones) .FirstOrDefaultAsync(user => user.Id == userId && !user.Deleted) .ConfigureAwait(false); if (targetUser != null) { if (!await verificationCodesService.IsVerificationCodeValidAsync( targetUser.Phones.FirstOrDefault().PhoneNumber, userId, vCode).ConfigureAwait(false)) { throw new WrongVerificationCodeException(); } string accessToken = RandomExtensions.NextString(64); string refreshToken = RandomExtensions.NextString(64); Token tokenPair = new Token() { UserId = userId, AccessToken = accessToken, RefreshToken = refreshToken, AccessTokenExpirationTime = DateTime.UtcNow.AddHours(ACCESS_LIFETIME_HOUR).ToUnixTime(), RefreshTokenExpirationTime = DateTime.UtcNow.AddHours(REFRESH_LIFETIME_HOUR).ToUnixTime(), DeviceTokenId = deviceTokenId }; context.Users.Update(targetUser); await context.Tokens.AddAsync(tokenPair).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); return(TokenConverter.GetTokenVm(tokenPair)); } throw new UserNotFoundException(); } }
public async Task <Response> CreateResponseAsync() { try { if (await verificationCodesService.IsVerificationCodeValidAsync(clientConnection.UserId.GetValueOrDefault().ToString(), clientConnection.UserId, request.VCode).ConfigureAwait(false)) { await deleteUsersService.DeleteUserAsync(clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); nodeNoticeService.SendDeleteUsersNodeNoticeAsync(clientConnection.UserId.GetValueOrDefault()); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateDeleteUserSegmentAsync( clientConnection.UserId.GetValueOrDefault(), new List <long> { NodeSettings.Configs.Node.Id }, NodeSettings.Configs.Node.Id).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); Task disconnectTask = new Task(async() => { try { List <ClientConnection> clientConnections = connectionsService .GetClientConnections(new List <long> { clientConnection.UserId.Value }) .Where(connection => connection != clientConnection) .ToList(); foreach (var connection in clientConnections) { await connection.ClientSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "User deleted.", CancellationToken.None).ConfigureAwait(false); } await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false); await clientConnection.ClientSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "User deleted.", CancellationToken.None).ConfigureAwait(false); } catch { return; } }); disconnectTask.Start(); return(new ResultResponse(request.RequestId)); } return(new ResultResponse(request.RequestId, null, ObjectsLibrary.Enums.ErrorCode.WrongVerificationCode)); } catch (Exception ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Can't delete user.", ObjectsLibrary.Enums.ErrorCode.DeleteUserProblem)); } }