Skip to content

feat: Anonymous WebSocket connections with Strategy Pattern - #101

Merged
tomassirio merged 7 commits into
mainfrom
feature/websocket_handling
Apr 1, 2026
Merged

feat: Anonymous WebSocket connections with Strategy Pattern#101
tomassirio merged 7 commits into
mainfrom
feature/websocket_handling

Conversation

@tomassirio

@tomassirio tomassirio commented Apr 1, 2026

Copy link
Copy Markdown
Owner

Problem

Guest users (not logged in) could not see real-time trip updates in the browser. The WebSocket server required authentication, causing connections to fail for anonymous users.

Solution

Implemented a Strategy Pattern for WebSocket authentication that supports both authenticated and anonymous connections with proper security controls.

Architecture

Strategy Pattern Components

  1. WebSocketAuthenticationStrategy - Interface defining authentication contract
  2. AuthenticationResult - Immutable result object with userId, auth status, and anonymous flag
  3. TokenAuthenticationStrategy - JWT token validation for authenticated users
  4. AnonymousAuthenticationStrategy - Fallback for guest users
  5. WebSocketAuthenticationService - Chain of responsibility orchestrator
  6. WebSocketAuthenticationConfig - Spring configuration defining strategy order

Security Features

  • Rate Limiting: Anonymous users limited to 10 topic subscriptions
  • Audit Logging: Tracks authenticated vs anonymous connections with remote address
  • Graceful Degradation: Invalid tokens fall back to anonymous mode
  • Resource Protection: Prevents subscription abuse
  • Read-Only Access: Anonymous users can only subscribe, not send commands

Strategy Order

  1. TokenAuthenticationStrategy (priority 1) - Tries JWT authentication first
  2. AnonymousAuthenticationStrategy (fallback) - Allows anonymous if no token

Code Quality

  • SOLID Principles: Single Responsibility, Open/Closed, Dependency Inversion
  • Design Patterns: Strategy + Chain of Responsibility
  • Extensibility: Easy to add OAuth, API keys, or other auth methods
  • Testability: Each strategy independently testable
  • Clean Architecture: Clear separation of concerns

Changes

  • WebSocketConnectionHandler.java - Uses strategy service instead of direct JWT validation
  • WebSocketSessionManager.java - Adds subscription counting and rate limiting
  • auth/WebSocketAuthenticationStrategy.java - Strategy interface
  • auth/AuthenticationResult.java - Immutable result object
  • auth/TokenAuthenticationStrategy.java - JWT validation strategy
  • auth/AnonymousAuthenticationStrategy.java - Anonymous fallback strategy
  • auth/WebSocketAuthenticationService.java - Strategy orchestrator
  • auth/WebSocketAuthenticationConfig.java - Spring bean configuration

Testing

# Compile and install
mvn clean install -DskipTests

# Verify compilation
mvn clean compile

Deployment

The frontend PR (#192) must be merged and deployed alongside this change.

Introduces a flexible, extensible authentication system for WebSocket
connections that supports both authenticated and anonymous users.

Architecture:
- Strategy Pattern: Pluggable authentication strategies
- Chain of Responsibility: Tries strategies in order until one succeeds
- SOLID Principles: Each strategy has a single responsibility

Components:
- WebSocketAuthenticationStrategy: Strategy interface
- AuthenticationResult: Immutable result object
- TokenAuthenticationStrategy: JWT token validation
- AnonymousAuthenticationStrategy: Guest user fallback
- WebSocketAuthenticationService: Orchestrates strategy chain
- WebSocketAuthenticationConfig: Defines strategy order

Security Features:
- Rate limiting: Anonymous users limited to 10 subscriptions
- Logging: Tracks authenticated vs anonymous connections with remote address
- Graceful degradation: Invalid tokens fall back to anonymous
- Resource protection: Prevents abuse via subscription limits

Benefits:
- Guest users can view public trip updates in real-time
- Authenticated users have unlimited subscriptions
- Easy to add new auth strategies (OAuth, API keys, etc.)
- Testable: Each strategy can be unit tested independently
- Maintainable: Clear separation of concerns
@tomassirio tomassirio changed the title Feature/websocket handling feat: Anonymous WebSocket connections with Strategy Pattern Apr 1, 2026
- Log session URI and query string
- Log when URI is null
- Log when query is empty
- Log token presence and length
- Will help diagnose why authenticated users are treated as anonymous
@tomassirio
tomassirio merged commit 1418cbf into main Apr 1, 2026
9 checks passed
@tomassirio
tomassirio deleted the feature/websocket_handling branch April 1, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant