diff --git a/src/main/java/com/example/holidayplanner/config/jwt/JwtRequestFilter.java b/src/main/java/com/example/holidayplanner/config/jwt/JwtRequestFilter.java index 4fc2794..9e8d8c7 100644 --- a/src/main/java/com/example/holidayplanner/config/jwt/JwtRequestFilter.java +++ b/src/main/java/com/example/holidayplanner/config/jwt/JwtRequestFilter.java @@ -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)); diff --git a/src/main/java/com/example/holidayplanner/config/jwt/JwtUtil.java b/src/main/java/com/example/holidayplanner/config/jwt/JwtUtil.java index a076a58..6008abc 100644 --- a/src/main/java/com/example/holidayplanner/config/jwt/JwtUtil.java +++ b/src/main/java/com/example/holidayplanner/config/jwt/JwtUtil.java @@ -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 claims = new HashMap<>(); return createToken(claims, userDetails); } diff --git a/src/main/java/com/example/holidayplanner/config/jwt/token/TokenController.java b/src/main/java/com/example/holidayplanner/config/jwt/token/TokenController.java new file mode 100644 index 0000000..f59a42f --- /dev/null +++ b/src/main/java/com/example/holidayplanner/config/jwt/token/TokenController.java @@ -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); + } +} diff --git a/src/main/java/com/example/holidayplanner/config/jwt/token/TokenService.java b/src/main/java/com/example/holidayplanner/config/jwt/token/TokenService.java index 72413fd..7473d46 100644 --- a/src/main/java/com/example/holidayplanner/config/jwt/token/TokenService.java +++ b/src/main/java/com/example/holidayplanner/config/jwt/token/TokenService.java @@ -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); } @@ -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); + } } diff --git a/src/main/java/com/example/holidayplanner/user/UserService.java b/src/main/java/com/example/holidayplanner/user/UserService.java index bf795c0..daefb7a 100644 --- a/src/main/java/com/example/holidayplanner/user/UserService.java +++ b/src/main/java/com/example/holidayplanner/user/UserService.java @@ -106,7 +106,7 @@ public ResponseEntity 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(); @@ -147,7 +147,7 @@ public ResponseEntity login(Map 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();