Skip to content

Commit

Permalink
Refresh the refreshToken
Browse files Browse the repository at this point in the history
  • Loading branch information
mwithi committed Sep 27, 2024
1 parent f8479c3 commit e09c400
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/isf/login/rest/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ public ResponseEntity<LoginResponse> authenticateUser(@Valid @RequestBody LoginR
String username = tokenProvider.getUsernameFromToken(refreshToken);
Authentication authentication = tokenProvider.getAuthenticationByUsername(username);
String newAccessToken = tokenProvider.generateJwtToken(authentication, false);
String newRefreshToken = tokenProvider.generateRefreshToken(authentication);

return ResponseEntity.ok(new LoginResponse(newAccessToken, refreshToken, username));
return ResponseEntity.ok(new LoginResponse(newAccessToken, newRefreshToken, username));
} else {
return ResponseEntity.badRequest().body("Invalid Refresh Token");
}
Expand Down
26 changes: 10 additions & 16 deletions src/test/java/org/isf/login/rest/LoginControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.isf.menu.manager.UserBrowsingManager;
import org.isf.menu.model.User;
import org.isf.security.CustomAuthenticationManager;
import org.isf.security.UserDetailsServiceImpl;
import org.isf.security.jwt.TokenProvider;
import org.isf.security.jwt.TokenValidationResult;
import org.isf.sessionaudit.manager.SessionAuditManager;
Expand All @@ -52,14 +51,12 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import io.jsonwebtoken.JwtException;
Expand All @@ -84,9 +81,6 @@ public class LoginControllerTest {
@Mock
private UserBrowsingManager userManager;

@MockBean
private UserDetailsServiceImpl customUserDetailsService;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -127,10 +121,9 @@ void testAuthenticateUser_Success() throws Exception {
String expectedJson = UserHelper.asJsonString(loginResponse);

// Perform the login request
MvcResult result = mvc.perform(
post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(UserHelper.asJsonString(loginRequest)))
mvc.perform(post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(UserHelper.asJsonString(loginRequest)))
.andExpect(status().isOk())
.andExpect(content().string(expectedJson))
.andReturn();
Expand All @@ -143,6 +136,7 @@ void testRefreshToken_Success() throws Exception {
String refreshToken = "validRefreshToken";
String newAccessToken = "newAccessToken";
String username = "testUser";
String newRefreshToken = "newValidRefreshToken";

// Create a mock TokenRefreshRequest object
TokenRefreshRequest request = new TokenRefreshRequest(refreshToken);
Expand All @@ -151,17 +145,17 @@ void testRefreshToken_Success() throws Exception {
when(tokenProvider.validateToken(refreshToken)).thenReturn(TokenValidationResult.VALID);
when(tokenProvider.getAuthenticationByUsername(username)).thenReturn(mock(Authentication.class));
when(tokenProvider.generateJwtToken(any(), eq(false))).thenReturn(newAccessToken);
when(tokenProvider.generateRefreshToken(any())).thenReturn(newRefreshToken);

// Expected LoginResponse object
LoginResponse loginResponse = new LoginResponse(newAccessToken, refreshToken, username);
LoginResponse loginResponse = new LoginResponse(newAccessToken, newRefreshToken, username);
String expectedJson = UserHelper.asJsonString(loginResponse);

// Perform POST request to refresh-token endpoint
var result = mvc.perform(
post("/auth/refresh-token")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(UserHelper.asJsonString(request)))
mvc.perform(post("/auth/refresh-token")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(UserHelper.asJsonString(request)))
.andExpect(status().isOk())
.andExpect(content().string(expectedJson))
.andReturn();
Expand Down

0 comments on commit e09c400

Please sign in to comment.