@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,229 @@
|
|
|
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.CustomerService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const kysely_1 = require("kysely");
|
|
18
|
+
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const errors_1 = require("../errors");
|
|
21
|
+
const identifier_1 = require("./identifier");
|
|
22
|
+
const isUniqueViolation = (e) => e?.code === '23505';
|
|
23
|
+
let CustomerService = class CustomerService {
|
|
24
|
+
db;
|
|
25
|
+
cfg;
|
|
26
|
+
constructor(db, cfg) {
|
|
27
|
+
this.db = db;
|
|
28
|
+
this.cfg = cfg;
|
|
29
|
+
}
|
|
30
|
+
async hydrate(db, rows) {
|
|
31
|
+
if (!rows.length)
|
|
32
|
+
return [];
|
|
33
|
+
const ids = await db
|
|
34
|
+
.selectFrom('auth_customer_identifiers')
|
|
35
|
+
.select(['id', 'customer_id', 'kind', 'value', 'verified_at'])
|
|
36
|
+
.where('customer_id', 'in', rows.map((r) => r.id))
|
|
37
|
+
.orderBy('created_at')
|
|
38
|
+
.orderBy('id')
|
|
39
|
+
.execute();
|
|
40
|
+
return rows.map((r) => ({
|
|
41
|
+
id: r.id,
|
|
42
|
+
name: r.name,
|
|
43
|
+
status: r.status,
|
|
44
|
+
hasPassword: r.password_hash !== null,
|
|
45
|
+
passwordHash: r.password_hash,
|
|
46
|
+
identifiers: ids
|
|
47
|
+
.filter((i) => i.customer_id === r.id)
|
|
48
|
+
.map((i) => ({ id: i.id, kind: i.kind, value: i.value, verified: i.verified_at !== null })),
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
strip({ passwordHash: _p, ...c }) {
|
|
52
|
+
return c;
|
|
53
|
+
}
|
|
54
|
+
async findById(id, trx) {
|
|
55
|
+
const db = trx ?? this.db;
|
|
56
|
+
const row = await db
|
|
57
|
+
.selectFrom('auth_customers')
|
|
58
|
+
.select(['id', 'name', 'status', 'password_hash'])
|
|
59
|
+
.where('id', '=', id)
|
|
60
|
+
.executeTakeFirst();
|
|
61
|
+
if (!row)
|
|
62
|
+
return undefined;
|
|
63
|
+
return this.strip((await this.hydrate(db, [row]))[0]);
|
|
64
|
+
}
|
|
65
|
+
async findByIdentifier(kind, value, trx) {
|
|
66
|
+
const db = trx ?? this.db;
|
|
67
|
+
const row = await db
|
|
68
|
+
.selectFrom('auth_customer_identifiers as i')
|
|
69
|
+
.innerJoin('auth_customers as c', 'c.id', 'i.customer_id')
|
|
70
|
+
.select(['c.id', 'c.name', 'c.status', 'c.password_hash'])
|
|
71
|
+
.where('i.kind', '=', kind)
|
|
72
|
+
.where('i.value', '=', value)
|
|
73
|
+
.executeTakeFirst();
|
|
74
|
+
if (!row)
|
|
75
|
+
return undefined;
|
|
76
|
+
return (await this.hydrate(db, [row]))[0];
|
|
77
|
+
}
|
|
78
|
+
async createWithIdentifier(input, verified, trx) {
|
|
79
|
+
const run = async (t) => {
|
|
80
|
+
const c = await t
|
|
81
|
+
.insertInto('auth_customers')
|
|
82
|
+
.values({ name: input.name ?? null })
|
|
83
|
+
.returning('id')
|
|
84
|
+
.executeTakeFirstOrThrow();
|
|
85
|
+
try {
|
|
86
|
+
await t
|
|
87
|
+
.insertInto('auth_customer_identifiers')
|
|
88
|
+
.values({ customer_id: c.id, kind: input.kind, value: input.value, verified_at: verified ? (0, kysely_1.sql) `now()` : null })
|
|
89
|
+
.execute();
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
if (isUniqueViolation(e))
|
|
93
|
+
throw new errors_1.AuthError('identifier_taken', 409, 'Identifier already in use');
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
return (await this.findById(c.id, t));
|
|
97
|
+
};
|
|
98
|
+
return trx ? run(trx) : this.db.transaction().execute(run);
|
|
99
|
+
}
|
|
100
|
+
async markVerified(identifierId) {
|
|
101
|
+
await this.db
|
|
102
|
+
.updateTable('auth_customer_identifiers')
|
|
103
|
+
.set({ verified_at: (0, kysely_1.sql) `coalesce(verified_at, now())` })
|
|
104
|
+
.where('id', '=', identifierId)
|
|
105
|
+
.execute();
|
|
106
|
+
}
|
|
107
|
+
/** `verified: true` stores it verified in the same insert (self-service add after a `verify` code). */
|
|
108
|
+
async addIdentifier(customerId, kind, value, verified = false) {
|
|
109
|
+
try {
|
|
110
|
+
await this.db
|
|
111
|
+
.insertInto('auth_customer_identifiers')
|
|
112
|
+
.values({ customer_id: customerId, kind, value, verified_at: verified ? (0, kysely_1.sql) `now()` : null })
|
|
113
|
+
.execute();
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
if (isUniqueViolation(e))
|
|
117
|
+
throw new errors_1.AuthError('identifier_taken', 409, 'Identifier already in use');
|
|
118
|
+
if (e?.code === '23503')
|
|
119
|
+
throw new errors_1.AuthError('not_found', 404, 'Customer not found');
|
|
120
|
+
throw e;
|
|
121
|
+
}
|
|
122
|
+
return (await this.findById(customerId));
|
|
123
|
+
}
|
|
124
|
+
/** Refuses to remove the customer's last verified identifier (409 `last_identifier`). */
|
|
125
|
+
async removeIdentifier(customerId, identifierId) {
|
|
126
|
+
await this.db.transaction().execute(async (t) => {
|
|
127
|
+
// Serialize concurrent removals for this customer.
|
|
128
|
+
await t.selectFrom('auth_customers').select('id').where('id', '=', customerId).forUpdate().executeTakeFirst();
|
|
129
|
+
const ids = await t
|
|
130
|
+
.selectFrom('auth_customer_identifiers')
|
|
131
|
+
.select(['id', 'verified_at'])
|
|
132
|
+
.where('customer_id', '=', customerId)
|
|
133
|
+
.execute();
|
|
134
|
+
const target = ids.find((i) => i.id === identifierId);
|
|
135
|
+
if (!target)
|
|
136
|
+
throw new errors_1.AuthError('not_found', 404, 'Identifier not found');
|
|
137
|
+
if (target.verified_at !== null && !ids.some((i) => i.id !== identifierId && i.verified_at !== null)) {
|
|
138
|
+
throw new errors_1.AuthError('last_identifier', 409, 'Cannot remove the last verified identifier');
|
|
139
|
+
}
|
|
140
|
+
await t.deleteFrom('auth_customer_identifiers').where('id', '=', identifierId).execute();
|
|
141
|
+
});
|
|
142
|
+
return (await this.findById(customerId));
|
|
143
|
+
}
|
|
144
|
+
/** The stored password hash (null: none set); undefined when the customer does not exist. */
|
|
145
|
+
async passwordHashOf(id) {
|
|
146
|
+
const row = await this.db.selectFrom('auth_customers').select('password_hash').where('id', '=', id).executeTakeFirst();
|
|
147
|
+
return row ? row.password_hash : undefined;
|
|
148
|
+
}
|
|
149
|
+
async setPassword(customerId, hash) {
|
|
150
|
+
await this.db
|
|
151
|
+
.updateTable('auth_customers')
|
|
152
|
+
.set({ password_hash: hash, updated_at: (0, kysely_1.sql) `now()` })
|
|
153
|
+
.where('id', '=', customerId)
|
|
154
|
+
.execute();
|
|
155
|
+
}
|
|
156
|
+
async setStatus(id, status) {
|
|
157
|
+
await this.db.updateTable('auth_customers').set({ status, updated_at: (0, kysely_1.sql) `now()` }).where('id', '=', id).execute();
|
|
158
|
+
}
|
|
159
|
+
async setName(id, name) {
|
|
160
|
+
await this.db.updateTable('auth_customers').set({ name, updated_at: (0, kysely_1.sql) `now()` }).where('id', '=', id).execute();
|
|
161
|
+
}
|
|
162
|
+
async touchLogin(id) {
|
|
163
|
+
await this.db.updateTable('auth_customers').set({ last_login_at: (0, kysely_1.sql) `now()` }).where('id', '=', id).execute();
|
|
164
|
+
}
|
|
165
|
+
async list(opts) {
|
|
166
|
+
const q = opts.q?.trim();
|
|
167
|
+
const esc = (v) => v.replace(/[\\%_]/g, (m) => `\\${m}`);
|
|
168
|
+
// Identifier matching: raw prefix (e.g. `+8491`, `zed@`), the normalized form's prefix (`0912 345 678` -> `+84912345678`),
|
|
169
|
+
// and, for phone-like input, a prefix of the national number in `storefront.phoneRegion` (`0912` -> `+84912%`)
|
|
170
|
+
// or of the international number typed without `+` (`8491` -> `+8491%`).
|
|
171
|
+
const region = this.cfg.storefront.phoneRegion;
|
|
172
|
+
const prefixes = [];
|
|
173
|
+
const phonePrefixes = [];
|
|
174
|
+
if (q) {
|
|
175
|
+
prefixes.push(`${esc(q.toLowerCase())}%`);
|
|
176
|
+
const n = (0, identifier_1.normalizeIdentifier)(q, region);
|
|
177
|
+
if (n)
|
|
178
|
+
prefixes.push(`${esc(n.value.toLowerCase())}%`);
|
|
179
|
+
if (/^[\d\s().+-]+$/.test(q)) {
|
|
180
|
+
const digits = q.replace(/\D/g, '');
|
|
181
|
+
const national = digits.replace(/^0+/, '');
|
|
182
|
+
let cc;
|
|
183
|
+
try {
|
|
184
|
+
cc = (0, libphonenumber_js_1.getCountryCallingCode)(region);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
cc = undefined;
|
|
188
|
+
}
|
|
189
|
+
if (national && cc)
|
|
190
|
+
phonePrefixes.push(`+${cc}${national}%`);
|
|
191
|
+
if (digits && !q.trim().startsWith('0'))
|
|
192
|
+
phonePrefixes.push(`+${digits}%`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const nameLike = q ? `%${esc(q.toLowerCase())}%` : undefined;
|
|
196
|
+
const filter = (qb) => q
|
|
197
|
+
? qb.where((eb) => eb.or([
|
|
198
|
+
eb((0, kysely_1.sql) `lower(coalesce(c.name, ''))`, 'like', nameLike),
|
|
199
|
+
eb.exists(eb
|
|
200
|
+
.selectFrom('auth_customer_identifiers as i')
|
|
201
|
+
.select((0, kysely_1.sql) `1`.as('one'))
|
|
202
|
+
.whereRef('i.customer_id', '=', 'c.id')
|
|
203
|
+
.where((ib) => ib.or([
|
|
204
|
+
...prefixes.map((p) => ib((0, kysely_1.sql) `lower(i.value)`, 'like', p)),
|
|
205
|
+
...phonePrefixes.map((p) => ib.and([ib('i.kind', '=', 'phone'), ib('i.value', 'like', p)])),
|
|
206
|
+
]))),
|
|
207
|
+
]))
|
|
208
|
+
: qb;
|
|
209
|
+
const total = await filter(this.db.selectFrom('auth_customers as c'))
|
|
210
|
+
.select((eb) => eb.fn.countAll().as('n'))
|
|
211
|
+
.executeTakeFirstOrThrow();
|
|
212
|
+
const rows = await filter(this.db.selectFrom('auth_customers as c'))
|
|
213
|
+
.select(['c.id', 'c.name', 'c.status', 'c.password_hash'])
|
|
214
|
+
.orderBy('c.created_at', 'desc')
|
|
215
|
+
.orderBy('c.id')
|
|
216
|
+
.limit(opts.limit)
|
|
217
|
+
.offset(opts.offset)
|
|
218
|
+
.execute();
|
|
219
|
+
return { items: (await this.hydrate(this.db, rows)).map((c) => this.strip(c)), total: Number(total.n) };
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
exports.CustomerService = CustomerService;
|
|
223
|
+
exports.CustomerService = CustomerService = __decorate([
|
|
224
|
+
(0, common_1.Injectable)(),
|
|
225
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_DB)),
|
|
226
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
227
|
+
__metadata("design:paramtypes", [Function, Object])
|
|
228
|
+
], CustomerService);
|
|
229
|
+
//# sourceMappingURL=customer.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer.service.js","sourceRoot":"","sources":["../../src/customers/customer.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,mCAA4D;AAC5D,yDAA4E;AAE5E,4CAAwD;AAExD,sCAAsC;AACtC,6CAAwE;AAaxE,MAAM,iBAAiB,GAAG,CAAC,CAAU,EAAE,EAAE,CAAE,CAAuB,EAAE,IAAI,KAAK,OAAO,CAAC;AAG9E,IAAM,eAAe,GAArB,MAAM,eAAe;IAEY;IACI;IAF1C,YACsC,EAAsB,EAClB,GAAqC;QADzC,OAAE,GAAF,EAAE,CAAoB;QAClB,QAAG,GAAH,GAAG,CAAkC;IAC5E,CAAC;IAEI,KAAK,CAAC,OAAO,CAAC,EAAM,EAAE,IAAmB;QAC/C,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO,EAAE,CAAC;QAC5B,MAAM,GAAG,GAAG,MAAM,EAAE;aACjB,UAAU,CAAC,2BAA2B,CAAC;aACvC,MAAM,CAAC,CAAC,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;aAC7D,KAAK,CAAC,aAAa,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;aACjD,OAAO,CAAC,YAAY,CAAC;aACrB,OAAO,CAAC,IAAI,CAAC;aACb,OAAO,EAAE,CAAC;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YACtB,EAAE,EAAE,CAAC,CAAC,EAAE;YACR,IAAI,EAAE,CAAC,CAAC,IAAI;YACZ,MAAM,EAAE,CAAC,CAAC,MAA4B;YACtC,WAAW,EAAE,CAAC,CAAC,aAAa,KAAK,IAAI;YACrC,YAAY,EAAE,CAAC,CAA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AAO,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAE,KAAuB,CAAC,CAAC,CAAC,EAAE,CAAC;IAC7H,CAAC;CACF,CAAA;AAhNY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,qBAAS,CAAC,CAAA;IACjB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;;GAHb,eAAe,CAgN3B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import { AuditService } from '../audit/audit.service';
|
|
3
|
+
import { type RequestUser } from '../guard/current-user.decorator';
|
|
4
|
+
import { CustomerSessionsRevoker } from './customer-sessions.revoker';
|
|
5
|
+
import { CustomerService, type Customer } from './customer.service';
|
|
6
|
+
export declare class CustomersAdminController {
|
|
7
|
+
private readonly customers;
|
|
8
|
+
private readonly revoker;
|
|
9
|
+
private readonly audit;
|
|
10
|
+
constructor(customers: CustomerService, revoker: CustomerSessionsRevoker, audit: AuditService);
|
|
11
|
+
list(query: unknown, res: Response): Promise<{
|
|
12
|
+
data: Customer[];
|
|
13
|
+
meta: {
|
|
14
|
+
total: number;
|
|
15
|
+
limit: number;
|
|
16
|
+
offset: number;
|
|
17
|
+
};
|
|
18
|
+
}>;
|
|
19
|
+
get(rawId: string, res: Response): Promise<Customer>;
|
|
20
|
+
update(rawId: string, body: unknown, actor: RequestUser, req: Request, res: Response): Promise<Customer>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
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.CustomersAdminController = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const audit_service_1 = require("../audit/audit.service");
|
|
19
|
+
const errors_1 = require("../errors");
|
|
20
|
+
const current_user_decorator_1 = require("../guard/current-user.decorator");
|
|
21
|
+
const validate_1 = require("../security/validate");
|
|
22
|
+
const customer_sessions_revoker_1 = require("./customer-sessions.revoker");
|
|
23
|
+
const customer_service_1 = require("./customer.service");
|
|
24
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
25
|
+
/** Params that are not UUIDs can never match a row: answer 404 instead of a Postgres cast error (500). */
|
|
26
|
+
function uuid(value) {
|
|
27
|
+
if (!UUID.test(value))
|
|
28
|
+
throw new errors_1.AuthError('not_found', 404, 'Customer not found');
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
const listSchema = zod_1.z.object({
|
|
32
|
+
q: zod_1.z.string().max(320).optional(),
|
|
33
|
+
limit: zod_1.z.coerce.number().int().min(1).max(200).default(50),
|
|
34
|
+
offset: zod_1.z.coerce.number().int().min(0).default(0),
|
|
35
|
+
});
|
|
36
|
+
const patchSchema = zod_1.z
|
|
37
|
+
.object({
|
|
38
|
+
status: zod_1.z.enum(['active', 'disabled']).optional(),
|
|
39
|
+
name: zod_1.z.string().nullable().transform((v) => (v === null ? null : v.trim() || null)).pipe(zod_1.z.string().max(200).nullable()).optional(),
|
|
40
|
+
})
|
|
41
|
+
.strict()
|
|
42
|
+
.refine((v) => v.status !== undefined || v.name !== undefined, { message: 'status or name is required' });
|
|
43
|
+
let CustomersAdminController = class CustomersAdminController {
|
|
44
|
+
customers;
|
|
45
|
+
revoker;
|
|
46
|
+
audit;
|
|
47
|
+
constructor(customers, revoker, audit) {
|
|
48
|
+
this.customers = customers;
|
|
49
|
+
this.revoker = revoker;
|
|
50
|
+
this.audit = audit;
|
|
51
|
+
}
|
|
52
|
+
async list(query, res) {
|
|
53
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
54
|
+
const { q, limit, offset } = (0, validate_1.parseBody)(listSchema, query);
|
|
55
|
+
const { items, total } = await this.customers.list({ q, limit, offset });
|
|
56
|
+
return { data: items, meta: { total, limit, offset } };
|
|
57
|
+
}
|
|
58
|
+
async get(rawId, res) {
|
|
59
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
60
|
+
const c = await this.customers.findById(uuid(rawId));
|
|
61
|
+
if (!c)
|
|
62
|
+
throw new errors_1.AuthError('not_found', 404, 'Customer not found');
|
|
63
|
+
return c;
|
|
64
|
+
}
|
|
65
|
+
async update(rawId, body, actor, req, res) {
|
|
66
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
67
|
+
const id = uuid(rawId);
|
|
68
|
+
const input = (0, validate_1.parseBody)(patchSchema, body);
|
|
69
|
+
const before = await this.customers.findById(id);
|
|
70
|
+
if (!before)
|
|
71
|
+
throw new errors_1.AuthError('not_found', 404, 'Customer not found');
|
|
72
|
+
if (input.name !== undefined)
|
|
73
|
+
await this.customers.setName(id, input.name);
|
|
74
|
+
if (input.status !== undefined) {
|
|
75
|
+
await this.customers.setStatus(id, input.status);
|
|
76
|
+
if (input.status === 'disabled')
|
|
77
|
+
await this.revoker.revokeAll(id);
|
|
78
|
+
}
|
|
79
|
+
const ua = req.headers['user-agent'];
|
|
80
|
+
await this.audit.record({
|
|
81
|
+
type: 'customer.update',
|
|
82
|
+
actorId: actor.id,
|
|
83
|
+
ip: req.ip,
|
|
84
|
+
userAgent: Array.isArray(ua) ? ua[0] : ua,
|
|
85
|
+
meta: {
|
|
86
|
+
customerId: id,
|
|
87
|
+
...(input.name !== undefined ? { name: { from: before.name, to: input.name } } : {}),
|
|
88
|
+
...(input.status !== undefined ? { status: { from: before.status, to: input.status } } : {}),
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
return (await this.customers.findById(id));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
exports.CustomersAdminController = CustomersAdminController;
|
|
95
|
+
__decorate([
|
|
96
|
+
(0, common_1.Get)(),
|
|
97
|
+
(0, current_user_decorator_1.RequirePermission)('customers.read'),
|
|
98
|
+
__param(0, (0, common_1.Query)()),
|
|
99
|
+
__param(1, (0, common_1.Res)({ passthrough: true })),
|
|
100
|
+
__metadata("design:type", Function),
|
|
101
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
102
|
+
__metadata("design:returntype", Promise)
|
|
103
|
+
], CustomersAdminController.prototype, "list", null);
|
|
104
|
+
__decorate([
|
|
105
|
+
(0, common_1.Get)(':id'),
|
|
106
|
+
(0, current_user_decorator_1.RequirePermission)('customers.read'),
|
|
107
|
+
__param(0, (0, common_1.Param)('id')),
|
|
108
|
+
__param(1, (0, common_1.Res)({ passthrough: true })),
|
|
109
|
+
__metadata("design:type", Function),
|
|
110
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
111
|
+
__metadata("design:returntype", Promise)
|
|
112
|
+
], CustomersAdminController.prototype, "get", null);
|
|
113
|
+
__decorate([
|
|
114
|
+
(0, common_1.Patch)(':id'),
|
|
115
|
+
(0, current_user_decorator_1.RequirePermission)('customers.manage'),
|
|
116
|
+
__param(0, (0, common_1.Param)('id')),
|
|
117
|
+
__param(1, (0, common_1.Body)()),
|
|
118
|
+
__param(2, (0, current_user_decorator_1.CurrentUser)()),
|
|
119
|
+
__param(3, (0, common_1.Req)()),
|
|
120
|
+
__param(4, (0, common_1.Res)({ passthrough: true })),
|
|
121
|
+
__metadata("design:type", Function),
|
|
122
|
+
__metadata("design:paramtypes", [String, Object, Object, Object, Object]),
|
|
123
|
+
__metadata("design:returntype", Promise)
|
|
124
|
+
], CustomersAdminController.prototype, "update", null);
|
|
125
|
+
exports.CustomersAdminController = CustomersAdminController = __decorate([
|
|
126
|
+
(0, common_1.Controller)('api/admin/customers'),
|
|
127
|
+
__param(0, (0, common_1.Inject)(customer_service_1.CustomerService)),
|
|
128
|
+
__param(1, (0, common_1.Inject)(customer_sessions_revoker_1.CustomerSessionsRevoker)),
|
|
129
|
+
__param(2, (0, common_1.Inject)(audit_service_1.AuditService)),
|
|
130
|
+
__metadata("design:paramtypes", [customer_service_1.CustomerService,
|
|
131
|
+
customer_sessions_revoker_1.CustomerSessionsRevoker,
|
|
132
|
+
audit_service_1.AuditService])
|
|
133
|
+
], CustomersAdminController);
|
|
134
|
+
//# sourceMappingURL=customers-admin.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customers-admin.controller.js","sourceRoot":"","sources":["../../src/customers/customers-admin.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8F;AAE9F,6BAAwB;AACxB,0DAAsD;AACtD,sCAAsC;AACtC,4EAAmG;AACnG,mDAAiD;AACjD,2EAAsE;AACtE,yDAAoE;AAEpE,MAAM,IAAI,GAAG,iEAAiE,CAAC;AAE/E,0GAA0G;AAC1G,SAAS,IAAI,CAAC,KAAa;IACzB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,kBAAS,CAAC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DAAuB,CAAC,CAAA;IAC/B,WAAA,IAAA,eAAM,EAAC,4BAAY,CAAC,CAAA;qCAFgC,kCAAe;QACT,mDAAuB;QACpC,4BAAY;GAJjD,wBAAwB,CA6DpC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type IdentifierKind = 'email' | 'phone';
|
|
2
|
+
/** Email -> trimmed + lower-cased; phone -> E.164 using `region` for local numbers. Anything else -> null. */
|
|
3
|
+
export declare function normalizeIdentifier(raw: string, region: string): {
|
|
4
|
+
kind: IdentifierKind;
|
|
5
|
+
value: string;
|
|
6
|
+
} | null;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeIdentifier = normalizeIdentifier;
|
|
4
|
+
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
5
|
+
const email_1 = require("../security/email");
|
|
6
|
+
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
7
|
+
/** Email -> trimmed + lower-cased; phone -> E.164 using `region` for local numbers. Anything else -> null. */
|
|
8
|
+
function normalizeIdentifier(raw, region) {
|
|
9
|
+
const s = (raw ?? '').trim();
|
|
10
|
+
if (!s)
|
|
11
|
+
return null;
|
|
12
|
+
if (s.includes('@')) {
|
|
13
|
+
const value = (0, email_1.normalizeEmail)(s);
|
|
14
|
+
return EMAIL_RE.test(value) ? { kind: 'email', value } : null;
|
|
15
|
+
}
|
|
16
|
+
const phone = (0, libphonenumber_js_1.parsePhoneNumberFromString)(s, region);
|
|
17
|
+
if (phone?.isValid())
|
|
18
|
+
return { kind: 'phone', value: phone.number };
|
|
19
|
+
// Country code typed without '+' (e.g. 84912345678).
|
|
20
|
+
if (/^\d{8,15}$/.test(s)) {
|
|
21
|
+
const intl = (0, libphonenumber_js_1.parsePhoneNumberFromString)(`+${s}`);
|
|
22
|
+
if (intl?.isValid())
|
|
23
|
+
return { kind: 'phone', value: intl.number };
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=identifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../src/customers/identifier.ts"],"names":[],"mappings":";;AAQA,kDAeC;AAvBD,yDAAiF;AACjF,6CAAmD;AAInD,MAAM,QAAQ,GAAG,4BAA4B,CAAC;AAE9C,8GAA8G;AAC9G,SAAgB,mBAAmB,CAAC,GAAW,EAAE,MAAc;IAC7D,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IACpB,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,sBAAc,EAAC,CAAC,CAAC,CAAC;QAChC,OAAO,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IAChE,CAAC;IACD,MAAM,KAAK,GAAG,IAAA,8CAA0B,EAAC,CAAC,EAAE,MAAqB,CAAC,CAAC;IACnE,IAAI,KAAK,EAAE,OAAO,EAAE;QAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;IACpE,qDAAqD;IACrD,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,IAAA,8CAA0B,EAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACjD,IAAI,IAAI,EAAE,OAAO,EAAE;YAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IACpE,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { OAuth2Config } from '../config/schema';
|
|
3
|
+
import { type RequestUser } from '../guard/current-user.decorator';
|
|
4
|
+
import { SignInSettingsService, type SignInMethods } from './sign-in-settings.service';
|
|
5
|
+
export declare class CustomerAuthSettingsController {
|
|
6
|
+
private readonly settings;
|
|
7
|
+
constructor(settings: SignInSettingsService);
|
|
8
|
+
read(res: Response): Promise<{
|
|
9
|
+
methods: SignInMethods;
|
|
10
|
+
}>;
|
|
11
|
+
preview(body: unknown, res: Response): Promise<{
|
|
12
|
+
affectedCustomers: number;
|
|
13
|
+
}>;
|
|
14
|
+
write(body: unknown, actor: RequestUser, req: Request, res: Response): Promise<{
|
|
15
|
+
methods: SignInMethods;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export declare class CustomerAuthMethodsController {
|
|
19
|
+
private readonly settings;
|
|
20
|
+
private readonly cfg;
|
|
21
|
+
constructor(settings: SignInSettingsService, cfg: OAuth2Config);
|
|
22
|
+
/** Anonymous: which methods the sign-in page may offer; a disabled kind is `null`. 404 while the storefront is off. */
|
|
23
|
+
methods(res: Response): Promise<Record<'email' | 'phone', {
|
|
24
|
+
otp: boolean;
|
|
25
|
+
password: boolean;
|
|
26
|
+
} | null>>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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.CustomerAuthMethodsController = exports.CustomerAuthSettingsController = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const constants_1 = require("../constants");
|
|
19
|
+
const errors_1 = require("../errors");
|
|
20
|
+
const current_user_decorator_1 = require("../guard/current-user.decorator");
|
|
21
|
+
const public_decorator_1 = require("../guard/public.decorator");
|
|
22
|
+
const validate_1 = require("../security/validate");
|
|
23
|
+
const sign_in_settings_service_1 = require("./sign-in-settings.service");
|
|
24
|
+
const kind = zod_1.z.object({ enabled: zod_1.z.boolean(), otp: zod_1.z.boolean(), password: zod_1.z.boolean() }).strict();
|
|
25
|
+
const bodySchema = zod_1.z.object({ methods: zod_1.z.object({ email: kind, phone: kind }).strict() }).strict();
|
|
26
|
+
let CustomerAuthSettingsController = class CustomerAuthSettingsController {
|
|
27
|
+
settings;
|
|
28
|
+
constructor(settings) {
|
|
29
|
+
this.settings = settings;
|
|
30
|
+
}
|
|
31
|
+
async read(res) {
|
|
32
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
33
|
+
return { methods: await this.settings.get() };
|
|
34
|
+
}
|
|
35
|
+
async preview(body, res) {
|
|
36
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
37
|
+
const { methods } = (0, validate_1.parseBody)(bodySchema, body);
|
|
38
|
+
return { affectedCustomers: await this.settings.affectedCustomers(methods) };
|
|
39
|
+
}
|
|
40
|
+
async write(body, actor, req, res) {
|
|
41
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
42
|
+
const { methods } = (0, validate_1.parseBody)(bodySchema, body);
|
|
43
|
+
const ua = req.headers['user-agent'];
|
|
44
|
+
return { methods: await this.settings.put(methods, actor.id, { ip: req.ip, userAgent: Array.isArray(ua) ? ua[0] : ua }) };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.CustomerAuthSettingsController = CustomerAuthSettingsController;
|
|
48
|
+
__decorate([
|
|
49
|
+
(0, common_1.Get)(),
|
|
50
|
+
__param(0, (0, common_1.Res)({ passthrough: true })),
|
|
51
|
+
__metadata("design:type", Function),
|
|
52
|
+
__metadata("design:paramtypes", [Object]),
|
|
53
|
+
__metadata("design:returntype", Promise)
|
|
54
|
+
], CustomerAuthSettingsController.prototype, "read", null);
|
|
55
|
+
__decorate([
|
|
56
|
+
(0, common_1.Post)('preview'),
|
|
57
|
+
(0, common_1.HttpCode)(200),
|
|
58
|
+
__param(0, (0, common_1.Body)()),
|
|
59
|
+
__param(1, (0, common_1.Res)({ passthrough: true })),
|
|
60
|
+
__metadata("design:type", Function),
|
|
61
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
62
|
+
__metadata("design:returntype", Promise)
|
|
63
|
+
], CustomerAuthSettingsController.prototype, "preview", null);
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, common_1.Put)(),
|
|
66
|
+
__param(0, (0, common_1.Body)()),
|
|
67
|
+
__param(1, (0, current_user_decorator_1.CurrentUser)()),
|
|
68
|
+
__param(2, (0, common_1.Req)()),
|
|
69
|
+
__param(3, (0, common_1.Res)({ passthrough: true })),
|
|
70
|
+
__metadata("design:type", Function),
|
|
71
|
+
__metadata("design:paramtypes", [Object, Object, Object, Object]),
|
|
72
|
+
__metadata("design:returntype", Promise)
|
|
73
|
+
], CustomerAuthSettingsController.prototype, "write", null);
|
|
74
|
+
exports.CustomerAuthSettingsController = CustomerAuthSettingsController = __decorate([
|
|
75
|
+
(0, common_1.Controller)('api/admin/customer-auth/settings'),
|
|
76
|
+
(0, current_user_decorator_1.RequirePermission)('settings.customer-auth'),
|
|
77
|
+
__param(0, (0, common_1.Inject)(sign_in_settings_service_1.SignInSettingsService)),
|
|
78
|
+
__metadata("design:paramtypes", [sign_in_settings_service_1.SignInSettingsService])
|
|
79
|
+
], CustomerAuthSettingsController);
|
|
80
|
+
let CustomerAuthMethodsController = class CustomerAuthMethodsController {
|
|
81
|
+
settings;
|
|
82
|
+
cfg;
|
|
83
|
+
constructor(settings, cfg) {
|
|
84
|
+
this.settings = settings;
|
|
85
|
+
this.cfg = cfg;
|
|
86
|
+
}
|
|
87
|
+
/** Anonymous: which methods the sign-in page may offer; a disabled kind is `null`. 404 while the storefront is off. */
|
|
88
|
+
async methods(res) {
|
|
89
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
90
|
+
if (!this.cfg.storefront.enabled)
|
|
91
|
+
throw new errors_1.AuthError('not_found', 404, 'Not found');
|
|
92
|
+
const m = await this.settings.get();
|
|
93
|
+
const view = (k) => (m[k].enabled ? { otp: m[k].otp, password: m[k].password } : null);
|
|
94
|
+
return { email: view('email'), phone: view('phone') };
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
exports.CustomerAuthMethodsController = CustomerAuthMethodsController;
|
|
98
|
+
__decorate([
|
|
99
|
+
(0, common_1.Get)(),
|
|
100
|
+
(0, public_decorator_1.Public)(),
|
|
101
|
+
__param(0, (0, common_1.Res)({ passthrough: true })),
|
|
102
|
+
__metadata("design:type", Function),
|
|
103
|
+
__metadata("design:paramtypes", [Object]),
|
|
104
|
+
__metadata("design:returntype", Promise)
|
|
105
|
+
], CustomerAuthMethodsController.prototype, "methods", null);
|
|
106
|
+
exports.CustomerAuthMethodsController = CustomerAuthMethodsController = __decorate([
|
|
107
|
+
(0, common_1.Controller)('api/customer/auth/methods'),
|
|
108
|
+
__param(0, (0, common_1.Inject)(sign_in_settings_service_1.SignInSettingsService)),
|
|
109
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
110
|
+
__metadata("design:paramtypes", [sign_in_settings_service_1.SignInSettingsService, Object])
|
|
111
|
+
], CustomerAuthMethodsController);
|
|
112
|
+
//# sourceMappingURL=sign-in-settings.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sign-in-settings.controller.js","sourceRoot":"","sources":["../../src/customers/sign-in-settings.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8F;AAE9F,6BAAwB;AAExB,4CAA6C;AAC7C,sCAAsC;AACtC,4EAAmG;AACnG,gEAAmD;AACnD,mDAAiD;AACjD,yEAAuF;AAEvF,MAAM,IAAI,GAAG,OAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,OAAC,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,OAAC,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,OAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;AAClG,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,OAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;AAI5F,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACmB;IAA5D,YAA4D,QAA+B;QAA/B,aAAQ,GAAR,QAAQ,CAAuB;IAAG,CAAC;IAGzF,AAAN,KAAK,CAAC,IAAI,CAA6B,GAAa;QAClD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;QAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC;IAChD,CAAC;IAIK,AAAN,KAAK,CAAC,OAAO,CAAS,IAAa,EAA8B,GAAa;QAC5E,GAAG,CAAC,SAAS,CAAC,eA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AhBY,sEAA6B;AASlC;IAFL,IAAA,YAAG,GAAE;IACL,IAAA,yBAAM,GAAE;IACM,WAAA,IAAA,YAAG,EAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;;;;4DAMxC;wCAfU,6BAA6B;IADzC,IAAA,mBAAU,EAAC,2BAA2B,CAAC;IAGnC,WAAA,IAAA,eAAM,EAAC,gDAAqB,CAAC,CAAA;IAC7B,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;qCADoC,gDAAqB;GAFtE,6BAA6B,CAgBzC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { AuditService } from '../audit/audit.service';
|
|
3
|
+
import type { AuthTables } from '../db/types';
|
|
4
|
+
import { type OtpOptions } from '../otp/otp.types';
|
|
5
|
+
import type { IdentifierKind } from './identifier';
|
|
6
|
+
export type SignInMethods = Record<IdentifierKind, {
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
otp: boolean;
|
|
9
|
+
password: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const defaultSignInMethods: () => SignInMethods;
|
|
12
|
+
/** Admin-configured customer sign-in methods (single row `id = 1`; defaults when absent). Cached 30 s per instance. */
|
|
13
|
+
export declare class SignInSettingsService {
|
|
14
|
+
private readonly db;
|
|
15
|
+
private readonly otp;
|
|
16
|
+
private readonly env;
|
|
17
|
+
private readonly audit;
|
|
18
|
+
private cache?;
|
|
19
|
+
constructor(db: Kysely<AuthTables>, otp: OtpOptions, env: NodeJS.ProcessEnv, audit: AuditService);
|
|
20
|
+
get(): Promise<SignInMethods>;
|
|
21
|
+
assertAllowed(kind: IdentifierKind, method: 'otp' | 'password'): Promise<void>;
|
|
22
|
+
/** The kind is on (whatever its methods): adding an identifier of a kind that is off → 422 identifier_kind_disabled. */
|
|
23
|
+
assertKindEnabled(kind: IdentifierKind): Promise<void>;
|
|
24
|
+
put(methods: SignInMethods, actorId: string, meta?: {
|
|
25
|
+
ip?: string;
|
|
26
|
+
userAgent?: string;
|
|
27
|
+
}): Promise<SignInMethods>;
|
|
28
|
+
private readFresh;
|
|
29
|
+
/**
|
|
30
|
+
* Active customers who could no longer sign in with `methods`. A customer is covered when they have
|
|
31
|
+
* an identifier of an enabled kind with OTP on, or a verified identifier of an enabled kind with
|
|
32
|
+
* password on and a password set. Disabled customers are not counted (they cannot sign in anyway).
|
|
33
|
+
*/
|
|
34
|
+
affectedCustomers(methods: SignInMethods): Promise<number>;
|
|
35
|
+
}
|