Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#109 Add an endpoint for refreshing tokens #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.zerhusen.security.rest.dto.LoginDto;
import org.zerhusen.security.jwt.JWTFilter;
import org.zerhusen.security.jwt.TokenProvider;
Expand Down Expand Up @@ -52,6 +49,18 @@ public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginDto loginDto)
return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);
}

@GetMapping("/token")
public ResponseEntity<JWTToken> refreshAuthenticationToken(Authentication authentication) {

String jwt = tokenProvider.createToken(authentication, false);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(JWTFilter.AUTHORIZATION_HEADER, "Bearer " + jwt);

return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);

}

/**
* Object to return as body in JWT Authentication.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.zerhusen.util.LogInUtils.getTokenForLogin;

public class AuthenticationRestControllerTest extends AbstractRestControllerTest {

Expand Down Expand Up @@ -57,4 +59,23 @@ public void unsuccessfulAuthenticationWithNotExistingUser() throws Exception {
.andExpect(content().string(not(containsString("id_token"))));
}

@Test
public void successfulRefreshToken() throws Exception {
final String token = getTokenForLogin("user", "password", getMockMvc());

getMockMvc().perform(get("/api/token")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + token))
.andExpect(status().isOk())
.andExpect(content().string(containsString("id_token")));

}

@Test
public void unsuccessfulRefreshToken() throws Exception{
getMockMvc().perform(get("/api/token")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized())
.andExpect(content().string(not(containsString("id_token"))));
}
}