medOS Gateway
Internal scope:
ever-api-gateway· Port: 3001 · Tier: core
The single ingress for all REST traffic into the medOS backend. The gateway terminates HTTP, verifies the caller's bearer token, and translates each request into a NATS / Moleculer service-mesh call — routing to the right microservice, aggregating multi-service responses, and applying global concerns (auth, CORS, body limits, request enrichment). No other service is exposed to the public network directly; everything reaches the mesh through here.
What it owns
The gateway is a thin but security-critical edge. It owns the HTTP surface, the route whitelist (which mesh actions are reachable from outside), the authentication handshake, and the OpenAPI documentation endpoints. Business logic lives downstream — the gateway's job is to admit, authenticate, and dispatch.
Responsibilities
- HTTP → NATS translation — incoming REST calls become Moleculer action calls over the broker.
- Route whitelisting — an explicit per-route allow-list controls exactly which
service.entity.*actions are reachable; anything not listed stays internal. - Authentication — dual-mode bearer verification (see below), with the resolved user, roles, and token propagated into request context (
ctx.meta). - Authorization hook — a route-level
authorizestep gates protected prefixes before the request fans out. - CORS, body parsing & limits — permissive CORS for the SPA plus per-route JSON / urlencoded body-size caps (20 MB on data routes, 1 MB on AI routes).
- OpenAPI surface — auto-generated API docs and a Swagger-style UI.
- Request enrichment & telemetry — client-IP resolution, geo lookup, and behavioral signal emission feeding downstream detection.
Authentication: dual-mode
The gateway accepts two token families on the standard Authorization: Bearer ... header and selects a path per request:
| Mode | Behavior |
|---|---|
| Keycloak (OIDC) | Tokens recognized as Keycloak are validated against the IdP's JWKS, then mapped to medOS users and roles. |
| Legacy JWT | Non-Keycloak tokens are verified via the auth service action aaa.auth.verifyToken. |
An AUTH_MODE flag selects policy: keycloak accepts only OIDC tokens, legacy accepts only the classic JWT path, and the default dual mode admits either. The resolved identity is attached to ctx.meta (user, roles, token, and an authSource marker) so downstream services never re-parse the token. Failures fail closed with an Unauthorized response rather than crashing the gateway.
Route map
client (HTTPS, SPA / external)
│ Authorization: Bearer <jwt|oidc>
▼
┌─────────────────────────────────────────────┐
│ medOS Gateway (:3001, Moleculer-Web) │
│ │
│ /api internal node + api.* actions │
│ /api/openapi spec + assets + UI │
│ /api/v2 authorized, whitelisted mesh │
│ /api/v2/ai auth'd AI proxy (voice order) │
└───────────────┬──────────────────────────────┘
│ ctx.call over NATS
▼
┌──────────┬──────────┬───────────┬─────────┐
│ aaa │ clinical │ financial │ medication ... │
│ (auth) │ │ │ │
└──────────┴──────────┴───────────┴─────────┘
The bulk of the public API lives under /api/v2, whose whitelist enumerates the reachable actions per service (for example aaa.auth.*, administration.patient.*, administration.encounter.*, medication.consultRequest.*). Adding a new externally callable action means adding it to this list — a deliberate allow-list, not auto-exposure.
Major modules
API Gateway service
The core Moleculer-Web mixin: route table, CORS, body parsers, the whitelist, and the authenticate / authorize hooks. The heart of the service.
Keycloak validator
OIDC token validation against the IdP JWKS, plus mapping of Keycloak claims to medOS users and roles.
OpenAPI service
Auto-generated OpenAPI spec, served assets, and a browsable docs UI under /api/openapi.
Voice-order proxy
A guarded /api/v2/ai route that forwards authenticated voice-order requests into the assistant pipeline.
RUDS enrichment
Client-IP, geo resolution, event enrichment and scoring that emit behavioral signals for rogue-user / threat detection.
Moleculer Labs
Optional broker introspection / dashboard mixin for observing mesh traffic in non-prod environments.
| Module | Source | Purpose |
|---|---|---|
| API Gateway service | ApiGatewayService.ts | Route table, CORS, body limits, whitelist, authenticate / authorize hooks |
| Keycloak validator | keycloak/ | OIDC token validation (JWKS) and claim → medOS user/role mapping |
| OpenAPI service | OpenApiService.ts | Auto-generated spec + assets + docs UI |
| Voice-order proxy | ai/voiceOrderProxy.ts | Authenticated AI voice-order route forwarding |
| RUDS enrichment | ruds/ | Client-IP, geo lookup, event enrichment, scoring & signal emission |
| Moleculer Labs | MoleculerLabsService.ts | Broker dashboard / introspection mixin |
Config flags
| Flag | Effect |
|---|---|
API_GATEWAY_PORT | HTTP listen port (defaults to 3001). |
AUTH_MODE | keycloak | legacy | unset (dual). Controls which token families are accepted. |
CORS is intentionally open at the gateway because TLS termination and origin policy are handled by the reverse proxy / load balancer in front of it.
Integration notes
- Identity — delegates legacy verification to the auth service (
aaa.auth.*) and OIDC to the external IdP. See Auth service. - Mesh — every downstream call travels over NATS via
ctx.call; the gateway holds no domain data of its own. - Telemetry — enrichment signals emitted here feed platform-wide behavioral threat detection. See Architecture.
Related pages
- Backend overview — full service catalog
- Auth service — JWT issuance, login, RBAC
- Architecture overview — mesh & data-flow model
Related catalog items
SYS-2— API GatewayST-7— Third-Party Access (OAuth)