@opencloudcms/oauth2 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +397 -2
- package/dist/audit/audit.service.d.ts +18 -0
- package/dist/audit/audit.service.js +61 -0
- package/dist/audit/audit.service.js.map +1 -0
- package/dist/bff/auth.controller.d.ts +41 -0
- package/dist/bff/auth.controller.js +269 -0
- package/dist/bff/auth.controller.js.map +1 -0
- package/dist/bff/cookies.d.ts +11 -0
- package/dist/bff/cookies.js +43 -0
- package/dist/bff/cookies.js.map +1 -0
- package/dist/bff/oidc-client.d.ts +73 -0
- package/dist/bff/oidc-client.js +158 -0
- package/dist/bff/oidc-client.js.map +1 -0
- package/dist/bff/session-refresher.d.ts +27 -0
- package/dist/bff/session-refresher.js +95 -0
- package/dist/bff/session-refresher.js.map +1 -0
- package/dist/bff/session.store.d.ts +88 -0
- package/dist/bff/session.store.js +167 -0
- package/dist/bff/session.store.js.map +1 -0
- package/dist/clients/clients.d.ts +37 -0
- package/dist/clients/clients.js +107 -0
- package/dist/clients/clients.js.map +1 -0
- package/dist/config/schema.d.ts +91 -0
- package/dist/config/schema.js +129 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/constants.d.ts +45 -0
- package/dist/constants.js +42 -0
- package/dist/constants.js.map +1 -0
- package/dist/cors/cors-origins.controller.d.ts +19 -0
- package/dist/cors/cors-origins.controller.js +122 -0
- package/dist/cors/cors-origins.controller.js.map +1 -0
- package/dist/cors/cors-origins.service.d.ts +73 -0
- package/dist/cors/cors-origins.service.js +287 -0
- package/dist/cors/cors-origins.service.js.map +1 -0
- package/dist/cors/customer-cors.middleware.d.ts +22 -0
- package/dist/cors/customer-cors.middleware.js +84 -0
- package/dist/cors/customer-cors.middleware.js.map +1 -0
- package/dist/customers/account.controller.d.ts +91 -0
- package/dist/customers/account.controller.js +443 -0
- package/dist/customers/account.controller.js.map +1 -0
- package/dist/customers/auth-hooks.d.ts +47 -0
- package/dist/customers/auth-hooks.js +79 -0
- package/dist/customers/auth-hooks.js.map +1 -0
- package/dist/customers/customer-auth.controller.d.ts +54 -0
- package/dist/customers/customer-auth.controller.js +287 -0
- package/dist/customers/customer-auth.controller.js.map +1 -0
- package/dist/customers/customer-sessions.revoker.d.ts +21 -0
- package/dist/customers/customer-sessions.revoker.js +47 -0
- package/dist/customers/customer-sessions.revoker.js.map +1 -0
- package/dist/customers/customer.service.d.ts +53 -0
- package/dist/customers/customer.service.js +229 -0
- package/dist/customers/customer.service.js.map +1 -0
- package/dist/customers/customers-admin.controller.d.ts +21 -0
- package/dist/customers/customers-admin.controller.js +134 -0
- package/dist/customers/customers-admin.controller.js.map +1 -0
- package/dist/customers/identifier.d.ts +6 -0
- package/dist/customers/identifier.js +27 -0
- package/dist/customers/identifier.js.map +1 -0
- package/dist/customers/sign-in-settings.controller.d.ts +27 -0
- package/dist/customers/sign-in-settings.controller.js +112 -0
- package/dist/customers/sign-in-settings.controller.js.map +1 -0
- package/dist/customers/sign-in-settings.service.d.ts +35 -0
- package/dist/customers/sign-in-settings.service.js +130 -0
- package/dist/customers/sign-in-settings.service.js.map +1 -0
- package/dist/db/migrations/index.d.ts +2 -0
- package/dist/db/migrations/index.js +14 -0
- package/dist/db/migrations/index.js.map +1 -0
- package/dist/db/migrations/oauth2_0001_init.d.ts +2 -0
- package/dist/db/migrations/oauth2_0001_init.js +127 -0
- package/dist/db/migrations/oauth2_0001_init.js.map +1 -0
- package/dist/db/migrations/oauth2_0002_user_locale.d.ts +3 -0
- package/dist/db/migrations/oauth2_0002_user_locale.js +13 -0
- package/dist/db/migrations/oauth2_0002_user_locale.js.map +1 -0
- package/dist/db/migrations/oauth2_0003_groups.d.ts +6 -0
- package/dist/db/migrations/oauth2_0003_groups.js +46 -0
- package/dist/db/migrations/oauth2_0003_groups.js.map +1 -0
- package/dist/db/migrations/oauth2_0004_customers.d.ts +3 -0
- package/dist/db/migrations/oauth2_0004_customers.js +108 -0
- package/dist/db/migrations/oauth2_0004_customers.js.map +1 -0
- package/dist/db/types.d.ts +176 -0
- package/dist/db/types.js +3 -0
- package/dist/db/types.js.map +1 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +115 -0
- package/dist/errors.js.map +1 -0
- package/dist/groups/groups.controller.d.ts +28 -0
- package/dist/groups/groups.controller.js +126 -0
- package/dist/groups/groups.controller.js.map +1 -0
- package/dist/groups/groups.service.d.ts +38 -0
- package/dist/groups/groups.service.js +134 -0
- package/dist/groups/groups.service.js.map +1 -0
- package/dist/guard/auth.guard.d.ts +29 -0
- package/dist/guard/auth.guard.js +119 -0
- package/dist/guard/auth.guard.js.map +1 -0
- package/dist/guard/current-user.decorator.d.ts +33 -0
- package/dist/guard/current-user.decorator.js +16 -0
- package/dist/guard/current-user.decorator.js.map +1 -0
- package/dist/guard/customer-auth.decorator.d.ts +26 -0
- package/dist/guard/customer-auth.decorator.js +16 -0
- package/dist/guard/customer-auth.decorator.js.map +1 -0
- package/dist/guard/customer-authenticator.d.ts +58 -0
- package/dist/guard/customer-authenticator.js +215 -0
- package/dist/guard/customer-authenticator.js.map +1 -0
- package/dist/guard/public.decorator.d.ts +2 -0
- package/dist/guard/public.decorator.js +8 -0
- package/dist/guard/public.decorator.js.map +1 -0
- package/dist/guard/session-auth.service.d.ts +28 -0
- package/dist/guard/session-auth.service.js +125 -0
- package/dist/guard/session-auth.service.js.map +1 -0
- package/dist/idp/identity-linker.d.ts +19 -0
- package/dist/idp/identity-linker.js +93 -0
- package/dist/idp/identity-linker.js.map +1 -0
- package/dist/idp/identity-provider.d.ts +54 -0
- package/dist/idp/identity-provider.js +36 -0
- package/dist/idp/identity-provider.js.map +1 -0
- package/dist/idp/idp.controller.d.ts +36 -0
- package/dist/idp/idp.controller.js +232 -0
- package/dist/idp/idp.controller.js.map +1 -0
- package/dist/idp/oidc.identity-provider.d.ts +42 -0
- package/dist/idp/oidc.identity-provider.js +96 -0
- package/dist/idp/oidc.identity-provider.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +98 -0
- package/dist/index.js.map +1 -0
- package/dist/interaction/customer-interaction.controller.d.ts +64 -0
- package/dist/interaction/customer-interaction.controller.js +336 -0
- package/dist/interaction/customer-interaction.controller.js.map +1 -0
- package/dist/interaction/interaction.controller.d.ts +36 -0
- package/dist/interaction/interaction.controller.js +175 -0
- package/dist/interaction/interaction.controller.js.map +1 -0
- package/dist/interaction/interaction.service.d.ts +40 -0
- package/dist/interaction/interaction.service.js +102 -0
- package/dist/interaction/interaction.service.js.map +1 -0
- package/dist/maintenance/cleanup.service.d.ts +22 -0
- package/dist/maintenance/cleanup.service.js +77 -0
- package/dist/maintenance/cleanup.service.js.map +1 -0
- package/dist/oauth2.module.d.ts +37 -0
- package/dist/oauth2.module.js +214 -0
- package/dist/oauth2.module.js.map +1 -0
- package/dist/otp/otp-guard.d.ts +7 -0
- package/dist/otp/otp-guard.js +27 -0
- package/dist/otp/otp-guard.js.map +1 -0
- package/dist/otp/otp.service.d.ts +57 -0
- package/dist/otp/otp.service.js +202 -0
- package/dist/otp/otp.service.js.map +1 -0
- package/dist/otp/otp.types.d.ts +36 -0
- package/dist/otp/otp.types.js +34 -0
- package/dist/otp/otp.types.js.map +1 -0
- package/dist/otp/storefront-boot-check.d.ts +13 -0
- package/dist/otp/storefront-boot-check.js +53 -0
- package/dist/otp/storefront-boot-check.js.map +1 -0
- package/dist/permissions.d.ts +12 -0
- package/dist/permissions.js +24 -0
- package/dist/permissions.js.map +1 -0
- package/dist/provider/kysely.adapter.d.ts +27 -0
- package/dist/provider/kysely.adapter.js +79 -0
- package/dist/provider/kysely.adapter.js.map +1 -0
- package/dist/provider/provider.factory.d.ts +101 -0
- package/dist/provider/provider.factory.js +377 -0
- package/dist/provider/provider.factory.js.map +1 -0
- package/dist/provider/provider.service.d.ts +60 -0
- package/dist/provider/provider.service.js +200 -0
- package/dist/provider/provider.service.js.map +1 -0
- package/dist/provider/signing-keys.d.ts +19 -0
- package/dist/provider/signing-keys.js +41 -0
- package/dist/provider/signing-keys.js.map +1 -0
- package/dist/register-migrations.d.ts +1 -0
- package/dist/register-migrations.js +7 -0
- package/dist/register-migrations.js.map +1 -0
- package/dist/security/crypto.d.ts +6 -0
- package/dist/security/crypto.js +29 -0
- package/dist/security/crypto.js.map +1 -0
- package/dist/security/email.d.ts +1 -0
- package/dist/security/email.js +7 -0
- package/dist/security/email.js.map +1 -0
- package/dist/security/origin.d.ts +1 -0
- package/dist/security/origin.js +15 -0
- package/dist/security/origin.js.map +1 -0
- package/dist/security/password.d.ts +12 -0
- package/dist/security/password.js +92 -0
- package/dist/security/password.js.map +1 -0
- package/dist/security/rate-limiter.d.ts +25 -0
- package/dist/security/rate-limiter.js +52 -0
- package/dist/security/rate-limiter.js.map +1 -0
- package/dist/security/return-to.d.ts +6 -0
- package/dist/security/return-to.js +33 -0
- package/dist/security/return-to.js.map +1 -0
- package/dist/security/validate.d.ts +3 -0
- package/dist/security/validate.js +14 -0
- package/dist/security/validate.js.map +1 -0
- package/dist/settings/settings.provider.d.ts +11 -0
- package/dist/settings/settings.provider.js +20 -0
- package/dist/settings/settings.provider.js.map +1 -0
- package/dist/setup/bootstrap.service.d.ts +24 -0
- package/dist/setup/bootstrap.service.js +117 -0
- package/dist/setup/bootstrap.service.js.map +1 -0
- package/dist/setup/setup-state.d.ts +8 -0
- package/dist/setup/setup-state.js +42 -0
- package/dist/setup/setup-state.js.map +1 -0
- package/dist/setup/setup.controller.d.ts +23 -0
- package/dist/setup/setup.controller.js +96 -0
- package/dist/setup/setup.controller.js.map +1 -0
- package/dist/users/password.controller.d.ts +19 -0
- package/dist/users/password.controller.js +87 -0
- package/dist/users/password.controller.js.map +1 -0
- package/dist/users/user.service.d.ts +114 -0
- package/dist/users/user.service.js +336 -0
- package/dist/users/user.service.js.map +1 -0
- package/dist/users/users.controller.d.ts +43 -0
- package/dist/users/users.controller.js +188 -0
- package/dist/users/users.controller.js.map +1 -0
- package/package.json +75 -3
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.OtpService = exports.OtpRejectedError = void 0;
|
|
16
|
+
const node_crypto_1 = require("node:crypto");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const audit_service_1 = require("../audit/audit.service");
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const errors_1 = require("../errors");
|
|
21
|
+
const crypto_1 = require("../security/crypto");
|
|
22
|
+
const otp_types_1 = require("./otp.types");
|
|
23
|
+
const HOUR = 3600;
|
|
24
|
+
/**
|
|
25
|
+
* A refused code. `code` is what the caller answers with; `reason` is the real cause (for audit only). With
|
|
26
|
+
* `uniformErrors` every refusal is `invalid_code`, so "no code was ever issued" (unknown / disabled / owned-by-another
|
|
27
|
+
* identifiers) cannot be told apart from "the code expired" or "attempts are used up".
|
|
28
|
+
*/
|
|
29
|
+
class OtpRejectedError extends errors_1.AuthError {
|
|
30
|
+
reason;
|
|
31
|
+
constructor(code, reason) {
|
|
32
|
+
super(code, 401);
|
|
33
|
+
this.reason = reason;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.OtpRejectedError = OtpRejectedError;
|
|
37
|
+
let OtpService = class OtpService {
|
|
38
|
+
db;
|
|
39
|
+
cfg;
|
|
40
|
+
limiter;
|
|
41
|
+
audit;
|
|
42
|
+
logger = new common_1.Logger('OtpService');
|
|
43
|
+
key;
|
|
44
|
+
generator;
|
|
45
|
+
senders;
|
|
46
|
+
constructor(db, cfg, limiter, audit, opts) {
|
|
47
|
+
this.db = db;
|
|
48
|
+
this.cfg = cfg;
|
|
49
|
+
this.limiter = limiter;
|
|
50
|
+
this.audit = audit;
|
|
51
|
+
this.key = (0, crypto_1.deriveKey)(cfg.secret, 'otp');
|
|
52
|
+
this.generator = opts.generator ?? new otp_types_1.RandomOtpGenerator();
|
|
53
|
+
this.senders = opts.senders ?? {};
|
|
54
|
+
}
|
|
55
|
+
hash(purpose, kind, value, code) {
|
|
56
|
+
return (0, node_crypto_1.createHmac)('sha256', this.key).update(`${purpose}\n${kind}\n${value}\n${code}`).digest('hex');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Rate limits apply whether or not `deliver` is set, so the answer (202 or 429) does not depend on whether the
|
|
60
|
+
* identifier has an account. With `deliver: false` nothing is stored or sent.
|
|
61
|
+
*/
|
|
62
|
+
async request(i) {
|
|
63
|
+
const o = this.cfg.storefront.otp;
|
|
64
|
+
const idKey = `${i.kind}:${i.value}`;
|
|
65
|
+
const cool = this.limiter.hit(`otp:cool:${i.purpose}:${idKey}`, 1, o.resendSec);
|
|
66
|
+
if (!cool.ok)
|
|
67
|
+
throw new errors_1.AuthError('rate_limited', 429, undefined, { retryAfterSec: cool.retryAfterSec });
|
|
68
|
+
const perId = this.limiter.hit(`otp:id:${idKey}`, o.perIdentifierPerHour, HOUR);
|
|
69
|
+
if (!perId.ok)
|
|
70
|
+
throw new errors_1.AuthError('rate_limited', 429, undefined, { retryAfterSec: perId.retryAfterSec });
|
|
71
|
+
if (i.ip) {
|
|
72
|
+
const perIp = this.limiter.hit(`otp:ip:${i.ip}`, o.perIpPerHour, HOUR);
|
|
73
|
+
if (!perIp.ok)
|
|
74
|
+
throw new errors_1.AuthError('rate_limited', 429, undefined, { retryAfterSec: perIp.retryAfterSec });
|
|
75
|
+
}
|
|
76
|
+
const result = { resendAfterSec: o.resendSec };
|
|
77
|
+
if (!i.deliver)
|
|
78
|
+
return result;
|
|
79
|
+
// Same synchronous path as deliver:false up to here; storing and sending happen in the background so the
|
|
80
|
+
// response time does not depend on whether the identifier has an account.
|
|
81
|
+
setImmediate(() => {
|
|
82
|
+
void this.storeAndSend(i.kind, i.value, i.purpose, i.ip);
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
async storeAndSend(kind, value, purpose, ip) {
|
|
87
|
+
try {
|
|
88
|
+
const o = this.cfg.storefront.otp;
|
|
89
|
+
const code = this.generator.generate();
|
|
90
|
+
const now = new Date();
|
|
91
|
+
await this.db.transaction().execute(async (trx) => {
|
|
92
|
+
await trx
|
|
93
|
+
.updateTable('auth_customer_otps')
|
|
94
|
+
.set({ consumed_at: now })
|
|
95
|
+
.where('kind', '=', kind)
|
|
96
|
+
.where('value', '=', value)
|
|
97
|
+
.where('purpose', '=', purpose)
|
|
98
|
+
.where('consumed_at', 'is', null)
|
|
99
|
+
.execute();
|
|
100
|
+
await trx
|
|
101
|
+
.insertInto('auth_customer_otps')
|
|
102
|
+
.values({
|
|
103
|
+
purpose, kind, value,
|
|
104
|
+
code_hash: this.hash(purpose, kind, value, code),
|
|
105
|
+
expires_at: new Date(now.getTime() + o.ttlSec * 1000),
|
|
106
|
+
created_at: now,
|
|
107
|
+
ip: ip ?? null,
|
|
108
|
+
})
|
|
109
|
+
.execute();
|
|
110
|
+
});
|
|
111
|
+
const channel = kind === 'phone' ? 'sms' : 'email';
|
|
112
|
+
const sender = this.senders[channel];
|
|
113
|
+
if (!sender)
|
|
114
|
+
throw new Error('no sender');
|
|
115
|
+
await sender.send({ channel, to: value, code, purpose });
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
// Class name only: the message may echo the code or the recipient.
|
|
119
|
+
this.logger.error(`OTP delivery failed (${purpose}, ${kind}): ${e?.name ?? 'Error'}`);
|
|
120
|
+
try {
|
|
121
|
+
await this.audit.record({ type: 'customer.otp_failed', ip, meta: { purpose, kind, reason: 'delivery' } });
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
this.logger.error('Could not audit OTP failure');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Consumes the live code when it matches. Refusals throw `OtpRejectedError` (401): `invalid_code` or
|
|
130
|
+
* `code_expired`, or always `invalid_code` with `opts.uniformErrors` (every customer-facing route uses it).
|
|
131
|
+
*/
|
|
132
|
+
async verify(i, opts = {}) {
|
|
133
|
+
const reject = (reason) => new OtpRejectedError(opts.uniformErrors || reason === 'no_code' || reason === 'wrong_code' ? 'invalid_code' : 'code_expired', reason);
|
|
134
|
+
const max = this.cfg.storefront.otp.maxAttempts;
|
|
135
|
+
const now = new Date();
|
|
136
|
+
const target = this.db
|
|
137
|
+
.selectFrom('auth_customer_otps')
|
|
138
|
+
.select('id')
|
|
139
|
+
.where('kind', '=', i.kind)
|
|
140
|
+
.where('value', '=', i.value)
|
|
141
|
+
.where('purpose', '=', i.purpose)
|
|
142
|
+
.where('consumed_at', 'is', null)
|
|
143
|
+
.orderBy('created_at', 'desc')
|
|
144
|
+
.limit(1);
|
|
145
|
+
// Reserve an attempt atomically before comparing, so parallel verifies cannot exceed maxAttempts.
|
|
146
|
+
const reserved = await this.db
|
|
147
|
+
.updateTable('auth_customer_otps')
|
|
148
|
+
.set((eb) => ({ attempts: eb('attempts', '+', 1) }))
|
|
149
|
+
.where('id', '=', target)
|
|
150
|
+
.where('consumed_at', 'is', null)
|
|
151
|
+
.where('attempts', '<', max)
|
|
152
|
+
.where('expires_at', '>', now)
|
|
153
|
+
.returning(['id', 'attempts', 'code_hash'])
|
|
154
|
+
.executeTakeFirst();
|
|
155
|
+
if (!reserved) {
|
|
156
|
+
// Distinguish "a live code exists but is expired / out of attempts" from "no live code".
|
|
157
|
+
const live = await this.db
|
|
158
|
+
.selectFrom('auth_customer_otps')
|
|
159
|
+
.select('id')
|
|
160
|
+
.where('kind', '=', i.kind)
|
|
161
|
+
.where('value', '=', i.value)
|
|
162
|
+
.where('purpose', '=', i.purpose)
|
|
163
|
+
.where('consumed_at', 'is', null)
|
|
164
|
+
.limit(1)
|
|
165
|
+
.select('expires_at')
|
|
166
|
+
.executeTakeFirst();
|
|
167
|
+
if (!live)
|
|
168
|
+
throw reject('no_code');
|
|
169
|
+
throw reject(new Date(live.expires_at) <= now ? 'expired' : 'exhausted');
|
|
170
|
+
}
|
|
171
|
+
const expected = Buffer.from(reserved.code_hash, 'hex');
|
|
172
|
+
const actual = Buffer.from(this.hash(i.purpose, i.kind, i.value, String(i.code ?? '')), 'hex');
|
|
173
|
+
const match = expected.length === actual.length && (0, node_crypto_1.timingSafeEqual)(expected, actual);
|
|
174
|
+
if (match) {
|
|
175
|
+
const consumed = await this.db
|
|
176
|
+
.updateTable('auth_customer_otps')
|
|
177
|
+
.set({ consumed_at: new Date() })
|
|
178
|
+
.where('id', '=', reserved.id)
|
|
179
|
+
.where('consumed_at', 'is', null)
|
|
180
|
+
.returning('id')
|
|
181
|
+
.executeTakeFirst();
|
|
182
|
+
if (consumed)
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
await this.audit.record({ type: 'customer.otp_failed', meta: { purpose: i.purpose, kind: i.kind, reason: 'wrong_code' } });
|
|
187
|
+
}
|
|
188
|
+
// A match that lost the consume race is an already-used code.
|
|
189
|
+
throw reject(!match && reserved.attempts >= max ? 'exhausted' : match ? 'no_code' : 'wrong_code');
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
exports.OtpService = OtpService;
|
|
193
|
+
exports.OtpService = OtpService = __decorate([
|
|
194
|
+
(0, common_1.Injectable)(),
|
|
195
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_DB)),
|
|
196
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
197
|
+
__param(2, (0, common_1.Inject)(constants_1.RATE_LIMITER)),
|
|
198
|
+
__param(3, (0, common_1.Inject)(audit_service_1.AuditService)),
|
|
199
|
+
__param(4, (0, common_1.Inject)(otp_types_1.OTP_OPTIONS)),
|
|
200
|
+
__metadata("design:paramtypes", [Function, Object, Object, audit_service_1.AuditService, Object])
|
|
201
|
+
], OtpService);
|
|
202
|
+
//# sourceMappingURL=otp.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp.service.js","sourceRoot":"","sources":["../../src/otp/otp.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,6CAA0D;AAC1D,2CAA4D;AAE5D,0DAAsD;AACtD,4CAAsE;AAItE,sCAAsC;AACtC,+CAA+C;AAE/C,2CAAoJ;AAEpJ,MAAM,IAAI,GAAG,IAAI,CAAC;AAKlB;;;;GAIG;AACH,MAAa,gBAAiB,SAAQ,kBAAS;IACe;IAA5D,YAAY,IAAqC,EAAW,MAAwB;QAClF,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QADyC,WAAM,GAAN,MAAM,CAAkB;IAEpF,CAAC;CACF;AAJD,4CAIC;AAGM,IAAM,UAAU,GAAhB,MAAM,UAAU;IAOiB;IACI;IACD;IACA;IATxB,MAAM,GAAG,IAAI,eAAM,CAAC,YAAY,CAAC,CAAC;IAClC,GAAG,CAAS;IACZ,SAAS,CAAe;IACxB,OAAO,CAAyC;IAEjE,YACsC,EAAsB,EAClB,GAAiB,EAClB,OAAoB,EACpB,KAAmB,EACrC,IAAgB;QAJD,OAAE,GAAF,EAAE,CAA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QAC9D,MAAM,MAAM,CAAC,CAAC,KAAK,IAAI,QAAQ,CAAC,QAAQ,IAAI,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;IACpG,CAAC;CACF,CAAA;AA1JY,gCAAU;qBAAV,UAAU;IADtB,IAAA,mBAAU,GAAE;IAQR,WAAA,IAAA,eAAM,EAAC,qBAAS,CAAC,CAAA;IACjB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,eAAM,EAAC,wBAAY,CAAC,CAAA;IACpB,WAAA,IAAA,eAAM,EAAC,4BAAY,CAAC,CAAA;IACpB,WAAA,IAAA,eAAM,EAAC,uBAAW,CAAC,CAAA;+DAD0B,4BAAY;GAVjD,UAAU,CA0JtB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type OtpPurpose = 'login' | 'reset' | 'verify';
|
|
2
|
+
export type OtpChannel = 'sms' | 'email';
|
|
3
|
+
export interface OtpGenerator {
|
|
4
|
+
generate(): string;
|
|
5
|
+
}
|
|
6
|
+
export interface OtpMessage {
|
|
7
|
+
channel: OtpChannel;
|
|
8
|
+
to: string;
|
|
9
|
+
code: string;
|
|
10
|
+
purpose: OtpPurpose;
|
|
11
|
+
locale?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface OtpSender {
|
|
14
|
+
send(m: OtpMessage): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export interface OtpOptions {
|
|
17
|
+
generator?: OtpGenerator;
|
|
18
|
+
senders?: Partial<Record<OtpChannel, OtpSender>>;
|
|
19
|
+
}
|
|
20
|
+
/** DI token for the resolved `OtpOptions` (defaults filled in by the module). */
|
|
21
|
+
export declare const OTP_OPTIONS: unique symbol;
|
|
22
|
+
/** 6 random digits (zero-padded). */
|
|
23
|
+
export declare class RandomOtpGenerator implements OtpGenerator {
|
|
24
|
+
generate(): string;
|
|
25
|
+
}
|
|
26
|
+
/** Always the same code. Development / tests only; refused in production by `assertOtpProductionReady`. */
|
|
27
|
+
export declare class FixedOtpGenerator implements OtpGenerator {
|
|
28
|
+
private readonly code;
|
|
29
|
+
constructor(code: string);
|
|
30
|
+
generate(): string;
|
|
31
|
+
}
|
|
32
|
+
/** Logs the recipient and the code. Development only; refused in production for enabled kinds. */
|
|
33
|
+
export declare class ConsoleOtpSender implements OtpSender {
|
|
34
|
+
private readonly logger;
|
|
35
|
+
send(m: OtpMessage): Promise<void>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConsoleOtpSender = exports.FixedOtpGenerator = exports.RandomOtpGenerator = exports.OTP_OPTIONS = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
/** DI token for the resolved `OtpOptions` (defaults filled in by the module). */
|
|
7
|
+
exports.OTP_OPTIONS = Symbol('OTP_OPTIONS');
|
|
8
|
+
/** 6 random digits (zero-padded). */
|
|
9
|
+
class RandomOtpGenerator {
|
|
10
|
+
generate() {
|
|
11
|
+
return String((0, node_crypto_1.randomInt)(0, 1_000_000)).padStart(6, '0');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.RandomOtpGenerator = RandomOtpGenerator;
|
|
15
|
+
/** Always the same code. Development / tests only; refused in production by `assertOtpProductionReady`. */
|
|
16
|
+
class FixedOtpGenerator {
|
|
17
|
+
code;
|
|
18
|
+
constructor(code) {
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
generate() {
|
|
22
|
+
return this.code;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.FixedOtpGenerator = FixedOtpGenerator;
|
|
26
|
+
/** Logs the recipient and the code. Development only; refused in production for enabled kinds. */
|
|
27
|
+
class ConsoleOtpSender {
|
|
28
|
+
logger = new common_1.Logger('OtpSender');
|
|
29
|
+
async send(m) {
|
|
30
|
+
this.logger.log(`OTP (${m.purpose}) via ${m.channel} to ${m.to}: ${m.code}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.ConsoleOtpSender = ConsoleOtpSender;
|
|
34
|
+
//# sourceMappingURL=otp.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp.types.js","sourceRoot":"","sources":["../../src/otp/otp.types.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,2CAAwC;AA0BxC,iFAAiF;AACpE,QAAA,WAAW,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAEjD,qCAAqC;AACrC,MAAa,kBAAkB;IAC7B,QAAQ;QACN,OAAO,MAAM,CAAC,IAAA,uBAAS,EAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAJD,gDAIC;AAED,2GAA2G;AAC3G,MAAa,iBAAiB;IACC;IAA7B,YAA6B,IAAY;QAAZ,SAAI,GAAJ,IAAI,CAAQ;IAAG,CAAC;IAC7C,QAAQ;QACN,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;CACF;AALD,8CAKC;AAED,kGAAkG;AAClG,MAAa,gBAAgB;IACV,MAAM,GAAG,IAAI,eAAM,CAAC,WAAW,CAAC,CAAC;IAClD,KAAK,CAAC,IAAI,CAAC,CAAa;QACtB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/E,CAAC;CACF;AALD,4CAKC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import type { OAuth2Config } from '../config/schema';
|
|
3
|
+
import { SignInSettingsService } from '../customers/sign-in-settings.service';
|
|
4
|
+
import { type OtpOptions } from './otp.types';
|
|
5
|
+
/** Refuses to boot a production app whose enabled customer sign-in kinds cannot deliver OTP codes safely. */
|
|
6
|
+
export declare class StorefrontBootCheck implements OnModuleInit {
|
|
7
|
+
private readonly env;
|
|
8
|
+
private readonly cfg;
|
|
9
|
+
private readonly otp;
|
|
10
|
+
private readonly settings;
|
|
11
|
+
constructor(env: NodeJS.ProcessEnv, cfg: OAuth2Config, otp: OtpOptions, settings: SignInSettingsService);
|
|
12
|
+
onModuleInit(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.StorefrontBootCheck = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
const sign_in_settings_service_1 = require("../customers/sign-in-settings.service");
|
|
19
|
+
const otp_guard_1 = require("./otp-guard");
|
|
20
|
+
const otp_types_1 = require("./otp.types");
|
|
21
|
+
/** Refuses to boot a production app whose enabled customer sign-in kinds cannot deliver OTP codes safely. */
|
|
22
|
+
let StorefrontBootCheck = class StorefrontBootCheck {
|
|
23
|
+
env;
|
|
24
|
+
cfg;
|
|
25
|
+
otp;
|
|
26
|
+
settings;
|
|
27
|
+
constructor(env, cfg, otp, settings) {
|
|
28
|
+
this.env = env;
|
|
29
|
+
this.cfg = cfg;
|
|
30
|
+
this.otp = otp;
|
|
31
|
+
this.settings = settings;
|
|
32
|
+
}
|
|
33
|
+
async onModuleInit() {
|
|
34
|
+
if (this.env.NODE_ENV !== 'production' || !this.cfg.storefront.enabled)
|
|
35
|
+
return;
|
|
36
|
+
// Any read failure (e.g. the table is not migrated yet) falls back to the defaults: every kind on is the strictest check.
|
|
37
|
+
// Runs before core has migrated a fresh database, so the settings table may not exist yet: then the defaults
|
|
38
|
+
// (every kind on) apply, which is also the strictest check.
|
|
39
|
+
const methods = await this.settings.get().catch(() => (0, sign_in_settings_service_1.defaultSignInMethods)());
|
|
40
|
+
const kinds = Object.keys(methods).filter((k) => methods[k].enabled);
|
|
41
|
+
(0, otp_guard_1.assertOtpProductionReady)(this.env, this.cfg.storefront.enabled, this.otp, kinds);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.StorefrontBootCheck = StorefrontBootCheck;
|
|
45
|
+
exports.StorefrontBootCheck = StorefrontBootCheck = __decorate([
|
|
46
|
+
(0, common_1.Injectable)(),
|
|
47
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_ENV)),
|
|
48
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
49
|
+
__param(2, (0, common_1.Inject)(otp_types_1.OTP_OPTIONS)),
|
|
50
|
+
__param(3, (0, common_1.Inject)(sign_in_settings_service_1.SignInSettingsService)),
|
|
51
|
+
__metadata("design:paramtypes", [Object, Object, Object, sign_in_settings_service_1.SignInSettingsService])
|
|
52
|
+
], StorefrontBootCheck);
|
|
53
|
+
//# sourceMappingURL=storefront-boot-check.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storefront-boot-check.js","sourceRoot":"","sources":["../../src/otp/storefront-boot-check.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAuE;AACvE,4CAAyD;AAEzD,oFAAoG;AAEpG,2CAAuD;AACvD,2CAA2D;AAE3D,6GAA6G;AAEtG,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAES;IACG;IACF;IACU;IAJlD,YACuC,GAAsB,EACnB,GAAiB,EACnB,GAAe,EACL,QAA+B;QAH1C,QAAG,GAAH,GAAG,CAAmB;QACnB,QAAG,GAAH,GAAG,CAAc;QACnB,QAAG,GAAH,GAAG,CAAY;QACL,aAAQ,GAAR,QAAQ,CAAuB;IAC9E,CAAC;IAEJ,KAAK,CAAC,YAAY;QAChB,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO;YAAE,OAAO;QAC/E,0HAA0H;QAC1H,6GAA6G;QAC7G,4DAA4D;QAC5D,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAA,+CAAoB,GAAE,CAAC,CAAC;QAC9E,MAAM,KAAK,GAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QAC3F,IAAA,oCAAwB,EAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACnF,CAAC;CACF,CAAA;AAjBY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,sBAAU,CAAC,CAAA;IAClB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,eAAM,EAAC,uBAAW,CAAC,CAAA;IACnB,WAAA,IAAA,eAAM,EAAC,gDAAqB,CAAC,CAAA;6DAA4B,gDAAqB;GALtE,mBAAmB,CAiB/B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PERMISSIONS, isPermission, type Permission } from '@opencloudcms/schema';
|
|
2
|
+
export { PERMISSIONS, isPermission, type Permission };
|
|
3
|
+
/** Key of the built-in system group (spec B §3.2). */
|
|
4
|
+
export declare const SUPER_ADMIN_GROUP = "super-admin";
|
|
5
|
+
/**
|
|
6
|
+
* Effective permissions of a user from their groups (spec B §3.3): any `system` group → every permission;
|
|
7
|
+
* otherwise the sorted, de-duplicated union of the groups' roles, unknown entries dropped.
|
|
8
|
+
*/
|
|
9
|
+
export declare function effectivePermissions(groups: ReadonlyArray<{
|
|
10
|
+
system: boolean;
|
|
11
|
+
roles: readonly string[];
|
|
12
|
+
}>): Permission[];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SUPER_ADMIN_GROUP = exports.isPermission = exports.PERMISSIONS = void 0;
|
|
4
|
+
exports.effectivePermissions = effectivePermissions;
|
|
5
|
+
const schema_1 = require("@opencloudcms/schema");
|
|
6
|
+
Object.defineProperty(exports, "PERMISSIONS", { enumerable: true, get: function () { return schema_1.PERMISSIONS; } });
|
|
7
|
+
Object.defineProperty(exports, "isPermission", { enumerable: true, get: function () { return schema_1.isPermission; } });
|
|
8
|
+
/** Key of the built-in system group (spec B §3.2). */
|
|
9
|
+
exports.SUPER_ADMIN_GROUP = 'super-admin';
|
|
10
|
+
/**
|
|
11
|
+
* Effective permissions of a user from their groups (spec B §3.3): any `system` group → every permission;
|
|
12
|
+
* otherwise the sorted, de-duplicated union of the groups' roles, unknown entries dropped.
|
|
13
|
+
*/
|
|
14
|
+
function effectivePermissions(groups) {
|
|
15
|
+
if (groups.some((g) => g.system))
|
|
16
|
+
return [...schema_1.PERMISSIONS].sort();
|
|
17
|
+
const out = new Set();
|
|
18
|
+
for (const g of groups)
|
|
19
|
+
for (const r of g.roles)
|
|
20
|
+
if ((0, schema_1.isPermission)(r))
|
|
21
|
+
out.add(r);
|
|
22
|
+
return [...out].sort();
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=permissions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":";;;AAWA,oDAKC;AAhBD,iDAAkF;AAEzE,4FAFA,oBAAW,OAEA;AAAE,6FAFA,qBAAY,OAEA;AAElC,sDAAsD;AACzC,QAAA,iBAAiB,GAAG,aAAa,CAAC;AAE/C;;;GAGG;AACH,SAAgB,oBAAoB,CAAC,MAAoE;IACvG,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;QAAE,OAAO,CAAC,GAAG,oBAAW,CAAC,CAAC,IAAI,EAAE,CAAC;IACjE,MAAM,GAAG,GAAG,IAAI,GAAG,EAAc,CAAC;IAClC,KAAK,MAAM,CAAC,IAAI,MAAM;QAAE,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK;YAAE,IAAI,IAAA,qBAAY,EAAC,CAAC,CAAC;gBAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACjF,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import type { AuthTables } from '../db/types';
|
|
3
|
+
/**
|
|
4
|
+
* Local copy of the oidc-provider v9 adapter contract (the package ships no type declarations
|
|
5
|
+
* and is ESM-only, so we avoid any import from it).
|
|
6
|
+
*/
|
|
7
|
+
export interface AdapterPayload {
|
|
8
|
+
grantId?: string;
|
|
9
|
+
uid?: string;
|
|
10
|
+
userCode?: string;
|
|
11
|
+
consumed?: number;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface Adapter {
|
|
15
|
+
upsert(id: string, payload: AdapterPayload, expiresIn?: number): Promise<void>;
|
|
16
|
+
find(id: string): Promise<AdapterPayload | undefined>;
|
|
17
|
+
findByUserCode(userCode: string): Promise<AdapterPayload | undefined>;
|
|
18
|
+
findByUid(uid: string): Promise<AdapterPayload | undefined>;
|
|
19
|
+
consume(id: string): Promise<void>;
|
|
20
|
+
destroy(id: string): Promise<void>;
|
|
21
|
+
revokeByGrantId(grantId: string): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export type AdapterConstructor = new (name: string) => Adapter;
|
|
24
|
+
export interface KyselyAdapterOptions {
|
|
25
|
+
now?: () => Date;
|
|
26
|
+
}
|
|
27
|
+
export declare function createKyselyAdapter(db: Kysely<AuthTables>, opts?: KyselyAdapterOptions): AdapterConstructor;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createKyselyAdapter = createKyselyAdapter;
|
|
4
|
+
function createKyselyAdapter(db, opts = {}) {
|
|
5
|
+
const now = opts.now ?? (() => new Date());
|
|
6
|
+
return class KyselyAdapter {
|
|
7
|
+
name;
|
|
8
|
+
constructor(name) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
}
|
|
11
|
+
async upsert(id, payload, expiresIn) {
|
|
12
|
+
const expiresAt = expiresIn ? new Date(now().getTime() + expiresIn * 1000) : null;
|
|
13
|
+
const row = {
|
|
14
|
+
payload: JSON.stringify(payload),
|
|
15
|
+
grant_id: payload.grantId ?? null,
|
|
16
|
+
uid: payload.uid ?? null,
|
|
17
|
+
user_code: payload.userCode ?? null,
|
|
18
|
+
expires_at: expiresAt,
|
|
19
|
+
};
|
|
20
|
+
await db
|
|
21
|
+
.insertInto('auth_oidc_payloads')
|
|
22
|
+
.values({ kind: this.name, id, ...row })
|
|
23
|
+
.onConflict((oc) => oc.columns(['kind', 'id']).doUpdateSet(row))
|
|
24
|
+
.execute();
|
|
25
|
+
}
|
|
26
|
+
async find(id) {
|
|
27
|
+
const row = await db
|
|
28
|
+
.selectFrom('auth_oidc_payloads')
|
|
29
|
+
.select(['payload', 'expires_at', 'consumed_at'])
|
|
30
|
+
.where('kind', '=', this.name)
|
|
31
|
+
.where('id', '=', id)
|
|
32
|
+
.executeTakeFirst();
|
|
33
|
+
return this.toPayload(row);
|
|
34
|
+
}
|
|
35
|
+
async findByUid(uid) {
|
|
36
|
+
const row = await db
|
|
37
|
+
.selectFrom('auth_oidc_payloads')
|
|
38
|
+
.select(['payload', 'expires_at', 'consumed_at'])
|
|
39
|
+
.where('kind', '=', this.name)
|
|
40
|
+
.where('uid', '=', uid)
|
|
41
|
+
.executeTakeFirst();
|
|
42
|
+
return this.toPayload(row);
|
|
43
|
+
}
|
|
44
|
+
async findByUserCode(userCode) {
|
|
45
|
+
const row = await db
|
|
46
|
+
.selectFrom('auth_oidc_payloads')
|
|
47
|
+
.select(['payload', 'expires_at', 'consumed_at'])
|
|
48
|
+
.where('kind', '=', this.name)
|
|
49
|
+
.where('user_code', '=', userCode)
|
|
50
|
+
.executeTakeFirst();
|
|
51
|
+
return this.toPayload(row);
|
|
52
|
+
}
|
|
53
|
+
async consume(id) {
|
|
54
|
+
await db
|
|
55
|
+
.updateTable('auth_oidc_payloads')
|
|
56
|
+
.set({ consumed_at: now() })
|
|
57
|
+
.where('kind', '=', this.name)
|
|
58
|
+
.where('id', '=', id)
|
|
59
|
+
.execute();
|
|
60
|
+
}
|
|
61
|
+
async destroy(id) {
|
|
62
|
+
await db.deleteFrom('auth_oidc_payloads').where('kind', '=', this.name).where('id', '=', id).execute();
|
|
63
|
+
}
|
|
64
|
+
async revokeByGrantId(grantId) {
|
|
65
|
+
await db.deleteFrom('auth_oidc_payloads').where('grant_id', '=', grantId).execute();
|
|
66
|
+
}
|
|
67
|
+
toPayload(row) {
|
|
68
|
+
if (!row)
|
|
69
|
+
return undefined;
|
|
70
|
+
if (row.expires_at && row.expires_at.getTime() <= now().getTime())
|
|
71
|
+
return undefined;
|
|
72
|
+
const payload = (typeof row.payload === 'string' ? JSON.parse(row.payload) : row.payload);
|
|
73
|
+
if (row.consumed_at)
|
|
74
|
+
return { ...payload, consumed: Math.floor(row.consumed_at.getTime() / 1000) };
|
|
75
|
+
return payload;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=kysely.adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kysely.adapter.js","sourceRoot":"","sources":["../../src/provider/kysely.adapter.ts"],"names":[],"mappings":";;AAiCA,kDA6EC;AA7ED,SAAgB,mBAAmB,CAAC,EAAsB,EAAE,OAA6B,EAAE;IACzF,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;IAE3C,OAAO,MAAM,aAAa;QACK;QAA7B,YAA6B,IAAY;YAAZ,SAAI,GAAJ,IAAI,CAAQ;QAAG,CAAC;QAE7C,KAAK,CAAC,MAAM,CAAC,EAAU,EAAE,OAAuB,EAAE,SAAkB;YAClE,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,SAAS,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAClF,MAAM,GAAG,GAAG;gBACV,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;gBAChC,QA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,OAAO,SAAS,CAAC;YAC3B,IAAI,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,GAAG,EAAE,CAAC,OAAO,EAAE;gBAAE,OAAO,SAAS,CAAC;YACpF,MAAM,OAAO,GAAG,CAAC,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAmB,CAAC;YAC5G,IAAI,GAAG,CAAC,WAAW;gBAAE,OAAO,EAAE,GAAG,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;YACnG,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
3
|
+
import { ADMIN_SCOPES, type ClientSpec } from '../clients/clients';
|
|
4
|
+
import type { OAuth2Config, StorefrontTtls, Ttls } from '../config/schema';
|
|
5
|
+
import type { Customer } from '../customers/customer.service';
|
|
6
|
+
import type { AuthTables } from '../db/types';
|
|
7
|
+
import type { AuthUser } from '../users/user.service';
|
|
8
|
+
import type { AdapterConstructor, AdapterPayload } from './kysely.adapter';
|
|
9
|
+
import type { JWK } from './signing-keys';
|
|
10
|
+
/** The part of oidc-provider's `Provider` (a Koa app) this package relies on. */
|
|
11
|
+
export interface OidcProvider {
|
|
12
|
+
issuer: string;
|
|
13
|
+
proxy: boolean;
|
|
14
|
+
callback(): (req: unknown, res: unknown) => unknown;
|
|
15
|
+
use(fn: (ctx: any, next: () => Promise<void>) => Promise<void>): unknown;
|
|
16
|
+
interactionDetails(req: unknown, res: unknown): Promise<any>;
|
|
17
|
+
interactionFinished(req: unknown, res: unknown, result: unknown, opts?: unknown): Promise<void>;
|
|
18
|
+
interactionResult(req: unknown, res: unknown, result: unknown, opts?: unknown): Promise<string>;
|
|
19
|
+
Grant: any;
|
|
20
|
+
Session: any;
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
}
|
|
23
|
+
export type OidcProviderClass = new (issuer: string, configuration: Record<string, unknown>) => OidcProvider;
|
|
24
|
+
/**
|
|
25
|
+
* oidc-provider is ESM-only. Node 22 can require() it synchronously; if that is ever refused
|
|
26
|
+
* (e.g. the package gains top-level await) fall back to a native dynamic import. Cached once.
|
|
27
|
+
*/
|
|
28
|
+
export declare function loadOidcProvider(): Promise<OidcProviderClass>;
|
|
29
|
+
/** R16: the provider's keygrip key sits one HKDF level below the `cookie` key that seals oc_login. */
|
|
30
|
+
export declare function providerCookieKey(secret: Buffer): string;
|
|
31
|
+
export { ADMIN_SCOPES };
|
|
32
|
+
export declare function issuerFor(cfg: Pick<OAuth2Config, 'publicUrl'>): string;
|
|
33
|
+
export declare function adminClientSecret(cfg: Pick<OAuth2Config, 'secret'>): string;
|
|
34
|
+
export declare function adminClientMetadata(cfg: Pick<OAuth2Config, 'secret' | 'publicUrl'>): Record<string, unknown>;
|
|
35
|
+
/**
|
|
36
|
+
* Spec §6.1 step 3. Stores the metadata without `client_secret`; returns the full metadata (with secret)
|
|
37
|
+
* that is handed to the provider as a static client.
|
|
38
|
+
*/
|
|
39
|
+
export declare function upsertAdminClient(db: Kysely<AuthTables>, cfg: Pick<OAuth2Config, 'secret' | 'publicUrl'>): Promise<Record<string, unknown>>;
|
|
40
|
+
/**
|
|
41
|
+
* oidc-provider calls `ttl.*` synchronously, while AuthSettingsProvider.getTtls() is async (spec D7).
|
|
42
|
+
* The snapshot is loaded at boot and refreshed by a provider middleware at the start of every
|
|
43
|
+
* /oauth2 request, so every token/session issued in a request uses the settings read in that request.
|
|
44
|
+
*/
|
|
45
|
+
export declare class TtlSnapshot {
|
|
46
|
+
private readonly load;
|
|
47
|
+
current: Ttls;
|
|
48
|
+
constructor(load: () => Promise<Ttls>, current: Ttls);
|
|
49
|
+
static create(load: () => Promise<Ttls>): Promise<TtlSnapshot>;
|
|
50
|
+
refresh(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
export interface ProviderConfigDeps {
|
|
53
|
+
cfg: OAuth2Config;
|
|
54
|
+
adapter: AdapterConstructor;
|
|
55
|
+
jwks: JWK[];
|
|
56
|
+
/**
|
|
57
|
+
* Every client of the instance (buildClients). All but the web client are static provider clients; the
|
|
58
|
+
* web client is served through the adapter (auth_oauth_clients) so its redirect URIs change without restart.
|
|
59
|
+
*/
|
|
60
|
+
clients: ClientSpec[];
|
|
61
|
+
ttls: TtlSnapshot;
|
|
62
|
+
findUser(id: string): Promise<AuthUser | undefined>;
|
|
63
|
+
findCustomer(id: string): Promise<Customer | undefined>;
|
|
64
|
+
customerTtls(): StorefrontTtls;
|
|
65
|
+
/**
|
|
66
|
+
* Called once per successful `authorization_code` exchange at /oauth2/token (never for refresh), after the
|
|
67
|
+
* tokens were issued and before the answer is sent. Errors are logged by the caller's own code; a throw here
|
|
68
|
+
* is swallowed so the client still gets its tokens.
|
|
69
|
+
*/
|
|
70
|
+
onCodeExchanged?(e: CodeExchange): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
/** A successful authorization-code exchange (see ProviderConfigDeps.onCodeExchanged). */
|
|
73
|
+
export interface CodeExchange {
|
|
74
|
+
clientId: string;
|
|
75
|
+
accountId: string;
|
|
76
|
+
/** `amr` of the login result the code was issued for (Ruling R2: customer interactions set ['otp'] | ['pwd']). */
|
|
77
|
+
amr?: string[];
|
|
78
|
+
ip?: string;
|
|
79
|
+
userAgent?: string;
|
|
80
|
+
}
|
|
81
|
+
/** Static provider clients (with their derived secrets): every spec except the adapter-served web client. */
|
|
82
|
+
export declare function staticClients(cfg: Pick<OAuth2Config, 'secret' | 'publicUrl'>, clients: ClientSpec[]): Record<string, unknown>[];
|
|
83
|
+
export declare function buildProviderConfig(d: ProviderConfigDeps): Record<string, unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* For an http public URL the provider does not trust X-Forwarded-* (`proxy` is only on for https), so it builds
|
|
86
|
+
* issuer-relative URLs (discovery endpoints, redirects) from `Host`. The BFF reaches the provider on the
|
|
87
|
+
* loopback address (internalFetch) and Node's fetch cannot set `Host`, so its X-Forwarded-Host (the public
|
|
88
|
+
* host) is applied as `Host` here. Only for loopback peers, and only when it equals the public URL's host: any
|
|
89
|
+
* other value (a client header passed through by a same-host proxy) is ignored and the request keeps its Host.
|
|
90
|
+
*/
|
|
91
|
+
export declare function loopbackForwardedHost(publicUrl: string): (req: IncomingMessage, _res: unknown, next: () => void) => void;
|
|
92
|
+
/** Handlers mounted at /oauth2: the loopback host rewrite only for an http public URL (https uses `proxy`). */
|
|
93
|
+
export declare function providerMountHandlers<T>(publicUrl: string, callback: T): Array<T | ReturnType<typeof loopbackForwardedHost>>;
|
|
94
|
+
export declare function createProvider(d: ProviderConfigDeps): Promise<OidcProvider>;
|
|
95
|
+
/**
|
|
96
|
+
* The provider's adapter: `base` for every model except `Client`, which `findClient` serves (clients that are not
|
|
97
|
+
* static provider clients; nothing comes from auth_oidc_payloads, there is no dynamic registration).
|
|
98
|
+
* oidc-provider calls Client.find → adapter.find on every request for a non-static client and caches the built
|
|
99
|
+
* client only by a hash of the returned metadata, so a changed row applies at once, without restart.
|
|
100
|
+
*/
|
|
101
|
+
export declare function withClientFinder(base: AdapterConstructor, findClient: (id: string) => Promise<AdapterPayload | undefined>): AdapterConstructor;
|