@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,54 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import type { Request, Response } from 'express';
|
|
3
|
+
import { AuditService } from '../audit/audit.service';
|
|
4
|
+
import type { OidcClient } from '../bff/oidc-client';
|
|
5
|
+
import type { SessionStore } from '../bff/session.store';
|
|
6
|
+
import type { OAuth2Config } from '../config/schema';
|
|
7
|
+
import type { AuthTables } from '../db/types';
|
|
8
|
+
import { type RequestCustomer } from '../guard/customer-auth.decorator';
|
|
9
|
+
import type { CustomerRequest } from '../guard/customer-authenticator';
|
|
10
|
+
import { ProviderService } from '../provider/provider.service';
|
|
11
|
+
import type { GrantRevoker } from '../users/user.service';
|
|
12
|
+
import { AuthHookRunner } from './auth-hooks';
|
|
13
|
+
import { CustomerService } from './customer.service';
|
|
14
|
+
/**
|
|
15
|
+
* The site's customer BFF (spec goal 7): the same design as the admin BFF (`/api/auth`), as the confidential
|
|
16
|
+
* client `opencloudcms-storefront`, with its own cookies (`oc_customer_login`, `oc_customer`) and table
|
|
17
|
+
* (`auth_customer_sessions`). No token reaches the browser. `login` / `callback` are `@Public()`; `me` / `logout` are
|
|
18
|
+
* `@CustomerAuth()` (the site's cookie or a mobile/web bearer token). All routes answer 404 while the storefront is off.
|
|
19
|
+
*/
|
|
20
|
+
export declare class CustomerAuthController {
|
|
21
|
+
private readonly cfg;
|
|
22
|
+
private readonly db;
|
|
23
|
+
private readonly oidc;
|
|
24
|
+
private readonly sessions;
|
|
25
|
+
private readonly customers;
|
|
26
|
+
private readonly hooks;
|
|
27
|
+
private readonly audit;
|
|
28
|
+
private readonly revoker;
|
|
29
|
+
private readonly providers;
|
|
30
|
+
private readonly logger;
|
|
31
|
+
private readonly cookieKey;
|
|
32
|
+
private readonly secure;
|
|
33
|
+
constructor(cfg: OAuth2Config, db: Kysely<AuthTables>, oidc: OidcClient, sessions: SessionStore, customers: CustomerService, hooks: AuthHookRunner, audit: AuditService, revoker: GrantRevoker, providers: ProviderService);
|
|
34
|
+
login(req: Request, res: Response): Promise<void>;
|
|
35
|
+
callback(req: Request, res: Response): Promise<void>;
|
|
36
|
+
me(customer: RequestCustomer, res: Response): Pick<RequestCustomer, 'id' | 'name' | 'identifiers'>;
|
|
37
|
+
/**
|
|
38
|
+
* Cookie (the site): revokes the session's refresh token, deletes the session row and clears `oc_customer`.
|
|
39
|
+
* Bearer (mobile/web): revokes the token's grant (its refresh tokens then fail). Both end the customer's provider
|
|
40
|
+
* sessions and fire `afterSignOut` with the credential's client id. Origin is checked by the guard (cookie only).
|
|
41
|
+
*/
|
|
42
|
+
logout(req: CustomerRequest, current: RequestCustomer, res: Response): Promise<{
|
|
43
|
+
redirectTo: string;
|
|
44
|
+
}>;
|
|
45
|
+
/** Deletes the session behind an incoming `oc_customer` cookie (and revokes its refresh token), if any. */
|
|
46
|
+
private endPreviousSession;
|
|
47
|
+
private revokeRefreshToken;
|
|
48
|
+
private assertEnabled;
|
|
49
|
+
private clearCookie;
|
|
50
|
+
private loginError;
|
|
51
|
+
/** Error name only: messages may echo request data or token-endpoint answers. */
|
|
52
|
+
private fail;
|
|
53
|
+
private readLoginState;
|
|
54
|
+
}
|
|
@@ -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.CustomerAuthController = void 0;
|
|
16
|
+
const node_crypto_1 = require("node:crypto");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const audit_service_1 = require("../audit/audit.service");
|
|
19
|
+
const cookies_1 = require("../bff/cookies");
|
|
20
|
+
const constants_1 = require("../constants");
|
|
21
|
+
const errors_1 = require("../errors");
|
|
22
|
+
const customer_auth_decorator_1 = require("../guard/customer-auth.decorator");
|
|
23
|
+
const public_decorator_1 = require("../guard/public.decorator");
|
|
24
|
+
const provider_service_1 = require("../provider/provider.service");
|
|
25
|
+
const crypto_1 = require("../security/crypto");
|
|
26
|
+
const return_to_1 = require("../security/return-to");
|
|
27
|
+
const auth_hooks_1 = require("./auth-hooks");
|
|
28
|
+
const customer_service_1 = require("./customer.service");
|
|
29
|
+
const LOGIN_COOKIE_PATH = '/api/customer/auth';
|
|
30
|
+
const LOGIN_COOKIE_MAX_AGE = 600;
|
|
31
|
+
const random = () => (0, node_crypto_1.randomBytes)(32).toString('base64url');
|
|
32
|
+
const first = (v) => typeof v === 'string' ? v : Array.isArray(v) && typeof v[0] === 'string' ? v[0] : undefined;
|
|
33
|
+
/** Ruling R2: customer interactions finish with `amr: ['otp'] | ['pwd']`. */
|
|
34
|
+
const methodOf = (amr) => (amr?.includes('pwd') ? 'password' : 'otp');
|
|
35
|
+
/**
|
|
36
|
+
* The site's customer BFF (spec goal 7): the same design as the admin BFF (`/api/auth`), as the confidential
|
|
37
|
+
* client `opencloudcms-storefront`, with its own cookies (`oc_customer_login`, `oc_customer`) and table
|
|
38
|
+
* (`auth_customer_sessions`). No token reaches the browser. `login` / `callback` are `@Public()`; `me` / `logout` are
|
|
39
|
+
* `@CustomerAuth()` (the site's cookie or a mobile/web bearer token). All routes answer 404 while the storefront is off.
|
|
40
|
+
*/
|
|
41
|
+
let CustomerAuthController = class CustomerAuthController {
|
|
42
|
+
cfg;
|
|
43
|
+
db;
|
|
44
|
+
oidc;
|
|
45
|
+
sessions;
|
|
46
|
+
customers;
|
|
47
|
+
hooks;
|
|
48
|
+
audit;
|
|
49
|
+
revoker;
|
|
50
|
+
providers;
|
|
51
|
+
logger = new common_1.Logger('OAuth2');
|
|
52
|
+
cookieKey;
|
|
53
|
+
secure;
|
|
54
|
+
constructor(cfg, db, oidc, sessions, customers, hooks, audit, revoker, providers) {
|
|
55
|
+
this.cfg = cfg;
|
|
56
|
+
this.db = db;
|
|
57
|
+
this.oidc = oidc;
|
|
58
|
+
this.sessions = sessions;
|
|
59
|
+
this.customers = customers;
|
|
60
|
+
this.hooks = hooks;
|
|
61
|
+
this.audit = audit;
|
|
62
|
+
this.revoker = revoker;
|
|
63
|
+
this.providers = providers;
|
|
64
|
+
this.cookieKey = (0, crypto_1.deriveKey)(cfg.secret, 'cookie');
|
|
65
|
+
this.secure = (0, cookies_1.isSecureUrl)(cfg.publicUrl);
|
|
66
|
+
}
|
|
67
|
+
async login(req, res) {
|
|
68
|
+
this.assertEnabled();
|
|
69
|
+
const st = {
|
|
70
|
+
state: random(),
|
|
71
|
+
nonce: random(),
|
|
72
|
+
verifier: random(),
|
|
73
|
+
returnTo: (0, return_to_1.sanitizeCustomerReturnTo)(first(req.query.returnTo)),
|
|
74
|
+
};
|
|
75
|
+
const codeChallenge = (0, node_crypto_1.createHash)('sha256').update(st.verifier).digest('base64url');
|
|
76
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
77
|
+
let url;
|
|
78
|
+
try {
|
|
79
|
+
// prompt=login always (OidcClient): a provider session of either realm never signs the browser in silently.
|
|
80
|
+
url = await this.oidc.buildLoginUrl({ state: st.state, nonce: st.nonce, codeChallenge });
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
this.fail('Customer BFF login: discovery failed', e);
|
|
84
|
+
return res.redirect(302, this.loginError('login_failed'));
|
|
85
|
+
}
|
|
86
|
+
res.append('Set-Cookie', (0, cookies_1.serializeCookie)(constants_1.CUSTOMER_LOGIN_COOKIE, (0, crypto_1.seal)(this.cookieKey, JSON.stringify(st)), {
|
|
87
|
+
path: LOGIN_COOKIE_PATH,
|
|
88
|
+
maxAge: LOGIN_COOKIE_MAX_AGE,
|
|
89
|
+
secure: this.secure,
|
|
90
|
+
}));
|
|
91
|
+
res.redirect(302, url.toString());
|
|
92
|
+
}
|
|
93
|
+
async callback(req, res) {
|
|
94
|
+
this.assertEnabled();
|
|
95
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
96
|
+
// oc_customer_login is single-use whatever the outcome.
|
|
97
|
+
res.append('Set-Cookie', (0, cookies_1.serializeCookie)(constants_1.CUSTOMER_LOGIN_COOKIE, '', { path: LOGIN_COOKIE_PATH, maxAge: 0, secure: this.secure }));
|
|
98
|
+
const st = this.readLoginState((0, cookies_1.parseCookies)(req.headers.cookie)[constants_1.CUSTOMER_LOGIN_COOKIE]);
|
|
99
|
+
if (!st || first(req.query.state) !== st.state)
|
|
100
|
+
return res.redirect(302, this.loginError('invalid_state'));
|
|
101
|
+
const providerError = first(req.query.error);
|
|
102
|
+
if (providerError !== undefined) {
|
|
103
|
+
return res.redirect(302, this.loginError(/^[a-z_]+$/.test(providerError) ? providerError : 'login_failed'));
|
|
104
|
+
}
|
|
105
|
+
// Rebuild the public callback URL rather than trusting Host (see the admin BFF).
|
|
106
|
+
const currentUrl = new URL(this.oidc.redirectUri);
|
|
107
|
+
currentUrl.search = new URL(req.originalUrl ?? req.url, 'http://internal').search;
|
|
108
|
+
let tokens;
|
|
109
|
+
try {
|
|
110
|
+
tokens = await this.oidc.exchange(currentUrl, { verifier: st.verifier, state: st.state, nonce: st.nonce });
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
this.fail('Customer BFF code exchange failed', e);
|
|
114
|
+
return res.redirect(302, this.loginError('login_failed'));
|
|
115
|
+
}
|
|
116
|
+
const ip = req.ip;
|
|
117
|
+
const userAgent = first(req.headers['user-agent']);
|
|
118
|
+
let customer;
|
|
119
|
+
let sessionCookie;
|
|
120
|
+
try {
|
|
121
|
+
customer = await this.customers.findById(tokens.sub);
|
|
122
|
+
// Disabled between the interaction and the callback: no session.
|
|
123
|
+
if (!customer || customer.status !== 'active') {
|
|
124
|
+
if (tokens.refreshToken)
|
|
125
|
+
await this.oidc.revoke(tokens.refreshToken).catch(() => { });
|
|
126
|
+
await this.revoker.destroyProviderSessions(tokens.sub);
|
|
127
|
+
return res.redirect(302, this.loginError('account_disabled'));
|
|
128
|
+
}
|
|
129
|
+
// No stale session survives a new sign-in in this browser (same or another customer).
|
|
130
|
+
await this.endPreviousSession((0, cookies_1.parseCookies)(req.headers.cookie)[constants_1.CUSTOMER_SESSION_COOKIE]);
|
|
131
|
+
const { cookieValue } = await this.sessions.create(customer.id, tokens, { ip, userAgent });
|
|
132
|
+
sessionCookie = (0, cookies_1.serializeCookie)(constants_1.CUSTOMER_SESSION_COOKIE, cookieValue, {
|
|
133
|
+
path: '/',
|
|
134
|
+
maxAge: this.cfg.storefront.ttl.sessionAbsolute,
|
|
135
|
+
secure: this.secure,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
this.fail('Customer BFF session creation failed', e);
|
|
140
|
+
return res.redirect(302, this.loginError('login_failed'));
|
|
141
|
+
}
|
|
142
|
+
// Fires here, once per site sign-in (the interaction itself does not fire it).
|
|
143
|
+
await this.hooks.after('afterSignIn', customer, {
|
|
144
|
+
db: this.db,
|
|
145
|
+
ip,
|
|
146
|
+
userAgent,
|
|
147
|
+
method: methodOf(tokens.amr),
|
|
148
|
+
clientId: constants_1.STOREFRONT_CLIENT_ID,
|
|
149
|
+
});
|
|
150
|
+
res.append('Set-Cookie', sessionCookie);
|
|
151
|
+
res.redirect(302, st.returnTo);
|
|
152
|
+
}
|
|
153
|
+
me(customer, res) {
|
|
154
|
+
this.assertEnabled();
|
|
155
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
156
|
+
return { id: customer.id, name: customer.name, identifiers: customer.identifiers };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Cookie (the site): revokes the session's refresh token, deletes the session row and clears `oc_customer`.
|
|
160
|
+
* Bearer (mobile/web): revokes the token's grant (its refresh tokens then fail). Both end the customer's provider
|
|
161
|
+
* sessions and fire `afterSignOut` with the credential's client id. Origin is checked by the guard (cookie only).
|
|
162
|
+
*/
|
|
163
|
+
async logout(req, current, res) {
|
|
164
|
+
this.assertEnabled();
|
|
165
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
166
|
+
const auth = req.customerAuth;
|
|
167
|
+
if (auth.via === 'cookie') {
|
|
168
|
+
const session = await this.sessions.getByHash(auth.idHash).catch(() => undefined);
|
|
169
|
+
if (session?.refreshToken)
|
|
170
|
+
await this.revokeRefreshToken(session.refreshToken);
|
|
171
|
+
await this.sessions.delete(auth.idHash);
|
|
172
|
+
this.clearCookie(res);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
await this.providers.revokeGrant(auth.grantId);
|
|
176
|
+
}
|
|
177
|
+
// Provider sessions are keyed by account id; a customer id never equals an admin user id (separate tables, uuids).
|
|
178
|
+
await this.revoker.destroyProviderSessions(current.id);
|
|
179
|
+
const ip = req.ip;
|
|
180
|
+
const userAgent = first(req.headers['user-agent']);
|
|
181
|
+
await this.audit.record({ type: 'customer.logout', ip, userAgent, meta: { customerId: current.id, clientId: current.clientId } });
|
|
182
|
+
const customer = await this.customers.findById(current.id);
|
|
183
|
+
if (customer)
|
|
184
|
+
await this.hooks.after('afterSignOut', customer, { db: this.db, ip, userAgent, clientId: current.clientId });
|
|
185
|
+
return { redirectTo: '/' };
|
|
186
|
+
}
|
|
187
|
+
/** Deletes the session behind an incoming `oc_customer` cookie (and revokes its refresh token), if any. */
|
|
188
|
+
async endPreviousSession(cookie) {
|
|
189
|
+
if (!cookie)
|
|
190
|
+
return;
|
|
191
|
+
const idHash = (0, crypto_1.sha256)(cookie);
|
|
192
|
+
const previous = await this.sessions.getByHash(idHash).catch(() => undefined);
|
|
193
|
+
if (previous?.refreshToken)
|
|
194
|
+
await this.revokeRefreshToken(previous.refreshToken);
|
|
195
|
+
await this.sessions.delete(idHash);
|
|
196
|
+
}
|
|
197
|
+
async revokeRefreshToken(refreshToken) {
|
|
198
|
+
try {
|
|
199
|
+
await this.oidc.revoke(refreshToken);
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
this.fail('Customer refresh token revocation failed', e);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
assertEnabled() {
|
|
206
|
+
if (!this.cfg.storefront.enabled)
|
|
207
|
+
throw new errors_1.AuthError('not_found', 404, 'Not found');
|
|
208
|
+
}
|
|
209
|
+
clearCookie(res) {
|
|
210
|
+
res.append('Set-Cookie', (0, cookies_1.serializeCookie)(constants_1.CUSTOMER_SESSION_COOKIE, '', { path: '/', maxAge: 0, secure: this.secure }));
|
|
211
|
+
}
|
|
212
|
+
loginError(code) {
|
|
213
|
+
return `${this.cfg.storefront.loginPath}?error=${code}`;
|
|
214
|
+
}
|
|
215
|
+
/** Error name only: messages may echo request data or token-endpoint answers. */
|
|
216
|
+
fail(what, e) {
|
|
217
|
+
this.logger.warn(`${what}: ${e?.name ?? 'Error'}`);
|
|
218
|
+
}
|
|
219
|
+
readLoginState(sealed) {
|
|
220
|
+
if (!sealed)
|
|
221
|
+
return undefined;
|
|
222
|
+
try {
|
|
223
|
+
const v = JSON.parse((0, crypto_1.open)(this.cookieKey, sealed));
|
|
224
|
+
if (typeof v.state !== 'string' || typeof v.nonce !== 'string' || typeof v.verifier !== 'string')
|
|
225
|
+
return undefined;
|
|
226
|
+
return { state: v.state, nonce: v.nonce, verifier: v.verifier, returnTo: (0, return_to_1.sanitizeCustomerReturnTo)(v.returnTo) };
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
exports.CustomerAuthController = CustomerAuthController;
|
|
234
|
+
__decorate([
|
|
235
|
+
(0, public_decorator_1.Public)(),
|
|
236
|
+
(0, common_1.Get)('login'),
|
|
237
|
+
__param(0, (0, common_1.Req)()),
|
|
238
|
+
__param(1, (0, common_1.Res)()),
|
|
239
|
+
__metadata("design:type", Function),
|
|
240
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
241
|
+
__metadata("design:returntype", Promise)
|
|
242
|
+
], CustomerAuthController.prototype, "login", null);
|
|
243
|
+
__decorate([
|
|
244
|
+
(0, public_decorator_1.Public)(),
|
|
245
|
+
(0, common_1.Get)('callback'),
|
|
246
|
+
__param(0, (0, common_1.Req)()),
|
|
247
|
+
__param(1, (0, common_1.Res)()),
|
|
248
|
+
__metadata("design:type", Function),
|
|
249
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
250
|
+
__metadata("design:returntype", Promise)
|
|
251
|
+
], CustomerAuthController.prototype, "callback", null);
|
|
252
|
+
__decorate([
|
|
253
|
+
(0, customer_auth_decorator_1.CustomerAuth)(),
|
|
254
|
+
(0, common_1.Get)('me'),
|
|
255
|
+
__param(0, (0, customer_auth_decorator_1.CurrentCustomer)()),
|
|
256
|
+
__param(1, (0, common_1.Res)({ passthrough: true })),
|
|
257
|
+
__metadata("design:type", Function),
|
|
258
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
259
|
+
__metadata("design:returntype", Object)
|
|
260
|
+
], CustomerAuthController.prototype, "me", null);
|
|
261
|
+
__decorate([
|
|
262
|
+
(0, customer_auth_decorator_1.CustomerAuth)(),
|
|
263
|
+
(0, common_1.Post)('logout'),
|
|
264
|
+
(0, common_1.HttpCode)(200),
|
|
265
|
+
__param(0, (0, common_1.Req)()),
|
|
266
|
+
__param(1, (0, customer_auth_decorator_1.CurrentCustomer)()),
|
|
267
|
+
__param(2, (0, common_1.Res)({ passthrough: true })),
|
|
268
|
+
__metadata("design:type", Function),
|
|
269
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
270
|
+
__metadata("design:returntype", Promise)
|
|
271
|
+
], CustomerAuthController.prototype, "logout", null);
|
|
272
|
+
exports.CustomerAuthController = CustomerAuthController = __decorate([
|
|
273
|
+
(0, common_1.Controller)('api/customer/auth'),
|
|
274
|
+
__param(0, (0, common_1.Inject)(constants_1.OAUTH2_CONFIG)),
|
|
275
|
+
__param(1, (0, common_1.Inject)(constants_1.OAUTH2_DB)),
|
|
276
|
+
__param(2, (0, common_1.Inject)(constants_1.CUSTOMER_OIDC_CLIENT)),
|
|
277
|
+
__param(3, (0, common_1.Inject)(constants_1.CUSTOMER_SESSIONS)),
|
|
278
|
+
__param(4, (0, common_1.Inject)(customer_service_1.CustomerService)),
|
|
279
|
+
__param(5, (0, common_1.Inject)(auth_hooks_1.AuthHookRunner)),
|
|
280
|
+
__param(6, (0, common_1.Inject)(audit_service_1.AuditService)),
|
|
281
|
+
__param(7, (0, common_1.Inject)(constants_1.GRANT_REVOKER)),
|
|
282
|
+
__param(8, (0, common_1.Inject)(provider_service_1.ProviderService)),
|
|
283
|
+
__metadata("design:paramtypes", [Object, Function, Function, Function, customer_service_1.CustomerService,
|
|
284
|
+
auth_hooks_1.AuthHookRunner,
|
|
285
|
+
audit_service_1.AuditService, Object, provider_service_1.ProviderService])
|
|
286
|
+
], CustomerAuthController);
|
|
287
|
+
//# sourceMappingURL=customer-auth.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer-auth.controller.js","sourceRoot":"","sources":["../../src/customers/customer-auth.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,6CAAsD;AACtD,2CAA2F;AAG3F,0DAAsD;AACtD,4CAA4E;AAI5E,4CAGsB;AAEtB,sCAAsC;AACtC,8EAAuG;AAEvG,gEAAmD;AACnD,mEAA+D;AAC/D,+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C,IAAA,mBAAU,EAAC,mBAAmB,CAAC;IAO3B,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,eAAM,EAAC,qBAAS,CAAC,CAAA;IACjB,WAAA,IAAA,eAAM,EAAC,gCAAoB,CAAC,CAAA;IAC5B,WAAA,IAAA,eAAM,EAAC,6BAAiB,CAAC,CAAA;IACzB,WAAA,IAAA,eAAM,EAAC,kCAAe,CAAC,CAAA;IACvB,WAAA,IAAA,eAAM,EAAC,2BAAc,CAAC,CAAA;IACtB,WAAA,IAAA,eAAM,EAAC,4BAAY,CAAC,CAAA;IACpB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;IACrB,WAAA,IAAA,eAAM,EAAC,kCAAe,CAAC,CAAA;2EAJ6B,kCAAe;QACpB,2BAAc;QAChB,4BAAY,UAEL,kCAAe;GAd3D,sBAAsB,CA+MlC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SessionStore } from '../bff/session.store';
|
|
2
|
+
import type { GrantRevoker } from '../users/user.service';
|
|
3
|
+
/**
|
|
4
|
+
* Ends a customer's sign-ins in one place: BFF session rows (`auth_customer_sessions`), provider grants (with every
|
|
5
|
+
* code / access / refresh token issued under them, so mobile and web tokens stop working at once) and provider
|
|
6
|
+
* sessions. Used by password change (keeping the current credential) and password reset (everything); admin
|
|
7
|
+
* disabling reuses it.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CustomerSessionsRevoker {
|
|
10
|
+
private readonly sessions;
|
|
11
|
+
private readonly grants;
|
|
12
|
+
constructor(sessions: SessionStore, grants: GrantRevoker);
|
|
13
|
+
/**
|
|
14
|
+
* To keep the caller's own credential working pass its session (`exceptSessionId`, cookie) and its grant
|
|
15
|
+
* (`exceptGrantId`; for a cookie session `SessionStore.grantIdOf`, for a bearer token the token's grant).
|
|
16
|
+
*/
|
|
17
|
+
revokeAll(customerId: string, opts?: {
|
|
18
|
+
exceptSessionId?: string;
|
|
19
|
+
exceptGrantId?: string;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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.CustomerSessionsRevoker = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
/**
|
|
19
|
+
* Ends a customer's sign-ins in one place: BFF session rows (`auth_customer_sessions`), provider grants (with every
|
|
20
|
+
* code / access / refresh token issued under them, so mobile and web tokens stop working at once) and provider
|
|
21
|
+
* sessions. Used by password change (keeping the current credential) and password reset (everything); admin
|
|
22
|
+
* disabling reuses it.
|
|
23
|
+
*/
|
|
24
|
+
let CustomerSessionsRevoker = class CustomerSessionsRevoker {
|
|
25
|
+
sessions;
|
|
26
|
+
grants;
|
|
27
|
+
constructor(sessions, grants) {
|
|
28
|
+
this.sessions = sessions;
|
|
29
|
+
this.grants = grants;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* To keep the caller's own credential working pass its session (`exceptSessionId`, cookie) and its grant
|
|
33
|
+
* (`exceptGrantId`; for a cookie session `SessionStore.grantIdOf`, for a bearer token the token's grant).
|
|
34
|
+
*/
|
|
35
|
+
async revokeAll(customerId, opts = {}) {
|
|
36
|
+
await this.sessions.deleteByOwner(customerId, opts.exceptSessionId);
|
|
37
|
+
await this.grants.revokeAllGrants(customerId, opts.exceptGrantId ? { exceptGrantId: opts.exceptGrantId } : {});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.CustomerSessionsRevoker = CustomerSessionsRevoker;
|
|
41
|
+
exports.CustomerSessionsRevoker = CustomerSessionsRevoker = __decorate([
|
|
42
|
+
(0, common_1.Injectable)(),
|
|
43
|
+
__param(0, (0, common_1.Inject)(constants_1.CUSTOMER_SESSIONS)),
|
|
44
|
+
__param(1, (0, common_1.Inject)(constants_1.GRANT_REVOKER)),
|
|
45
|
+
__metadata("design:paramtypes", [Function, Object])
|
|
46
|
+
], CustomerSessionsRevoker);
|
|
47
|
+
//# sourceMappingURL=customer-sessions.revoker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customer-sessions.revoker.js","sourceRoot":"","sources":["../../src/customers/customer-sessions.revoker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AAEpD,4CAAgE;AAGhE;;;;;GAKG;AAEI,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;IAEY;IACJ;IAF1C,YAC8C,QAAsB,EAC1B,MAAoB;QADhB,aAAQ,GAAR,QAAQ,CAAc;QAC1B,WAAM,GAAN,MAAM,CAAc;IAC3D,CAAC;IAEJ;;;OAGG;IACH,KAAK,CAAC,SAAS,CAAC,UAAkB,EAAE,OAA6D,EAAE;QACjG,MAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QACpE,MAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACjH,CAAC;CACF,CAAA;AAdY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,6BAAiB,CAAC,CAAA;IACzB,WAAA,IAAA,eAAM,EAAC,yBAAa,CAAC,CAAA;;GAHb,uBAAuB,CAcnC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type Kysely, type Transaction } from 'kysely';
|
|
2
|
+
import type { OAuth2Config } from '../config/schema';
|
|
3
|
+
import type { AuthTables } from '../db/types';
|
|
4
|
+
import { type IdentifierKind } from './identifier';
|
|
5
|
+
export interface Customer {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string | null;
|
|
8
|
+
status: 'active' | 'disabled';
|
|
9
|
+
hasPassword: boolean;
|
|
10
|
+
identifiers: {
|
|
11
|
+
id: string;
|
|
12
|
+
kind: IdentifierKind;
|
|
13
|
+
value: string;
|
|
14
|
+
verified: boolean;
|
|
15
|
+
}[];
|
|
16
|
+
}
|
|
17
|
+
type Db = Kysely<AuthTables> | Transaction<AuthTables>;
|
|
18
|
+
export declare class CustomerService {
|
|
19
|
+
private readonly db;
|
|
20
|
+
private readonly cfg;
|
|
21
|
+
constructor(db: Kysely<AuthTables>, cfg: Pick<OAuth2Config, 'storefront'>);
|
|
22
|
+
private hydrate;
|
|
23
|
+
private strip;
|
|
24
|
+
findById(id: string, trx?: Db): Promise<Customer | undefined>;
|
|
25
|
+
findByIdentifier(kind: IdentifierKind, value: string, trx?: Db): Promise<(Customer & {
|
|
26
|
+
passwordHash: string | null;
|
|
27
|
+
}) | undefined>;
|
|
28
|
+
createWithIdentifier(input: {
|
|
29
|
+
kind: IdentifierKind;
|
|
30
|
+
value: string;
|
|
31
|
+
name?: string | null;
|
|
32
|
+
}, verified: boolean, trx?: Transaction<AuthTables>): Promise<Customer>;
|
|
33
|
+
markVerified(identifierId: string): Promise<void>;
|
|
34
|
+
/** `verified: true` stores it verified in the same insert (self-service add after a `verify` code). */
|
|
35
|
+
addIdentifier(customerId: string, kind: IdentifierKind, value: string, verified?: boolean): Promise<Customer>;
|
|
36
|
+
/** Refuses to remove the customer's last verified identifier (409 `last_identifier`). */
|
|
37
|
+
removeIdentifier(customerId: string, identifierId: string): Promise<Customer>;
|
|
38
|
+
/** The stored password hash (null: none set); undefined when the customer does not exist. */
|
|
39
|
+
passwordHashOf(id: string): Promise<string | null | undefined>;
|
|
40
|
+
setPassword(customerId: string, hash: string | null): Promise<void>;
|
|
41
|
+
setStatus(id: string, status: 'active' | 'disabled'): Promise<void>;
|
|
42
|
+
setName(id: string, name: string | null): Promise<void>;
|
|
43
|
+
touchLogin(id: string): Promise<void>;
|
|
44
|
+
list(opts: {
|
|
45
|
+
q?: string;
|
|
46
|
+
limit: number;
|
|
47
|
+
offset: number;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
items: Customer[];
|
|
50
|
+
total: number;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
export {};
|