@nauth-toolkit/oidc-provider 0.4.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 +158 -0
- package/dist/nestjs/index.d.ts +12 -0
- package/dist/nestjs/index.d.ts.map +1 -0
- package/dist/nestjs/index.js +23 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/nestjs/mount.d.ts +38 -0
- package/dist/nestjs/mount.d.ts.map +1 -0
- package/dist/nestjs/mount.js +35 -0
- package/dist/nestjs/mount.js.map +1 -0
- package/dist/nestjs/oidc-interaction.controller.d.ts +40 -0
- package/dist/nestjs/oidc-interaction.controller.d.ts.map +1 -0
- package/dist/nestjs/oidc-interaction.controller.js +157 -0
- package/dist/nestjs/oidc-interaction.controller.js.map +1 -0
- package/dist/nestjs/oidc-provider.module.d.ts +70 -0
- package/dist/nestjs/oidc-provider.module.d.ts.map +1 -0
- package/dist/nestjs/oidc-provider.module.js +84 -0
- package/dist/nestjs/oidc-provider.module.js.map +1 -0
- package/dist/nestjs/tokens.d.ts +13 -0
- package/dist/nestjs/tokens.d.ts.map +1 -0
- package/dist/nestjs/tokens.js +16 -0
- package/dist/nestjs/tokens.js.map +1 -0
- package/dist/src/config.types.d.ts +102 -0
- package/dist/src/config.types.d.ts.map +1 -0
- package/dist/src/config.types.js +3 -0
- package/dist/src/config.types.js.map +1 -0
- package/dist/src/create-provider.d.ts +47 -0
- package/dist/src/create-provider.d.ts.map +1 -0
- package/dist/src/create-provider.js +159 -0
- package/dist/src/create-provider.js.map +1 -0
- package/dist/src/find-account.d.ts +33 -0
- package/dist/src/find-account.d.ts.map +1 -0
- package/dist/src/find-account.js +69 -0
- package/dist/src/find-account.js.map +1 -0
- package/dist/src/index.d.ts +41 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +51 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/interaction-bridge.d.ts +147 -0
- package/dist/src/interaction-bridge.d.ts.map +1 -0
- package/dist/src/interaction-bridge.js +283 -0
- package/dist/src/interaction-bridge.js.map +1 -0
- package/dist/src/mount/express.d.ts +54 -0
- package/dist/src/mount/express.d.ts.map +1 -0
- package/dist/src/mount/express.js +56 -0
- package/dist/src/mount/express.js.map +1 -0
- package/dist/src/rate-limit.d.ts +61 -0
- package/dist/src/rate-limit.d.ts.map +1 -0
- package/dist/src/rate-limit.js +93 -0
- package/dist/src/rate-limit.js.map +1 -0
- package/dist/src/raw-http.d.ts +25 -0
- package/dist/src/raw-http.d.ts.map +1 -0
- package/dist/src/raw-http.js +33 -0
- package/dist/src/raw-http.js.map +1 -0
- package/dist/src/session-termination.d.ts +60 -0
- package/dist/src/session-termination.d.ts.map +1 -0
- package/dist/src/session-termination.js +70 -0
- package/dist/src/session-termination.js.map +1 -0
- package/dist/src/storage.adapter.d.ts +99 -0
- package/dist/src/storage.adapter.d.ts.map +1 -0
- package/dist/src/storage.adapter.js +197 -0
- package/dist/src/storage.adapter.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +95 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { StorageAdapter } from '@nauth-toolkit/core';
|
|
2
|
+
/** Limits for one endpoint. */
|
|
3
|
+
export interface OIDCEndpointLimit {
|
|
4
|
+
/** Maximum requests allowed in the window. */
|
|
5
|
+
max: number;
|
|
6
|
+
/** Window length, in seconds. */
|
|
7
|
+
windowSeconds: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Per-endpoint limits for the provider.
|
|
11
|
+
*
|
|
12
|
+
* Endpoints not listed are unlimited.
|
|
13
|
+
*/
|
|
14
|
+
export interface OIDCRateLimitConfig {
|
|
15
|
+
/** The authorization endpoint. Keyed by source IP. */
|
|
16
|
+
authorize?: OIDCEndpointLimit;
|
|
17
|
+
/** The token endpoint. Keyed by source IP — the brute-force surface that matters most. */
|
|
18
|
+
token?: OIDCEndpointLimit;
|
|
19
|
+
/** The introspection endpoint. Often on a gateway's hot path — set generously or leave unset. */
|
|
20
|
+
introspection?: OIDCEndpointLimit;
|
|
21
|
+
}
|
|
22
|
+
/** The request shape this middleware inspects. */
|
|
23
|
+
interface RateLimitedRequest {
|
|
24
|
+
url?: string;
|
|
25
|
+
method?: string;
|
|
26
|
+
ip?: string;
|
|
27
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
28
|
+
socket?: {
|
|
29
|
+
remoteAddress?: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** The response shape this middleware writes a rejection to. */
|
|
33
|
+
interface RateLimitedResponse {
|
|
34
|
+
statusCode: number;
|
|
35
|
+
setHeader(name: string, value: string): void;
|
|
36
|
+
end(body?: string): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Rate limiting for the OpenID Connect provider's endpoints.
|
|
40
|
+
*
|
|
41
|
+
* `oidc-provider` ships none, and the provider is mounted outside nauth's guard chain,
|
|
42
|
+
* so nothing else covers these paths either. `POST /token` in particular is an
|
|
43
|
+
* unauthenticated brute-force surface against client secrets and authorization codes.
|
|
44
|
+
*
|
|
45
|
+
* Counting uses the storage adapter's atomic `incr`, which is the same primitive the
|
|
46
|
+
* rest of the toolkit rate-limits with, so it holds across instances rather than per
|
|
47
|
+
* process.
|
|
48
|
+
*
|
|
49
|
+
* Mount it immediately **before** the provider, so a rejected request never reaches it.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* app.use(createOIDCRateLimiter(nauth.storage, { token: { max: 30, windowSeconds: 60 } }));
|
|
54
|
+
* mountOIDCProviderExpress(app, provider);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function createOIDCRateLimiter(storage: StorageAdapter, config?: OIDCRateLimitConfig, options?: {
|
|
58
|
+
pathPrefix?: string;
|
|
59
|
+
}): (req: RateLimitedRequest, res: RateLimitedResponse, next: () => void) => void;
|
|
60
|
+
export {};
|
|
61
|
+
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,GACpC,CAAC,GAAG,EAAE,kBAAkB,EAAE,GAAG,EAAE,mBAAmB,EAAE,IAAI,EAAE,MAAM,IAAI,KAAK,IAAI,CA2D/E"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createOIDCRateLimiter = createOIDCRateLimiter;
|
|
4
|
+
/** Sensible starting limits, applied when a section is configured with no values. */
|
|
5
|
+
const DEFAULTS = {
|
|
6
|
+
authorize: { max: 60, windowSeconds: 60 },
|
|
7
|
+
token: { max: 60, windowSeconds: 60 },
|
|
8
|
+
introspection: { max: 600, windowSeconds: 60 },
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Rate limiting for the OpenID Connect provider's endpoints.
|
|
12
|
+
*
|
|
13
|
+
* `oidc-provider` ships none, and the provider is mounted outside nauth's guard chain,
|
|
14
|
+
* so nothing else covers these paths either. `POST /token` in particular is an
|
|
15
|
+
* unauthenticated brute-force surface against client secrets and authorization codes.
|
|
16
|
+
*
|
|
17
|
+
* Counting uses the storage adapter's atomic `incr`, which is the same primitive the
|
|
18
|
+
* rest of the toolkit rate-limits with, so it holds across instances rather than per
|
|
19
|
+
* process.
|
|
20
|
+
*
|
|
21
|
+
* Mount it immediately **before** the provider, so a rejected request never reaches it.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* app.use(createOIDCRateLimiter(nauth.storage, { token: { max: 30, windowSeconds: 60 } }));
|
|
26
|
+
* mountOIDCProviderExpress(app, provider);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function createOIDCRateLimiter(storage, config = {}, options = {}) {
|
|
30
|
+
const prefix = options.pathPrefix ?? '/oidc';
|
|
31
|
+
const limits = [];
|
|
32
|
+
if (config.authorize) {
|
|
33
|
+
limits.push({ path: `${prefix}/auth`, name: 'authorize', limit: { ...DEFAULTS.authorize, ...config.authorize } });
|
|
34
|
+
}
|
|
35
|
+
if (config.token) {
|
|
36
|
+
limits.push({ path: `${prefix}/token`, name: 'token', limit: { ...DEFAULTS.token, ...config.token } });
|
|
37
|
+
}
|
|
38
|
+
if (config.introspection) {
|
|
39
|
+
limits.push({
|
|
40
|
+
path: `${prefix}/token/introspection`,
|
|
41
|
+
name: 'introspection',
|
|
42
|
+
limit: { ...DEFAULTS.introspection, ...config.introspection },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return (req, res, next) => {
|
|
46
|
+
if (limits.length === 0) {
|
|
47
|
+
next();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const path = (req.url ?? '').split('?')[0];
|
|
51
|
+
// Longest path first, so /token/introspection is not swallowed by /token.
|
|
52
|
+
const match = [...limits].sort((a, b) => b.path.length - a.path.length).find((l) => path === l.path);
|
|
53
|
+
if (!match) {
|
|
54
|
+
next();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const key = `oidc:rl:${match.name}:${clientIp(req)}`;
|
|
58
|
+
void storage
|
|
59
|
+
.incr(key, match.limit.windowSeconds)
|
|
60
|
+
.then((count) => {
|
|
61
|
+
if (count <= match.limit.max) {
|
|
62
|
+
next();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// RFC 6749 §5.2 has no rate-limit error, so this is a plain 429 with
|
|
66
|
+
// Retry-After rather than an OAuth error body.
|
|
67
|
+
res.statusCode = 429;
|
|
68
|
+
res.setHeader('content-type', 'application/json');
|
|
69
|
+
res.setHeader('retry-after', String(match.limit.windowSeconds));
|
|
70
|
+
res.setHeader('cache-control', 'no-store');
|
|
71
|
+
res.end(JSON.stringify({
|
|
72
|
+
error: 'temporarily_unavailable',
|
|
73
|
+
error_description: 'Too many requests. Try again shortly.',
|
|
74
|
+
}));
|
|
75
|
+
})
|
|
76
|
+
.catch(() => {
|
|
77
|
+
// Storage being unavailable must not take the provider down with it.
|
|
78
|
+
next();
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Best-effort source address, preferring the proxy's forwarded value.
|
|
84
|
+
*/
|
|
85
|
+
function clientIp(req) {
|
|
86
|
+
const forwarded = req.headers?.['x-forwarded-for'];
|
|
87
|
+
const first = Array.isArray(forwarded) ? forwarded[0] : forwarded;
|
|
88
|
+
if (first) {
|
|
89
|
+
return first.split(',')[0].trim();
|
|
90
|
+
}
|
|
91
|
+
return req.ip ?? req.socket?.remoteAddress ?? 'unknown';
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=rate-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../../src/rate-limit.ts"],"names":[],"mappings":";;AAkEA,sDA+DC;AAzGD,qFAAqF;AACrF,MAAM,QAAQ,GAAkC;IAC9C,SAAS,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,aAAa,EAAE,EAAE,EAAE;IACzC,KAAK,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,aAAa,EAAE,EAAE,EAAE;IACrC,aAAa,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE;CAC/C,CAAC;AAkBF;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,qBAAqB,CACnC,OAAuB,EACvB,SAA8B,EAAE,EAChC,UAAmC,EAAE;IAErC,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC;IAE7C,MAAM,MAAM,GAA+D,EAAE,CAAC;IAC9E,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,MAAM,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,EAAE,GAAG,QAAQ,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IACpH,CAAC;IACD,IAAI,M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AAE,aAAa,IAAI,SAAS,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
/**
|
|
3
|
+
* Recover the Node request and response from whatever a framework handed us.
|
|
4
|
+
*
|
|
5
|
+
* `oidc-provider`'s interaction API takes raw Node objects. Express request and
|
|
6
|
+
* response objects *are* `IncomingMessage`/`ServerResponse` subclasses, so they pass
|
|
7
|
+
* through unchanged; Fastify wraps them, exposing the originals as `.raw`. nauth's own
|
|
8
|
+
* `NAuthRequest.raw` / `NAuthResponse.raw` hold the *framework* object, so a value
|
|
9
|
+
* taken from there unwraps correctly here too.
|
|
10
|
+
*
|
|
11
|
+
* @param req - An Express request, Fastify request, or `NAuthRequest.raw`
|
|
12
|
+
* @param res - An Express response, Fastify reply, or `NAuthResponse.raw`
|
|
13
|
+
* @returns The underlying Node request and response
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const { req: rawReq, res: rawRes } = toRawHttp(nauthReq.raw, nauthRes.raw);
|
|
18
|
+
* const details = await provider.interactionDetails(rawReq, rawRes);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function toRawHttp(req: unknown, res: unknown): {
|
|
22
|
+
req: IncomingMessage;
|
|
23
|
+
res: ServerResponse;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=raw-http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw-http.d.ts","sourceRoot":"","sources":["../../src/raw-http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEjE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,GAAG;IAAE,GAAG,EAAE,eAAe,CAAC;IAAC,GAAG,EAAE,cAAc,CAAA;CAAE,CAWnG"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toRawHttp = toRawHttp;
|
|
4
|
+
/**
|
|
5
|
+
* Recover the Node request and response from whatever a framework handed us.
|
|
6
|
+
*
|
|
7
|
+
* `oidc-provider`'s interaction API takes raw Node objects. Express request and
|
|
8
|
+
* response objects *are* `IncomingMessage`/`ServerResponse` subclasses, so they pass
|
|
9
|
+
* through unchanged; Fastify wraps them, exposing the originals as `.raw`. nauth's own
|
|
10
|
+
* `NAuthRequest.raw` / `NAuthResponse.raw` hold the *framework* object, so a value
|
|
11
|
+
* taken from there unwraps correctly here too.
|
|
12
|
+
*
|
|
13
|
+
* @param req - An Express request, Fastify request, or `NAuthRequest.raw`
|
|
14
|
+
* @param res - An Express response, Fastify reply, or `NAuthResponse.raw`
|
|
15
|
+
* @returns The underlying Node request and response
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const { req: rawReq, res: rawRes } = toRawHttp(nauthReq.raw, nauthRes.raw);
|
|
20
|
+
* const details = await provider.interactionDetails(rawReq, rawRes);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function toRawHttp(req, res) {
|
|
24
|
+
const reqObj = req;
|
|
25
|
+
const resObj = res;
|
|
26
|
+
const rawReq = reqObj?.raw;
|
|
27
|
+
const rawRes = resObj?.raw;
|
|
28
|
+
return {
|
|
29
|
+
req: (rawReq && typeof rawReq.headers === 'object' ? rawReq : reqObj),
|
|
30
|
+
res: (rawRes && typeof rawRes.setHeader === 'function' ? rawRes : resObj),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=raw-http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw-http.js","sourceRoot":"","sources":["../../src/raw-http.ts"],"names":[],"mappings":";;AAqBA,8BAWC;AA9BD;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,SAAS,CAAC,GAAY,EAAE,GAAY;IAClD,MAAM,MAAM,GAAG,GAA8B,CAAC;IAC9C,MAAM,MAAM,GAAG,GAA8B,CAAC;IAE9C,MAAM,MAAM,GAAG,MAAM,EAAE,GAA0C,CAAC;IAClE,MAAM,MAAM,GAAG,MAAM,EAAE,GAA0C,CAAC;IAElE,OAAO;QACL,GAAG,EAAE,CAAC,MAAM,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAA+B;QACnG,GAAG,EAAE,CAAC,MAAM,IAAI,OAAO,MAAM,CAAC,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAA8B;KACvG,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { StorageAdapter } from '@nauth-toolkit/core';
|
|
2
|
+
/**
|
|
3
|
+
* Ends a user's OpenID Connect sessions and grants.
|
|
4
|
+
*
|
|
5
|
+
* The provider keeps its own SSO session, entirely separate from nauth's. Without
|
|
6
|
+
* this, logging out of the application leaves that session standing, and the next
|
|
7
|
+
* authorization request from any client silently issues a fresh code for a user who
|
|
8
|
+
* believes they signed out. That is the single most important thing to wire up when
|
|
9
|
+
* adopting this package.
|
|
10
|
+
*
|
|
11
|
+
* Sessions and grants are found through the per-account marker keys the storage
|
|
12
|
+
* adapter writes, so this needs no extra index and no table.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // In your logout controller, after nauth has revoked its own session:
|
|
17
|
+
* await sessionTerminator.terminateFor(user.sub);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class OIDCSessionTerminator {
|
|
21
|
+
private readonly storage;
|
|
22
|
+
constructor(storage: StorageAdapter);
|
|
23
|
+
/**
|
|
24
|
+
* End every OpenID Connect session and grant belonging to a user.
|
|
25
|
+
*
|
|
26
|
+
* Destroying the sessions is what makes the provider's `_session` cookie stop
|
|
27
|
+
* resolving; destroying the grants means the next authorization request asks for
|
|
28
|
+
* consent again rather than reusing a remembered one.
|
|
29
|
+
*
|
|
30
|
+
* Tokens already issued to clients are deliberately **not** revoked. They have their
|
|
31
|
+
* own lifecycle: a third party holding a valid access token should not lose it
|
|
32
|
+
* because the user closed a browser tab. Revoke those explicitly, or let them
|
|
33
|
+
* expire.
|
|
34
|
+
*
|
|
35
|
+
* @param sub - The user's external identifier
|
|
36
|
+
* @returns How many sessions and grants were destroyed
|
|
37
|
+
*/
|
|
38
|
+
terminateFor(sub: string): Promise<{
|
|
39
|
+
sessions: number;
|
|
40
|
+
grants: number;
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* End every OpenID Connect session and grant, and revoke outstanding tokens too.
|
|
44
|
+
*
|
|
45
|
+
* The stronger form, for when an account is disabled or compromised: a suspended
|
|
46
|
+
* user's third-party integrations must stop working immediately, not at the next
|
|
47
|
+
* token expiry.
|
|
48
|
+
*
|
|
49
|
+
* @param sub - The user's external identifier
|
|
50
|
+
* @returns How many artifacts of each kind were destroyed
|
|
51
|
+
*/
|
|
52
|
+
terminateAndRevokeFor(sub: string): Promise<{
|
|
53
|
+
sessions: number;
|
|
54
|
+
grants: number;
|
|
55
|
+
accessTokens: number;
|
|
56
|
+
refreshTokens: number;
|
|
57
|
+
authorizationCodes: number;
|
|
58
|
+
}>;
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=session-termination.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-termination.d.ts","sourceRoot":"","sources":["../../src/session-termination.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG1D;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,qBAAqB;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,cAAc;IAEpD;;;;;;;;;;;;;;OAcG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IAM9E;;;;;;;;;OASG;IACG,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;QAChD,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,MAAM,CAAC;QACtB,kBAAkB,EAAE,MAAM,CAAC;KAC5B,CAAC;CAUH"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OIDCSessionTerminator = void 0;
|
|
4
|
+
const storage_adapter_1 = require("./storage.adapter");
|
|
5
|
+
/**
|
|
6
|
+
* Ends a user's OpenID Connect sessions and grants.
|
|
7
|
+
*
|
|
8
|
+
* The provider keeps its own SSO session, entirely separate from nauth's. Without
|
|
9
|
+
* this, logging out of the application leaves that session standing, and the next
|
|
10
|
+
* authorization request from any client silently issues a fresh code for a user who
|
|
11
|
+
* believes they signed out. That is the single most important thing to wire up when
|
|
12
|
+
* adopting this package.
|
|
13
|
+
*
|
|
14
|
+
* Sessions and grants are found through the per-account marker keys the storage
|
|
15
|
+
* adapter writes, so this needs no extra index and no table.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // In your logout controller, after nauth has revoked its own session:
|
|
20
|
+
* await sessionTerminator.terminateFor(user.sub);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class OIDCSessionTerminator {
|
|
24
|
+
storage;
|
|
25
|
+
constructor(storage) {
|
|
26
|
+
this.storage = storage;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* End every OpenID Connect session and grant belonging to a user.
|
|
30
|
+
*
|
|
31
|
+
* Destroying the sessions is what makes the provider's `_session` cookie stop
|
|
32
|
+
* resolving; destroying the grants means the next authorization request asks for
|
|
33
|
+
* consent again rather than reusing a remembered one.
|
|
34
|
+
*
|
|
35
|
+
* Tokens already issued to clients are deliberately **not** revoked. They have their
|
|
36
|
+
* own lifecycle: a third party holding a valid access token should not lose it
|
|
37
|
+
* because the user closed a browser tab. Revoke those explicitly, or let them
|
|
38
|
+
* expire.
|
|
39
|
+
*
|
|
40
|
+
* @param sub - The user's external identifier
|
|
41
|
+
* @returns How many sessions and grants were destroyed
|
|
42
|
+
*/
|
|
43
|
+
async terminateFor(sub) {
|
|
44
|
+
const sessions = await new storage_adapter_1.NAuthOIDCAdapter('Session', this.storage).revokeByAccountId(sub);
|
|
45
|
+
const grants = await new storage_adapter_1.NAuthOIDCAdapter('Grant', this.storage).revokeByAccountId(sub);
|
|
46
|
+
return { sessions, grants };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* End every OpenID Connect session and grant, and revoke outstanding tokens too.
|
|
50
|
+
*
|
|
51
|
+
* The stronger form, for when an account is disabled or compromised: a suspended
|
|
52
|
+
* user's third-party integrations must stop working immediately, not at the next
|
|
53
|
+
* token expiry.
|
|
54
|
+
*
|
|
55
|
+
* @param sub - The user's external identifier
|
|
56
|
+
* @returns How many artifacts of each kind were destroyed
|
|
57
|
+
*/
|
|
58
|
+
async terminateAndRevokeFor(sub) {
|
|
59
|
+
const { sessions, grants } = await this.terminateFor(sub);
|
|
60
|
+
return {
|
|
61
|
+
sessions,
|
|
62
|
+
grants,
|
|
63
|
+
accessTokens: await new storage_adapter_1.NAuthOIDCAdapter('AccessToken', this.storage).revokeByAccountId(sub),
|
|
64
|
+
refreshTokens: await new storage_adapter_1.NAuthOIDCAdapter('RefreshToken', this.storage).revokeByAccountId(sub),
|
|
65
|
+
authorizationCodes: await new storage_adapter_1.NAuthOIDCAdapter('AuthorizationCode', this.storage).revokeByAccountId(sub),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.OIDCSessionTerminator = OIDCSessionTerminator;
|
|
70
|
+
//# sourceMappingURL=session-termination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-termination.js","sourceRoot":"","sources":["../../src/session-termination.ts"],"names":[],"mappings":";;;AACA,uDAAqD;AAErD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,qBAAqB;IACH;IAA7B,YAA6B,OAAuB;QAAvB,YAAO,GAAP,OAAO,CAAgB;IAAG,CAAC;IAExD;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,YAAY,CAAC,GAAW;QAC5B,MAAM,QAAQ,GAAG,MAAM,IAAI,kCAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;QAC5F,MAAM,MAAM,GAAG,MAAM,IAAI,kCAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;QACxF,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,qBAAqB,CAAC,GAAW;QAOrC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;QAC1D,OAAO;YACL,QAAQ;YACR,MAAM;YACN,YAAY,EAAE,MAAM,IAAI,kCAAgB,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC;YAC5F,aAAa,EAAE,MAAM,IAAI,kCAAgB,CAAC,cAAc,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC;YAC9F,kBAAkB,EAAE,MAAM,IAAI,kCAAgB,CAAC,mBAAmB,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC;SACzG,CAAC;IACJ,CAAC;CACF;AAlDD,sDAkDC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Adapter, AdapterPayload } from 'oidc-provider';
|
|
2
|
+
import type { StorageAdapter } from '@nauth-toolkit/core';
|
|
3
|
+
/**
|
|
4
|
+
* Build an `oidc-provider` storage adapter over nauth's {@link StorageAdapter}.
|
|
5
|
+
*
|
|
6
|
+
* **This needs no database tables.** Every model `oidc-provider` persists is
|
|
7
|
+
* TTL-bounded, which is exactly what `StorageAdapter` is for — the same reasoning
|
|
8
|
+
* that put authorization codes there rather than in a table.
|
|
9
|
+
*
|
|
10
|
+
* Secondary lookups (`findByUid`, `findByUserCode`) and grant revocation are built
|
|
11
|
+
* from **marker keys carrying their own TTL**, not from the adapter's `hset`/`lpush`
|
|
12
|
+
* operations. Those are deliberately avoided: `DatabaseStorageAdapter` writes them to
|
|
13
|
+
* internally re-prefixed keys (`hash:`/`list:`) with **no expiry**, and `del`/`expire`
|
|
14
|
+
* operate on the un-prefixed key — so an index built on them would leak forever and
|
|
15
|
+
* could not be reaped through the interface.
|
|
16
|
+
*
|
|
17
|
+
* Key scheme:
|
|
18
|
+
* ```
|
|
19
|
+
* oidc:{model}:{id} → JSON payload
|
|
20
|
+
* oidc:{model}:uid:{uid} → id (Session, Interaction)
|
|
21
|
+
* oidc:DeviceCode:uc:{userCode} → id
|
|
22
|
+
* oidc:g:{grantId}:{model}:{id} → '1' (swept by revokeByGrantId)
|
|
23
|
+
* oidc:acct:{accountId}:{model}:{id} → '1' (swept when a user logs out)
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param storage - The nauth storage adapter (Redis in production, Database or Memory otherwise)
|
|
27
|
+
* @returns A factory `oidc-provider` calls once per model name
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* new Provider(issuer, { adapter: createOIDCStorageAdapter(nauth.storage) });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function createOIDCStorageAdapter(storage: StorageAdapter): (name: string) => Adapter;
|
|
35
|
+
/**
|
|
36
|
+
* `oidc-provider` adapter for one model, backed by nauth's storage abstraction.
|
|
37
|
+
*/
|
|
38
|
+
export declare class NAuthOIDCAdapter implements Adapter {
|
|
39
|
+
private readonly name;
|
|
40
|
+
private readonly storage;
|
|
41
|
+
constructor(name: string, storage: StorageAdapter);
|
|
42
|
+
/**
|
|
43
|
+
* Create or replace a model instance, plus whatever secondary markers it needs.
|
|
44
|
+
*
|
|
45
|
+
* Every marker is written with the *same* TTL as the payload, so the index can
|
|
46
|
+
* never outlive what it points at.
|
|
47
|
+
*/
|
|
48
|
+
upsert(id: string, payload: AdapterPayload, expiresIn: number): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Load a model instance by id, or undefined once it has expired or been destroyed.
|
|
51
|
+
*/
|
|
52
|
+
find(id: string): Promise<AdapterPayload | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* Load a Session or Interaction by its `uid`.
|
|
55
|
+
*/
|
|
56
|
+
findByUid(uid: string): Promise<AdapterPayload | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* Load a DeviceCode by the code the end user typed in.
|
|
59
|
+
*/
|
|
60
|
+
findByUserCode(userCode: string): Promise<AdapterPayload | undefined>;
|
|
61
|
+
/**
|
|
62
|
+
* Mark a single-use artifact as spent, preserving its remaining lifetime.
|
|
63
|
+
*
|
|
64
|
+
* Read-modify-write is safe here: `consumed` is a monotonic marker, so a lost
|
|
65
|
+
* update cannot un-consume anything. `oidc-provider`'s actual replay defence is its
|
|
66
|
+
* separate `ReplayDetection` model, not this flag.
|
|
67
|
+
*/
|
|
68
|
+
consume(id: string): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Delete a model instance.
|
|
71
|
+
*/
|
|
72
|
+
destroy(id: string): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Revoke every artifact issued under a grant.
|
|
75
|
+
*
|
|
76
|
+
* Sweeps the grant's marker keys and deletes both the marker and the payload it
|
|
77
|
+
* points at. This is how a consent withdrawal, or `oidc-provider`'s own refresh
|
|
78
|
+
* replay detection, cascades across access tokens, refresh tokens and codes.
|
|
79
|
+
*/
|
|
80
|
+
revokeByGrantId(grantId: string): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Delete every artifact of this model belonging to an account.
|
|
83
|
+
*
|
|
84
|
+
* Not part of `oidc-provider`'s Adapter contract — used by the single-logout helper
|
|
85
|
+
* so that ending a nauth session can also end the provider's own SSO session.
|
|
86
|
+
*
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
revokeByAccountId(accountId: string): Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Read and parse a payload, treating unparseable data as absent.
|
|
92
|
+
*/
|
|
93
|
+
private read;
|
|
94
|
+
/**
|
|
95
|
+
* Storage key for a model instance.
|
|
96
|
+
*/
|
|
97
|
+
private key;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=storage.adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.adapter.d.ts","sourceRoot":"","sources":["../../src/storage.adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC7D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAoB1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,cAAc,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAE3F;AAED;;GAEG;AACH,qBAAa,gBAAiB,YAAW,OAAO;IAE5C,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC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