Ejemplo n.º 1
0
        public async Task UserLoginSuccess()
        {
            var email     = new Email("[email protected]");
            var password  = "******";
            var ipAddress = IPAddress.Parse("1.1.1.1"); //yeee, cloudflare dns
            var twoFaCode = "valid";

            A.CallTo(() => _userRepositoryService.FindAsync(A <Email> ._)).Returns(new User {
                Email = email, TwoFactorSecret = "secret"
            });
            A.CallTo(() => _passwordHash.IsValidHash(password, A <string> ._)).Returns(true);
            A.CallTo(() => _userRepositoryService.HasVerifiedEmailAsync(A <int> ._)).Returns(true);
            A.CallTo(() => _whiteListRepostirotyService.IsIpWhitelistedAsync(A <int> ._, ipAddress)).Returns(true);
            A.CallTo(() => _factorAuthenticatorManager.VerifyCode(A <string> ._, A <string> ._, A <string> ._)).Returns(true);

            var loginResult = await _userService.LoginUser(email, password, twoFaCode,
                                                           new ConnectionInfo { IpAddress = ipAddress, BrowserInfo = new BrowserInfo() });

            Assert.True(loginResult.Success);
            Assert.Equal(LoginError.None, loginResult.Error);

            A.CallTo(() => _httpRequestParser.ExecuteAsType <IpLookupResource>(A <RequestMessage> ._)).MustHaveHappened();
            A.CallTo(() => _loginAttemptRepoService.AddLoginAttemptsAsync(A <LoginAttempt> ._)).MustHaveHappened();
            A.CallTo(() => _whiteListRepostirotyService.IsIpWhitelistedAsync(A <int> ._, ipAddress)).MustHaveHappened();
            A.CallTo(() => _passwordHash.IsValidHash(password, A <string> ._)).MustHaveHappened();
            A.CallTo(() => _userRepositoryService.HasVerifiedEmailAsync(A <int> ._)).MustHaveHappened();
            A.CallTo(() => _refreshTokenRepositoryService.AddRefreshToken(A <RefreshToken> ._)).MustHaveHappened();
            A.CallTo(() => _jwtService.CreateLoginToken(A <User> ._, A <IPAddress> ._, A <string> ._, A <string> ._, A <TimeSpan?> ._)).MustHaveHappened();
            A.CallTo(() => _factorAuthenticatorManager.VerifyCode(twoFaCode, "secret", email.Value)).MustHaveHappened();
        }
        public async Task <IActionResult> Test2Fa(string code)
        {
            var user = await _userService.FindAsync(int.Parse(User.Identity.Name));

            var isValid = _twoFactorAuthenticatorManager.VerifyCode(code, user.TwoFactorSecret, user.Email.Value);

            return(Ok(new { IsValid = isValid }));
        }