Skip to content

Commit 469a9e1

Browse files
authored
Merge pull request #10 from fells-code/messaging-service-integration
Feat: Messaging service integration
2 parents 0d96c4e + 3296ae2 commit 469a9e1

67 files changed

Lines changed: 5623 additions & 1844 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
# NODE
22
NODE_ENV=development
33

4+
# SERVER
5+
HOST=0.0.0.0
6+
PORT=5312
7+
48
# APPLICATION
5-
VERSION=1.0.0
69
APP_NAME=Seamless Auth Example
710
APP_ID=local-dev
8-
APP_ORIGIN=http://localhost:3000
11+
12+
# Who can call this server
13+
APP_ORIGINS=http://localhost:3000
14+
915
ISSUER=http://localhost:5312
1016

1117
# "web" for website to auth server, "server" for api server to auth server auth
1218
AUTH_MODE=server
13-
DEMO=true
1419

1520
# Roles assigned to every new user
1621
DEFAULT_ROLES=user,betaUser
1722
# Roles that are allowed in the system
1823
AVAILABLE_ROLES=user,admin,betaUser,team
1924

2025
# DATABASE
26+
# Prefer DATABASE_URL in containers and hosted environments if you already have one.
27+
# DATABASE_URL=postgres://myuser:mypassword@localhost:5432/seamless_auth
28+
# Set to true to see SQL from both the running app and startup migrations.
2129
DB_LOGGING=false
2230
DB_HOST=localhost
2331
DB_PORT=5432
@@ -31,20 +39,36 @@ REFRESH_TOKEN_TTL=1h
3139
RATE_LIMIT=100
3240
DELAY_AFTER=50
3341

34-
# OPTIONAL SERVICE TOKENS
35-
# Used only if enabling admin or m2m communications
42+
# SERVICE TOKENS
43+
# Required when AUTH_MODE=server.
3644
API_SERVICE_TOKEN=32-byte-hex-string
37-
38-
# JWKS
39-
JWKS_ACTIVE_KID=dev-main
45+
# Optional dedicated secret for indexed refresh-token lookup fingerprints.
46+
# If unset, the server falls back to API_SERVICE_TOKEN, and in development only
47+
# it will use a derived local secret.
48+
REFRESH_TOKEN_LOOKUP_SECRET=
4049

4150
# WEBAUTHN
4251
RPID=localhost
4352
ORIGINS=http://localhost:5173,http://localhost:5174
4453

4554
# ADMIN BOOTSTRAP
46-
# Enables bootstrap feature
4755
SEAMLESS_BOOTSTRAP_ENABLED=true
56+
SEAMLESS_BOOTSTRAP_SECRET=dev-bootstrap-secret-123
57+
58+
# OPTIONAL DIRECT DELIVERY
59+
# Needed only if this auth API sends OTPs or magic links itself.
60+
# Not needed when a SeamlessAuth server adapter handles external delivery.
61+
# Set to true to exercise direct email/SMS delivery even when NODE_ENV=development.
62+
MESSAGING_ENABLE_IN_DEV=false
63+
MESSAGING_AWS_REGION=us-east-1
64+
MESSAGING_EMAIL_FROM=noreply@example.com
65+
MESSAGING_SMS_PROVIDER=aws
66+
MESSAGING_SMS_FROM=
67+
MESSAGING_TWILIO_ACCOUNT_SID=
68+
MESSAGING_TWILIO_AUTH_TOKEN=
4869

49-
# Secret used to authorize bootstrap invite creation
50-
SEAMLESS_BOOTSTRAP_SECRET=dev-bootstrap-secret-123
70+
# PRODUCTION SIGNING AND JWKS SECRETS
71+
# Required when NODE_ENV=production.
72+
# SEAMLESS_JWKS_ACTIVE_KID=main-2026-04
73+
# SEAMLESS_JWKS_KEY_main-2026-04_PRIVATE="-----BEGIN PRIVATE KEY-----..."
74+
# JWKS_PUBLIC_KEYS={"keys":[{"kid":"main-2026-04","pem":"-----BEGIN PUBLIC KEY-----...","createdAt":"2026-04-22T00:00:00.000Z"}]}

AGENTS.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
## Seamless Auth API Agent Guide
2+
3+
This repository is an Express + TypeScript authentication service for passwordless auth flows. It supports WebAuthn/passkeys, OTP, magic links, JWKS publication, session management, admin endpoints, and a small system configuration layer persisted in Postgres.
4+
5+
Use this file as the fast path. For the deeper architecture walkthrough, see [docs/architecture.md](/Users/brandoncorbett/git/seamless-auth-api/docs/architecture.md).
6+
7+
## Start Here
8+
9+
- Install dependencies: `npm install`
10+
- Run in development: `npm run dev`
11+
- Type-check/build: `npm run build`
12+
- Lint: `npm run lint`
13+
- Run tests once: `npm run test:run`
14+
- Coverage: `npm run coverage`
15+
16+
The service starts from [src/server.ts](/Users/brandoncorbett/git/seamless-auth-api/src/server.ts), which initializes models, connects to the database, bootstraps `system_config`, builds the Express app, and begins listening.
17+
18+
## Core Runtime Shape
19+
20+
- [src/app.ts](/Users/brandoncorbett/git/seamless-auth-api/src/app.ts): global middleware, CORS, OpenAPI docs in dev, rate limiting, and top-level error handlers.
21+
- [src/lib/loadRoutes.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/loadRoutes.ts): dynamically imports every `*.routes.ts` file under `src/routes`.
22+
- [src/lib/createRouter.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/createRouter.ts): thin wrapper used by route modules.
23+
- [src/lib/defineRoute.ts](/Users/brandoncorbett/git/seamless-auth-api/src/lib/defineRoute.ts): registers Express handlers and OpenAPI metadata, validates request payloads, and optionally validates JSON responses.
24+
25+
When tracing behavior, start at the route file, then the controller, then the service/model/helper layers.
26+
27+
## Important Folders
28+
29+
- [src/routes](/Users/brandoncorbett/git/seamless-auth-api/src/routes): endpoint registration
30+
- [src/controllers](/Users/brandoncorbett/git/seamless-auth-api/src/controllers): request handling
31+
- [src/services](/Users/brandoncorbett/git/seamless-auth-api/src/services): session issuance, messaging, bootstrap promotion, auth event logging
32+
- [src/models](/Users/brandoncorbett/git/seamless-auth-api/src/models): Sequelize models
33+
- [src/config](/Users/brandoncorbett/git/seamless-auth-api/src/config): bootstrapped system config, required config metadata
34+
- [src/lib](/Users/brandoncorbett/git/seamless-auth-api/src/lib): routing, cookies, tokens, OpenAPI helpers
35+
- [src/middleware](/Users/brandoncorbett/git/seamless-auth-api/src/middleware): auth, rate limits, logging
36+
- [tests](/Users/brandoncorbett/git/seamless-auth-api/tests): unit, integration, e2e, and shared factories/setup
37+
38+
## Auth Model
39+
40+
There are three token states worth keeping straight:
41+
42+
- Ephemeral token: short-lived pre-auth token used to continue registration/login flows.
43+
- Access token: signed JWT used for authenticated application access.
44+
- Refresh token: opaque random token stored hashed in the `sessions` table.
45+
46+
Auth behavior depends on `AUTH_MODE`:
47+
48+
- `web`: access/refresh/ephemeral tokens are primarily stored in cookies.
49+
- `server`: endpoints expect bearer tokens more often and return token payloads in JSON.
50+
51+
Auth middleware is chosen centrally by [src/middleware/attachAuthMiddleware.ts](/Users/brandoncorbett/git/seamless-auth-api/src/middleware/attachAuthMiddleware.ts).
52+
53+
## Config Model
54+
55+
There are two config layers:
56+
57+
- Environment variables: needed to boot the process and connect external systems.
58+
- `system_config` table: runtime configuration such as token TTLs, origins, roles, and WebAuthn RP settings.
59+
60+
[src/config/bootstrapSystemConfig.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/bootstrapSystemConfig.ts) seeds missing `system_config` rows from env vars at startup. [src/config/getSystemConfig.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/getSystemConfig.ts) caches reads in-process, so remember to invalidate the cache after writes.
61+
62+
## Messaging And Delivery
63+
64+
OTP and magic link flows can operate in two modes:
65+
66+
- Direct delivery: the API sends email/SMS itself via [src/services/messagingService.ts](/Users/brandoncorbett/git/seamless-auth-api/src/services/messagingService.ts).
67+
- External delivery: callers send header `x-seamless-auth-delivery-mode: external` and the API returns delivery payloads instead of sending them directly.
68+
69+
Direct provider wiring currently lives in [src/config/directMessaging.ts](/Users/brandoncorbett/git/seamless-auth-api/src/config/directMessaging.ts).
70+
71+
## Working Safely In This Repo
72+
73+
- Prefer changing controllers/services over adding logic directly in routes.
74+
- When adding or updating routes, use `schemas`, not `schema`, so request parsing and OpenAPI generation both work.
75+
- If a route requires auth, prefer the `auth` option in `createRouter` definitions. That keeps middleware wiring and OpenAPI security metadata aligned.
76+
- If a route also needs admin checks or rate limiting, combine `auth` with extra `middleware`.
77+
- Be careful around cookie names and auth mode branching. Several flows have separate `web` and `server` response shapes.
78+
- Preserve existing local worktree changes unless the user explicitly asks you to clean them up.
79+
80+
## Before You Finish A Change
81+
82+
- Run `npm run build`
83+
- Run `npm run lint`
84+
- Run targeted tests for the touched area when practical
85+
- If you change request/response schemas or route auth, inspect the generated `/openapi.json` behavior in dev
86+
87+
## Known Maintenance Traps
88+
89+
- OpenAPI security metadata is only added when routes use the `auth` field in route definitions. Routes that manually call `attachAuthMiddleware(...)` can drift from the documented security model.
90+
- Cookie names in runtime code should be treated as the source of truth when checking auth behavior.
91+
- `system_config` values may come from the database even when `.env` changes, so config bugs are not always fixed by editing env vars alone.

LICENSE.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ For production deployments:
219219

220220
See [CONTRIBUTING.md](./CONTRIBUTING.md).
221221

222+
## Maintainer Docs
223+
224+
- [AGENTS.md](./AGENTS.md) for a fast codebase briefing aimed at coding agents and maintainers
225+
- [docs/architecture.md](./docs/architecture.md) for a deeper walkthrough of runtime flow, auth modes, config, and testing
226+
222227
## Security
223228

224229
**Do not open public issues for security vulnerabilities.**

0 commit comments

Comments
 (0)