@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,287 @@
|
|
|
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.CorsOriginsService = exports.CORS_CACHE_MS = void 0;
|
|
16
|
+
exports.normalizeOrigin = normalizeOrigin;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const kysely_1 = require("kysely");
|
|
19
|
+
const audit_service_1 = require("../audit/audit.service");
|
|
20
|
+
const constants_1 = require("../constants");
|
|
21
|
+
const errors_1 = require("../errors");
|
|
22
|
+
const provider_service_1 = require("../provider/provider.service");
|
|
23
|
+
/** Per-instance cache lifetime: a change made on another instance applies here within this time. */
|
|
24
|
+
exports.CORS_CACHE_MS = 30_000;
|
|
25
|
+
const LOCAL_HOSTS = new Set(['localhost', '127.0.0.1']);
|
|
26
|
+
/**
|
|
27
|
+
* `scheme://host[:port]` → its normalized form (lower-case, default port dropped, no trailing `/`). No path, query,
|
|
28
|
+
* fragment or user info, no wildcard and no trailing-dot host; `https` unless the host is `localhost` / `127.0.0.1`.
|
|
29
|
+
* Otherwise `422 invalid_origin`.
|
|
30
|
+
*/
|
|
31
|
+
function normalizeOrigin(raw) {
|
|
32
|
+
const fail = () => {
|
|
33
|
+
throw new errors_1.AuthError('invalid_origin', 422, 'Origin must be https://host[:port] (http only for localhost)');
|
|
34
|
+
};
|
|
35
|
+
let u;
|
|
36
|
+
try {
|
|
37
|
+
u = new URL(raw.trim());
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return fail();
|
|
41
|
+
}
|
|
42
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:')
|
|
43
|
+
fail();
|
|
44
|
+
if (u.protocol === 'http:' && !LOCAL_HOSTS.has(u.hostname))
|
|
45
|
+
fail();
|
|
46
|
+
if (u.username || u.password || u.search || u.hash || u.pathname !== '/')
|
|
47
|
+
fail();
|
|
48
|
+
if (u.hostname.includes('*') || u.hostname.endsWith('.'))
|
|
49
|
+
fail();
|
|
50
|
+
// `new URL` drops a bare "?" / "#"; refuse them too, so what is stored is exactly what was meant.
|
|
51
|
+
if (/[?#]/.test(raw))
|
|
52
|
+
fail();
|
|
53
|
+
return u.origin;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Every redirect URI must be an absolute URL on `origin`, without a fragment or user info; else
|
|
57
|
+
* `422 invalid_redirect_uri`. Returned normalized (`URL.href`: lower-case scheme and host) and de-duplicated.
|
|
58
|
+
*/
|
|
59
|
+
function checkRedirectUris(origin, uris) {
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const uri of uris) {
|
|
62
|
+
let u;
|
|
63
|
+
try {
|
|
64
|
+
u = new URL(uri);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
/* refused below */
|
|
68
|
+
}
|
|
69
|
+
if (!u || u.origin !== origin || u.hash || uri.includes('#') || u.username || u.password) {
|
|
70
|
+
throw new errors_1.AuthError('invalid_redirect_uri', 422, `Redirect URI must be on ${origin}: ${uri}`);
|
|
71
|
+
}
|
|
72
|
+
out.push(u.href);
|
|
73
|
+
}
|
|
74
|
+
return [...new Set(out)];
|
|
75
|
+
}
|
|
76
|
+
const view = (r) => ({
|
|
77
|
+
id: r.id,
|
|
78
|
+
origin: r.origin,
|
|
79
|
+
label: r.label,
|
|
80
|
+
allowContent: r.allow_content,
|
|
81
|
+
allowCustomer: r.allow_customer,
|
|
82
|
+
redirectUris: r.redirect_uris ?? [],
|
|
83
|
+
});
|
|
84
|
+
const isUniqueViolation = (e) => e?.code === '23505';
|
|
85
|
+
/**
|
|
86
|
+
* Admin-managed CORS origins (spec goal 14), stored in `auth_cors_origins`, plus the read-only origins of code config
|
|
87
|
+
* (`cors.origins`, content scope). `allows()` reads a per-instance cache that is re-read at most every 30 s and right
|
|
88
|
+
* after this instance's own writes; a failed re-read keeps the last good list (none yet: config origins only). Writes are serialized (advisory
|
|
89
|
+
* transaction lock): row, audit event and the `opencloudcms-storefront-web` client's redirect URIs (from the origins
|
|
90
|
+
* allowed with customer scope) change together.
|
|
91
|
+
*/
|
|
92
|
+
let CorsOriginsService = class CorsOriginsService {
|
|
93
|
+
db;
|
|
94
|
+
audit;
|
|
95
|
+
provider;
|
|
96
|
+
logger = new common_1.Logger('OAuth2');
|
|
97
|
+
configOrigins;
|
|
98
|
+
cache;
|
|
99
|
+
loading;
|
|
100
|
+
constructor(db, audit, provider, configOrigins) {
|
|
101
|
+
this.db = db;
|
|
102
|
+
this.audit = audit;
|
|
103
|
+
this.provider = provider;
|
|
104
|
+
this.configOrigins = [...new Set(configOrigins.map((o) => this.normalizeConfig(o)).filter((o) => !!o))];
|
|
105
|
+
}
|
|
106
|
+
/** Origins from code config, normalized (read-only in the admin). */
|
|
107
|
+
config() {
|
|
108
|
+
return [...this.configOrigins];
|
|
109
|
+
}
|
|
110
|
+
async list() {
|
|
111
|
+
const rows = await this.db.selectFrom('auth_cors_origins').selectAll().orderBy('origin').execute();
|
|
112
|
+
return rows.map(view);
|
|
113
|
+
}
|
|
114
|
+
async create(input, actorId) {
|
|
115
|
+
const origin = normalizeOrigin(input.origin);
|
|
116
|
+
const redirectUris = checkRedirectUris(origin, input.redirectUris ?? []);
|
|
117
|
+
return this.write(async (trx) => {
|
|
118
|
+
const row = await trx
|
|
119
|
+
.insertInto('auth_cors_origins')
|
|
120
|
+
.values({
|
|
121
|
+
origin,
|
|
122
|
+
label: input.label ?? '',
|
|
123
|
+
allow_content: input.allowContent ?? false,
|
|
124
|
+
allow_customer: input.allowCustomer ?? false,
|
|
125
|
+
redirect_uris: redirectUris,
|
|
126
|
+
created_at: new Date(),
|
|
127
|
+
updated_at: new Date(),
|
|
128
|
+
})
|
|
129
|
+
.returningAll()
|
|
130
|
+
.executeTakeFirstOrThrow();
|
|
131
|
+
const out = view(row);
|
|
132
|
+
return { out, audit: { type: 'cors.create', actorId, meta: { id: out.id, origin: out.origin, ...scopes(out) } } };
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async update(id, patch, actorId) {
|
|
136
|
+
const origin = patch.origin !== undefined ? normalizeOrigin(patch.origin) : undefined;
|
|
137
|
+
return this.write(async (trx) => {
|
|
138
|
+
// Read under the write lock: concurrent patches of one row apply one after the other, none is lost.
|
|
139
|
+
const curRow = await trx.selectFrom('auth_cors_origins').selectAll().where('id', '=', id).executeTakeFirst();
|
|
140
|
+
if (!curRow)
|
|
141
|
+
throw notFound();
|
|
142
|
+
const cur = view(curRow);
|
|
143
|
+
const nextOrigin = origin ?? cur.origin;
|
|
144
|
+
const redirectUris = checkRedirectUris(nextOrigin, patch.redirectUris ?? cur.redirectUris);
|
|
145
|
+
const row = await trx
|
|
146
|
+
.updateTable('auth_cors_origins')
|
|
147
|
+
.set({
|
|
148
|
+
origin: nextOrigin,
|
|
149
|
+
label: patch.label ?? cur.label,
|
|
150
|
+
allow_content: patch.allowContent ?? cur.allowContent,
|
|
151
|
+
allow_customer: patch.allowCustomer ?? cur.allowCustomer,
|
|
152
|
+
redirect_uris: redirectUris,
|
|
153
|
+
updated_at: new Date(),
|
|
154
|
+
})
|
|
155
|
+
.where('id', '=', id)
|
|
156
|
+
.returningAll()
|
|
157
|
+
.executeTakeFirstOrThrow();
|
|
158
|
+
const out = view(row);
|
|
159
|
+
return {
|
|
160
|
+
out,
|
|
161
|
+
audit: {
|
|
162
|
+
type: 'cors.update',
|
|
163
|
+
actorId,
|
|
164
|
+
meta: { id, origin: out.origin, ...(cur.origin !== out.origin ? { previousOrigin: cur.origin } : {}), ...scopes(out) },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async remove(id, actorId) {
|
|
170
|
+
await this.write(async (trx) => {
|
|
171
|
+
const row = await trx.deleteFrom('auth_cors_origins').where('id', '=', id).returning(['id', 'origin']).executeTakeFirst();
|
|
172
|
+
if (!row)
|
|
173
|
+
throw notFound();
|
|
174
|
+
return { out: undefined, audit: { type: 'cors.delete', actorId, meta: { id: row.id, origin: row.origin } } };
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Whether `origin` (an `Origin` header value) may call the given API: stored origins with that scope, and for
|
|
179
|
+
* `content` also the code-config origins. Stored origins come from the 30 s cache.
|
|
180
|
+
*/
|
|
181
|
+
async allows(origin, scope) {
|
|
182
|
+
if (!origin)
|
|
183
|
+
return false;
|
|
184
|
+
const wanted = origin.toLowerCase();
|
|
185
|
+
if (scope === 'content' && this.configOrigins.includes(wanted))
|
|
186
|
+
return true;
|
|
187
|
+
const rows = await this.cached();
|
|
188
|
+
return rows.some((r) => r.origin === wanted && (scope === 'content' ? r.allowContent : r.allowCustomer));
|
|
189
|
+
}
|
|
190
|
+
/** Redirect URIs of the origins allowed with customer scope: the web client's `redirect_uris`. */
|
|
191
|
+
async webRedirectUris(db = this.db) {
|
|
192
|
+
const rows = await db
|
|
193
|
+
.selectFrom('auth_cors_origins')
|
|
194
|
+
.select('redirect_uris')
|
|
195
|
+
.where('allow_customer', '=', true)
|
|
196
|
+
.orderBy('origin')
|
|
197
|
+
.execute();
|
|
198
|
+
return [...new Set(rows.flatMap((r) => r.redirect_uris ?? []))];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* One serialized write: advisory transaction lock, the change and its audit event, then the web client rebuilt from
|
|
202
|
+
* what is stored now, all committed together; then this instance's cache is re-read.
|
|
203
|
+
*/
|
|
204
|
+
async write(change) {
|
|
205
|
+
let out;
|
|
206
|
+
try {
|
|
207
|
+
out = await this.db.transaction().execute(async (trx) => {
|
|
208
|
+
await (0, kysely_1.sql) `select pg_advisory_xact_lock(hashtext('opencloudcms.auth_cors_origins'))`.execute(trx);
|
|
209
|
+
const r = await change(trx);
|
|
210
|
+
await this.audit.record(r.audit, trx);
|
|
211
|
+
await this.provider.syncWebClient(await this.webRedirectUris(trx), trx);
|
|
212
|
+
return r.out;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
if (isUniqueViolation(e))
|
|
217
|
+
throw taken();
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
// A load started before the write may hold old rows: wait for it, then read again.
|
|
221
|
+
await this.loading?.catch(() => { });
|
|
222
|
+
await this.reload();
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
async cached() {
|
|
226
|
+
if (this.cache && Date.now() - this.cache.at < exports.CORS_CACHE_MS)
|
|
227
|
+
return this.cache.rows;
|
|
228
|
+
return this.reload();
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Re-reads the stored origins; on failure keeps serving the last good list. When there is none yet (the DB is
|
|
232
|
+
* down at cold start) it serves no stored origins — content scope then allows the code-config origins only and
|
|
233
|
+
* customer scope allows none — and nothing is cached, so the next call reads again.
|
|
234
|
+
*/
|
|
235
|
+
reload() {
|
|
236
|
+
this.loading ??= this.list()
|
|
237
|
+
.then((rows) => {
|
|
238
|
+
this.cache = { at: Date.now(), rows };
|
|
239
|
+
return rows;
|
|
240
|
+
})
|
|
241
|
+
.catch((e) => {
|
|
242
|
+
const msg = e?.message ?? String(e);
|
|
243
|
+
if (!this.cache) {
|
|
244
|
+
this.logger.error(`Reading CORS origins failed; serving the config origins only until it succeeds: ${msg}`);
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
this.logger.error(`Reading CORS origins failed; keeping the last list: ${msg}`);
|
|
248
|
+
return this.cache.rows;
|
|
249
|
+
})
|
|
250
|
+
.finally(() => {
|
|
251
|
+
this.loading = undefined;
|
|
252
|
+
});
|
|
253
|
+
return this.loading;
|
|
254
|
+
}
|
|
255
|
+
/** Config entries keep working as before: the admin's rules where they parse, else the plain URL origin. */
|
|
256
|
+
normalizeConfig(raw) {
|
|
257
|
+
try {
|
|
258
|
+
return normalizeOrigin(raw);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
try {
|
|
262
|
+
const u = new URL(raw.trim());
|
|
263
|
+
if (u.protocol === 'https:' || u.protocol === 'http:')
|
|
264
|
+
return u.origin;
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
/* skipped below */
|
|
268
|
+
}
|
|
269
|
+
this.logger.warn(`Ignoring invalid cors.origins entry: ${raw}`);
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
exports.CorsOriginsService = CorsOriginsService;
|
|
275
|
+
exports.CorsOriginsService = CorsOriginsService = __decorate([
|
|
276
|
+
(0, common_1.Injectable)(),
|
|
277
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_DB)),
|
|
278
|
+
__param(1, (0, common_1.Inject)(audit_service_1.AuditService)),
|
|
279
|
+
__param(2, (0, common_1.Inject)(provider_service_1.ProviderService)),
|
|
280
|
+
__param(3, (0, common_1.Inject)(constants_1.CORS_CONFIG_ORIGINS)),
|
|
281
|
+
__metadata("design:paramtypes", [Function, audit_service_1.AuditService,
|
|
282
|
+
provider_service_1.ProviderService, Array])
|
|
283
|
+
], CorsOriginsService);
|
|
284
|
+
const scopes = (o) => ({ allowContent: o.allowContent, allowCustomer: o.allowCustomer, redirectUris: o.redirectUris });
|
|
285
|
+
const notFound = () => new errors_1.AuthError('not_found', 404, 'CORS origin not found');
|
|
286
|
+
const taken = () => new errors_1.AuthError('origin_taken', 409, 'Origin already exists');
|
|
287
|
+
//# sourceMappingURL=cors-origins.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-origins.service.js","sourceRoot":"","sources":["../../src/cors/cors-origins.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAuCA,0CAiBC;AAxDD,2CAA4D;AAC5D,mCAA6E;AAC7E,0DAAuE;AACvE,4CAA8D;AAE9D,sCAAsC;AACtC,mEAA+D;AAuB/D,oGAAoG;AACvF,QAAA,aAAa,GAAG,MAAM,CAAC;AAEpC,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC;AAExD;;;;GAIG;AACH,SAAgB,eAAe,CAAC,GAAW;IACzC,MAAM,IAAI,GAAG,GAAU,EAAE;QACvB,MAAM,IAAI,kBAAS,CAAC,gBAAgB,EAAE,GAAG,EAAE,8DAA8D,CAAC,CAAC;IAC7G,CAAC,CAAC;IACF,IAAI,CAAM,CAAC;IACX,IAAI,CAAC;QACH,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IACD,IAAI,CAAC,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,KAAK,OAAO;QAAE,IAAI,EAAE,CAAC;IAC9D,IAAI,CAAC,CAAC,QAAQ,KAAK,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC;QAAE,IAAI,EAAE,CAAC;IACnE,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,CA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,uBAAuB,CAAC,CAAC;AAChF,MAAM,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,kBAAS,CAAC,cAAc,EAAE,GAAG,EAAE,uBAAuB,CAAC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
3
|
+
import { CorsOriginsService } from './cors-origins.service';
|
|
4
|
+
export declare const CUSTOMER_CORS_METHODS = "GET, POST, PATCH, DELETE, OPTIONS";
|
|
5
|
+
export declare const CORS_ALLOWED_HEADERS = "Authorization, Content-Type";
|
|
6
|
+
type Handler = (req: Request, res: Response, next: NextFunction) => void;
|
|
7
|
+
/**
|
|
8
|
+
* CORS for the customer API and sign-in (`/oauth2/token`, `/oauth2/token/revocation`, `/oauth2/me`,
|
|
9
|
+
* `/api/customer/**`, and core's `/api/store/**`): origins allowed with customer scope only. Never
|
|
10
|
+
* `Access-Control-Allow-Credentials` (cross-origin calls carry a bearer token); always `Vary: Origin`.
|
|
11
|
+
* A preflight is answered here with `204` (headers only for an allowed origin) and never reaches the route or
|
|
12
|
+
* oidc-provider, whose own CORS would echo any origin. Other requests continue; a disallowed origin gets no CORS
|
|
13
|
+
* headers, so the browser blocks the answer.
|
|
14
|
+
*/
|
|
15
|
+
export declare function customerCorsMiddleware(service: CorsOriginsService): Handler;
|
|
16
|
+
/** Nest form of `customerCorsMiddleware`, for `consumer.apply(CustomerCorsMiddleware)`. */
|
|
17
|
+
export declare class CustomerCorsMiddleware implements NestMiddleware {
|
|
18
|
+
private readonly handler;
|
|
19
|
+
constructor(service: CorsOriginsService);
|
|
20
|
+
use(req: Request, res: Response, next: NextFunction): void;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
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.CustomerCorsMiddleware = exports.CORS_ALLOWED_HEADERS = exports.CUSTOMER_CORS_METHODS = void 0;
|
|
16
|
+
exports.customerCorsMiddleware = customerCorsMiddleware;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const cors_origins_service_1 = require("./cors-origins.service");
|
|
19
|
+
exports.CUSTOMER_CORS_METHODS = 'GET, POST, PATCH, DELETE, OPTIONS';
|
|
20
|
+
exports.CORS_ALLOWED_HEADERS = 'Authorization, Content-Type';
|
|
21
|
+
const isProviderPath = (req) => /^\/oauth2\//i.test(req.originalUrl ?? req.url);
|
|
22
|
+
/**
|
|
23
|
+
* oidc-provider (mounted at /oauth2) runs its own CORS unless the answer already has `Access-Control-*` headers: it
|
|
24
|
+
* echoes any Origin until the client is known (so every early error, e.g. an unknown client, would carry it). For an
|
|
25
|
+
* origin that is not allowed here the answer must carry no CORS at all, so later `Access-Control-*` headers are dropped.
|
|
26
|
+
*/
|
|
27
|
+
function suppressCorsHeaders(res) {
|
|
28
|
+
const set = res.setHeader.bind(res);
|
|
29
|
+
res.setHeader = ((name, value) => /^access-control-/i.test(name) ? res : set(name, value));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* CORS for the customer API and sign-in (`/oauth2/token`, `/oauth2/token/revocation`, `/oauth2/me`,
|
|
33
|
+
* `/api/customer/**`, and core's `/api/store/**`): origins allowed with customer scope only. Never
|
|
34
|
+
* `Access-Control-Allow-Credentials` (cross-origin calls carry a bearer token); always `Vary: Origin`.
|
|
35
|
+
* A preflight is answered here with `204` (headers only for an allowed origin) and never reaches the route or
|
|
36
|
+
* oidc-provider, whose own CORS would echo any origin. Other requests continue; a disallowed origin gets no CORS
|
|
37
|
+
* headers, so the browser blocks the answer.
|
|
38
|
+
*/
|
|
39
|
+
function customerCorsMiddleware(service) {
|
|
40
|
+
return (req, res, next) => {
|
|
41
|
+
res.vary('Origin');
|
|
42
|
+
const raw = req.headers.origin;
|
|
43
|
+
const origin = Array.isArray(raw) ? raw[0] : raw;
|
|
44
|
+
const preflight = req.method === 'OPTIONS';
|
|
45
|
+
const done = (allowed) => {
|
|
46
|
+
if (!allowed)
|
|
47
|
+
suppressCorsHeaders(res);
|
|
48
|
+
if (allowed && origin) {
|
|
49
|
+
res.setHeader('Access-Control-Allow-Origin', origin);
|
|
50
|
+
res.setHeader('Access-Control-Allow-Methods', exports.CUSTOMER_CORS_METHODS);
|
|
51
|
+
res.setHeader('Access-Control-Allow-Headers', exports.CORS_ALLOWED_HEADERS);
|
|
52
|
+
res.setHeader('Access-Control-Max-Age', '600');
|
|
53
|
+
// oidc-provider's 401s (userinfo, revocation) explain themselves in WWW-Authenticate.
|
|
54
|
+
if (isProviderPath(req))
|
|
55
|
+
res.setHeader('Access-Control-Expose-Headers', 'WWW-Authenticate');
|
|
56
|
+
}
|
|
57
|
+
if (preflight) {
|
|
58
|
+
res.status(204).end();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
next();
|
|
62
|
+
};
|
|
63
|
+
if (!origin)
|
|
64
|
+
return done(false);
|
|
65
|
+
service.allows(origin, 'customer').then(done, next);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Nest form of `customerCorsMiddleware`, for `consumer.apply(CustomerCorsMiddleware)`. */
|
|
69
|
+
let CustomerCorsMiddleware = class CustomerCorsMiddleware {
|
|
70
|
+
handler;
|
|
71
|
+
constructor(service) {
|
|
72
|
+
this.handler = customerCorsMiddleware(service);
|
|
73
|
+
}
|
|
74
|
+
use(req, res, next) {
|
|
75
|
+
this.handler(req, res, next);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
exports.CustomerCorsMiddleware = CustomerCorsMiddleware;
|
|
79
|
+
exports.CustomerCorsMiddleware = CustomerCorsMiddleware = __decorate([
|
|
80
|
+
(0, common_1.Injectable)(),
|
|
81
|
+
__param(0, (0, common_1.Inject)(cors_origins_service_1.CorsOriginsService)),
|
|
82
|
+
__metadata("design:paramtypes", [cors_origins_service_1.CorsOriginsService])
|
|
83
|
+
], CustomerCorsMiddleware);
|
|
84
|
+
//# sourceMappingURL=customer-cors.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer-cors.middleware.js","sourceRoot":"","sources":["../../src/cors/customer-cors.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA8BA,wDAyBC;AAvDD,2CAAyE;AAEzE,iEAA4D;AAE/C,QAAA,qBAAqB,GAAG,mCAAmC,CAAC;AAC5D,QAAA,oBAAoB,GAAG,6BAA6B,CAAC;AAIlE,MAAM,cAAc,GAAG,CAAC,GAAY,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;AAEzF;;;;GAIG;AACH,SAAS,mBAAmB,CAAC,GAAa;IACxC,MAAM,GAAG,GAAG,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpC,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC,IAAY,EAAE,KAA0C,EAAE,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,CAAC;YACD,IAAI,EAAE,CAAC;QACT,CAAC,CAAC;QACF,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC,CAAC;AACJ,CAAC;AAED,2FAA2F;AAEpF,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAChB,OAAO,CAAU;IAElC,YAAwC,OAA2B;QACjE,IAAI,CAAC,OAAO,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACjD,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;CACF,CAAA;AAVY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,mBAAU,GAAE;IAIE,WAAA,IAAA,eAAM,EAAC,yCAAkB,CAAC,CAAA;qCAAU,yCAAkB;GAHxD,sBAAsB,CAUlC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { Kysely } from 'kysely';
|
|
3
|
+
import { AuditService } from '../audit/audit.service';
|
|
4
|
+
import type { SessionStore } from '../bff/session.store';
|
|
5
|
+
import type { OAuth2Config } from '../config/schema';
|
|
6
|
+
import type { AuthTables } from '../db/types';
|
|
7
|
+
import { type RequestCustomer } from '../guard/customer-auth.decorator';
|
|
8
|
+
import type { CustomerRequest } from '../guard/customer-authenticator';
|
|
9
|
+
import { OtpService } from '../otp/otp.service';
|
|
10
|
+
import { CorsOriginsService } from '../cors/cors-origins.service';
|
|
11
|
+
import { PasswordService } from '../security/password';
|
|
12
|
+
import type { RateLimiter } from '../security/rate-limiter';
|
|
13
|
+
import { AuthHookRunner } from './auth-hooks';
|
|
14
|
+
import { CustomerSessionsRevoker } from './customer-sessions.revoker';
|
|
15
|
+
import { CustomerService, type Customer } from './customer.service';
|
|
16
|
+
import { SignInSettingsService } from './sign-in-settings.service';
|
|
17
|
+
/**
|
|
18
|
+
* Spec "Customer API": the signed-in customer's own account (`@CustomerAuth()`: the site's cookie, or a mobile/web
|
|
19
|
+
* bearer token). Cookie writes are Origin-checked by the guard. Every answer is `no-store`.
|
|
20
|
+
*/
|
|
21
|
+
export declare class CustomerAccountController {
|
|
22
|
+
private readonly cfg;
|
|
23
|
+
private readonly db;
|
|
24
|
+
private readonly customers;
|
|
25
|
+
private readonly settings;
|
|
26
|
+
private readonly otp;
|
|
27
|
+
private readonly passwords;
|
|
28
|
+
private readonly hooks;
|
|
29
|
+
private readonly audit;
|
|
30
|
+
private readonly limiter;
|
|
31
|
+
private readonly sessions;
|
|
32
|
+
private readonly revoker;
|
|
33
|
+
constructor(cfg: OAuth2Config, db: Kysely<AuthTables>, customers: CustomerService, settings: SignInSettingsService, otp: OtpService, passwords: PasswordService, hooks: AuthHookRunner, audit: AuditService, limiter: RateLimiter, sessions: SessionStore, revoker: CustomerSessionsRevoker);
|
|
34
|
+
/**
|
|
35
|
+
* `{ currentPassword?, newPassword }` → 204. With a password already set, `currentPassword` must match (else
|
|
36
|
+
* `401 invalid_credentials`, also when missing); without one it is ignored (`reason: 'set'`). A change ends the
|
|
37
|
+
* customer's other sessions and grants and keeps the calling credential.
|
|
38
|
+
*/
|
|
39
|
+
password(req: CustomerRequest, me: RequestCustomer, res: Response, body: unknown): Promise<void>;
|
|
40
|
+
/** `{ name }` (trimmed; empty or null clears it) → the customer. */
|
|
41
|
+
update(me: RequestCustomer, res: Response, body: unknown): Promise<Customer>;
|
|
42
|
+
/**
|
|
43
|
+
* `{ identifier }` → `202 { resendAfterSec }` and a `verify` code to it. An identifier that already has an owner
|
|
44
|
+
* (another customer, or this one) gets the same answer and no code, so the answer never reveals ownership.
|
|
45
|
+
*/
|
|
46
|
+
addIdentifier(req: Request, me: RequestCustomer, res: Response, body: unknown): Promise<{
|
|
47
|
+
resendAfterSec: number;
|
|
48
|
+
}>;
|
|
49
|
+
/** `{ identifier, code }` → the customer with the identifier added, verified. Wrong / no code → 401 invalid_code. */
|
|
50
|
+
verifyIdentifier(req: Request, me: RequestCustomer, res: Response, body: unknown): Promise<Customer>;
|
|
51
|
+
/** Removes one of the customer's identifiers; the last verified one → 409 last_identifier; not theirs → 404. */
|
|
52
|
+
removeIdentifier(me: RequestCustomer, res: Response, identifierId: string): Promise<Customer>;
|
|
53
|
+
private load;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Public password reset (spec "Customer API"): `POST password-reset` sends a `reset` code; `POST
|
|
57
|
+
* password-reset/confirm` sets the new password and ends every session and grant of the customer.
|
|
58
|
+
*
|
|
59
|
+
* Enumeration safety: for an enabled kind the request answers `202` after the same lookup for every identifier;
|
|
60
|
+
* unknown, unverified, password-less and disabled accounts get no code (nothing stored or sent), so confirm then
|
|
61
|
+
* fails `401 invalid_code` exactly like a wrong code. A browser call must come from the site's own Origin or an
|
|
62
|
+
* admin-managed origin allowed with customer scope; a call without an Origin header (a mobile app) is accepted — no
|
|
63
|
+
* ambient credential is used here.
|
|
64
|
+
*/
|
|
65
|
+
export declare class CustomerPasswordResetController {
|
|
66
|
+
private readonly cfg;
|
|
67
|
+
private readonly db;
|
|
68
|
+
private readonly customers;
|
|
69
|
+
private readonly settings;
|
|
70
|
+
private readonly otp;
|
|
71
|
+
private readonly passwords;
|
|
72
|
+
private readonly hooks;
|
|
73
|
+
private readonly audit;
|
|
74
|
+
private readonly limiter;
|
|
75
|
+
private readonly revoker;
|
|
76
|
+
private readonly cors;
|
|
77
|
+
constructor(cfg: OAuth2Config, db: Kysely<AuthTables>, customers: CustomerService, settings: SignInSettingsService, otp: OtpService, passwords: PasswordService, hooks: AuthHookRunner, audit: AuditService, limiter: RateLimiter, revoker: CustomerSessionsRevoker, cors: CorsOriginsService);
|
|
78
|
+
request(req: Request, res: Response, body: unknown): Promise<{
|
|
79
|
+
resendAfterSec: number;
|
|
80
|
+
}>;
|
|
81
|
+
confirm(req: Request, res: Response, body: unknown): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* 404 while the storefront is off; no-store; a browser (Origin header present) must be the site itself or an origin
|
|
84
|
+
* allowed with customer scope (spec goal 14).
|
|
85
|
+
*/
|
|
86
|
+
private begin;
|
|
87
|
+
/** IP first; the identifier key is only created for an IP that is not limited (as customer sign-in). */
|
|
88
|
+
private limit;
|
|
89
|
+
/** One indexed query whether or not the identifier exists (same cost for known and unknown identifiers). */
|
|
90
|
+
private lookup;
|
|
91
|
+
}
|