Overview
The Auth Service is a Node.js/Express microservice that provides JWT-based authentication using RSA-256 signing. It supports user registration, login, token verification, and exposes a JWKS endpoint for public key distribution.Source code:
node-services/auth-service/app.jsEntry point: node-services/auth-service/server.jsTechnology Stack
- Language: Node.js
- Framework: Express
- Database: PostgreSQL via pg driver
- Authentication: JWT with RSA-256 signing
- Password Hashing: bcrypt
- Observability: OpenTelemetry
Configuration
Environment Variables
HTTP server port for the auth service
PostgreSQL connection stringExample:
postgresql://devuser:devpass@postgres:5432/auth_dbIf not provided, the service operates in legacy mode (JWT-only without database)Secret used for JWT signing (deprecated in favor of RSA keys)This is kept for backward compatibility but not used with RSA signing
OpenTelemetry collector endpoint
Service name for distributed tracing
Docker Compose Configuration
API Reference
POST /auth/register
Register a new user with email and password.User’s email address (must be unique)
User’s password (will be hashed with bcrypt)
User ID assigned by the database
Registered email address
User role (default: “user”)
Example Request
Example Response
Error Responses
- 400 Bad Request: Missing email or password
- 409 Conflict: Email already exists
- 500 Internal Server Error: Database error
- 503 Service Unavailable: Database not configured
POST /auth/login
Authenticate a user and receive a JWT token.User’s email address
User’s password
JWT access token (RS256 signed, 24-hour expiry)
Example Request
Example Response
JWT Claims
The JWT token contains:Error Responses
- 400 Bad Request: Missing email or password
- 401 Unauthorized: Invalid email or password
- 500 Internal Server Error: Database error
GET /verify
Verify a JWT token and extract user information.Bearer token to verifyFormat:
Bearer <token>Always
true for successful verificationUser ID extracted from the token
Example Request
Example Response
X-User-Id header with the user ID.
Error Responses
- 401 Unauthorized: Missing bearer token or invalid token
GET /.well-known/jwks.json
Expose public keys for JWT verification (JWKS endpoint).Array of JWK (JSON Web Key) objects
Example Request
Example Response
The JWKS endpoint allows other services to verify JWT tokens without shared secrets. Services can fetch the public key and validate tokens independently.
GET /healthz
Health check endpoint.Example Request
Example Response
Error Responses
- 503 Service Unavailable: Database health check failed
Implementation Details
RSA Key Generation
The service generates an RSA key pair at startup: Fromnode-services/auth-service/app.js:12-17:
JWT Signing
Fromnode-services/auth-service/app.js:102-109:
Password Hashing
Fromnode-services/auth-service/app.js:62-67:
Legacy Mode
If no database is configured, the service operates in legacy mode: Fromnode-services/auth-service/app.js:84-92:
- No user validation
- Tokens are issued for any email/password combination
- Useful for development without database
Database Schema
The service expects ausers table (must be created manually or via migration):
Unlike the Go services, the Node.js services don’t include automatic migrations. You need to create the table manually or add a migration system.
Integration with Traefik
The auth service is used as a middleware by Traefik for protecting other services. Traefik middleware configuration indeploy/traefik/dynamic/middlewares.yml:
auth@file middleware:
- Traefik forwards the request to
/verifywith the Authorization header - Auth service validates the JWT
- If valid, Traefik forwards the original request with
X-User-Idheader - If invalid, Traefik returns 401 to the client
Security Considerations
Password Storage
- Passwords are hashed with bcrypt (cost factor 10)
- Never stored in plain text
- Salt is automatically generated by bcrypt
JWT Security
- Tokens signed with RSA-256 (asymmetric)
- 24-hour expiration
- Contains minimal claims (sub, role)
- Public key available via JWKS endpoint
Production Recommendations
Service Dependencies
Upstream Dependencies
- PostgreSQL: Optional, service runs in legacy mode without DB
Downstream Consumers
- Traefik: ForwardAuth middleware
- Frontend: Login/register flows
- All Protected Services: Via Traefik middleware
Testing
Test the service using curl:Observability
Logging
The service logs errors using console.error:Distributed Tracing
OpenTelemetry instrumentation captures:- HTTP requests
- Database queries
- JWT operations
- Error tracking
node-services/auth-service/tracing.js.
Performance Characteristics
- Register Latency: ~50-100ms (bcrypt hashing)
- Login Latency: ~50-100ms (bcrypt comparison)
- Verify Latency: ~1-5ms (JWT verification only)
- JWKS Latency: Less than 1ms (returns cached key)
- Database Pool: 10 connections
Common Issues
Database Not Configured
Log: Service runs but operates in legacy mode Solution: SetDATABASE_URL environment variable
Tokens Invalid After Restart
Cause: RSA keys regenerated on startup Solution: Load keys from persistent storage (Kubernetes Secrets)Email Already Exists
Error:409 Conflict: email already exists
Cause: Duplicate registration attempt
Solution: Return error to user or implement login flow
Invalid Token
Error:401 Unauthorized: invalid token
Causes:
- Token expired (>24 hours old)
- Token signed with different key (after restart)
- Token format invalid
- Token signature invalid
Related Services
Services Overview
Learn about service architecture
Deployment
Deploy with authentication