-
Notifications
You must be signed in to change notification settings - Fork 0
fix: routes available without authentication #134
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
Open
driedpampas
wants to merge
10
commits into
main
Choose a base branch
from
fix/routes-without-authentication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
df06ec5
Update SecurityConfig.java
driedpampas d4603b4
feat: implement JWT authentication for WebSocket connections and add …
driedpampas 3b7625f
refactor: simplify exception handling and improve type inference in S…
driedpampas 3756b60
feat: enhance JWT authentication handling for WebSocket connections a…
driedpampas 1b572ce
refactor: remove WebSocket path exclusion from JwtAuthFilter and enha…
driedpampas 4b3a4ae
refactor: remove unused tests from JwtAuthFilterTest and improve exce…
driedpampas c1c0b2c
refactor: streamline authentication logic in JwtAuthFilter and improv…
driedpampas b23e62b
refactor: simplify StompJwtAuthInterceptorTest by using parameterized…
driedpampas ff58c59
fix: apply CodeRabbit auto-fixes
coderabbitai[bot] 55a6e88
fix: update .gitignore to include application-local.properties and en…
driedpampas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| # P2P-Shopping | ||
|
|
||
| Please check [CONTRIBUTING.md](/docs/CONTRIBUTING.md) for guidelines, and [HELP.md](/docs/HELP.md) for help and reference documentation. | ||
|
|
||
| ## Local development | ||
|
|
||
| You may create a local properties file at `src/main/resources/application-local.properties`, which overrides matching keys from `application.properties`. | ||
| To use it, run with the `local` Spring profile: | ||
|
|
||
| ```bash | ||
| SPRING_PROFILES_ACTIVE=local ./gradlew bootRun | ||
| ``` | ||
|
|
||
| In IntelliJ IDEA, you can set this up by following these steps: | ||
|
|
||
| - Click the configurations dropdown | ||
| - Click **Edit Configurations...** | ||
| - Select the only configuration under **Spring Boot** | ||
| - In the right pane, type *local* in the **Active profiles** box. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/p2ps/config/JwtHandshakeInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.p2ps.config; | ||
|
|
||
| import com.p2ps.auth.security.JwtAuthFilter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.server.ServerHttpRequest; | ||
| import org.springframework.http.server.ServerHttpResponse; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.socket.WebSocketHandler; | ||
| import org.springframework.web.socket.server.HandshakeInterceptor; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class JwtHandshakeInterceptor implements HandshakeInterceptor { | ||
|
|
||
| public static final String SESSION_TOKEN_ATTRIBUTE = "wsJwtToken"; | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(JwtHandshakeInterceptor.class); | ||
|
|
||
| private final JwtAuthFilter jwtAuthFilter; | ||
| private final boolean enableUrlToken; | ||
|
|
||
| public JwtHandshakeInterceptor(JwtAuthFilter jwtAuthFilter, | ||
| @Value("${websocket.compatibility.enableUrlToken:false}") boolean enableUrlToken) { | ||
| this.jwtAuthFilter = jwtAuthFilter; | ||
| this.enableUrlToken = enableUrlToken; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, | ||
| Map<String, Object> attributes) { | ||
| if (!enableUrlToken) { | ||
| return true; | ||
| } | ||
|
|
||
| String token = UriComponentsBuilder.fromUri(request.getURI()) | ||
| .build() | ||
| .getQueryParams() | ||
| .getFirst("token"); | ||
|
|
||
| if (token == null || token.isBlank()) { | ||
| return true; | ||
| } | ||
|
|
||
| if (jwtAuthFilter.authenticateToken(token) == null) { | ||
| logger.warn("Rejecting websocket handshake with invalid JWT query token"); | ||
| response.setStatusCode(HttpStatus.UNAUTHORIZED); | ||
| return false; | ||
| } | ||
|
|
||
| attributes.put(SESSION_TOKEN_ATTRIBUTE, token); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, | ||
| Exception exception) { | ||
| // No-op. | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/com/p2ps/config/StompJwtAuthInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.p2ps.config; | ||
|
|
||
| import com.p2ps.auth.security.JwtAuthFilter; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.springframework.messaging.Message; | ||
| import org.springframework.messaging.MessageChannel; | ||
| import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
| import org.springframework.messaging.simp.stomp.StompCommand; | ||
| import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
| import org.springframework.messaging.support.ChannelInterceptor; | ||
| import org.springframework.messaging.support.MessageBuilder; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.authentication.BadCredentialsException; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class StompJwtAuthInterceptor implements ChannelInterceptor { | ||
|
|
||
| private final JwtAuthFilter jwtAuthFilter; | ||
|
|
||
| public StompJwtAuthInterceptor(JwtAuthFilter jwtAuthFilter) { | ||
| this.jwtAuthFilter = jwtAuthFilter; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
|
Check failure on line 29 in src/main/java/com/p2ps/config/StompJwtAuthInterceptor.java
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
|
|
||
| if (StompCommand.CONNECT.equals(accessor.getCommand())) { | ||
| UsernamePasswordAuthenticationToken authentication = resolveAuthentication(accessor); | ||
|
|
||
| if (authentication != null) { | ||
| accessor.setUser(authentication); | ||
| return MessageBuilder.fromMessage(message) | ||
| .setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication) | ||
| .build(); | ||
| } | ||
|
|
||
| return message; | ||
| } | ||
|
|
||
| return message; | ||
| } | ||
|
|
||
| private UsernamePasswordAuthenticationToken resolveAuthentication(StompHeaderAccessor accessor) { | ||
| String token = resolveToken(accessor); | ||
| if (token == null) { | ||
| return null; | ||
| } | ||
|
|
||
| UsernamePasswordAuthenticationToken authentication = jwtAuthFilter.authenticateToken(token); | ||
| if (authentication == null) { | ||
| throw new BadCredentialsException("Invalid JWT token"); | ||
| } | ||
| return authentication; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private String resolveToken(StompHeaderAccessor accessor) { | ||
| String headerToken = accessor.getFirstNativeHeader("Authorization"); | ||
| if (headerToken == null) { | ||
| headerToken = accessor.getFirstNativeHeader("authorization"); | ||
| } | ||
| if (headerToken == null) { | ||
| headerToken = accessor.getFirstNativeHeader("token"); | ||
| } | ||
| if (headerToken == null) { | ||
| headerToken = accessor.getFirstNativeHeader("access_token"); | ||
| } | ||
|
|
||
| if (headerToken != null) { | ||
| return extractBearerToken(headerToken); | ||
| } | ||
|
|
||
| Map<String, Object> sessionAttributes = accessor.getSessionAttributes(); | ||
| if (sessionAttributes == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Object sessionToken = sessionAttributes.get(JwtHandshakeInterceptor.SESSION_TOKEN_ATTRIBUTE); | ||
| return sessionToken instanceof String string ? string : null; | ||
| } | ||
|
|
||
| private String extractBearerToken(String authorizationHeader) { | ||
| if (authorizationHeader == null || authorizationHeader.isBlank()) { | ||
| return null; | ||
| } | ||
|
|
||
| String token = authorizationHeader.trim(); | ||
| if (token.regionMatches(true, 0, "Bearer ", 0, 7)) { | ||
| return token.substring(7).trim(); | ||
| } | ||
|
|
||
| return token; | ||
| } | ||
driedpampas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.