@prsm/auth 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +226 -0
  2. package/index.d.ts +19 -0
  3. package/package.json +76 -0
  4. package/src/__tests__/auth.test.js +1171 -0
  5. package/src/__tests__/impersonation-test-setup.js +208 -0
  6. package/src/__tests__/impersonation.test.js +473 -0
  7. package/src/__tests__/oauth-test-setup.js +136 -0
  8. package/src/__tests__/oauth.test.js +400 -0
  9. package/src/__tests__/prsm.test.js +215 -0
  10. package/src/__tests__/test-setup.js +385 -0
  11. package/src/__tests__/totp.test.js +158 -0
  12. package/src/__tests__/two-factor-test-setup.js +331 -0
  13. package/src/__tests__/two-factor.test.js +396 -0
  14. package/src/activity-logger.js +228 -0
  15. package/src/auth-context.js +120 -0
  16. package/src/auth-functions.js +520 -0
  17. package/src/auth-manager.js +1371 -0
  18. package/src/errors.js +173 -0
  19. package/src/hooks.js +41 -0
  20. package/src/index.js +23 -0
  21. package/src/invalidation.js +166 -0
  22. package/src/middleware.js +33 -0
  23. package/src/providers/azure-provider.js +114 -0
  24. package/src/providers/base-provider.js +152 -0
  25. package/src/providers/github-provider.js +86 -0
  26. package/src/providers/google-provider.js +76 -0
  27. package/src/providers/index.js +4 -0
  28. package/src/queries.js +543 -0
  29. package/src/schema.js +261 -0
  30. package/src/totp.js +221 -0
  31. package/src/two-factor/index.js +3 -0
  32. package/src/two-factor/otp-provider.js +128 -0
  33. package/src/two-factor/totp-provider.js +98 -0
  34. package/src/two-factor/two-factor-manager.js +676 -0
  35. package/src/types.js +399 -0
  36. package/src/user-roles.js +128 -0
  37. package/src/util.js +32 -0
  38. package/types/activity-logger.d.ts +73 -0
  39. package/types/auth-context.d.ts +88 -0
  40. package/types/auth-functions.d.ts +151 -0
  41. package/types/auth-manager.d.ts +365 -0
  42. package/types/errors.d.ts +108 -0
  43. package/types/hooks.d.ts +30 -0
  44. package/types/index.d.ts +13 -0
  45. package/types/invalidation.d.ts +40 -0
  46. package/types/middleware.d.ts +11 -0
  47. package/types/providers/azure-provider.d.ts +35 -0
  48. package/types/providers/base-provider.d.ts +52 -0
  49. package/types/providers/github-provider.d.ts +29 -0
  50. package/types/providers/google-provider.d.ts +29 -0
  51. package/types/providers/index.d.ts +4 -0
  52. package/types/queries.d.ts +287 -0
  53. package/types/schema.d.ts +37 -0
  54. package/types/totp.d.ts +72 -0
  55. package/types/two-factor/index.d.ts +3 -0
  56. package/types/two-factor/otp-provider.d.ts +57 -0
  57. package/types/two-factor/totp-provider.d.ts +58 -0
  58. package/types/two-factor/two-factor-manager.d.ts +191 -0
  59. package/types/types.d.ts +688 -0
  60. package/types/user-roles.d.ts +47 -0
  61. package/types/util.d.ts +3 -0
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ <img src="logo.svg" alt="@prsm/auth" width="96" height="96">
2
+
3
+ # @prsm/auth
4
+
5
+ [![test](https://github.com/prsmjs/auth/actions/workflows/test.yml/badge.svg)](https://github.com/prsmjs/auth/actions/workflows/test.yml)
6
+ [![npm](https://img.shields.io/npm/v/@prsm/auth)](https://www.npmjs.com/package/@prsm/auth)
7
+
8
+ PostgreSQL-backed authentication for Express. It owns its own auth tables and links to your user records through `user_id`, so it stays out of the way of however you model application users. One middleware attaches everything to `req.auth`: registration, login, sessions, remember-me, email confirmation, password reset, OAuth, role bitmasks, two-factor authentication, and audited impersonation.
9
+
10
+ It runs on a single shared PostgreSQL database behind any number of stateless app instances. Session storage is delegated to `express-session`, so you pick the store. Optional PostgreSQL `LISTEN/NOTIFY` propagates bans, role changes, and force-logouts across the fleet the instant they happen, with no Redis required.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install @prsm/auth express express-session pg
16
+ ```
17
+
18
+ For remember-me cookies, also mount `cookie-parser`:
19
+
20
+ ```bash
21
+ npm install cookie-parser
22
+ ```
23
+
24
+ Requires Node 24+.
25
+
26
+ ## Quick start
27
+
28
+ ```js
29
+ import express from "express"
30
+ import session from "express-session"
31
+ import cookieParser from "cookie-parser"
32
+ import pg from "pg"
33
+ import { createAuthMiddleware, createAuthTables, AuthRole } from "@prsm/auth"
34
+
35
+ const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL })
36
+
37
+ const authConfig = {
38
+ db: pool,
39
+ tablePrefix: "auth_",
40
+ }
41
+
42
+ await createAuthTables(authConfig)
43
+
44
+ const app = express()
45
+ app.use(express.json())
46
+ app.use(cookieParser())
47
+ app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false }))
48
+ app.use(createAuthMiddleware(authConfig))
49
+
50
+ app.post("/register", async (req, res) => {
51
+ const account = await req.auth.register(req.body.email, req.body.password)
52
+ res.json({ id: account.id })
53
+ })
54
+
55
+ app.post("/login", async (req, res) => {
56
+ await req.auth.login(req.body.email, req.body.password, req.body.remember)
57
+ res.json({ ok: true })
58
+ })
59
+
60
+ app.get("/me", (req, res) => {
61
+ if (!req.auth.isLoggedIn()) return res.status(401).json({ error: "not logged in" })
62
+ res.json({ id: req.auth.getId(), email: req.auth.getEmail(), roles: req.auth.getRoleNames() })
63
+ })
64
+ ```
65
+
66
+ Everything else hangs off `req.auth`. Mount `createAuthMiddleware` after `express-session` and `cookie-parser`, and call `createAuthTables(config)` once before the first request.
67
+
68
+ ## Role-based access
69
+
70
+ Roles are integer bitmasks. Use the built-in `AuthRole` set or define your own:
71
+
72
+ ```js
73
+ import { defineRoles } from "@prsm/auth"
74
+
75
+ export const Roles = defineRoles("owner", "editor", "viewer")
76
+
77
+ function requireRole(role) {
78
+ return async (req, res, next) => {
79
+ if (!req.auth.isLoggedIn()) return res.status(401).json({ error: "not logged in" })
80
+ if (!(await req.auth.hasRole(role))) return res.status(403).json({ error: "forbidden" })
81
+ next()
82
+ }
83
+ }
84
+
85
+ app.get("/admin", requireRole(Roles.owner), (req, res) => res.json({ ok: true }))
86
+ ```
87
+
88
+ Pass your role set as `config.roles` so `req.auth.getRoleNames()` and the devtools panel render the right names.
89
+
90
+ ## Two-factor authentication
91
+
92
+ TOTP (authenticator apps), email OTP, SMS OTP, and backup codes are built in. Enable it in config, then drive enrollment and the second-factor login step through `req.auth.twoFactor`:
93
+
94
+ ```js
95
+ const authConfig = { db: pool, twoFactor: { enabled: true, issuer: "MyApp" } }
96
+
97
+ // enrollment
98
+ const setup = await req.auth.twoFactor.setup.totp(true)
99
+ // render setup.qrCode, then confirm with a code from the app:
100
+ const backupCodes = await req.auth.twoFactor.complete.totp(req.body.code)
101
+
102
+ // login: req.auth.login throws SecondFactorRequiredError when 2FA is required
103
+ app.post("/login/2fa", async (req, res) => {
104
+ await req.auth.twoFactor.verify.totp(req.body.code)
105
+ await req.auth.completeTwoFactorLogin()
106
+ res.json({ ok: true })
107
+ })
108
+ ```
109
+
110
+ The TOTP implementation (RFC 6238) is built in, so there is no separate dependency to install or keep in sync.
111
+
112
+ ## OAuth
113
+
114
+ GitHub, Google, and Azure are supported. Configure providers and the flow is route-driven through `req.auth.providers`:
115
+
116
+ ```js
117
+ const authConfig = {
118
+ db: pool,
119
+ createUser: async (userData) => (await appDb.users.create(userData)).id,
120
+ providers: {
121
+ github: { clientId, clientSecret, redirectUri: "https://app.example.com/auth/github/callback" },
122
+ },
123
+ }
124
+
125
+ app.get("/auth/github", (req, res) => res.redirect(req.auth.providers.github.getAuthUrl()))
126
+ app.get("/auth/github/callback", async (req, res) => {
127
+ await req.auth.providers.github.handleCallback(req)
128
+ res.redirect("/dashboard")
129
+ })
130
+ ```
131
+
132
+ ## Impersonation
133
+
134
+ Audited impersonation preserves the original actor so support sessions stay traceable:
135
+
136
+ ```js
137
+ const authConfig = {
138
+ db: pool,
139
+ impersonation: {
140
+ enabled: true,
141
+ maxTtl: "1h",
142
+ canImpersonate: (actor, target) => (actor.rolemask & AuthRole.Admin) !== 0,
143
+ },
144
+ }
145
+
146
+ await req.auth.startImpersonation({ email: "customer@example.com" }, { reason: "ticket #123", ttl: "30m" })
147
+ // activity rows record actor_account_id throughout
148
+ await req.auth.stopImpersonation()
149
+ ```
150
+
151
+ ## Cross-instance invalidation
152
+
153
+ By default a session re-reads account state from the database on an interval (`resyncInterval`, default `30s`), so a ban or role change can take up to that long to reach instances that already cached the session. Turn on `LISTEN/NOTIFY` and those changes propagate immediately:
154
+
155
+ ```js
156
+ const authConfig = {
157
+ db: pool,
158
+ invalidation: { listen: true },
159
+ }
160
+ ```
161
+
162
+ When enabled, security-relevant writes (force-logout, status, role, password) emit a notification and every instance drops the affected session on its next request. It uses the PostgreSQL connection you already have. If the listener connection is unavailable (for example, a pooler in transaction mode), it falls back to interval-based resync automatically.
163
+
164
+ ## Observability and rate limiting
165
+
166
+ Both are optional and duck-typed, so the package never depends on them:
167
+
168
+ ```js
169
+ import { createTracer } from "@prsm/trace"
170
+ import { tokenBucket } from "@prsm/limit"
171
+
172
+ const authConfig = {
173
+ db: pool,
174
+ tracer: createTracer({ service: "api" }), // login is wrapped in a span
175
+ limiter: tokenBucket({ redis, capacity: 5, refillRate: 5, refillInterval: "1m" }), // throttles login
176
+ }
177
+ ```
178
+
179
+ When a limiter is configured, login attempts are throttled per email and a `RateLimitedError` is thrown once the limit is hit. Without these options, behavior is unchanged.
180
+
181
+ ## Requestless context and admin tooling
182
+
183
+ `createAuthContext(config)` gives you the same user-management operations without a request, for scripts, workers, and cron jobs. The same object is the binding surface for the [`@prsm/devtools`](https://github.com/prsmjs/devtools) admin panel: it exposes `listAccounts`, `getAccount`, `getStats`, `getRecentActivity`, `getRoles`, and the role/status/force-logout/impersonation actions.
184
+
185
+ ```js
186
+ import { createAuthContext, AuthStatus } from "@prsm/auth"
187
+
188
+ const auth = createAuthContext(authConfig)
189
+ await auth.setStatusForUserBy({ email: "abusive@example.com" }, AuthStatus.Banned)
190
+ await auth.forceLogoutForUserBy({ email: "abusive@example.com" })
191
+
192
+ const { accounts, total } = await auth.listAccounts({ search: "@example.com", limit: 50 })
193
+ ```
194
+
195
+ ## Maintenance
196
+
197
+ ```js
198
+ import { cleanupExpiredTokens, getAuthTableStats } from "@prsm/auth"
199
+
200
+ await cleanupExpiredTokens(authConfig) // run on a schedule
201
+ const stats = await getAuthTableStats(authConfig)
202
+ ```
203
+
204
+ ## Documentation
205
+
206
+ Deeper guides live in [`docs/`](./docs):
207
+
208
+ - [Express middleware](./docs/middleware.md) - setup, the `req.auth` surface, and the full config reference
209
+ - [Sessions and resync](./docs/sessions.md) - how the middleware keeps session state fresh
210
+ - [Registration and confirmation](./docs/registration.md) - account creation with optional email verification
211
+ - [Authentication and MFA](./docs/authentication.md) - login flow, 2FA challenges, remember-me
212
+ - [MFA patterns](./docs/mfa.md) - TOTP, email and SMS OTP, backup codes, delivery
213
+ - [Roles](./docs/roles.md) - bitmask roles with `defineRoles` and built-in defaults
214
+ - [Password reset](./docs/password-reset.md) - forgot-password with secure tokens
215
+ - [Multi-tenant mapping](./docs/multi-tenant.md) - linking auth accounts to your own user tables
216
+ - [OAuth providers](./docs/providers.md) - GitHub, Google, and Azure
217
+ - [Impersonation](./docs/impersonation.md) - admin-as-user sessions with actor preservation and audit
218
+ - [Standalone and requestless auth](./docs/standalone.md) - `createAuthContext` and `authenticateRequest`
219
+ - [Admin panel with devtools](./docs/devtools.md) - binding the admin dashboard through `@prsm/devtools`
220
+ - [Cross-instance invalidation, tracing, and rate limiting](./docs/invalidation.md) - the optional prsm integrations
221
+ - [API reference](./docs/api.md) - the full method surface
222
+ - [Errors](./docs/errors.md) - every error, when and why it is thrown
223
+
224
+ ## License
225
+
226
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ // hand-authored types entry: re-exports the JSDoc-generated declarations and layers
2
+ // on the express/express-session module augmentation, which jsdoc can't express
3
+ import type { AuthManager, AuthSession } from "./types/index.js"
4
+
5
+ export * from "./types/index.js"
6
+
7
+ declare global {
8
+ namespace Express {
9
+ interface Request {
10
+ auth: AuthManager
11
+ }
12
+ }
13
+ }
14
+
15
+ declare module "express-session" {
16
+ interface SessionData {
17
+ auth?: AuthSession
18
+ }
19
+ }
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@prsm/auth",
3
+ "version": "1.0.0",
4
+ "description": "PostgreSQL-backed authentication for Express: sessions, remember-me, password reset, email confirmation, OAuth, roles, two-factor, and audited impersonation",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./index.d.ts",
9
+ "default": "./src/index.js"
10
+ }
11
+ },
12
+ "types": "./index.d.ts",
13
+ "files": [
14
+ "src",
15
+ "types",
16
+ "index.d.ts"
17
+ ],
18
+ "scripts": {
19
+ "test": "vitest --reporter=verbose --run",
20
+ "test:watch": "vitest",
21
+ "prepublishOnly": "npx tsc --declaration --allowJs --emitDeclarationOnly --skipLibCheck --target es2020 --module nodenext --moduleResolution nodenext --strict false --esModuleInterop true --outDir ./types src/index.js"
22
+ },
23
+ "keywords": [
24
+ "auth",
25
+ "authentication",
26
+ "express",
27
+ "session",
28
+ "postgres",
29
+ "oauth",
30
+ "two-factor",
31
+ "totp"
32
+ ],
33
+ "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/prsmjs/auth.git"
37
+ },
38
+ "homepage": "https://github.com/prsmjs/auth#readme",
39
+ "bugs": {
40
+ "url": "https://github.com/prsmjs/auth/issues"
41
+ },
42
+ "dependencies": {
43
+ "@prsm/hash": "^2.0.1",
44
+ "@prsm/ms": "^2.0.0",
45
+ "bowser": "^2.11.0"
46
+ },
47
+ "peerDependencies": {
48
+ "cookie-parser": "^1.4.0",
49
+ "express": "^5.0.0",
50
+ "express-session": "^1.18.0",
51
+ "pg": "^8.0.0"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "cookie-parser": {
55
+ "optional": true
56
+ }
57
+ },
58
+ "devDependencies": {
59
+ "@prsm/trace": "^1.2.0",
60
+ "@types/cookie-parser": "^1.4.9",
61
+ "@types/express": "^5.0.0",
62
+ "@types/express-session": "^1.18.2",
63
+ "@types/node": "^24.0.0",
64
+ "@types/pg": "^8.15.5",
65
+ "cookie-parser": "^1.4.7",
66
+ "express": "^5.1.0",
67
+ "express-session": "^1.18.2",
68
+ "pg": "^8.16.3",
69
+ "supertest": "^7.1.4",
70
+ "typescript": "^5.9.3",
71
+ "vitest": "^3.2.4"
72
+ },
73
+ "engines": {
74
+ "node": ">=24"
75
+ }
76
+ }