private async Task <AuthorizationResponse> CreateNewJwtAndStoreWithRefreshToken(string refreshToken, string email, List <Claim> existingClaims) { string ip = GetIpAddress(); string jwtToken = _jwtTokenService.CreateJwtToken(existingClaims, email); string newRefreshToken = await _jwtTokenService.StoreRefreshToken(jwtToken, refreshToken, ip, email); var response = new AuthorizationResponse() { JwtToken = jwtToken, RefreshToken = newRefreshToken }; return(response); }
public IActionResult Token([FromBody] LoginViewModel model) { _logger.LogInformation("Login"); var loginServiceModel = _mapper.Map <LoginServiceModel>(model); var response = _jwtTokenService.CreateJwtToken(loginServiceModel); if (response == null) { Response.StatusCode = 400; return(BadRequest("Invalid username or password.")); } return(Ok(response)); }
public async Task Authenticate_should_return_jwt_and_refresh_token_logging_ip() { // given string ipAddress = "9.8.7.6"; string refreshToken = "refresh token"; string jwtToken = "jwt token"; string email = "*****@*****.**"; string password = "******"; var roadkillUser = new RoadkillIdentityUser() { Id = "1", UserName = email, NormalizedUserName = email.ToUpperInvariant(), Email = email, NormalizedEmail = email.ToUpperInvariant(), RoleClaims = new List <string>() }; var model = new AuthorizationRequest() { Email = email, Password = password }; _userManagerMock.FindByEmailAsync(email) .Returns(Task.FromResult(roadkillUser)); _signinManagerMock.PasswordSignInAsync(roadkillUser, password, true, false) .Returns(Task.FromResult(SignInResult.Success)); var claims = new List <Claim>() { new Claim("any", "thing") } as IList <Claim>; _userManagerMock.GetClaimsAsync(roadkillUser) .Returns(Task.FromResult(claims)); _jwtTokenService .CreateJwtToken(claims, roadkillUser.Email) .Returns(jwtToken); var httpContext = new DefaultHttpContext(); httpContext.Connection.RemoteIpAddress = IPAddress.Parse(ipAddress); _authorizationController.ControllerContext.HttpContext = httpContext; // TODO _jwtTokenService .StoreRefreshToken("TODO jwttoken", "TODO refresh token", email, ipAddress) .Returns(refreshToken); // when ActionResult <AuthorizationResponse> actionResult = await _authorizationController.Authenticate(model); // then actionResult.Result.ShouldBeOfType <OkObjectResult>(); var okResult = actionResult.Result as OkObjectResult; var response = okResult.Value as AuthorizationResponse; response.ShouldNotBeNull(); response.JwtToken.ShouldBe(jwtToken); response.RefreshToken.ShouldBe(refreshToken); }