Overview
The Justina Backend uses JWT (JSON Web Token) authentication to secure API endpoints and WebSocket connections. This stateless authentication mechanism ensures that only authorized users can access protected resources.Authentication Flow
JWT Token Structure
Justina tokens are signed using HMAC-SHA256 algorithm and contain the following claims:Header
Payload
Claims Explanation
Token issuer, always set to
Justina_BackendSubject - the username of the authenticated user
Unique identifier for the user in UUID format
User role, either
ROLE_SURGEON or ROLE_AIToken issued at timestamp (Unix epoch)
Token expiration timestamp (24 hours from issuance)
Token Expiration
JWT tokens are valid for 24 hours (86400 seconds) from the time of issuance. After expiration, users must log in again to receive a new token.Including JWT in Requests
Method 1: Authorization Header (Recommended)
Include the JWT token in theAuthorization header with the Bearer scheme:
Method 2: HttpOnly Cookie (Web Clients)
The login endpoint automatically sets an HttpOnly cookie namedjwt-token. Web browsers will include this cookie in subsequent requests automatically.
Cookie attributes:
HttpOnly: true- Prevents JavaScript access (XSS protection)Secure: true- Requires HTTPS in productionSameSite: None- Allows cross-origin requestsMax-Age: 86400- 24-hour expirationPath: /- Valid for all routes
Both authentication methods (header and cookie) are validated. Use the Authorization header for non-browser clients and APIs.
WebSocket Authentication
WebSocket connections require JWT authentication via query parameter since WebSocket handshake doesn’t support custom headers easily:Authentication Process
- Extract
tokenfrom query parameter - Validate JWT signature and expiration
- Extract
userId,username, androlefrom token claims - Store user information in WebSocket session attributes
- Allow or deny connection based on validation
Role-Based Authorization
After authentication, authorization is enforced based on user roles:ROLE_SURGEON (ROLE_CIRUJANO)
Surgeons can:- Access their own surgery trajectories
- Connect to
/ws/simulationfor telemetry streaming - View their user profile
ROLE_AI (ROLE_IA)
AI systems can:- Access all surgery trajectories
- Submit analysis and scoring data
- Connect to
/ws/aifor surgery notifications
Security Best Practices
Store Tokens Securely
- Web: Use HttpOnly cookies or sessionStorage (not localStorage)
- Mobile: Use secure keychain/keystore
- Backend: Environment variables for JWT secret
Password Security
Passwords are hashed using BCrypt with automatic salt generation before storage. Plain-text passwords are never stored in the database.Error Responses
401 Unauthorized
- Missing Authorization header
- Invalid or malformed token
- Expired token
- Invalid JWT signature
403 Forbidden
- Insufficient role permissions
- Surgeon accessing another surgeon’s data
Next Steps
Auth Endpoints
Learn how to login, register, and get user info
WebSocket Auth
Authenticate WebSocket connections