@pattern-stack/codegen 0.6.4 → 0.6.6
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.
- package/CHANGELOG.md +20 -0
- package/README.md +2 -0
- package/dist/runtime/subsystems/auth/auth-oauth-state.schema.d.ts +81 -0
- package/dist/runtime/subsystems/auth/auth-oauth-state.schema.js +12 -0
- package/dist/runtime/subsystems/auth/auth-oauth-state.schema.js.map +1 -0
- package/dist/runtime/subsystems/auth/auth.module.d.ts +39 -24
- package/dist/runtime/subsystems/auth/auth.module.js +247 -14
- package/dist/runtime/subsystems/auth/auth.module.js.map +1 -1
- package/dist/runtime/subsystems/auth/auth.tokens.d.ts +15 -2
- package/dist/runtime/subsystems/auth/auth.tokens.js +9 -1
- package/dist/runtime/subsystems/auth/auth.tokens.js.map +1 -1
- package/dist/runtime/subsystems/auth/backends/encryption-key/env.d.ts +1 -1
- package/dist/runtime/subsystems/auth/backends/encryption-key/env.js +1 -1
- package/dist/runtime/subsystems/auth/backends/encryption-key/env.js.map +1 -1
- package/dist/runtime/subsystems/auth/backends/state-store.drizzle-backend.d.ts +23 -0
- package/dist/runtime/subsystems/auth/backends/state-store.drizzle-backend.js +68 -0
- package/dist/runtime/subsystems/auth/backends/state-store.drizzle-backend.js.map +1 -0
- package/dist/runtime/subsystems/auth/backends/state-store.memory-backend.d.ts +21 -0
- package/dist/runtime/subsystems/auth/backends/state-store.memory-backend.js +51 -0
- package/dist/runtime/subsystems/auth/backends/state-store.memory-backend.js.map +1 -0
- package/dist/runtime/subsystems/auth/controllers/auth.controller.d.ts +31 -0
- package/dist/runtime/subsystems/auth/controllers/auth.controller.js +137 -0
- package/dist/runtime/subsystems/auth/controllers/auth.controller.js.map +1 -0
- package/dist/runtime/subsystems/auth/index.d.ts +13 -4
- package/dist/runtime/subsystems/auth/index.js +254 -15
- package/dist/runtime/subsystems/auth/index.js.map +1 -1
- package/dist/runtime/subsystems/auth/protocols/auth-strategy.d.ts +1 -1
- package/dist/runtime/subsystems/auth/protocols/integration-store.d.ts +37 -2
- package/dist/runtime/subsystems/auth/protocols/oauth-state-store.d.ts +33 -7
- package/dist/runtime/subsystems/auth/protocols/oauth-state-store.js +12 -0
- package/dist/runtime/subsystems/auth/protocols/oauth-state-store.js.map +1 -1
- package/dist/runtime/subsystems/auth/protocols/provider-strategy.d.ts +54 -0
- package/dist/runtime/subsystems/auth/protocols/provider-strategy.js +1 -0
- package/dist/runtime/subsystems/auth/protocols/provider-strategy.js.map +1 -0
- package/dist/runtime/subsystems/auth/protocols/user-context.d.ts +24 -0
- package/dist/runtime/subsystems/auth/protocols/user-context.js +1 -0
- package/dist/runtime/subsystems/auth/protocols/user-context.js.map +1 -0
- package/dist/runtime/subsystems/auth/runtime/oauth2-refresh.strategy.d.ts +2 -2
- package/dist/runtime/subsystems/auth/runtime/oauth2-refresh.strategy.js.map +1 -1
- package/dist/runtime/subsystems/auth/runtime/session-expired.error.d.ts +2 -2
- package/dist/runtime/subsystems/auth/runtime/session-expired.error.js.map +1 -1
- package/dist/runtime/subsystems/auth/runtime/with-auth-retry.d.ts +1 -1
- package/dist/runtime/subsystems/auth/runtime/with-auth-retry.js.map +1 -1
- package/dist/runtime/subsystems/index.d.ts +9 -4
- package/dist/runtime/subsystems/index.js +248 -15
- package/dist/runtime/subsystems/index.js.map +1 -1
- package/dist/runtime/subsystems/sync/deep-equal.differ.js.map +1 -1
- package/dist/runtime/subsystems/sync/execute-sync.use-case.js.map +1 -1
- package/dist/runtime/subsystems/sync/index.js.map +1 -1
- package/dist/runtime/subsystems/sync/sync-change-source.protocol.d.ts +1 -1
- package/dist/runtime/subsystems/sync/sync-cursor-store.memory-backend.js.map +1 -1
- package/dist/runtime/subsystems/sync/sync-loopback.protocol.d.ts +3 -4
- package/dist/runtime/subsystems/sync/sync-run-recorder.drizzle-backend.js.map +1 -1
- package/dist/runtime/subsystems/sync/sync.module.js.map +1 -1
- package/dist/src/cli/index.js +574 -142
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/package.json +1 -1
- package/runtime/subsystems/auth/auth-oauth-state.schema.ts +30 -0
- package/runtime/subsystems/auth/auth.module.ts +89 -32
- package/runtime/subsystems/auth/auth.tokens.ts +14 -1
- package/runtime/subsystems/auth/backends/encryption-key/env.ts +3 -3
- package/runtime/subsystems/auth/backends/state-store.drizzle-backend.ts +83 -0
- package/runtime/subsystems/auth/backends/state-store.memory-backend.ts +76 -0
- package/runtime/subsystems/auth/controllers/auth.controller.ts +155 -0
- package/runtime/subsystems/auth/index.ts +43 -4
- package/runtime/subsystems/auth/protocols/auth-strategy.ts +1 -1
- package/runtime/subsystems/auth/protocols/integration-store.ts +38 -1
- package/runtime/subsystems/auth/protocols/oauth-state-store.ts +38 -6
- package/runtime/subsystems/auth/protocols/provider-strategy.ts +48 -0
- package/runtime/subsystems/auth/protocols/user-context.ts +22 -0
- package/runtime/subsystems/auth/runtime/oauth2-refresh.strategy.ts +2 -2
- package/runtime/subsystems/auth/runtime/session-expired.error.ts +2 -2
- package/runtime/subsystems/auth/runtime/with-auth-retry.ts +1 -1
- package/runtime/subsystems/index.ts +17 -2
- package/runtime/subsystems/sync/deep-equal.differ.ts +1 -1
- package/runtime/subsystems/sync/execute-sync.use-case.ts +1 -1
- package/runtime/subsystems/sync/sync-change-source.protocol.ts +1 -1
- package/runtime/subsystems/sync/sync-cursor-store.memory-backend.ts +1 -1
- package/runtime/subsystems/sync/sync-loopback.protocol.ts +3 -4
- package/runtime/subsystems/sync/sync-run-recorder.drizzle-backend.ts +1 -1
- package/templates/subsystem/auth/app-module-hook.ejs.t +21 -0
- package/templates/subsystem/auth/auth-oauth-state.schema.ejs.t +35 -0
- package/templates/subsystem/auth/env-config.ejs.t +20 -0
- package/templates/subsystem/auth/prompt.js +46 -0
- package/templates/subsystem/auth-config/codegen-config-auth-block.ejs.t +20 -0
- package/templates/subsystem/auth-config/prompt.js +20 -0
- package/templates/subsystem/auth-integrations/app-module-hook.ejs.t +16 -0
- package/templates/subsystem/auth-integrations/prompt.js +23 -0
- package/dist/runtime/subsystems/auth/backends/oauth-state-store/in-memory.d.ts +0 -24
- package/dist/runtime/subsystems/auth/backends/oauth-state-store/in-memory.js +0 -24
- package/dist/runtime/subsystems/auth/backends/oauth-state-store/in-memory.js.map +0 -1
- package/runtime/subsystems/auth/backends/oauth-state-store/in-memory.ts +0 -42
|
@@ -8,12 +8,27 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
8
8
|
if (kind && result) __defProp(target, key, result);
|
|
9
9
|
return result;
|
|
10
10
|
};
|
|
11
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
12
|
+
|
|
13
|
+
// runtime/subsystems/auth/protocols/oauth-state-store.ts
|
|
14
|
+
var OAuthStateError = class extends Error {
|
|
15
|
+
constructor(message, reason) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.reason = reason;
|
|
18
|
+
this.name = "OAuthStateError";
|
|
19
|
+
}
|
|
20
|
+
reason;
|
|
21
|
+
};
|
|
11
22
|
|
|
12
23
|
// runtime/subsystems/auth/auth.tokens.ts
|
|
13
24
|
var ENCRYPTION_KEY = /* @__PURE__ */ Symbol("ENCRYPTION_KEY");
|
|
14
25
|
var OAUTH_STATE_STORE = /* @__PURE__ */ Symbol("OAUTH_STATE_STORE");
|
|
15
26
|
var AUTH_INTEGRATION_READER = /* @__PURE__ */ Symbol("AUTH_INTEGRATION_READER");
|
|
16
27
|
var AUTH_INTEGRATION_TOKEN_WRITER = /* @__PURE__ */ Symbol("AUTH_INTEGRATION_TOKEN_WRITER");
|
|
28
|
+
var AUTH_INTEGRATION_GRANT_SINK = /* @__PURE__ */ Symbol("AUTH_INTEGRATION_GRANT_SINK");
|
|
29
|
+
var AUTH_USER_CONTEXT = /* @__PURE__ */ Symbol("AUTH_USER_CONTEXT");
|
|
30
|
+
var STRATEGY_REGISTRY = /* @__PURE__ */ Symbol("STRATEGY_REGISTRY");
|
|
31
|
+
var AUTH_OPTIONS = /* @__PURE__ */ Symbol("AUTH_OPTIONS");
|
|
17
32
|
|
|
18
33
|
// runtime/subsystems/auth/runtime/integration-broken.error.ts
|
|
19
34
|
var IntegrationBrokenError = class extends Error {
|
|
@@ -150,6 +165,15 @@ async function withAuthRetry(authStrategy, integrationId, op, options = {}) {
|
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
// runtime/subsystems/auth/auth-oauth-state.schema.ts
|
|
169
|
+
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
|
|
170
|
+
var authOAuthState = pgTable("auth_oauth_state", {
|
|
171
|
+
state: text("state").primaryKey(),
|
|
172
|
+
userId: text("user_id").notNull(),
|
|
173
|
+
redirect: text("redirect"),
|
|
174
|
+
expiresAt: timestamp("expires_at", { withTimezone: true }).notNull()
|
|
175
|
+
});
|
|
176
|
+
|
|
153
177
|
// runtime/subsystems/auth/backends/encryption-key/env.ts
|
|
154
178
|
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
|
|
155
179
|
var ALGO = "aes-256-gcm";
|
|
@@ -160,7 +184,7 @@ var EnvEncryptionKey = class {
|
|
|
160
184
|
key;
|
|
161
185
|
constructor(opts = {}) {
|
|
162
186
|
const env = opts.env ?? process.env;
|
|
163
|
-
const envVar = opts.envVar ?? "
|
|
187
|
+
const envVar = opts.envVar ?? "INTEGRATION_TOKEN_ENCRYPTION_KEY";
|
|
164
188
|
const raw = env[envVar];
|
|
165
189
|
if (!raw) {
|
|
166
190
|
throw new Error(
|
|
@@ -200,29 +224,206 @@ var EnvEncryptionKey = class {
|
|
|
200
224
|
}
|
|
201
225
|
};
|
|
202
226
|
|
|
203
|
-
// runtime/subsystems/auth/backends/
|
|
204
|
-
|
|
227
|
+
// runtime/subsystems/auth/backends/state-store.memory-backend.ts
|
|
228
|
+
import { randomBytes as randomBytes2 } from "crypto";
|
|
229
|
+
var MemoryOAuthStateStore = class {
|
|
205
230
|
store = /* @__PURE__ */ new Map();
|
|
206
231
|
ttlMs;
|
|
207
232
|
now;
|
|
233
|
+
generateToken;
|
|
208
234
|
constructor(opts = {}) {
|
|
209
235
|
this.ttlMs = opts.ttlMs ?? 10 * 60 * 1e3;
|
|
210
236
|
this.now = opts.now ?? (() => Date.now());
|
|
237
|
+
this.generateToken = opts.generateToken ?? (() => randomBytes2(32).toString("base64url"));
|
|
211
238
|
}
|
|
212
|
-
async
|
|
213
|
-
|
|
239
|
+
async generate(record) {
|
|
240
|
+
const state = this.generateToken();
|
|
241
|
+
this.store.set(state, {
|
|
242
|
+
record: { ...record },
|
|
243
|
+
expiresAt: this.now() + this.ttlMs
|
|
244
|
+
});
|
|
245
|
+
return state;
|
|
214
246
|
}
|
|
215
247
|
async consume(state) {
|
|
216
248
|
const slot = this.store.get(state);
|
|
217
|
-
if (!slot)
|
|
249
|
+
if (!slot) {
|
|
250
|
+
throw new OAuthStateError(
|
|
251
|
+
`OAuth state token unknown or already consumed`,
|
|
252
|
+
"missing"
|
|
253
|
+
);
|
|
254
|
+
}
|
|
218
255
|
this.store.delete(state);
|
|
219
|
-
if (slot.expiresAt <= this.now())
|
|
220
|
-
|
|
256
|
+
if (slot.expiresAt <= this.now()) {
|
|
257
|
+
throw new OAuthStateError(`OAuth state token expired`, "expired");
|
|
258
|
+
}
|
|
259
|
+
return slot.record;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// runtime/subsystems/auth/backends/state-store.drizzle-backend.ts
|
|
264
|
+
import { randomBytes as randomBytes3 } from "crypto";
|
|
265
|
+
import { eq } from "drizzle-orm";
|
|
266
|
+
var DrizzleOAuthStateStore = class {
|
|
267
|
+
constructor(db, opts = {}) {
|
|
268
|
+
this.db = db;
|
|
269
|
+
this.ttlMs = opts.ttlMs ?? 10 * 60 * 1e3;
|
|
270
|
+
this.now = opts.now ?? (() => Date.now());
|
|
271
|
+
this.generateToken = opts.generateToken ?? (() => randomBytes3(32).toString("base64url"));
|
|
272
|
+
}
|
|
273
|
+
db;
|
|
274
|
+
ttlMs;
|
|
275
|
+
now;
|
|
276
|
+
generateToken;
|
|
277
|
+
async generate(record) {
|
|
278
|
+
const state = this.generateToken();
|
|
279
|
+
const expiresAt = new Date(this.now() + this.ttlMs);
|
|
280
|
+
await this.db.insert(authOAuthState).values({
|
|
281
|
+
state,
|
|
282
|
+
userId: record.userId,
|
|
283
|
+
redirect: record.redirect ?? null,
|
|
284
|
+
expiresAt
|
|
285
|
+
});
|
|
286
|
+
return state;
|
|
287
|
+
}
|
|
288
|
+
async consume(state) {
|
|
289
|
+
const rows = await this.db.delete(authOAuthState).where(eq(authOAuthState.state, state)).returning();
|
|
290
|
+
const row = rows[0];
|
|
291
|
+
if (!row) {
|
|
292
|
+
throw new OAuthStateError(
|
|
293
|
+
`OAuth state token unknown or already consumed`,
|
|
294
|
+
"missing"
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
if (row.expiresAt.getTime() <= this.now()) {
|
|
298
|
+
throw new OAuthStateError(`OAuth state token expired`, "expired");
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
userId: row.userId,
|
|
302
|
+
redirect: row.redirect ?? void 0
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// runtime/subsystems/auth/controllers/auth.controller.ts
|
|
308
|
+
import {
|
|
309
|
+
Controller,
|
|
310
|
+
Get,
|
|
311
|
+
Inject,
|
|
312
|
+
Param,
|
|
313
|
+
Query,
|
|
314
|
+
Req,
|
|
315
|
+
Res,
|
|
316
|
+
HttpException,
|
|
317
|
+
HttpStatus
|
|
318
|
+
} from "@nestjs/common";
|
|
319
|
+
var AuthController = class {
|
|
320
|
+
constructor(registry, userContext, stateStore, grantSink, options) {
|
|
321
|
+
this.registry = registry;
|
|
322
|
+
this.userContext = userContext;
|
|
323
|
+
this.stateStore = stateStore;
|
|
324
|
+
this.grantSink = grantSink;
|
|
325
|
+
this.options = options;
|
|
326
|
+
}
|
|
327
|
+
registry;
|
|
328
|
+
userContext;
|
|
329
|
+
stateStore;
|
|
330
|
+
grantSink;
|
|
331
|
+
options;
|
|
332
|
+
async connect(slug, redirect, req, res) {
|
|
333
|
+
const strategy = this.requireStrategy(slug);
|
|
334
|
+
const userId = await this.userContext.getCurrentUserId(req);
|
|
335
|
+
const state = await this.stateStore.generate({ userId, redirect });
|
|
336
|
+
const url = strategy.buildAuthorizeUrl({
|
|
337
|
+
state,
|
|
338
|
+
redirectUri: this.redirectUriFor(slug)
|
|
339
|
+
});
|
|
340
|
+
return res.redirect(HttpStatus.FOUND, url);
|
|
341
|
+
}
|
|
342
|
+
async callback(slug, code, state, res) {
|
|
343
|
+
const strategy = this.requireStrategy(slug);
|
|
344
|
+
if (!code) {
|
|
345
|
+
throw new HttpException(
|
|
346
|
+
`Missing 'code' query param`,
|
|
347
|
+
HttpStatus.BAD_REQUEST
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
if (!state) {
|
|
351
|
+
throw new HttpException(
|
|
352
|
+
`Missing 'state' query param`,
|
|
353
|
+
HttpStatus.BAD_REQUEST
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
const { userId, redirect } = await this.stateStore.consume(state);
|
|
357
|
+
const tokens = await strategy.exchangeCodeForTokens({
|
|
358
|
+
code,
|
|
359
|
+
redirectUri: this.redirectUriFor(slug)
|
|
360
|
+
});
|
|
361
|
+
await this.grantSink.createOrUpdateFromOAuthGrant({
|
|
362
|
+
userId,
|
|
363
|
+
provider: slug,
|
|
364
|
+
accessToken: tokens.accessToken,
|
|
365
|
+
refreshToken: tokens.refreshToken,
|
|
366
|
+
expiresAt: tokens.expiresAt,
|
|
367
|
+
scope: tokens.scope,
|
|
368
|
+
externalAccountId: tokens.externalAccountId,
|
|
369
|
+
providerMetadata: tokens.providerMetadata
|
|
370
|
+
});
|
|
371
|
+
return res.redirect(
|
|
372
|
+
HttpStatus.FOUND,
|
|
373
|
+
redirect ?? `/settings/integrations?connected=${encodeURIComponent(slug)}`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
requireStrategy(slug) {
|
|
377
|
+
const strategy = this.registry.get(slug);
|
|
378
|
+
if (!strategy) {
|
|
379
|
+
throw new HttpException(
|
|
380
|
+
`Unknown provider '${slug}'`,
|
|
381
|
+
HttpStatus.NOT_FOUND
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
return strategy;
|
|
385
|
+
}
|
|
386
|
+
redirectUriFor(slug) {
|
|
387
|
+
const base = this.options.redirectUriBase;
|
|
388
|
+
if (!base) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
`AuthModule.forRoot: redirectUriBase is required when AuthController is enabled`
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
const trimmed = base.replace(/\/+$/, "");
|
|
394
|
+
return `${trimmed}/auth/${encodeURIComponent(slug)}/callback`;
|
|
221
395
|
}
|
|
222
396
|
};
|
|
397
|
+
__decorateClass([
|
|
398
|
+
Get(":provider/connect"),
|
|
399
|
+
__decorateParam(0, Param("provider")),
|
|
400
|
+
__decorateParam(1, Query("redirect")),
|
|
401
|
+
__decorateParam(2, Req()),
|
|
402
|
+
__decorateParam(3, Res())
|
|
403
|
+
], AuthController.prototype, "connect", 1);
|
|
404
|
+
__decorateClass([
|
|
405
|
+
Get(":provider/callback"),
|
|
406
|
+
__decorateParam(0, Param("provider")),
|
|
407
|
+
__decorateParam(1, Query("code")),
|
|
408
|
+
__decorateParam(2, Query("state")),
|
|
409
|
+
__decorateParam(3, Res())
|
|
410
|
+
], AuthController.prototype, "callback", 1);
|
|
411
|
+
AuthController = __decorateClass([
|
|
412
|
+
Controller("auth"),
|
|
413
|
+
__decorateParam(0, Inject(STRATEGY_REGISTRY)),
|
|
414
|
+
__decorateParam(1, Inject(AUTH_USER_CONTEXT)),
|
|
415
|
+
__decorateParam(2, Inject(OAUTH_STATE_STORE)),
|
|
416
|
+
__decorateParam(3, Inject(AUTH_INTEGRATION_GRANT_SINK)),
|
|
417
|
+
__decorateParam(4, Inject(AUTH_OPTIONS))
|
|
418
|
+
], AuthController);
|
|
223
419
|
|
|
224
420
|
// runtime/subsystems/auth/auth.module.ts
|
|
225
421
|
import { Module } from "@nestjs/common";
|
|
422
|
+
|
|
423
|
+
// runtime/constants/tokens.ts
|
|
424
|
+
var DRIZZLE = "DRIZZLE";
|
|
425
|
+
|
|
426
|
+
// runtime/subsystems/auth/auth.module.ts
|
|
226
427
|
function resolveEncryptionKeyProvider(choice) {
|
|
227
428
|
if (choice === "env") {
|
|
228
429
|
return { provide: ENCRYPTION_KEY, useClass: EnvEncryptionKey };
|
|
@@ -230,24 +431,54 @@ function resolveEncryptionKeyProvider(choice) {
|
|
|
230
431
|
return { provide: ENCRYPTION_KEY, ...choice };
|
|
231
432
|
}
|
|
232
433
|
function resolveOAuthStateStoreProvider(choice) {
|
|
233
|
-
if (choice === "
|
|
234
|
-
return { provide: OAUTH_STATE_STORE, useClass:
|
|
434
|
+
if (choice === "memory") {
|
|
435
|
+
return { provide: OAUTH_STATE_STORE, useClass: MemoryOAuthStateStore };
|
|
436
|
+
}
|
|
437
|
+
if (choice === "drizzle") {
|
|
438
|
+
return {
|
|
439
|
+
provide: OAUTH_STATE_STORE,
|
|
440
|
+
useFactory: (db) => {
|
|
441
|
+
if (!db) {
|
|
442
|
+
throw new Error(
|
|
443
|
+
"AuthModule.forRoot: oauthStateStore: 'drizzle' selected but DRIZZLE provider is not available. Ensure DatabaseModule (or another provider exposing DRIZZLE) is imported before AuthModule.forRoot."
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return new DrizzleOAuthStateStore(db);
|
|
447
|
+
},
|
|
448
|
+
inject: [{ token: DRIZZLE, optional: true }]
|
|
449
|
+
};
|
|
235
450
|
}
|
|
236
451
|
return { provide: OAUTH_STATE_STORE, ...choice };
|
|
237
452
|
}
|
|
238
453
|
var AuthModule = class {
|
|
239
454
|
static forRoot(options = {}) {
|
|
455
|
+
const resolved = {
|
|
456
|
+
encryptionKey: options.encryptionKey ?? "env",
|
|
457
|
+
oauthStateStore: options.oauthStateStore ?? "memory",
|
|
458
|
+
enableController: options.enableController ?? false,
|
|
459
|
+
redirectUriBase: options.redirectUriBase
|
|
460
|
+
};
|
|
461
|
+
if (resolved.enableController && !resolved.redirectUriBase) {
|
|
462
|
+
throw new Error(
|
|
463
|
+
"AuthModule.forRoot: redirectUriBase is required when enableController: true"
|
|
464
|
+
);
|
|
465
|
+
}
|
|
240
466
|
const encryptionKeyProvider = resolveEncryptionKeyProvider(
|
|
241
|
-
|
|
467
|
+
resolved.encryptionKey
|
|
242
468
|
);
|
|
243
469
|
const oauthStateStoreProvider = resolveOAuthStateStoreProvider(
|
|
244
|
-
|
|
470
|
+
resolved.oauthStateStore
|
|
245
471
|
);
|
|
472
|
+
const optionsProvider = {
|
|
473
|
+
provide: AUTH_OPTIONS,
|
|
474
|
+
useValue: resolved
|
|
475
|
+
};
|
|
246
476
|
return {
|
|
247
477
|
module: AuthModule,
|
|
248
478
|
global: true,
|
|
249
|
-
providers: [encryptionKeyProvider, oauthStateStoreProvider],
|
|
250
|
-
|
|
479
|
+
providers: [encryptionKeyProvider, oauthStateStoreProvider, optionsProvider],
|
|
480
|
+
controllers: resolved.enableController ? [AuthController] : [],
|
|
481
|
+
exports: [ENCRYPTION_KEY, OAUTH_STATE_STORE, AUTH_OPTIONS]
|
|
251
482
|
};
|
|
252
483
|
}
|
|
253
484
|
};
|
|
@@ -255,16 +486,24 @@ AuthModule = __decorateClass([
|
|
|
255
486
|
Module({})
|
|
256
487
|
], AuthModule);
|
|
257
488
|
export {
|
|
489
|
+
AUTH_INTEGRATION_GRANT_SINK,
|
|
258
490
|
AUTH_INTEGRATION_READER,
|
|
259
491
|
AUTH_INTEGRATION_TOKEN_WRITER,
|
|
492
|
+
AUTH_OPTIONS,
|
|
493
|
+
AUTH_USER_CONTEXT,
|
|
494
|
+
AuthController,
|
|
260
495
|
AuthModule,
|
|
496
|
+
DrizzleOAuthStateStore,
|
|
261
497
|
ENCRYPTION_KEY,
|
|
262
498
|
EnvEncryptionKey,
|
|
263
|
-
InMemoryOAuthStateStore,
|
|
264
499
|
IntegrationBrokenError,
|
|
500
|
+
MemoryOAuthStateStore,
|
|
265
501
|
OAUTH_STATE_STORE,
|
|
266
502
|
OAuth2RefreshStrategy,
|
|
503
|
+
OAuthStateError,
|
|
504
|
+
STRATEGY_REGISTRY,
|
|
267
505
|
SessionExpiredError,
|
|
506
|
+
authOAuthState,
|
|
268
507
|
isSessionExpiredError,
|
|
269
508
|
withAuthRetry
|
|
270
509
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../runtime/subsystems/auth/auth.tokens.ts","../../../../runtime/subsystems/auth/runtime/integration-broken.error.ts","../../../../runtime/subsystems/auth/runtime/oauth2-refresh.strategy.ts","../../../../runtime/subsystems/auth/runtime/session-expired.error.ts","../../../../runtime/subsystems/auth/runtime/with-auth-retry.ts","../../../../runtime/subsystems/auth/backends/encryption-key/env.ts","../../../../runtime/subsystems/auth/backends/oauth-state-store/in-memory.ts","../../../../runtime/subsystems/auth/auth.module.ts"],"sourcesContent":["/**\n * Auth subsystem — injection tokens.\n *\n * Following ADR-008 guidance: `Symbol()` tokens for type safety and collision\n * avoidance. Consumers inject these via `@Inject(...)` against the matching\n * protocol interface.\n *\n * Usage:\n * ```typescript\n * constructor(\n * @Inject(ENCRYPTION_KEY) private readonly key: IEncryptionKey,\n * @Inject(OAUTH_STATE_STORE) private readonly states: IOAuthStateStore,\n * @Inject(AUTH_INTEGRATION_READER) private readonly reader: IIntegrationReader,\n * @Inject(AUTH_INTEGRATION_TOKEN_WRITER) private readonly writer: IIntegrationTokenWriter,\n * ) {}\n * ```\n *\n * `IAuthStrategy` implementations are provider-specific and registered under\n * provider-specific tokens (e.g. `SALESFORCE_AUTH_STRATEGY`,\n * `HUBSPOT_AUTH_STRATEGY`) by each integration module — this subsystem does\n * not mandate a single `AUTH_STRATEGY` token because an app typically has\n * many concurrent strategies, one per provider.\n */\nexport const ENCRYPTION_KEY = Symbol('ENCRYPTION_KEY');\nexport const OAUTH_STATE_STORE = Symbol('OAUTH_STATE_STORE');\nexport const AUTH_INTEGRATION_READER = Symbol('AUTH_INTEGRATION_READER');\nexport const AUTH_INTEGRATION_TOKEN_WRITER = Symbol('AUTH_INTEGRATION_TOKEN_WRITER');\n","/**\n * Thrown when an OAuth2 provider returns `400 invalid_grant`/`invalid_token`\n * on refresh — the refresh token itself is dead (user revoked, org\n * deactivated, token expired beyond the provider's rotation window). The\n * integration should be marked broken so background sync stops picking it\n * up; the user re-initiates OAuth.\n *\n * Shared across every OAuth2 strategy.\n */\nexport class IntegrationBrokenError extends Error {\n constructor(\n readonly integrationId: string,\n readonly errorCode: string,\n readonly errorDescription: string,\n ) {\n super(\n `Integration ${integrationId} broken: ${errorCode} - ${errorDescription}`,\n );\n this.name = 'IntegrationBrokenError';\n }\n}\n","/**\n * Abstract base class for OAuth2 refresh-token strategies.\n *\n * Template-method pattern: `resolve()` is concrete; four small hooks inject\n * provider specifics. Validated across two providers in dealbrain-v2\n * (SalesforceAuthStrategy, HubSpotAuthStrategy) before extraction here — see\n * `docs/gate-1-auth-extraction-findings.md` for the \"build first, extract\n * later\" evidence.\n *\n * Subclass contract:\n * - `provider` — slug matched against `integrations.provider`\n * - `defaultExpiresInSec` — fallback when refresh response omits `expires_in`\n * - `tokenEndpoint()` — URL to POST the refresh grant\n * - `refreshBodyExtras()` — provider-specific body params\n * - `parseRefreshResponse()` — raw JSON → ParsedRefreshResponse\n * - `buildCredentials()` — stored or freshly-refreshed access token +\n * integration + optional raw refresh response\n * → provider credentials\n *\n * Base handles: expiry check w/ 5-min safety window, `forceRefresh` escape\n * hatch, POST form-urlencoded body, OAuth2 error mapping to\n * `IntegrationBrokenError`, refresh-token rotation persistence, fetch +\n * clock injection for tests.\n */\nimport type {\n AuthCredentials,\n AuthResolveOptions,\n IAuthStrategy,\n} from '../protocols/auth-strategy';\nimport type {\n DecryptedIntegration,\n IIntegrationReader,\n IIntegrationTokenWriter,\n} from '../protocols/integration-store';\nimport { IntegrationBrokenError } from './integration-broken.error';\n\nexport type FetchLike = (\n input: string | URL | Request,\n init?: RequestInit,\n) => Promise<Response>;\n\n/** Safety window before expiry that triggers a refresh. */\nconst REFRESH_SAFETY_MS = 5 * 60 * 1000;\n\nexport interface OAuth2RefreshStrategyOptions {\n integrationReader: IIntegrationReader;\n tokenWriter: IIntegrationTokenWriter;\n /** Injectable fetch for tests. Defaults to the global `fetch`. */\n fetch?: FetchLike;\n /** Injectable clock for tests. Defaults to `Date.now`. */\n now?: () => number;\n}\n\nexport interface ParsedRefreshResponse {\n accessToken: string;\n /**\n * New refresh token if the provider rotated it (HubSpot: always, Salesforce:\n * sometimes). Omit when the provider reused the old refresh token.\n */\n refreshToken?: string;\n /** Seconds from now. If omitted, subclass `defaultExpiresInSec` applies. */\n expiresInSec?: number;\n}\n\nexport abstract class OAuth2RefreshStrategy implements IAuthStrategy {\n protected abstract readonly provider: string;\n protected abstract readonly defaultExpiresInSec: number;\n\n protected readonly integrationReader: IIntegrationReader;\n protected readonly tokenWriter: IIntegrationTokenWriter;\n protected readonly fetchImpl: FetchLike;\n protected readonly now: () => number;\n\n constructor(opts: OAuth2RefreshStrategyOptions) {\n this.integrationReader = opts.integrationReader;\n this.tokenWriter = opts.tokenWriter;\n this.fetchImpl = opts.fetch ?? fetch;\n this.now = opts.now ?? Date.now;\n }\n\n async resolve(\n integrationId: string,\n opts: AuthResolveOptions = {},\n ): Promise<AuthCredentials> {\n const integration =\n await this.integrationReader.findByIdDecrypted(integrationId);\n if (!integration) {\n throw new Error(`Integration ${integrationId} not found`);\n }\n if (integration.provider !== this.provider) {\n throw new Error(\n `${this.constructor.name} called for non-${this.provider} integration ${integrationId} (provider=${integration.provider})`,\n );\n }\n\n const needsRefresh =\n opts.forceRefresh ||\n this.isExpiring(integration.expiresAt) ||\n !integration.accessToken;\n\n if (!needsRefresh) {\n return this.buildCredentials(integration.accessToken, integration);\n }\n\n if (!integration.refreshToken) {\n throw new IntegrationBrokenError(\n integrationId,\n 'no_refresh_token',\n 'Integration has no refresh token; user must reconnect',\n );\n }\n\n const { parsed, raw } = await this.executeRefresh(\n integrationId,\n integration.refreshToken,\n );\n const newExpiresAt = new Date(\n this.now() + (parsed.expiresInSec ?? this.defaultExpiresInSec) * 1000,\n );\n await this.tokenWriter.persistRefresh({\n integrationId,\n accessToken: parsed.accessToken,\n refreshToken: parsed.refreshToken ?? undefined,\n expiresAt: newExpiresAt,\n });\n\n return this.buildCredentials(parsed.accessToken, integration, raw);\n }\n\n protected abstract tokenEndpoint(): string;\n protected abstract refreshBodyExtras(): Record<string, string>;\n protected abstract parseRefreshResponse(raw: unknown): ParsedRefreshResponse;\n protected abstract buildCredentials(\n accessToken: string,\n integration: DecryptedIntegration,\n refreshRaw?: unknown,\n ): AuthCredentials;\n\n private async executeRefresh(\n integrationId: string,\n refreshToken: string,\n ): Promise<{ parsed: ParsedRefreshResponse; raw: unknown }> {\n const body = new URLSearchParams({\n grant_type: 'refresh_token',\n refresh_token: refreshToken,\n ...this.refreshBodyExtras(),\n });\n const response = await this.fetchImpl(this.tokenEndpoint(), {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: body.toString(),\n });\n if (!response.ok) {\n const err = (await safeJson(response)) as Partial<{\n error: string;\n error_description: string;\n message: string;\n }>;\n if (\n response.status === 400 &&\n (err.error === 'invalid_grant' || err.error === 'invalid_token')\n ) {\n throw new IntegrationBrokenError(\n integrationId,\n err.error ?? 'invalid_grant',\n err.error_description ?? err.message ?? 'refresh token rejected',\n );\n }\n throw new Error(\n `${this.provider} token refresh failed: ${response.status} ${err.error ?? ''} ${err.error_description ?? err.message ?? ''}`.trim(),\n );\n }\n const raw = await response.json();\n return { parsed: this.parseRefreshResponse(raw), raw };\n }\n\n private isExpiring(expiresAt: Date | null): boolean {\n if (!expiresAt) return true;\n return expiresAt.getTime() - this.now() < REFRESH_SAFETY_MS;\n }\n}\n\nasync function safeJson(response: Response): Promise<unknown> {\n try {\n return await response.clone().json();\n } catch {\n return {};\n }\n}\n","/**\n * Provider-agnostic marker for \"the access token was rejected; a forced\n * refresh may recover.\"\n *\n * Concrete provider error classes (e.g. SalesforceSessionExpiredError,\n * HubSpotUnauthorizedError) either extend `SessionExpiredError` directly or\n * set `isSessionExpired === true` on their instances. `withAuthRetry` uses\n * the `isSessionExpiredError` predicate to decide whether to force-refresh\n * and retry once.\n *\n * This discriminator replaces the SFDC-only `instanceof` check from\n * dealbrain-v2's original `withAuthRetry`. See\n * `docs/gate-1-auth-extraction-findings.md` (recommendation 4).\n */\nexport class SessionExpiredError extends Error {\n /** Duck-type marker — works across package boundaries where `instanceof` fails. */\n readonly isSessionExpired = true as const;\n\n constructor(message = 'Access token rejected by provider') {\n super(message);\n this.name = 'SessionExpiredError';\n }\n}\n\n/**\n * Predicate used by `withAuthRetry` by default.\n *\n * Matches any error that either `instanceof SessionExpiredError` or carries\n * the `isSessionExpired === true` marker property. Provider adapters that\n * want their existing error classes to participate can simply add the\n * marker property without touching the class hierarchy.\n */\nexport function isSessionExpiredError(err: unknown): boolean {\n if (err instanceof SessionExpiredError) return true;\n if (err !== null && typeof err === 'object' && 'isSessionExpired' in err) {\n return (err as { isSessionExpired?: unknown }).isSessionExpired === true;\n }\n return false;\n}\n","/**\n * Run `op` with auth-aware retry-once on session-expired errors.\n *\n * Pattern: resolve creds → run op → if `isSessionExpired(e)` → resolve with\n * `forceRefresh: true` → retry → propagate. A second session-expired error\n * on the refreshed token propagates rather than looping, so transient\n * adapter bugs can't hang the caller.\n *\n * Generalisation over dealbrain's original SFDC-specific version: the\n * session-expired classifier is injected. Providers mark their session-\n * expired errors (via `instanceof` of a marker class, or by setting a known\n * property) and pass a classifier matching that shape.\n *\n * Default classifier recognises the marker interface `SessionExpiredError`\n * shipped in `session-expired.error.ts` — concrete provider errors that\n * extend it (or set `isSessionExpired === true`) get retried without any\n * further wiring.\n */\nimport type {\n AuthCredentials,\n IAuthStrategy,\n} from '../protocols/auth-strategy';\nimport { isSessionExpiredError } from './session-expired.error';\n\nexport interface WithAuthRetryOptions {\n /**\n * Classifier that decides whether a thrown error is a session-expired\n * signal worth retrying once with a fresh token. Defaults to the marker-\n * interface check in `session-expired.error.ts`.\n */\n isSessionExpired?: (err: unknown) => boolean;\n}\n\nexport async function withAuthRetry<T>(\n authStrategy: IAuthStrategy,\n integrationId: string,\n op: (credentials: AuthCredentials) => Promise<T>,\n options: WithAuthRetryOptions = {},\n): Promise<T> {\n const classify = options.isSessionExpired ?? isSessionExpiredError;\n\n let creds = await authStrategy.resolve(integrationId);\n try {\n return await op(creds);\n } catch (e) {\n if (!classify(e)) throw e;\n creds = await authStrategy.resolve(integrationId, { forceRefresh: true });\n return op(creds);\n }\n}\n","/**\n * Env-backed AES-256-GCM encryption.\n *\n * Framing: `base64( nonce(12B) || ciphertext || authTag(16B) )`. Random nonce\n * per call means two encryptions of the same plaintext produce different\n * ciphertexts — prevents replay-style inference. Auth tag enforces integrity;\n * any tampering throws on decrypt.\n *\n * Key source: `TOKEN_ENCRYPTION_KEY` env var, 32 bytes base64-encoded.\n * Generate via `openssl rand -base64 32`.\n *\n * Future backend: `kms.ts` (AWS/GCP KMS) for production deployments that\n * need key rotation + audit trails.\n */\nimport { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';\nimport type { IEncryptionKey } from '../../protocols/encryption-key';\n\nexport interface EnvEncryptionKeyOptions {\n /** Defaults to `process.env`. Tests inject a fixture. */\n env?: NodeJS.ProcessEnv;\n /** Defaults to `'TOKEN_ENCRYPTION_KEY'`. */\n envVar?: string;\n}\n\nconst ALGO = 'aes-256-gcm';\nconst NONCE_BYTES = 12;\nconst TAG_BYTES = 16;\nconst KEY_BYTES = 32;\n\nexport class EnvEncryptionKey implements IEncryptionKey {\n private readonly key: Buffer;\n\n constructor(opts: EnvEncryptionKeyOptions = {}) {\n const env = opts.env ?? process.env;\n const envVar = opts.envVar ?? 'TOKEN_ENCRYPTION_KEY';\n const raw = env[envVar];\n if (!raw) {\n throw new Error(\n `EnvEncryptionKey: ${envVar} is not set. Generate with: openssl rand -base64 32`,\n );\n }\n const decoded = Buffer.from(raw, 'base64');\n if (decoded.length !== KEY_BYTES) {\n throw new Error(\n `EnvEncryptionKey: ${envVar} must decode to ${KEY_BYTES} bytes (got ${decoded.length}). Use: openssl rand -base64 32`,\n );\n }\n this.key = decoded;\n }\n\n async encrypt(plaintext: string): Promise<string> {\n const nonce = randomBytes(NONCE_BYTES);\n const cipher = createCipheriv(ALGO, this.key, nonce);\n const ciphertext = Buffer.concat([\n cipher.update(plaintext, 'utf8'),\n cipher.final(),\n ]);\n const authTag = cipher.getAuthTag();\n return Buffer.concat([nonce, ciphertext, authTag]).toString('base64');\n }\n\n async decrypt(ciphertext: string): Promise<string> {\n const buf = Buffer.from(ciphertext, 'base64');\n if (buf.length < NONCE_BYTES + TAG_BYTES) {\n throw new Error('EnvEncryptionKey: ciphertext too short');\n }\n const nonce = buf.subarray(0, NONCE_BYTES);\n const authTag = buf.subarray(buf.length - TAG_BYTES);\n const body = buf.subarray(NONCE_BYTES, buf.length - TAG_BYTES);\n\n const decipher = createDecipheriv(ALGO, this.key, nonce);\n decipher.setAuthTag(authTag);\n const plain = Buffer.concat([decipher.update(body), decipher.final()]);\n return plain.toString('utf8');\n }\n}\n","/**\n * In-memory OAuth state store.\n *\n * Single-process dev store. Production deployments need a Redis-backed impl\n * (follow-up) so state survives restarts + is shared across workers.\n */\nimport type {\n IOAuthStateStore,\n OAuthStateEntry,\n} from '../../protocols/oauth-state-store';\n\nexport interface InMemoryOAuthStateStoreOptions {\n /** TTL in ms. Entries older than this are treated as absent. Default 10min. */\n ttlMs?: number;\n now?: () => number;\n}\n\nexport class InMemoryOAuthStateStore implements IOAuthStateStore {\n private readonly store = new Map<\n string,\n { entry: OAuthStateEntry; expiresAt: number }\n >();\n private readonly ttlMs: number;\n private readonly now: () => number;\n\n constructor(opts: InMemoryOAuthStateStoreOptions = {}) {\n this.ttlMs = opts.ttlMs ?? 10 * 60 * 1000;\n this.now = opts.now ?? (() => Date.now());\n }\n\n async put(state: string, entry: OAuthStateEntry): Promise<void> {\n this.store.set(state, { entry, expiresAt: this.now() + this.ttlMs });\n }\n\n async consume(state: string): Promise<OAuthStateEntry | null> {\n const slot = this.store.get(state);\n if (!slot) return null;\n this.store.delete(state);\n if (slot.expiresAt <= this.now()) return null;\n return slot.entry;\n }\n}\n","/**\n * AuthModule — DynamicModule factory for the auth subsystem.\n *\n * Wires the two pluggable backends the subsystem ships with:\n * - `ENCRYPTION_KEY` → `EnvEncryptionKey` (AES-256-GCM from env)\n * - `OAUTH_STATE_STORE` → `InMemoryOAuthStateStore` (dev) / custom Redis impl (prod)\n *\n * The two integration-store ports (`AUTH_INTEGRATION_READER`,\n * `AUTH_INTEGRATION_TOKEN_WRITER`) are deliberately **not** wired by this\n * module — they are always consumer-specific (adapters over the app's own\n * integrations entity/service). Consumers provide them in the module that\n * owns the integrations domain, not here.\n *\n * `IAuthStrategy` implementations are also per-provider and live in the\n * integration module that uses them (`SalesforceModule`, `HubSpotModule`, …).\n * The subsystem provides the abstract base class\n * (`OAuth2RefreshStrategy`) — binding concrete strategies is an app concern.\n *\n * Usage in AppModule:\n * ```typescript\n * AuthModule.forRoot({\n * encryptionKey: 'env',\n * oauthStateStore: 'in-memory',\n * });\n * ```\n *\n * Or inject custom providers directly:\n * ```typescript\n * AuthModule.forRoot({\n * encryptionKey: { useClass: MyKmsEncryptionKey },\n * oauthStateStore: { useClass: RedisOAuthStateStore },\n * });\n * ```\n *\n * `global: true` means other modules don't need to re-import AuthModule to\n * inject `ENCRYPTION_KEY` / `OAUTH_STATE_STORE`.\n */\nimport { Module, type DynamicModule, type Provider } from '@nestjs/common';\nimport { ENCRYPTION_KEY, OAUTH_STATE_STORE } from './auth.tokens';\nimport { EnvEncryptionKey } from './backends/encryption-key/env';\nimport { InMemoryOAuthStateStore } from './backends/oauth-state-store/in-memory';\n\ntype EncryptionKeyChoice =\n | 'env'\n | Omit<Provider, 'provide'>;\n\ntype OAuthStateStoreChoice =\n | 'in-memory'\n | Omit<Provider, 'provide'>;\n\nexport interface AuthModuleOptions {\n /** `'env'` (default) or a full provider definition (e.g. `{ useClass: MyKmsEncryptionKey }`). */\n encryptionKey?: EncryptionKeyChoice;\n /** `'in-memory'` (default) or a full provider definition for a Redis/DB impl. */\n oauthStateStore?: OAuthStateStoreChoice;\n}\n\nfunction resolveEncryptionKeyProvider(choice: EncryptionKeyChoice): Provider {\n if (choice === 'env') {\n return { provide: ENCRYPTION_KEY, useClass: EnvEncryptionKey };\n }\n return { provide: ENCRYPTION_KEY, ...choice } as Provider;\n}\n\nfunction resolveOAuthStateStoreProvider(\n choice: OAuthStateStoreChoice,\n): Provider {\n if (choice === 'in-memory') {\n return { provide: OAUTH_STATE_STORE, useClass: InMemoryOAuthStateStore };\n }\n return { provide: OAUTH_STATE_STORE, ...choice } as Provider;\n}\n\n@Module({})\nexport class AuthModule {\n static forRoot(options: AuthModuleOptions = {}): DynamicModule {\n const encryptionKeyProvider = resolveEncryptionKeyProvider(\n options.encryptionKey ?? 'env',\n );\n const oauthStateStoreProvider = resolveOAuthStateStoreProvider(\n options.oauthStateStore ?? 'in-memory',\n );\n\n return {\n module: AuthModule,\n global: true,\n providers: [encryptionKeyProvider, oauthStateStoreProvider],\n exports: [ENCRYPTION_KEY, OAUTH_STATE_STORE],\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;AAuBO,IAAM,iBAAiB,uBAAO,gBAAgB;AAC9C,IAAM,oBAAoB,uBAAO,mBAAmB;AACpD,IAAM,0BAA0B,uBAAO,yBAAyB;AAChE,IAAM,gCAAgC,uBAAO,+BAA+B;;;ACjB5E,IAAM,yBAAN,cAAqC,MAAM;AAAA,EAChD,YACW,eACA,WACA,kBACT;AACA;AAAA,MACE,eAAe,aAAa,YAAY,SAAS,MAAM,gBAAgB;AAAA,IACzE;AANS;AACA;AACA;AA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AAK,MAAM,IAAI,KAAK;AACjC,QAAI,CAAC,KAAM,QAAO;AAClB,SAAK,MAAM,OAAO,KAAK;AACvB,QAAI,KAAK,aAAa,KAAK,IAAI,EAAG,QAAO;AACzC,WAAO,KAAK;AAAA,EACd;AACF;;;ACJA,SAAS,cAAiD;AAoB1D,SAAS,6BAA6B,QAAuC;AAC3E,MAAI,WAAW,OAAO;AACpB,WAAO,EAAE,SAAS,gBAAgB,UAAU,iBAAiB;AAAA,EAC/D;AACA,SAAO,EAAE,SAAS,gBAAgB,GAAG,OAAO;AAC9C;AAEA,SAAS,+BACP,QACU;AACV,MAAI,WAAW,aAAa;AAC1B,WAAO,EAAE,SAAS,mBAAmB,UAAU,wBAAwB;AAAA,EACzE;AACA,SAAO,EAAE,SAAS,mBAAmB,GAAG,OAAO;AACjD;AAGO,IAAM,aAAN,MAAiB;AAAA,EACtB,OAAO,QAAQ,UAA6B,CAAC,GAAkB;AAC7D,UAAM,wBAAwB;AAAA,MAC5B,QAAQ,iBAAiB;AAAA,IAC3B;AACA,UAAM,0BAA0B;AAAA,MAC9B,QAAQ,mBAAmB;AAAA,IAC7B;AAEA,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,WAAW,CAAC,uBAAuB,uBAAuB;AAAA,MAC1D,SAAS,CAAC,gBAAgB,iBAAiB;AAAA,IAC7C;AAAA,EACF;AACF;AAhBa,aAAN;AAAA,EADN,OAAO,CAAC,CAAC;AAAA,GACG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../runtime/subsystems/auth/protocols/oauth-state-store.ts","../../../../runtime/subsystems/auth/auth.tokens.ts","../../../../runtime/subsystems/auth/runtime/integration-broken.error.ts","../../../../runtime/subsystems/auth/runtime/oauth2-refresh.strategy.ts","../../../../runtime/subsystems/auth/runtime/session-expired.error.ts","../../../../runtime/subsystems/auth/runtime/with-auth-retry.ts","../../../../runtime/subsystems/auth/auth-oauth-state.schema.ts","../../../../runtime/subsystems/auth/backends/encryption-key/env.ts","../../../../runtime/subsystems/auth/backends/state-store.memory-backend.ts","../../../../runtime/subsystems/auth/backends/state-store.drizzle-backend.ts","../../../../runtime/subsystems/auth/controllers/auth.controller.ts","../../../../runtime/subsystems/auth/auth.module.ts","../../../../runtime/constants/tokens.ts"],"sourcesContent":["/**\n * Auth subsystem — `IOAuthStateStore` port.\n *\n * CSRF protection for the OAuth2 authorize-code callback. Generic across\n * providers. The store mints opaque state tokens at /connect time and\n * single-use consumes them at /callback time, returning the original\n * record (userId + optional post-callback redirect path).\n *\n * Concrete backends live under `../backends/`:\n * - `state-store.memory-backend.ts` — in-process Map (tests/dev).\n * - `state-store.drizzle-backend.ts` — Postgres (prod).\n *\n * Semantics:\n * - `generate(record)` → returns an opaque state token; record is stored\n * under that token until consumed or until TTL expires.\n * - `consume(state)` → atomically deletes the entry and returns the\n * record. Throws on missing, expired, or replayed state. Never returns\n * null — a missing/expired state is a CSRF signal.\n */\nexport interface OAuthStateRecord {\n userId: string;\n /** Optional post-callback redirect path (relative URL). */\n redirect?: string;\n}\n\nexport interface IOAuthStateStore {\n /** Mint an opaque state token bound to `record`. Single-use. */\n generate(record: OAuthStateRecord): Promise<string>;\n /**\n * Atomically consume `state`, returning the bound record. Throws on\n * missing / expired / replayed state.\n */\n consume(state: string): Promise<OAuthStateRecord>;\n}\n\n/**\n * Thrown by `IOAuthStateStore.consume` when the state token is unknown,\n * expired, or has already been consumed (replay attempt).\n */\nexport class OAuthStateError extends Error {\n constructor(\n message: string,\n public readonly reason: 'missing' | 'expired',\n ) {\n super(message);\n this.name = 'OAuthStateError';\n }\n}\n","/**\n * Auth subsystem — injection tokens.\n *\n * Following ADR-008 guidance: `Symbol()` tokens for type safety and collision\n * avoidance. Consumers inject these via `@Inject(...)` against the matching\n * protocol interface.\n *\n * Usage:\n * ```typescript\n * constructor(\n * @Inject(ENCRYPTION_KEY) private readonly key: IEncryptionKey,\n * @Inject(OAUTH_STATE_STORE) private readonly states: IOAuthStateStore,\n * @Inject(AUTH_INTEGRATION_READER) private readonly reader: IIntegrationReader,\n * @Inject(AUTH_INTEGRATION_TOKEN_WRITER) private readonly writer: IIntegrationTokenWriter,\n * @Inject(AUTH_INTEGRATION_GRANT_SINK) private readonly grants: IIntegrationGrantSink,\n * @Inject(AUTH_USER_CONTEXT) private readonly userCtx: IUserContext,\n * @Inject(STRATEGY_REGISTRY) private readonly registry: ProviderStrategyRegistry,\n * ) {}\n * ```\n *\n * `IAuthStrategy` implementations are provider-specific and registered under\n * provider-specific tokens (e.g. `SALESFORCE_AUTH_STRATEGY`,\n * `HUBSPOT_AUTH_STRATEGY`) by each integration module — this subsystem does\n * not mandate a single `AUTH_STRATEGY` token because an app typically has\n * many concurrent strategies, one per provider. They are dispatched through\n * `STRATEGY_REGISTRY` (a `ReadonlyMap<slug, IProviderStrategy>`), populated\n * by per-provider modules via a `useFactory` provider.\n */\nexport const ENCRYPTION_KEY = Symbol('ENCRYPTION_KEY');\nexport const OAUTH_STATE_STORE = Symbol('OAUTH_STATE_STORE');\nexport const AUTH_INTEGRATION_READER = Symbol('AUTH_INTEGRATION_READER');\nexport const AUTH_INTEGRATION_TOKEN_WRITER = Symbol('AUTH_INTEGRATION_TOKEN_WRITER');\nexport const AUTH_INTEGRATION_GRANT_SINK = Symbol('AUTH_INTEGRATION_GRANT_SINK');\nexport const AUTH_USER_CONTEXT = Symbol('AUTH_USER_CONTEXT');\nexport const STRATEGY_REGISTRY = Symbol('STRATEGY_REGISTRY');\n/**\n * Holds the resolved `AuthModuleOptions` (used by `AuthController` to read\n * `redirectUriBase` for building per-provider callback URIs).\n */\nexport const AUTH_OPTIONS = Symbol('AUTH_OPTIONS');\n","/**\n * Thrown when an OAuth2 provider returns `400 invalid_grant`/`invalid_token`\n * on refresh — the refresh token itself is dead (user revoked, org\n * deactivated, token expired beyond the provider's rotation window). The\n * integration should be marked broken so background sync stops picking it\n * up; the user re-initiates OAuth.\n *\n * Shared across every OAuth2 strategy.\n */\nexport class IntegrationBrokenError extends Error {\n constructor(\n readonly integrationId: string,\n readonly errorCode: string,\n readonly errorDescription: string,\n ) {\n super(\n `Integration ${integrationId} broken: ${errorCode} - ${errorDescription}`,\n );\n this.name = 'IntegrationBrokenError';\n }\n}\n","/**\n * Abstract base class for OAuth2 refresh-token strategies.\n *\n * Template-method pattern: `resolve()` is concrete; four small hooks inject\n * provider specifics. Validated across two providers (Salesforce, HubSpot)\n * in the extraction-source app before being extracted here — see\n * `docs/gate-1-auth-extraction-findings.md` for the \"build first, extract\n * later\" evidence.\n *\n * Subclass contract:\n * - `provider` — slug matched against `integrations.provider`\n * - `defaultExpiresInSec` — fallback when refresh response omits `expires_in`\n * - `tokenEndpoint()` — URL to POST the refresh grant\n * - `refreshBodyExtras()` — provider-specific body params\n * - `parseRefreshResponse()` — raw JSON → ParsedRefreshResponse\n * - `buildCredentials()` — stored or freshly-refreshed access token +\n * integration + optional raw refresh response\n * → provider credentials\n *\n * Base handles: expiry check w/ 5-min safety window, `forceRefresh` escape\n * hatch, POST form-urlencoded body, OAuth2 error mapping to\n * `IntegrationBrokenError`, refresh-token rotation persistence, fetch +\n * clock injection for tests.\n */\nimport type {\n AuthCredentials,\n AuthResolveOptions,\n IAuthStrategy,\n} from '../protocols/auth-strategy';\nimport type {\n DecryptedIntegration,\n IIntegrationReader,\n IIntegrationTokenWriter,\n} from '../protocols/integration-store';\nimport { IntegrationBrokenError } from './integration-broken.error';\n\nexport type FetchLike = (\n input: string | URL | Request,\n init?: RequestInit,\n) => Promise<Response>;\n\n/** Safety window before expiry that triggers a refresh. */\nconst REFRESH_SAFETY_MS = 5 * 60 * 1000;\n\nexport interface OAuth2RefreshStrategyOptions {\n integrationReader: IIntegrationReader;\n tokenWriter: IIntegrationTokenWriter;\n /** Injectable fetch for tests. Defaults to the global `fetch`. */\n fetch?: FetchLike;\n /** Injectable clock for tests. Defaults to `Date.now`. */\n now?: () => number;\n}\n\nexport interface ParsedRefreshResponse {\n accessToken: string;\n /**\n * New refresh token if the provider rotated it (HubSpot: always, Salesforce:\n * sometimes). Omit when the provider reused the old refresh token.\n */\n refreshToken?: string;\n /** Seconds from now. If omitted, subclass `defaultExpiresInSec` applies. */\n expiresInSec?: number;\n}\n\nexport abstract class OAuth2RefreshStrategy implements IAuthStrategy {\n protected abstract readonly provider: string;\n protected abstract readonly defaultExpiresInSec: number;\n\n protected readonly integrationReader: IIntegrationReader;\n protected readonly tokenWriter: IIntegrationTokenWriter;\n protected readonly fetchImpl: FetchLike;\n protected readonly now: () => number;\n\n constructor(opts: OAuth2RefreshStrategyOptions) {\n this.integrationReader = opts.integrationReader;\n this.tokenWriter = opts.tokenWriter;\n this.fetchImpl = opts.fetch ?? fetch;\n this.now = opts.now ?? Date.now;\n }\n\n async resolve(\n integrationId: string,\n opts: AuthResolveOptions = {},\n ): Promise<AuthCredentials> {\n const integration =\n await this.integrationReader.findByIdDecrypted(integrationId);\n if (!integration) {\n throw new Error(`Integration ${integrationId} not found`);\n }\n if (integration.provider !== this.provider) {\n throw new Error(\n `${this.constructor.name} called for non-${this.provider} integration ${integrationId} (provider=${integration.provider})`,\n );\n }\n\n const needsRefresh =\n opts.forceRefresh ||\n this.isExpiring(integration.expiresAt) ||\n !integration.accessToken;\n\n if (!needsRefresh) {\n return this.buildCredentials(integration.accessToken, integration);\n }\n\n if (!integration.refreshToken) {\n throw new IntegrationBrokenError(\n integrationId,\n 'no_refresh_token',\n 'Integration has no refresh token; user must reconnect',\n );\n }\n\n const { parsed, raw } = await this.executeRefresh(\n integrationId,\n integration.refreshToken,\n );\n const newExpiresAt = new Date(\n this.now() + (parsed.expiresInSec ?? this.defaultExpiresInSec) * 1000,\n );\n await this.tokenWriter.persistRefresh({\n integrationId,\n accessToken: parsed.accessToken,\n refreshToken: parsed.refreshToken ?? undefined,\n expiresAt: newExpiresAt,\n });\n\n return this.buildCredentials(parsed.accessToken, integration, raw);\n }\n\n protected abstract tokenEndpoint(): string;\n protected abstract refreshBodyExtras(): Record<string, string>;\n protected abstract parseRefreshResponse(raw: unknown): ParsedRefreshResponse;\n protected abstract buildCredentials(\n accessToken: string,\n integration: DecryptedIntegration,\n refreshRaw?: unknown,\n ): AuthCredentials;\n\n private async executeRefresh(\n integrationId: string,\n refreshToken: string,\n ): Promise<{ parsed: ParsedRefreshResponse; raw: unknown }> {\n const body = new URLSearchParams({\n grant_type: 'refresh_token',\n refresh_token: refreshToken,\n ...this.refreshBodyExtras(),\n });\n const response = await this.fetchImpl(this.tokenEndpoint(), {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: body.toString(),\n });\n if (!response.ok) {\n const err = (await safeJson(response)) as Partial<{\n error: string;\n error_description: string;\n message: string;\n }>;\n if (\n response.status === 400 &&\n (err.error === 'invalid_grant' || err.error === 'invalid_token')\n ) {\n throw new IntegrationBrokenError(\n integrationId,\n err.error ?? 'invalid_grant',\n err.error_description ?? err.message ?? 'refresh token rejected',\n );\n }\n throw new Error(\n `${this.provider} token refresh failed: ${response.status} ${err.error ?? ''} ${err.error_description ?? err.message ?? ''}`.trim(),\n );\n }\n const raw = await response.json();\n return { parsed: this.parseRefreshResponse(raw), raw };\n }\n\n private isExpiring(expiresAt: Date | null): boolean {\n if (!expiresAt) return true;\n return expiresAt.getTime() - this.now() < REFRESH_SAFETY_MS;\n }\n}\n\nasync function safeJson(response: Response): Promise<unknown> {\n try {\n return await response.clone().json();\n } catch {\n return {};\n }\n}\n","/**\n * Provider-agnostic marker for \"the access token was rejected; a forced\n * refresh may recover.\"\n *\n * Concrete provider error classes (e.g. SalesforceSessionExpiredError,\n * HubSpotUnauthorizedError) either extend `SessionExpiredError` directly or\n * set `isSessionExpired === true` on their instances. `withAuthRetry` uses\n * the `isSessionExpiredError` predicate to decide whether to force-refresh\n * and retry once.\n *\n * This discriminator replaces the SFDC-only `instanceof` check from the\n * extraction-source app's original `withAuthRetry`. See\n * `docs/gate-1-auth-extraction-findings.md` (recommendation 4).\n */\nexport class SessionExpiredError extends Error {\n /** Duck-type marker — works across package boundaries where `instanceof` fails. */\n readonly isSessionExpired = true as const;\n\n constructor(message = 'Access token rejected by provider') {\n super(message);\n this.name = 'SessionExpiredError';\n }\n}\n\n/**\n * Predicate used by `withAuthRetry` by default.\n *\n * Matches any error that either `instanceof SessionExpiredError` or carries\n * the `isSessionExpired === true` marker property. Provider adapters that\n * want their existing error classes to participate can simply add the\n * marker property without touching the class hierarchy.\n */\nexport function isSessionExpiredError(err: unknown): boolean {\n if (err instanceof SessionExpiredError) return true;\n if (err !== null && typeof err === 'object' && 'isSessionExpired' in err) {\n return (err as { isSessionExpired?: unknown }).isSessionExpired === true;\n }\n return false;\n}\n","/**\n * Run `op` with auth-aware retry-once on session-expired errors.\n *\n * Pattern: resolve creds → run op → if `isSessionExpired(e)` → resolve with\n * `forceRefresh: true` → retry → propagate. A second session-expired error\n * on the refreshed token propagates rather than looping, so transient\n * adapter bugs can't hang the caller.\n *\n * Generalisation over the extraction source's SFDC-specific original: the\n * session-expired classifier is injected. Providers mark their session-\n * expired errors (via `instanceof` of a marker class, or by setting a known\n * property) and pass a classifier matching that shape.\n *\n * Default classifier recognises the marker interface `SessionExpiredError`\n * shipped in `session-expired.error.ts` — concrete provider errors that\n * extend it (or set `isSessionExpired === true`) get retried without any\n * further wiring.\n */\nimport type {\n AuthCredentials,\n IAuthStrategy,\n} from '../protocols/auth-strategy';\nimport { isSessionExpiredError } from './session-expired.error';\n\nexport interface WithAuthRetryOptions {\n /**\n * Classifier that decides whether a thrown error is a session-expired\n * signal worth retrying once with a fresh token. Defaults to the marker-\n * interface check in `session-expired.error.ts`.\n */\n isSessionExpired?: (err: unknown) => boolean;\n}\n\nexport async function withAuthRetry<T>(\n authStrategy: IAuthStrategy,\n integrationId: string,\n op: (credentials: AuthCredentials) => Promise<T>,\n options: WithAuthRetryOptions = {},\n): Promise<T> {\n const classify = options.isSessionExpired ?? isSessionExpiredError;\n\n let creds = await authStrategy.resolve(integrationId);\n try {\n return await op(creds);\n } catch (e) {\n if (!classify(e)) throw e;\n creds = await authStrategy.resolve(integrationId, { forceRefresh: true });\n return op(creds);\n }\n}\n","/**\n * Drizzle schema for the `auth_oauth_state` table — backs the\n * `DrizzleOAuthStateStore` (`state-store.drizzle-backend.ts`).\n *\n * One row per outstanding /connect → /callback dance. Single-use; rows are\n * deleted on consume. A periodic sweep (or a `WHERE expires_at < now()`\n * filter on read) clears abandoned rows.\n *\n * Columns:\n * - `state` — opaque random token, primary key.\n * - `user_id` — text (matches the consumer-defined user-id shape;\n * the auth subsystem doesn't constrain this to UUID\n * because some apps key users by external id).\n * - `redirect` — optional post-callback redirect path.\n * - `expires_at` — TTL boundary; entries past this are treated as absent.\n *\n * Convention: schema files live at the root of the subsystem dir\n * (mirrors `cache.schema.ts`, `sync-audit.schema.ts`, `domain-events.schema.ts`).\n */\nimport { pgTable, text, timestamp } from 'drizzle-orm/pg-core';\nimport type { InferSelectModel } from 'drizzle-orm';\n\nexport const authOAuthState = pgTable('auth_oauth_state', {\n state: text('state').primaryKey(),\n userId: text('user_id').notNull(),\n redirect: text('redirect'),\n expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),\n});\n\nexport type AuthOAuthState = InferSelectModel<typeof authOAuthState>;\n","/**\n * Env-backed AES-256-GCM encryption.\n *\n * Framing: `base64( nonce(12B) || ciphertext || authTag(16B) )`. Random nonce\n * per call means two encryptions of the same plaintext produce different\n * ciphertexts — prevents replay-style inference. Auth tag enforces integrity;\n * any tampering throws on decrypt.\n *\n * Key source: `INTEGRATION_TOKEN_ENCRYPTION_KEY` env var, 32 bytes base64-encoded.\n * Generate via `openssl rand -base64 32`.\n *\n * Future backend: `kms.ts` (AWS/GCP KMS) for production deployments that\n * need key rotation + audit trails.\n */\nimport { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';\nimport type { IEncryptionKey } from '../../protocols/encryption-key';\n\nexport interface EnvEncryptionKeyOptions {\n /** Defaults to `process.env`. Tests inject a fixture. */\n env?: NodeJS.ProcessEnv;\n /** Defaults to `'INTEGRATION_TOKEN_ENCRYPTION_KEY'`. */\n envVar?: string;\n}\n\nconst ALGO = 'aes-256-gcm';\nconst NONCE_BYTES = 12;\nconst TAG_BYTES = 16;\nconst KEY_BYTES = 32;\n\nexport class EnvEncryptionKey implements IEncryptionKey {\n private readonly key: Buffer;\n\n constructor(opts: EnvEncryptionKeyOptions = {}) {\n const env = opts.env ?? process.env;\n const envVar = opts.envVar ?? 'INTEGRATION_TOKEN_ENCRYPTION_KEY';\n const raw = env[envVar];\n if (!raw) {\n throw new Error(\n `EnvEncryptionKey: ${envVar} is not set. Generate with: openssl rand -base64 32`,\n );\n }\n const decoded = Buffer.from(raw, 'base64');\n if (decoded.length !== KEY_BYTES) {\n throw new Error(\n `EnvEncryptionKey: ${envVar} must decode to ${KEY_BYTES} bytes (got ${decoded.length}). Use: openssl rand -base64 32`,\n );\n }\n this.key = decoded;\n }\n\n async encrypt(plaintext: string): Promise<string> {\n const nonce = randomBytes(NONCE_BYTES);\n const cipher = createCipheriv(ALGO, this.key, nonce);\n const ciphertext = Buffer.concat([\n cipher.update(plaintext, 'utf8'),\n cipher.final(),\n ]);\n const authTag = cipher.getAuthTag();\n return Buffer.concat([nonce, ciphertext, authTag]).toString('base64');\n }\n\n async decrypt(ciphertext: string): Promise<string> {\n const buf = Buffer.from(ciphertext, 'base64');\n if (buf.length < NONCE_BYTES + TAG_BYTES) {\n throw new Error('EnvEncryptionKey: ciphertext too short');\n }\n const nonce = buf.subarray(0, NONCE_BYTES);\n const authTag = buf.subarray(buf.length - TAG_BYTES);\n const body = buf.subarray(NONCE_BYTES, buf.length - TAG_BYTES);\n\n const decipher = createDecipheriv(ALGO, this.key, nonce);\n decipher.setAuthTag(authTag);\n const plain = Buffer.concat([decipher.update(body), decipher.final()]);\n return plain.toString('utf8');\n }\n}\n","/**\n * In-memory `IOAuthStateStore` backend.\n *\n * Single-process store — Map<state, { record, expiresAt }>. Suitable for\n * tests and single-worker dev. Production deployments select the drizzle\n * backend so state survives restarts and is shared across workers.\n *\n * Single-use semantics:\n * - `generate(record)` mints a 256-bit random token (base64url, opaque).\n * - `consume(state)` deletes the entry on read. A second call with the\n * same state throws `OAuthStateError('replay')`.\n * - Expired entries also throw (`'expired'`); the entry is deleted as a\n * side effect so a later replay still surfaces correctly.\n *\n * TTL defaults to 10 minutes — long enough for a user to complete the\n * provider's consent screen, short enough that abandoned states age out.\n */\nimport { randomBytes } from 'node:crypto';\nimport {\n type IOAuthStateStore,\n type OAuthStateRecord,\n OAuthStateError,\n} from '../protocols/oauth-state-store';\n\nexport interface MemoryOAuthStateStoreOptions {\n /** TTL in ms. Default 10 minutes. */\n ttlMs?: number;\n /** Injectable clock for tests. Default `Date.now`. */\n now?: () => number;\n /** Injectable token generator for tests. Default 32-byte base64url. */\n generateToken?: () => string;\n}\n\ninterface Slot {\n record: OAuthStateRecord;\n expiresAt: number;\n}\n\nexport class MemoryOAuthStateStore implements IOAuthStateStore {\n private readonly store = new Map<string, Slot>();\n private readonly ttlMs: number;\n private readonly now: () => number;\n private readonly generateToken: () => string;\n\n constructor(opts: MemoryOAuthStateStoreOptions = {}) {\n this.ttlMs = opts.ttlMs ?? 10 * 60 * 1000;\n this.now = opts.now ?? (() => Date.now());\n this.generateToken =\n opts.generateToken ?? (() => randomBytes(32).toString('base64url'));\n }\n\n async generate(record: OAuthStateRecord): Promise<string> {\n const state = this.generateToken();\n this.store.set(state, {\n record: { ...record },\n expiresAt: this.now() + this.ttlMs,\n });\n return state;\n }\n\n async consume(state: string): Promise<OAuthStateRecord> {\n const slot = this.store.get(state);\n if (!slot) {\n throw new OAuthStateError(\n `OAuth state token unknown or already consumed`,\n 'missing',\n );\n }\n // Delete first so a concurrent consume can't replay.\n this.store.delete(state);\n if (slot.expiresAt <= this.now()) {\n throw new OAuthStateError(`OAuth state token expired`, 'expired');\n }\n return slot.record;\n }\n}\n","/**\n * Drizzle-backed `IOAuthStateStore`.\n *\n * Uses the `auth_oauth_state` table (see `auth-oauth-state.schema.ts`).\n * Single-use semantics enforced via `DELETE ... RETURNING`: the consume\n * path atomically deletes and returns the row, so a concurrent /callback\n * with the same state cannot replay.\n *\n * Behaviour:\n * - `generate(record)` mints a 256-bit base64url token, INSERTs the row\n * with `expires_at = now() + ttlMs`.\n * - `consume(state)` runs `DELETE ... WHERE state = $1 RETURNING ...`\n * once. Throws `OAuthStateError('missing')` if no row was deleted\n * (unknown or already consumed) and `OAuthStateError('expired')` if\n * the deleted row was past its `expires_at`.\n */\nimport { randomBytes } from 'node:crypto';\nimport { eq } from 'drizzle-orm';\nimport type { DrizzleClient } from '../../../types/drizzle';\nimport { authOAuthState } from '../auth-oauth-state.schema';\nimport {\n type IOAuthStateStore,\n type OAuthStateRecord,\n OAuthStateError,\n} from '../protocols/oauth-state-store';\n\nexport interface DrizzleOAuthStateStoreOptions {\n /** TTL in ms. Default 10 minutes. */\n ttlMs?: number;\n /** Injectable clock for tests. Default `Date.now`. */\n now?: () => number;\n /** Injectable token generator for tests. Default 32-byte base64url. */\n generateToken?: () => string;\n}\n\nexport class DrizzleOAuthStateStore implements IOAuthStateStore {\n private readonly ttlMs: number;\n private readonly now: () => number;\n private readonly generateToken: () => string;\n\n constructor(\n private readonly db: DrizzleClient,\n opts: DrizzleOAuthStateStoreOptions = {},\n ) {\n this.ttlMs = opts.ttlMs ?? 10 * 60 * 1000;\n this.now = opts.now ?? (() => Date.now());\n this.generateToken =\n opts.generateToken ?? (() => randomBytes(32).toString('base64url'));\n }\n\n async generate(record: OAuthStateRecord): Promise<string> {\n const state = this.generateToken();\n const expiresAt = new Date(this.now() + this.ttlMs);\n await this.db.insert(authOAuthState).values({\n state,\n userId: record.userId,\n redirect: record.redirect ?? null,\n expiresAt,\n });\n return state;\n }\n\n async consume(state: string): Promise<OAuthStateRecord> {\n const rows = await this.db\n .delete(authOAuthState)\n .where(eq(authOAuthState.state, state))\n .returning();\n const row = rows[0];\n if (!row) {\n throw new OAuthStateError(\n `OAuth state token unknown or already consumed`,\n 'missing',\n );\n }\n if (row.expiresAt.getTime() <= this.now()) {\n throw new OAuthStateError(`OAuth state token expired`, 'expired');\n }\n return {\n userId: row.userId,\n redirect: row.redirect ?? undefined,\n };\n }\n}\n","/**\n * AuthController — provider-agnostic OAuth2 connect/callback dance.\n *\n * Mounts two routes:\n * - `GET /auth/:provider/connect?redirect=...` — generates state, builds\n * the provider's authorize-url, 302-redirects the browser there.\n * - `GET /auth/:provider/callback?code=...&state=...` — consumes state,\n * exchanges the code for tokens, hands them to the grant sink, then\n * 302-redirects to the post-connect path.\n *\n * Hexagonal seams:\n * - `STRATEGY_REGISTRY` (ReadonlyMap<slug, IProviderStrategy>) — dispatch.\n * Concrete per-provider strategies live consumer-side and contribute\n * entries via a `useFactory` in the consumer's app module.\n * - `AUTH_USER_CONTEXT` (IUserContext) — resolves \"who is this request\"\n * from the consumer's session/JWT/etc.\n * - `OAUTH_STATE_STORE` (IOAuthStateStore) — CSRF state minting/consume.\n * - `AUTH_INTEGRATION_GRANT_SINK` (IIntegrationGrantSink) — persists the\n * freshly-minted grant. Adapter lives consumer-side (e.g. the\n * auth-integrations starter from #285).\n *\n * The controller never imports `IntegrationsService` or any other concrete\n * consumer type — it goes through ports only.\n */\nimport {\n Controller,\n Get,\n Inject,\n Param,\n Query,\n Req,\n Res,\n HttpException,\n HttpStatus,\n} from '@nestjs/common';\nimport {\n AUTH_INTEGRATION_GRANT_SINK,\n AUTH_OPTIONS,\n AUTH_USER_CONTEXT,\n OAUTH_STATE_STORE,\n STRATEGY_REGISTRY,\n} from '../auth.tokens';\nimport type { AuthModuleOptions } from '../auth.module';\nimport type { IOAuthStateStore } from '../protocols/oauth-state-store';\nimport type { IUserContext } from '../protocols/user-context';\nimport type {\n IProviderStrategy,\n ProviderStrategyRegistry,\n} from '../protocols/provider-strategy';\nimport type { IIntegrationGrantSink } from '../protocols/integration-store';\n\n/**\n * Minimal response surface used by the controller — typed loosely so we\n * don't pull a hard dep on `express` or `fastify`. Both popular HTTP\n * adapters expose `redirect(status, url)`.\n */\ninterface RedirectingResponse {\n redirect(statusCode: number, url: string): unknown;\n}\n\n@Controller('auth')\nexport class AuthController {\n constructor(\n @Inject(STRATEGY_REGISTRY)\n private readonly registry: ProviderStrategyRegistry,\n @Inject(AUTH_USER_CONTEXT)\n private readonly userContext: IUserContext,\n @Inject(OAUTH_STATE_STORE)\n private readonly stateStore: IOAuthStateStore,\n @Inject(AUTH_INTEGRATION_GRANT_SINK)\n private readonly grantSink: IIntegrationGrantSink,\n @Inject(AUTH_OPTIONS)\n private readonly options: AuthModuleOptions,\n ) {}\n\n @Get(':provider/connect')\n async connect(\n @Param('provider') slug: string,\n @Query('redirect') redirect: string | undefined,\n @Req() req: unknown,\n @Res() res: RedirectingResponse,\n ): Promise<unknown> {\n const strategy = this.requireStrategy(slug);\n const userId = await this.userContext.getCurrentUserId(req);\n const state = await this.stateStore.generate({ userId, redirect });\n const url = strategy.buildAuthorizeUrl({\n state,\n redirectUri: this.redirectUriFor(slug),\n });\n return res.redirect(HttpStatus.FOUND, url);\n }\n\n @Get(':provider/callback')\n async callback(\n @Param('provider') slug: string,\n @Query('code') code: string | undefined,\n @Query('state') state: string | undefined,\n @Res() res: RedirectingResponse,\n ): Promise<unknown> {\n const strategy = this.requireStrategy(slug);\n if (!code) {\n throw new HttpException(\n `Missing 'code' query param`,\n HttpStatus.BAD_REQUEST,\n );\n }\n if (!state) {\n throw new HttpException(\n `Missing 'state' query param`,\n HttpStatus.BAD_REQUEST,\n );\n }\n const { userId, redirect } = await this.stateStore.consume(state);\n const tokens = await strategy.exchangeCodeForTokens({\n code,\n redirectUri: this.redirectUriFor(slug),\n });\n await this.grantSink.createOrUpdateFromOAuthGrant({\n userId,\n provider: slug,\n accessToken: tokens.accessToken,\n refreshToken: tokens.refreshToken,\n expiresAt: tokens.expiresAt,\n scope: tokens.scope,\n externalAccountId: tokens.externalAccountId,\n providerMetadata: tokens.providerMetadata,\n });\n return res.redirect(\n HttpStatus.FOUND,\n redirect ?? `/settings/integrations?connected=${encodeURIComponent(slug)}`,\n );\n }\n\n private requireStrategy(slug: string): IProviderStrategy {\n const strategy = this.registry.get(slug);\n if (!strategy) {\n throw new HttpException(\n `Unknown provider '${slug}'`,\n HttpStatus.NOT_FOUND,\n );\n }\n return strategy;\n }\n\n private redirectUriFor(slug: string): string {\n const base = this.options.redirectUriBase;\n if (!base) {\n throw new Error(\n `AuthModule.forRoot: redirectUriBase is required when AuthController is enabled`,\n );\n }\n const trimmed = base.replace(/\\/+$/, '');\n return `${trimmed}/auth/${encodeURIComponent(slug)}/callback`;\n }\n}\n","/**\n * AuthModule — DynamicModule factory for the auth subsystem.\n *\n * Wires the pluggable backends the subsystem ships with:\n * - `ENCRYPTION_KEY` → `EnvEncryptionKey` (AES-256-GCM from env)\n * - `OAUTH_STATE_STORE` → `MemoryOAuthStateStore` (dev/tests) or\n * `DrizzleOAuthStateStore` (prod, requires\n * DRIZZLE provider).\n * - `AUTH_OPTIONS` → resolved options bag (used by AuthController\n * for `redirectUriBase`).\n *\n * The integration-store ports (`AUTH_INTEGRATION_READER`,\n * `AUTH_INTEGRATION_TOKEN_WRITER`, `AUTH_INTEGRATION_GRANT_SINK`),\n * `AUTH_USER_CONTEXT`, and `STRATEGY_REGISTRY` are deliberately **not**\n * wired here — they are always consumer-specific:\n * - integration-store ports adapt the consumer's `integrations` storage;\n * - `IUserContext` adapts the app's session/JWT scheme;\n * - `STRATEGY_REGISTRY` is populated from the per-provider strategy\n * classes the consumer maintains.\n *\n * Consumers provide them in their app module (or by importing the\n * `auth-integrations` starter, which binds the three integration-store\n * ports off a single canonical entity).\n *\n * Usage in AppModule:\n * ```typescript\n * AuthModule.forRoot({\n * encryptionKey: 'env',\n * oauthStateStore: 'memory', // or 'drizzle'\n * enableController: true,\n * redirectUriBase: 'http://localhost:3000',\n * });\n * ```\n *\n * `global: true` means other modules don't need to re-import AuthModule to\n * inject the auth tokens.\n */\nimport { Module, type DynamicModule, type Provider } from '@nestjs/common';\nimport {\n AUTH_OPTIONS,\n ENCRYPTION_KEY,\n OAUTH_STATE_STORE,\n} from './auth.tokens';\nimport { EnvEncryptionKey } from './backends/encryption-key/env';\nimport { MemoryOAuthStateStore } from './backends/state-store.memory-backend';\nimport { DrizzleOAuthStateStore } from './backends/state-store.drizzle-backend';\nimport { AuthController } from './controllers/auth.controller';\nimport { DRIZZLE } from '../../constants/tokens';\nimport type { DrizzleClient } from '../../types/drizzle';\n\ntype EncryptionKeyChoice =\n | 'env'\n | Omit<Provider, 'provide'>;\n\ntype OAuthStateStoreChoice =\n | 'memory'\n | 'drizzle'\n | Omit<Provider, 'provide'>;\n\nexport interface AuthModuleOptions {\n /** `'env'` (default) or a full provider definition (e.g. `{ useClass: MyKmsEncryptionKey }`). */\n encryptionKey?: EncryptionKeyChoice;\n /**\n * `'memory'` (default — tests/dev) or `'drizzle'` (prod, requires DRIZZLE\n * provider) or a full provider definition for a custom impl.\n */\n oauthStateStore?: OAuthStateStoreChoice;\n /**\n * Mount `AuthController` (`/auth/:provider/connect` + `/callback`).\n * Default `false` — apps that hand-roll connect/callback (rare) or that\n * use the subsystem only for the refresh path can opt out.\n */\n enableController?: boolean;\n /**\n * Public base URL of the API server. Used to construct per-provider\n * callback URIs as `${redirectUriBase}/auth/:provider/callback`.\n * Required when `enableController: true`.\n */\n redirectUriBase?: string;\n}\n\nfunction resolveEncryptionKeyProvider(choice: EncryptionKeyChoice): Provider {\n if (choice === 'env') {\n return { provide: ENCRYPTION_KEY, useClass: EnvEncryptionKey };\n }\n return { provide: ENCRYPTION_KEY, ...choice } as Provider;\n}\n\nfunction resolveOAuthStateStoreProvider(\n choice: OAuthStateStoreChoice,\n): Provider {\n if (choice === 'memory') {\n return { provide: OAUTH_STATE_STORE, useClass: MemoryOAuthStateStore };\n }\n if (choice === 'drizzle') {\n return {\n provide: OAUTH_STATE_STORE,\n useFactory: (db: DrizzleClient | null) => {\n if (!db) {\n throw new Error(\n \"AuthModule.forRoot: oauthStateStore: 'drizzle' selected but DRIZZLE provider is not available. \" +\n 'Ensure DatabaseModule (or another provider exposing DRIZZLE) is imported before AuthModule.forRoot.',\n );\n }\n return new DrizzleOAuthStateStore(db);\n },\n inject: [{ token: DRIZZLE, optional: true }],\n };\n }\n return { provide: OAUTH_STATE_STORE, ...choice } as Provider;\n}\n\n@Module({})\nexport class AuthModule {\n static forRoot(options: AuthModuleOptions = {}): DynamicModule {\n const resolved: AuthModuleOptions = {\n encryptionKey: options.encryptionKey ?? 'env',\n oauthStateStore: options.oauthStateStore ?? 'memory',\n enableController: options.enableController ?? false,\n redirectUriBase: options.redirectUriBase,\n };\n\n if (resolved.enableController && !resolved.redirectUriBase) {\n throw new Error(\n 'AuthModule.forRoot: redirectUriBase is required when enableController: true',\n );\n }\n\n const encryptionKeyProvider = resolveEncryptionKeyProvider(\n resolved.encryptionKey!,\n );\n const oauthStateStoreProvider = resolveOAuthStateStoreProvider(\n resolved.oauthStateStore!,\n );\n const optionsProvider: Provider = {\n provide: AUTH_OPTIONS,\n useValue: resolved,\n };\n\n return {\n module: AuthModule,\n global: true,\n providers: [encryptionKeyProvider, oauthStateStoreProvider, optionsProvider],\n controllers: resolved.enableController ? [AuthController] : [],\n exports: [ENCRYPTION_KEY, OAUTH_STATE_STORE, AUTH_OPTIONS],\n };\n }\n}\n","/**\n * NestJS injection tokens\n *\n * Used with @Inject() decorator in concrete repository constructors.\n */\n\n/**\n * Injection token for the Drizzle ORM database client.\n *\n * Usage in concrete repositories:\n * ```typescript\n * constructor(@Inject(DRIZZLE) db: DrizzleClient) { super(db); }\n * ```\n */\nexport const DRIZZLE = 'DRIZZLE' as const;\n\n/**\n * Injection token for the event bus (IEventBus).\n *\n * Optional — only resolved when EventsModule.forRoot() is registered.\n * BaseService uses this with @Optional() to emit lifecycle events\n * without requiring the events subsystem to be installed.\n *\n * Usage in services/use cases:\n * ```typescript\n * @Optional() @Inject(EVENT_BUS) eventBus?: IEventBus\n * ```\n */\nexport const EVENT_BUS = 'EVENT_BUS' as const;\n"],"mappings":";;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AAC,CAAC;AAAA,GACG;","names":["randomBytes","randomBytes","randomBytes","randomBytes"]}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* `OAuth2RefreshStrategy` template-method base in `../runtime/`.
|
|
8
8
|
*
|
|
9
9
|
* See `docs/adrs/ADR-031-auth-subsystem.md` and
|
|
10
|
-
* `docs/gate-1-auth-extraction-findings.md` (
|
|
10
|
+
* `docs/gate-1-auth-extraction-findings.md` (extraction-source findings) for
|
|
11
11
|
* the rationale.
|
|
12
12
|
*/
|
|
13
13
|
/**
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* those rows — consumers implement these narrow ports against whatever
|
|
7
7
|
* `integrations` table their app uses.
|
|
8
8
|
*
|
|
9
|
-
* In
|
|
9
|
+
* In the extraction-source app both ports are satisfied by a
|
|
10
10
|
* pair of thin adapters over `IntegrationService` + `RefreshIntegrationUseCase`.
|
|
11
11
|
* The codegen-patterns `examples/auth-integrations/` starter (separate PR)
|
|
12
12
|
* ships a canonical `integration.yaml` whose generated service + use case
|
|
@@ -60,5 +60,40 @@ interface IntegrationTokenUpdate {
|
|
|
60
60
|
interface IIntegrationTokenWriter {
|
|
61
61
|
persistRefresh(update: IntegrationTokenUpdate): Promise<void>;
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Grant-sink port — persists a freshly-minted OAuth2 grant from the
|
|
65
|
+
* authorize-code callback (i.e. the user just connected a new provider, or
|
|
66
|
+
* re-connected an existing one).
|
|
67
|
+
*
|
|
68
|
+
* `AuthController.callback` invokes this after `IProviderStrategy.exchangeCodeForTokens`.
|
|
69
|
+
* The subsystem itself never imports a concrete `IntegrationsService` — the
|
|
70
|
+
* consumer's `auth-integrations` starter (or any equivalent) adapts this
|
|
71
|
+
* port. Keeps the auth subsystem standalone: a non-codegen consumer can
|
|
72
|
+
* satisfy the port against its own integrations storage.
|
|
73
|
+
*
|
|
74
|
+
* Semantics:
|
|
75
|
+
* - Upserts on `(userId, provider)`. Repeated grants for the same pair
|
|
76
|
+
* replace the prior tokens (re-connect flow).
|
|
77
|
+
* - Implementations are responsible for encrypting tokens at rest.
|
|
78
|
+
* - `expiresAt` / `refreshToken` / `scope` / `externalAccountId` /
|
|
79
|
+
* `providerMetadata` are optional because not every provider supplies
|
|
80
|
+
* them (e.g. some providers omit `expires_in`; not every flow returns
|
|
81
|
+
* a refresh token on first grant).
|
|
82
|
+
*/
|
|
83
|
+
interface IntegrationGrantInput {
|
|
84
|
+
userId: string;
|
|
85
|
+
/** Provider slug — must match the strategy's `provider`. */
|
|
86
|
+
provider: string;
|
|
87
|
+
accessToken: string;
|
|
88
|
+
refreshToken?: string;
|
|
89
|
+
expiresAt?: Date;
|
|
90
|
+
scope?: string[];
|
|
91
|
+
externalAccountId?: string;
|
|
92
|
+
/** Provider-specific bag (SFDC `instance_url`, Google `sub`, …). */
|
|
93
|
+
providerMetadata?: Record<string, unknown>;
|
|
94
|
+
}
|
|
95
|
+
interface IIntegrationGrantSink {
|
|
96
|
+
createOrUpdateFromOAuthGrant(input: IntegrationGrantInput): Promise<void>;
|
|
97
|
+
}
|
|
63
98
|
|
|
64
|
-
export type { DecryptedIntegration, IIntegrationReader, IIntegrationTokenWriter, IntegrationTokenUpdate };
|
|
99
|
+
export type { DecryptedIntegration, IIntegrationGrantSink, IIntegrationReader, IIntegrationTokenWriter, IntegrationGrantInput, IntegrationTokenUpdate };
|
|
@@ -2,16 +2,42 @@
|
|
|
2
2
|
* Auth subsystem — `IOAuthStateStore` port.
|
|
3
3
|
*
|
|
4
4
|
* CSRF protection for the OAuth2 authorize-code callback. Generic across
|
|
5
|
-
* providers.
|
|
5
|
+
* providers. The store mints opaque state tokens at /connect time and
|
|
6
|
+
* single-use consumes them at /callback time, returning the original
|
|
7
|
+
* record (userId + optional post-callback redirect path).
|
|
8
|
+
*
|
|
9
|
+
* Concrete backends live under `../backends/`:
|
|
10
|
+
* - `state-store.memory-backend.ts` — in-process Map (tests/dev).
|
|
11
|
+
* - `state-store.drizzle-backend.ts` — Postgres (prod).
|
|
12
|
+
*
|
|
13
|
+
* Semantics:
|
|
14
|
+
* - `generate(record)` → returns an opaque state token; record is stored
|
|
15
|
+
* under that token until consumed or until TTL expires.
|
|
16
|
+
* - `consume(state)` → atomically deletes the entry and returns the
|
|
17
|
+
* record. Throws on missing, expired, or replayed state. Never returns
|
|
18
|
+
* null — a missing/expired state is a CSRF signal.
|
|
6
19
|
*/
|
|
7
|
-
interface
|
|
20
|
+
interface OAuthStateRecord {
|
|
8
21
|
userId: string;
|
|
9
|
-
|
|
22
|
+
/** Optional post-callback redirect path (relative URL). */
|
|
23
|
+
redirect?: string;
|
|
10
24
|
}
|
|
11
25
|
interface IOAuthStateStore {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
26
|
+
/** Mint an opaque state token bound to `record`. Single-use. */
|
|
27
|
+
generate(record: OAuthStateRecord): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Atomically consume `state`, returning the bound record. Throws on
|
|
30
|
+
* missing / expired / replayed state.
|
|
31
|
+
*/
|
|
32
|
+
consume(state: string): Promise<OAuthStateRecord>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Thrown by `IOAuthStateStore.consume` when the state token is unknown,
|
|
36
|
+
* expired, or has already been consumed (replay attempt).
|
|
37
|
+
*/
|
|
38
|
+
declare class OAuthStateError extends Error {
|
|
39
|
+
readonly reason: 'missing' | 'expired';
|
|
40
|
+
constructor(message: string, reason: 'missing' | 'expired');
|
|
15
41
|
}
|
|
16
42
|
|
|
17
|
-
export type
|
|
43
|
+
export { type IOAuthStateStore, OAuthStateError, type OAuthStateRecord };
|
|
@@ -1 +1,13 @@
|
|
|
1
|
+
// runtime/subsystems/auth/protocols/oauth-state-store.ts
|
|
2
|
+
var OAuthStateError = class extends Error {
|
|
3
|
+
constructor(message, reason) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.reason = reason;
|
|
6
|
+
this.name = "OAuthStateError";
|
|
7
|
+
}
|
|
8
|
+
reason;
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
OAuthStateError
|
|
12
|
+
};
|
|
1
13
|
//# sourceMappingURL=oauth-state-store.js.map
|