Skip to content

Commit

Permalink
Create new endpoint to refresh access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
fizzy-fifs committed Sep 2, 2023
1 parent bc4e5a1 commit 706dd13
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
UserDetails userDetails = this.myUserDetailsService.loadUserByUsername(token.getOwner().getEmail());

if (!jwtUtil.validateAccessToken(token.getAccessToken()) && token.getRefreshTokenExpiration().after(new Date())) {
String newAccessToken = jwtUtil.generateToken(userDetails);
String newAccessToken = jwtUtil.generateAccessToken(userDetails);

token.setAccessToken(newAccessToken);
token.setAccessTokenExpiration(jwtUtil.extractExpiration(newAccessToken));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JwtUtil {

private final SecretKey secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);

public String generateToken(UserDetails userDetails) {
public String generateAccessToken(UserDetails userDetails) {
Map<String, Object> claims = new HashMap<>();
return createToken(claims, userDetails);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.holidayplanner.config.jwt.token;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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;

@RestController
@RequestMapping(path = "/api/v1.0/tokens")
@Tag(name = "Token")
@SecurityRequirement(name = "holidayPlannerSecurity")
public class TokenController {
@Autowired
private final TokenService tokenService;

public TokenController(TokenService tokenService) {
this.tokenService = tokenService;
}

@PostMapping(path = "/refreshaccesstoken")
public ResponseEntity refreshAccessToken(@RequestBody String refreshToken) {
return tokenService.refreshAccessToken(refreshToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package com.example.holidayplanner.config.jwt.token;

import com.example.holidayplanner.config.MyUserDetailsService;
import com.example.holidayplanner.config.jwt.JwtUtil;
import com.example.holidayplanner.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenService {
@Autowired
private final TokenRepository tokenRepository;

public TokenService(TokenRepository tokenRepository) {
@Autowired
private final JwtUtil jwtUtil;

@Autowired
private final MyUserDetailsService myUserDetailsService;

public TokenService(TokenRepository tokenRepository, JwtUtil jwtUtil, MyUserDetailsService myUserDetailsService) {
this.tokenRepository = tokenRepository;
this.jwtUtil = jwtUtil;
this.myUserDetailsService = myUserDetailsService;
}

public Token saveToken(Token token) {
return tokenRepository.save(token);
}

public Token findByUserId(User user) {
public Token findByOwner(User user) {
return tokenRepository.findByOwner(user);
}

Expand All @@ -29,4 +46,25 @@ public Token findByRefreshToken(String refreshToken) {
public void deleteToken(Token token) {
tokenRepository.delete(token);
}

public ResponseEntity refreshAccessToken(String refreshToken) {
Token token = findByRefreshToken(refreshToken);
if (token == null) {
return ResponseEntity.badRequest().body("Invalid refresh token");
}

if (token.getRefreshTokenExpiration().before(new Date())) {
return ResponseEntity.badRequest().body("Refresh token expired");
}

final UserDetails userDetails = myUserDetailsService.loadUserByUsername(token.getOwner().getEmail());
String newAccessToken = jwtUtil.generateAccessToken(userDetails);

token.setAccessToken(newAccessToken);
token.setAccessTokenExpiration(jwtUtil.extractExpiration(newAccessToken));

saveToken(token);

return ResponseEntity.ok().body(newAccessToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ResponseEntity<Object> create(User user) throws JsonProcessingException {

//Generate JWT
final UserDetails userDetails = myUserDetailsService.loadUserByUsername(user.getEmail());
final String accessToken = jwtTokenUtil.generateToken(userDetails);
final String accessToken = jwtTokenUtil.generateAccessToken(userDetails);
final String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails);

Token token = new Token();
Expand Down Expand Up @@ -147,7 +147,7 @@ public ResponseEntity<Object> login(Map<String, String> emailAndPassword) throws


final UserDetails userDetails = myUserDetailsService.loadUserByUsername(email);
final String accessToken = jwtTokenUtil.generateToken(userDetails);
final String accessToken = jwtTokenUtil.generateAccessToken(userDetails);
final String refreshToken = jwtTokenUtil.generateRefreshToken(userDetails);

Token token = new Token();
Expand Down

0 comments on commit 706dd13

Please sign in to comment.