harbor run -t openthoughts/token-auth-websocketharbor download openthoughts/token-auth-websocketYou are a backend engineer at a real-time communication platform. The platform requires a unified authentication system that handles concurrent access from multiple clients (Browser tabs, API consumers, WebSocket connections). The system must maintain strict security and consistency guarantees using a centralized database state.
Your task is to implement a token-based authentication system that issues and manages three types of tokens with different lifecycles and usage patterns, ensuring race-condition-free operations.
TASK: Implement a unified authentication system with Access Token, Refresh Token, and WebSocket One-Time Token management, ensuring atomic operations and race-condition safety using H2 database storage.
INPUT:
ENVIRONMENT:
1. Access Token
Authorization header (Bearer <token>) for API requests.2. Refresh Token
3. One-Time Token
REQUIREMENTS:
1.1. Provide a login endpoint (POST /api/auth/login) that accepts username and password.
1.2. Upon successful login, issue three tokens atomically:
1.3. Store all tokens in the H2 database with metadata:
1.4. Return tokens in response:
REQUIREMENTS:
2.1. Provide an endpoint (POST /api/auth/renew) for Access Token renewal.
refreshToken) OR Request Header (key: refreshToken).2.2. Implement Refresh Token Rotation:
2.3. Handle concurrent renewal attempts:
2.4. Return renewed tokens:
REQUIREMENTS:
3.1. Provide an endpoint (POST /api/auth/validate-token) to validate and consume one-time tokens.
3.2. Validate token atomically:
3.3. Handle concurrent validation attempts:
REQUIREMENTS:
4.1. Provide a logout endpoint (POST /api/auth/logout).
Authorization header.4.2. Invalidate all tokens for the authenticated user.
4.3. Return success response (200).
REQUIREMENTS:
5.1. Implement a scheduled background task (e.g., every 10 seconds) to clean up tokens.
5.2. Verification of cleanup:
[CLEANUP] Deleted X tokens whenever the cleanup runs (where X is the count).POST /api/auth/login
{ "username": "string", "password": "string" }{ "accessToken": "string", "oneTimeToken": "string" }refreshToken=<token>; HttpOnly; Max-Age=86400POST /api/auth/renew
refreshToken=<token> OR Header refreshToken: <token>{ "accessToken": "string" }refreshToken=<new-token>; HttpOnlyPOST /api/auth/logout
Authorization: Bearer <accessToken>{ "message": "Logged out successfully" }POST /api/auth/validate-token
{ "token": "string" }{ "valid": true, "message": "..." }{ "valid": false, "message": "..." }OUTPUT:
app.jar/app/build/libs/app.jar