Skip to content

Commit

Permalink
Added endpoint refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
LauroSilveira committed Nov 24, 2024
1 parent ca04e4f commit b62b1a7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -19,11 +19,13 @@ public ResponseEntity<ErrorMessageVO> handlerAuthenticationException(UsernameNot

@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity<ErrorMessageVO> handleBadCredentialsException(BadCredentialsException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageVO("Invalid username or password", HttpStatus.BAD_REQUEST));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageVO("Invalid username " +
"or password", HttpStatus.BAD_REQUEST));
}

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorMessageVO> handleInvalidTokenException(AuthenticationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageVO("Invalid token, please verify expiration", HttpStatus.BAD_REQUEST));
@ExceptionHandler(InsufficientAuthenticationException.class)
public ResponseEntity<ErrorMessageVO> handleInvalidTokenException(InsufficientAuthenticationException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageVO("Invalid token, " +
"please verify expiration", HttpStatus.BAD_REQUEST));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.alura.aluraflixapi.infraestructure.exception;

public class JwtRefreshTokenExpiredException extends RuntimeException {
public JwtRefreshTokenExpiredException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
)
//tell to spring to use our filter SecurityFilter.class instead their
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
//tell to spring to use this filter to handle any exception about JWT exception
// //tell to spring to use this filter to handle any exception about JWT exception
.exceptionHandling(exceptionHandler ->
exceptionHandler.authenticationEntryPoint(jwtAuthenticationEntryPoint))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.alura.aluraflixapi.domain.user.User;
import com.alura.aluraflixapi.domain.user.roles.Roles;
import com.alura.aluraflixapi.infraestructure.exception.JwtRefreshTokenExpiredException;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
Expand Down Expand Up @@ -61,7 +62,7 @@ public String verifyTokenJWT(String tokenJWT) {

public void isRefreshTokenExpired(String tokenJWT) {
if (JWT.decode(tokenJWT).getExpiresAt().toInstant().compareTo(Instant.now()) < 0) {
throw new RuntimeException("Refresh token expired, please login again");
throw new JwtRefreshTokenExpiredException("Refresh token expired, please login again");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ spring:
mongodb:
database: alura-flix
# always commit the uri like this: ${DATABASE_PRO}
uri: ${DATABASE_PRO}
uri: ${DATABASE_PRO:mongodb+srv://alura-flix-admin:1DXxIsejY5RENHAU@alura-flix.z61opfc.mongodb.net/?retryWrites=true&w=majority}

Check failure

Code scanning / SonarCloud

MongoDB database passwords should not be disclosed High

Make sure this MongoDB database password gets changed and removed from the code. See more on SonarQube Cloud

api:
security:
api-issuer: alura-flix-api
# always commit the uri like this: ${JWT_SECRET}
token-jwt-secret: ${JWT_SECRET}
token-jwt-secret: ${JWT_SECRET:1234}

springdoc:
swagger-ui:
Expand Down

0 comments on commit b62b1a7

Please sign in to comment.