public TwoFactorAuthService_Tests() { // Arrange var mockOptionsAccessor = Substitute.For <IOptions <IdentityOptions> >(); var mockPasswordHasher = Substitute.For <IPasswordHasher <UserModel> >(); var mockUserValidators = Substitute.For <IEnumerable <IUserValidator <UserModel> > >(); var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >(); var mockKeyNormalizer = Substitute.For <ILookupNormalizer>(); var mockErrors = Substitute.For <IdentityErrorDescriber>(); var mockServices = Substitute.For <IServiceProvider>(); var mockUserLogger = Substitute.For <ILogger <UserManager <UserModel> > >(); var mockConfiguration = Substitute.For <IConfiguration>(); var a3SContextFake = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>()); var customUserStoreFake = new CustomUserStoreFake(a3SContextFake, mockConfiguration); customUserManagerFake = new CustomUserManagerFake(customUserStoreFake, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer, mockErrors, mockServices, mockUserLogger); mockedUserModel = new UserModel() { Id = Guid.NewGuid().ToString(), UserTokens = new List <UserTokenModel>() { new UserTokenModel(), new UserTokenModel(), new UserTokenModel(), } }; twoFactorAuthOTP = new TwoFactorAuthOTP() { UserId = Guid.NewGuid(), OTP = "232020" }; }
public abstract Task <IActionResult> ValidateTwoFactorAuthenticationOTPAsync([FromBody] TwoFactorAuthOTP twoFactorAuthOTP);
public async Task <ValidationResultResponse> ValidateTwoFactorAuthenticationOTPAsync(TwoFactorAuthOTP twoFactorAuthOTP) { var response = new ValidationResultResponse() { Messages = new List <string>() }; UserModel user = await userRepository.GetByIdAsync(twoFactorAuthOTP.UserId, true); if (user == null) { throw new ItemNotFoundException($"User with Id '{twoFactorAuthOTP.UserId}' not found while attempting to validate user OTP."); } // Confirm that this user has a valid authenticator registered if (!userManager.IsAuthenticatorTokenVerified(user)) { response.Success = false; return(response); } twoFactorAuthOTP.OTP = twoFactorAuthOTP.OTP.Replace(" ", string.Empty).Replace("-", string.Empty); response.Success = await userManager.VerifyTwoFactorTokenAsync(user, userManager.Options.Tokens.AuthenticatorTokenProvider, twoFactorAuthOTP.OTP); return(response); }
public async override Task <IActionResult> ValidateTwoFactorAuthenticationOTPAsync([FromBody] TwoFactorAuthOTP twoFactorAuthOTP) { if (twoFactorAuthOTP.UserId == Guid.Empty) { return(BadRequest()); } return(Ok(await twoFactorAuthService.ValidateTwoFactorAuthenticationOTPAsync(twoFactorAuthOTP))); }