private async Task <ResultWithError <ErrorData> > Process(ValidateAuthenticatorAppCommand request, CancellationToken cancellationToken) { var userMaybe = await this._userRepository.Find(this._currentUserService.CurrentUser.Value.UserId, cancellationToken); if (userMaybe.HasNoValue) { return(ResultWithError.Fail(new ErrorData(ErrorCodes.UserNotFound))); } var user = userMaybe.Value; var authApp = user.AuthenticatorApps.SingleOrDefault(x => x.WhenRevoked == null); if (authApp == null) { return(ResultWithError.Fail(new ErrorData(ErrorCodes.AuthenticatorAppAlreadyEnrolled))); } var secretBytes = Base32Encoding.ToBytes(authApp.Key); var topt = new Totp(secretBytes); var isVerified = topt.VerifyTotp(request.Token, out _); return(isVerified ? ResultWithError.Ok <ErrorData>() : ResultWithError.Fail(new ErrorData(ErrorCodes.FailedVerifyingAuthenticatorCode))); }
public async Task <ResultWithError <ErrorData> > Handle(ValidateAuthenticatorAppCommand request, CancellationToken cancellationToken) { var result = await this.Process(request, cancellationToken); var dbResult = await this._userRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); if (!dbResult) { return(ResultWithError.Fail(new ErrorData( ErrorCodes.SavingChanges, "Failed To Save Database"))); } return(result); }