Skip to content
Merged
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 @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import org.decepticons.linkshortener.api.exception.InvalidTokenException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -92,7 +93,7 @@ public boolean validateToken(
final String username = extractUsername(token);
return (username.equals(userDetails.getUsername())
&& !isTokenExpired(token));
} catch (JwtException | IllegalArgumentException e) {
} catch (InvalidTokenException e) {
return false;
}
}
Expand Down Expand Up @@ -162,13 +163,17 @@ private String createToken(
* @param token the JWT token
* @return the claims
*/

private Claims extractAllClaims(final String token) {
return Jwts.parserBuilder()
.setSigningKey(signingKey)
.build()
.parseClaimsJws(token)
.getBody();
try {
return Jwts.parserBuilder()
.setSigningKey(signingKey)
.build()
.parseClaimsJws(token)
.getBody();
} catch (JwtException | IllegalArgumentException ex) {
throw new InvalidTokenException(
"Invalid token provided during claims extraction.", ex);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,25 @@ public ResponseEntity<Map<String, Object>> handleShortLinkOutOfDate(
);
}

/**
* Handles exceptions for bad login credentials.
*
* @param ex The BadCredentialsException instance.
* @return A ResponseEntity with an UNAUTHORIZED status.
*/
@ExceptionHandler(
org.springframework.security.authentication
.BadCredentialsException.class
)
public ResponseEntity<Map<String, Object>> handleBadCredentials(
final org.springframework.security.authentication
.BadCredentialsException ex
) {
return buildErrorResponseSecurity(
HttpStatus.UNAUTHORIZED,
"Invalid Credentials",
ex.getMessage());
}


}