@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,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.GroupsService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const audit_service_1 = require("../audit/audit.service");
|
|
18
|
+
const constants_1 = require("../constants");
|
|
19
|
+
const errors_1 = require("../errors");
|
|
20
|
+
const permissions_1 = require("../permissions");
|
|
21
|
+
/** Sorted, de-duplicated (spec B §3.3: stored sorted, no duplicates). */
|
|
22
|
+
const normalizeRoles = (roles) => [...new Set(roles)].sort();
|
|
23
|
+
/**
|
|
24
|
+
* Privilege escalation rule (plan Review Focus): a non-super-admin cannot grant a group a permission they do
|
|
25
|
+
* not hold themselves. Only newly added roles count; keeping or removing existing ones is not a grant.
|
|
26
|
+
*/
|
|
27
|
+
function assertCanGrant(actor, added) {
|
|
28
|
+
if (actor.isSuperAdmin)
|
|
29
|
+
return;
|
|
30
|
+
const missing = added.find((p) => !actor.permissions.includes(p));
|
|
31
|
+
if (missing)
|
|
32
|
+
throw new errors_1.AuthError('forbidden', 403, `Cannot grant a permission you do not hold: ${missing}`);
|
|
33
|
+
}
|
|
34
|
+
let GroupsService = class GroupsService {
|
|
35
|
+
db;
|
|
36
|
+
audit;
|
|
37
|
+
constructor(db, audit) {
|
|
38
|
+
this.db = db;
|
|
39
|
+
this.audit = audit;
|
|
40
|
+
}
|
|
41
|
+
async list(ids) {
|
|
42
|
+
let q = this.db
|
|
43
|
+
.selectFrom('auth_groups as g')
|
|
44
|
+
.leftJoin('auth_user_groups as ug', 'ug.group_id', 'g.id')
|
|
45
|
+
.select(['g.id', 'g.key', 'g.name', 'g.roles', 'g.system'])
|
|
46
|
+
.select((eb) => eb.fn.count('ug.user_id').as('user_count'))
|
|
47
|
+
.groupBy('g.id')
|
|
48
|
+
.orderBy('g.system', 'desc')
|
|
49
|
+
.orderBy('g.key');
|
|
50
|
+
if (ids)
|
|
51
|
+
q = q.where('g.id', 'in', ids);
|
|
52
|
+
const rows = await q.execute();
|
|
53
|
+
return rows.map((g) => ({
|
|
54
|
+
id: g.id,
|
|
55
|
+
key: g.key,
|
|
56
|
+
name: g.name,
|
|
57
|
+
roles: g.system ? [...permissions_1.PERMISSIONS] : g.roles,
|
|
58
|
+
system: g.system,
|
|
59
|
+
userCount: Number(g.user_count),
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
async view(id) {
|
|
63
|
+
const [v] = await this.list([id]);
|
|
64
|
+
if (!v)
|
|
65
|
+
throw new errors_1.AuthError('not_found', 404, 'Group not found');
|
|
66
|
+
return v;
|
|
67
|
+
}
|
|
68
|
+
async create(input, actor) {
|
|
69
|
+
const roles = normalizeRoles(input.roles);
|
|
70
|
+
assertCanGrant(actor, roles);
|
|
71
|
+
const id = await this.db.transaction().execute(async (trx) => {
|
|
72
|
+
let row;
|
|
73
|
+
try {
|
|
74
|
+
row = await trx
|
|
75
|
+
.insertInto('auth_groups')
|
|
76
|
+
.values({ key: input.key, name: input.name, roles, updated_at: new Date() })
|
|
77
|
+
.returning('id')
|
|
78
|
+
.executeTakeFirstOrThrow();
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
if (e.code === '23505')
|
|
82
|
+
throw new errors_1.AuthError('group_key_taken', 409, 'Group key already exists');
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
await this.audit.record({ type: 'group.create', actorId: actor.id, meta: { groupId: row.id, key: input.key, roles } }, trx);
|
|
86
|
+
return row.id;
|
|
87
|
+
});
|
|
88
|
+
return this.view(id);
|
|
89
|
+
}
|
|
90
|
+
async update(id, patch, actor) {
|
|
91
|
+
await this.db.transaction().execute(async (trx) => {
|
|
92
|
+
const g = await trx.selectFrom('auth_groups').select(['key', 'roles', 'system']).where('id', '=', id).forUpdate().executeTakeFirst();
|
|
93
|
+
if (!g)
|
|
94
|
+
throw new errors_1.AuthError('not_found', 404, 'Group not found');
|
|
95
|
+
if (g.system)
|
|
96
|
+
throw new errors_1.AuthError('system_group', 409, 'System groups cannot be changed');
|
|
97
|
+
const roles = patch.roles === undefined ? undefined : normalizeRoles(patch.roles);
|
|
98
|
+
if (roles)
|
|
99
|
+
assertCanGrant(actor, roles.filter((r) => !g.roles.includes(r)));
|
|
100
|
+
await trx
|
|
101
|
+
.updateTable('auth_groups')
|
|
102
|
+
.set({ ...(patch.name !== undefined ? { name: patch.name } : {}), ...(roles ? { roles } : {}), updated_at: new Date() })
|
|
103
|
+
.where('id', '=', id)
|
|
104
|
+
.execute();
|
|
105
|
+
await this.audit.record({
|
|
106
|
+
type: 'group.update',
|
|
107
|
+
actorId: actor.id,
|
|
108
|
+
meta: { groupId: id, key: g.key, ...(patch.name !== undefined ? { name: patch.name } : {}), ...(roles ? { before: g.roles, roles } : {}) },
|
|
109
|
+
}, trx);
|
|
110
|
+
});
|
|
111
|
+
return this.view(id);
|
|
112
|
+
}
|
|
113
|
+
/** Deletes a non-system group and its user links (members may end up with 0 groups, spec B §4.3). */
|
|
114
|
+
async delete(id, actor) {
|
|
115
|
+
await this.db.transaction().execute(async (trx) => {
|
|
116
|
+
const g = await trx.selectFrom('auth_groups').select(['key', 'system']).where('id', '=', id).forUpdate().executeTakeFirst();
|
|
117
|
+
if (!g)
|
|
118
|
+
throw new errors_1.AuthError('not_found', 404, 'Group not found');
|
|
119
|
+
if (g.system)
|
|
120
|
+
throw new errors_1.AuthError('system_group', 409, 'System groups cannot be deleted');
|
|
121
|
+
const removed = await trx.deleteFrom('auth_user_groups').where('group_id', '=', id).executeTakeFirst();
|
|
122
|
+
await trx.deleteFrom('auth_groups').where('id', '=', id).execute();
|
|
123
|
+
await this.audit.record({ type: 'group.delete', actorId: actor.id, meta: { groupId: id, key: g.key, userCount: Number(removed.numDeletedRows) } }, trx);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
exports.GroupsService = GroupsService;
|
|
128
|
+
exports.GroupsService = GroupsService = __decorate([
|
|
129
|
+
(0, common_1.Injectable)(),
|
|
130
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_DB)),
|
|
131
|
+
__param(1, (0, common_1.Inject)(audit_service_1.AuditService)),
|
|
132
|
+
__metadata("design:paramtypes", [Function, audit_service_1.AuditService])
|
|
133
|
+
], GroupsService);
|
|
134
|
+
//# sourceMappingURL=groups.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"groups.service.js","sourceRoot":"","sources":["../../src/groups/groups.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AAEpD,0DAAsD;AACtD,4CAAyC;AAEzC,sCAAsC;AACtC,gDAA8D;AAoB9D,yEAAyE;AACzE,MAAM,cAAc,GAAG,CAAC,KAA4B,EAAgB,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AAElG;;;GAGG;AACH,SAAS,cAAc,CAAC,KAAiB,EAAE,KAA4B;IACr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AAA,eAAM,EAAC,qBAAS,CAAC,CAAA;IACjB,WAAA,IAAA,eAAM,EAAC,4BAAY,CAAC,CAAA;+CAAyB,4BAAY;GAHjD,aAAa,CA4FzB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type CanActivate, type ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import type { OAuth2Config } from '../config/schema';
|
|
4
|
+
import { SetupState } from '../setup/setup-state';
|
|
5
|
+
import { CustomerAuthenticator } from './customer-authenticator';
|
|
6
|
+
import { SessionAuthenticator } from './session-auth.service';
|
|
7
|
+
/**
|
|
8
|
+
* Global guard (APP_GUARD) for `/api/**` (spec §6.5). Routes outside `/api` (host app, `/admin`, `/oauth2`
|
|
9
|
+
* Nest routes) pass untouched; `@Public()` handlers pass.
|
|
10
|
+
*
|
|
11
|
+
* `@CustomerAuth()` routes (any path, checked first) are authenticated by the customer realm only, through
|
|
12
|
+
* CustomerAuthenticator (cookie or bearer; Origin check for cookie writes only). Every other route keeps the admin
|
|
13
|
+
* behaviour, which a customer cookie or bearer token never satisfies.
|
|
14
|
+
*/
|
|
15
|
+
export declare class AuthGuard implements CanActivate {
|
|
16
|
+
private readonly reflector;
|
|
17
|
+
private readonly cfg;
|
|
18
|
+
private readonly publicPrefixes;
|
|
19
|
+
private readonly setup;
|
|
20
|
+
private readonly auth;
|
|
21
|
+
private readonly customerAuth;
|
|
22
|
+
constructor(reflector: Reflector, cfg: OAuth2Config, publicPrefixes: string[], setup: SetupState, auth: SessionAuthenticator, customerAuth: CustomerAuthenticator);
|
|
23
|
+
canActivate(ctx: ExecutionContext): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* The handler's own `@Public()` / `@CustomerAuth()` wins over the class's; on the same level `@CustomerAuth()` wins
|
|
26
|
+
* (the stricter reading). Neither → the admin path.
|
|
27
|
+
*/
|
|
28
|
+
private modeOf;
|
|
29
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
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.AuthGuard = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const constants_1 = require("../constants");
|
|
19
|
+
const errors_1 = require("../errors");
|
|
20
|
+
const origin_1 = require("../security/origin");
|
|
21
|
+
const setup_state_1 = require("../setup/setup-state");
|
|
22
|
+
const current_user_decorator_1 = require("./current-user.decorator");
|
|
23
|
+
const public_decorator_1 = require("./public.decorator");
|
|
24
|
+
const customer_auth_decorator_1 = require("./customer-auth.decorator");
|
|
25
|
+
const customer_authenticator_1 = require("./customer-authenticator");
|
|
26
|
+
const session_auth_service_1 = require("./session-auth.service");
|
|
27
|
+
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
28
|
+
/**
|
|
29
|
+
* Scope is decided from the matched route pattern (set by Express before guards run), lower-cased:
|
|
30
|
+
* Express routes case-insensitively, so `/API/x` reaches an `api/x` handler and must be guarded too.
|
|
31
|
+
*/
|
|
32
|
+
const scopePath = (req) => {
|
|
33
|
+
const route = req.route?.path;
|
|
34
|
+
return (typeof route === 'string' ? route : req.path ?? '').toLowerCase();
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Global guard (APP_GUARD) for `/api/**` (spec §6.5). Routes outside `/api` (host app, `/admin`, `/oauth2`
|
|
38
|
+
* Nest routes) pass untouched; `@Public()` handlers pass.
|
|
39
|
+
*
|
|
40
|
+
* `@CustomerAuth()` routes (any path, checked first) are authenticated by the customer realm only, through
|
|
41
|
+
* CustomerAuthenticator (cookie or bearer; Origin check for cookie writes only). Every other route keeps the admin
|
|
42
|
+
* behaviour, which a customer cookie or bearer token never satisfies.
|
|
43
|
+
*/
|
|
44
|
+
let AuthGuard = class AuthGuard {
|
|
45
|
+
reflector;
|
|
46
|
+
cfg;
|
|
47
|
+
publicPrefixes;
|
|
48
|
+
setup;
|
|
49
|
+
auth;
|
|
50
|
+
customerAuth;
|
|
51
|
+
constructor(reflector, cfg, publicPrefixes, setup, auth, customerAuth) {
|
|
52
|
+
this.reflector = reflector;
|
|
53
|
+
this.cfg = cfg;
|
|
54
|
+
this.publicPrefixes = publicPrefixes;
|
|
55
|
+
this.setup = setup;
|
|
56
|
+
this.auth = auth;
|
|
57
|
+
this.customerAuth = customerAuth;
|
|
58
|
+
}
|
|
59
|
+
async canActivate(ctx) {
|
|
60
|
+
if (ctx.getType() !== 'http')
|
|
61
|
+
return true;
|
|
62
|
+
const req = ctx.switchToHttp().getRequest();
|
|
63
|
+
const targets = [ctx.getHandler(), ctx.getClass()];
|
|
64
|
+
const mode = this.modeOf(ctx);
|
|
65
|
+
if (mode === 'customer') {
|
|
66
|
+
await this.customerAuth.authenticate(req, ctx.switchToHttp().getResponse());
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
const path = scopePath(req);
|
|
70
|
+
if (path !== '/api' && !path.startsWith('/api/'))
|
|
71
|
+
return true;
|
|
72
|
+
if (this.publicPrefixes.some((p) => path.startsWith(p)))
|
|
73
|
+
return true;
|
|
74
|
+
if (mode === 'public')
|
|
75
|
+
return true;
|
|
76
|
+
if (await this.setup.isRequired())
|
|
77
|
+
throw new errors_1.AuthError('setup_required', 409, 'Setup is required');
|
|
78
|
+
// Step 1: Origin on writes, before touching the session.
|
|
79
|
+
if (!SAFE_METHODS.has(req.method.toUpperCase())) {
|
|
80
|
+
const origin = req.headers.origin;
|
|
81
|
+
(0, origin_1.assertSameOrigin)(Array.isArray(origin) ? origin[0] : origin, this.cfg.publicUrl);
|
|
82
|
+
}
|
|
83
|
+
const user = await this.auth.authenticate(req, ctx.switchToHttp().getResponse());
|
|
84
|
+
// Class and method requirements merge (the method adds to the class); de-duplicated, order kept.
|
|
85
|
+
const perms = new Set(this.reflector.getAllAndMerge(current_user_decorator_1.REQUIRED_PERMISSIONS_KEY, targets) ?? []);
|
|
86
|
+
for (const p of perms) {
|
|
87
|
+
if (!user.permissions.includes(p))
|
|
88
|
+
throw new errors_1.AuthError('forbidden', 403, `Missing permission: ${p}`);
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The handler's own `@Public()` / `@CustomerAuth()` wins over the class's; on the same level `@CustomerAuth()` wins
|
|
94
|
+
* (the stricter reading). Neither → the admin path.
|
|
95
|
+
*/
|
|
96
|
+
modeOf(ctx) {
|
|
97
|
+
for (const target of [ctx.getHandler(), ctx.getClass()]) {
|
|
98
|
+
if (this.reflector.get(customer_auth_decorator_1.CUSTOMER_AUTH_KEY, target))
|
|
99
|
+
return 'customer';
|
|
100
|
+
if (this.reflector.get(public_decorator_1.IS_PUBLIC_KEY, target))
|
|
101
|
+
return 'public';
|
|
102
|
+
}
|
|
103
|
+
return 'admin';
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
exports.AuthGuard = AuthGuard;
|
|
107
|
+
exports.AuthGuard = AuthGuard = __decorate([
|
|
108
|
+
(0, common_1.Injectable)(),
|
|
109
|
+
__param(0, (0, common_1.Inject)(core_1.Reflector)),
|
|
110
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
111
|
+
__param(2, (0, common_1.Inject)(constants_1.PUBLIC_PATH_PREFIXES)),
|
|
112
|
+
__param(3, (0, common_1.Inject)(setup_state_1.SetupState)),
|
|
113
|
+
__param(4, (0, common_1.Inject)(session_auth_service_1.SessionAuthenticator)),
|
|
114
|
+
__param(5, (0, common_1.Inject)(customer_authenticator_1.CustomerAuthenticator)),
|
|
115
|
+
__metadata("design:paramtypes", [core_1.Reflector, Object, Array, setup_state_1.SetupState,
|
|
116
|
+
session_auth_service_1.SessionAuthenticator,
|
|
117
|
+
customer_authenticator_1.CustomerAuthenticator])
|
|
118
|
+
], AuthGuard);
|
|
119
|
+
//# sourceMappingURL=auth.guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../../src/guard/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA6F;AAC7F,uCAAyC;AAGzC,4CAAmE;AACnE,sCAAsC;AACtC,+CAAsD;AACtD,sDAAkD;AAClD,qEAAsF;AAEtF,yDAAmD;AACnD,uEAA8D;AAC9D,qEAAiE;AACjE,iEAAyF;AAEzF,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;AAEzD;;;GAGG;AACH,MAAM,SAAS,GAAG,CAAC,GAAyB,EAAU,EAAE;IACtD,MAAM,KAAK,GAAI,GAAsC,CAAC,KAAK,EAAE,IAAI,CAAC;IAClE,OAAO,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;AAC5E,CAAC,CAA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AS,CAAC,GAAG,CAAU,2CAAiB,EAAE,MAAM,CAAC;gBAAE,OAAO,UAAU,CAAC;YAC9E,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAU,gCAAa,EAAE,MAAM,CAAC;gBAAE,OAAO,QAAQ,CAAC;QAC1E,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF,CAAA;AAtDY,8BAAS;oBAAT,SAAS;IADrB,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,gBAAS,CAAC,CAAA;IACjB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,eAAM,EAAC,gCAAoB,CAAC,CAAA;IAC5B,WAAA,IAAA,eAAM,EAAC,wBAAU,CAAC,CAAA;IAClB,WAAA,IAAA,eAAM,EAAC,2CAAoB,CAAC,CAAA;IAC5B,WAAA,IAAA,eAAM,EAAC,8CAAqB,CAAC,CAAA;qCALiB,gBAAS,iBAGZ,wBAAU;QACD,2CAAoB;QACX,8CAAqB;GAP1E,SAAS,CAsDrB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Locale } from '@opencloudcms/schema';
|
|
2
|
+
import type { Permission } from '../permissions';
|
|
3
|
+
/** What the guard attaches as `request.user` (spec §6.5 step 6, spec B §4.1). */
|
|
4
|
+
export interface RequestUser {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
name: string;
|
|
8
|
+
/** Admin UI language; null = not chosen. */
|
|
9
|
+
locale: Locale | null;
|
|
10
|
+
/** Groups the user belongs to, sorted by key. */
|
|
11
|
+
groups: {
|
|
12
|
+
key: string;
|
|
13
|
+
name: string;
|
|
14
|
+
}[];
|
|
15
|
+
/** Effective permissions, computed from the groups on every request (spec B §3.3, B10). */
|
|
16
|
+
permissions: Permission[];
|
|
17
|
+
/** Member of a `system` group (Super Admin). */
|
|
18
|
+
isSuperAdmin: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Attached as `request.authSession`: identifies the BFF session of the request (e.g. for revokeAll except it). */
|
|
21
|
+
export interface RequestAuthSession {
|
|
22
|
+
idHash: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const REQUIRED_PERMISSIONS_KEY = "oauth2:requiredPermissions";
|
|
25
|
+
/**
|
|
26
|
+
* The handler (or every handler of the class) requires **all** of the listed permissions; checked by AuthGuard →
|
|
27
|
+
* `403 forbidden` "Missing permission: <perm>" (spec B §4.1). Class-level and method-level requirements are
|
|
28
|
+
* merged (the method adds to the class, it never replaces it), so a method cannot loosen its controller.
|
|
29
|
+
* At least one permission is required at compile time; a route that needs only a session has no decorator.
|
|
30
|
+
*/
|
|
31
|
+
export declare const RequirePermission: (first: Permission, ...rest: Permission[]) => MethodDecorator & ClassDecorator;
|
|
32
|
+
/** `request.user` as set by AuthGuard. Undefined on public routes. */
|
|
33
|
+
export declare const CurrentUser: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CurrentUser = exports.RequirePermission = exports.REQUIRED_PERMISSIONS_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.REQUIRED_PERMISSIONS_KEY = 'oauth2:requiredPermissions';
|
|
6
|
+
/**
|
|
7
|
+
* The handler (or every handler of the class) requires **all** of the listed permissions; checked by AuthGuard →
|
|
8
|
+
* `403 forbidden` "Missing permission: <perm>" (spec B §4.1). Class-level and method-level requirements are
|
|
9
|
+
* merged (the method adds to the class, it never replaces it), so a method cannot loosen its controller.
|
|
10
|
+
* At least one permission is required at compile time; a route that needs only a session has no decorator.
|
|
11
|
+
*/
|
|
12
|
+
const RequirePermission = (first, ...rest) => (0, common_1.SetMetadata)(exports.REQUIRED_PERMISSIONS_KEY, [first, ...rest]);
|
|
13
|
+
exports.RequirePermission = RequirePermission;
|
|
14
|
+
/** `request.user` as set by AuthGuard. Undefined on public routes. */
|
|
15
|
+
exports.CurrentUser = (0, common_1.createParamDecorator)((_data, ctx) => ctx.switchToHttp().getRequest().user);
|
|
16
|
+
//# sourceMappingURL=current-user.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-user.decorator.js","sourceRoot":"","sources":["../../src/guard/current-user.decorator.ts"],"names":[],"mappings":";;;AACA,2CAA0F;AAuB7E,QAAA,wBAAwB,GAAG,4BAA4B,CAAC;AAErE;;;;;GAKG;AACI,MAAM,iBAAiB,GAAG,CAAC,KAAiB,EAAE,GAAG,IAAkB,EAAoC,EAAE,CAC9G,IAAA,oBAAW,EAAC,gCAAwB,EAAE,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;AAD7C,QAAA,iBAAiB,qBAC4B;AAE1D,sEAAsE;AACzD,QAAA,WAAW,GAAG,IAAA,6BAAoB,EAC7C,CAAC,KAAc,EAAE,GAAqB,EAA2B,EAAE,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC,IAAI,CACzG,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { IdentifierKind } from '../customers/identifier';
|
|
2
|
+
/** What the guard attaches as `request.customer` on a `@CustomerAuth()` route (spec goal 9). */
|
|
3
|
+
export interface RequestCustomer {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string | null;
|
|
6
|
+
identifiers: {
|
|
7
|
+
id: string;
|
|
8
|
+
kind: IdentifierKind;
|
|
9
|
+
value: string;
|
|
10
|
+
verified: boolean;
|
|
11
|
+
}[];
|
|
12
|
+
/** How the request was authenticated: the site's `oc_customer` BFF cookie or an `Authorization: Bearer` token. */
|
|
13
|
+
via: 'cookie' | 'bearer';
|
|
14
|
+
/** The OAuth client the credential was issued to (`opencloudcms-storefront` for the cookie). */
|
|
15
|
+
clientId: string;
|
|
16
|
+
}
|
|
17
|
+
export declare const CUSTOMER_AUTH_KEY = "oauth2:customerAuth";
|
|
18
|
+
/**
|
|
19
|
+
* The handler (or every handler of the class) is authenticated by the customer realm only: the `oc_customer`
|
|
20
|
+
* cookie or a customer-realm bearer access token. An admin session never satisfies it, and a customer credential
|
|
21
|
+
* never satisfies a route without it. Applies on any path, also outside `/api`. A method-level `@Public()` /
|
|
22
|
+
* `@CustomerAuth()` overrides the class-level one.
|
|
23
|
+
*/
|
|
24
|
+
export declare const CustomerAuth: () => MethodDecorator & ClassDecorator;
|
|
25
|
+
/** `request.customer` as set by AuthGuard on a `@CustomerAuth()` route. */
|
|
26
|
+
export declare const CurrentCustomer: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CurrentCustomer = exports.CustomerAuth = exports.CUSTOMER_AUTH_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.CUSTOMER_AUTH_KEY = 'oauth2:customerAuth';
|
|
6
|
+
/**
|
|
7
|
+
* The handler (or every handler of the class) is authenticated by the customer realm only: the `oc_customer`
|
|
8
|
+
* cookie or a customer-realm bearer access token. An admin session never satisfies it, and a customer credential
|
|
9
|
+
* never satisfies a route without it. Applies on any path, also outside `/api`. A method-level `@Public()` /
|
|
10
|
+
* `@CustomerAuth()` overrides the class-level one.
|
|
11
|
+
*/
|
|
12
|
+
const CustomerAuth = () => (0, common_1.SetMetadata)(exports.CUSTOMER_AUTH_KEY, true);
|
|
13
|
+
exports.CustomerAuth = CustomerAuth;
|
|
14
|
+
/** `request.customer` as set by AuthGuard on a `@CustomerAuth()` route. */
|
|
15
|
+
exports.CurrentCustomer = (0, common_1.createParamDecorator)((_data, ctx) => ctx.switchToHttp().getRequest().customer);
|
|
16
|
+
//# sourceMappingURL=customer-auth.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer-auth.decorator.js","sourceRoot":"","sources":["../../src/guard/customer-auth.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAA0F;AAc7E,QAAA,iBAAiB,GAAG,qBAAqB,CAAC;AAEvD;;;;;GAKG;AACI,MAAM,YAAY,GAAG,GAAqC,EAAE,CAAC,IAAA,oBAAW,EAAC,yBAAiB,EAAE,IAAI,CAAC,CAAC;AAA5F,QAAA,YAAY,gBAAgF;AAEzG,2EAA2E;AAC9D,QAAA,eAAe,GAAG,IAAA,6BAAoB,EACjD,CAAC,KAAc,EAAE,GAAqB,EAA+B,EAAE,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC,QAAQ,CACjH,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { OidcClient } from '../bff/oidc-client';
|
|
3
|
+
import type { SessionStore } from '../bff/session.store';
|
|
4
|
+
import type { OAuth2Config } from '../config/schema';
|
|
5
|
+
import { CustomerService, type Customer } from '../customers/customer.service';
|
|
6
|
+
import { ProviderService } from '../provider/provider.service';
|
|
7
|
+
import type { GrantRevoker } from '../users/user.service';
|
|
8
|
+
import type { RequestCustomer } from './customer-auth.decorator';
|
|
9
|
+
/** What authenticated the request, attached as `request.customerAuth` next to `request.customer`. */
|
|
10
|
+
export type RequestCustomerAuth = {
|
|
11
|
+
via: 'cookie';
|
|
12
|
+
idHash: string;
|
|
13
|
+
} | {
|
|
14
|
+
via: 'bearer';
|
|
15
|
+
grantId: string;
|
|
16
|
+
clientId: string;
|
|
17
|
+
};
|
|
18
|
+
export type CustomerRequest = Request & {
|
|
19
|
+
customer?: RequestCustomer;
|
|
20
|
+
customerAuth?: RequestCustomerAuth;
|
|
21
|
+
};
|
|
22
|
+
export declare function toRequestCustomer(c: Customer, via: RequestCustomer['via'], clientId: string): RequestCustomer;
|
|
23
|
+
/**
|
|
24
|
+
* Customer-realm authentication (spec "Guards", goals 8–9), used by AuthGuard for `@CustomerAuth()` routes:
|
|
25
|
+
*
|
|
26
|
+
* - **Bearer**: an opaque access token looked up with `provider.AccessToken.find`; it must be valid (unexpired),
|
|
27
|
+
* issued to a customer-realm client, not sender-constrained, its Grant must still exist for the same account and
|
|
28
|
+
* client, and the customer must be active (a disabled customer's grant is revoked). Not CSRF-able: no Origin check.
|
|
29
|
+
* - **Cookie** (`oc_customer`, the site's BFF session): Origin check on writes before the session is read; the same
|
|
30
|
+
* idle/absolute expiry and access-token refresh as the admin path. A session that ends (expired, customer gone or
|
|
31
|
+
* disabled, refresh failed) is deleted together with the customer's provider sessions.
|
|
32
|
+
*
|
|
33
|
+
* Every 401 is `unauthenticated` (the cookie path also clears `oc_customer`). With the storefront disabled the
|
|
34
|
+
* customer realm does not exist: `404 not_found`. Sets `req.customer` / `req.customerAuth`; never `req.user`.
|
|
35
|
+
*/
|
|
36
|
+
export declare class CustomerAuthenticator {
|
|
37
|
+
private readonly cfg;
|
|
38
|
+
private readonly sessions;
|
|
39
|
+
private readonly customers;
|
|
40
|
+
private readonly providers;
|
|
41
|
+
private readonly revoker;
|
|
42
|
+
private readonly logger;
|
|
43
|
+
private readonly secure;
|
|
44
|
+
private readonly refresher;
|
|
45
|
+
constructor(cfg: OAuth2Config, sessions: SessionStore, oidc: OidcClient, customers: CustomerService, providers: ProviderService, revoker: GrantRevoker);
|
|
46
|
+
authenticate(req: Request, res: Response): Promise<RequestCustomer>;
|
|
47
|
+
/**
|
|
48
|
+
* For routes where a customer is optional: undefined when the request carries no usable customer credential
|
|
49
|
+
* (none, unknown, expired, disabled customer, storefront off). A cookie write from a foreign Origin still
|
|
50
|
+
* answers 403; other errors propagate.
|
|
51
|
+
*/
|
|
52
|
+
tryAuthenticate(req: Request, res: Response): Promise<RequestCustomer | undefined>;
|
|
53
|
+
clearCookie(res: Response): void;
|
|
54
|
+
private fromBearer;
|
|
55
|
+
private fromCookie;
|
|
56
|
+
/** Error name only: messages may carry token-endpoint responses. */
|
|
57
|
+
private warn;
|
|
58
|
+
}
|