@dokebi/oauth 0.0.0-stage → 0.1.1
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/AI.md +469 -0
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/bounded.d.ts +9 -0
- package/dist/bounded.js +36 -0
- package/dist/cimd.d.ts +97 -0
- package/dist/cimd.js +384 -0
- package/dist/clients.d.ts +85 -0
- package/dist/clients.js +303 -0
- package/dist/dependency.d.ts +4 -0
- package/dist/dependency.js +14 -0
- package/dist/errors.d.ts +87 -0
- package/dist/errors.js +34 -0
- package/dist/extension.d.ts +161 -0
- package/dist/extension.js +108 -0
- package/dist/grants.d.ts +126 -0
- package/dist/grants.js +280 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +28 -0
- package/dist/install.d.ts +33 -0
- package/dist/install.js +112 -0
- package/dist/live.d.ts +47 -0
- package/dist/live.js +105 -0
- package/dist/options.d.ts +173 -0
- package/dist/options.js +521 -0
- package/dist/plugin.d.ts +113 -0
- package/dist/plugin.js +766 -0
- package/dist/protocol.d.ts +81 -0
- package/dist/protocol.js +249 -0
- package/dist/server.d.ts +148 -0
- package/dist/server.js +1516 -0
- package/dist/signin.d.ts +40 -0
- package/dist/signin.js +86 -0
- package/dist/store-redis.d.ts +38 -0
- package/dist/store-redis.js +173 -0
- package/dist/store-sql.d.ts +46 -0
- package/dist/store-sql.js +363 -0
- package/dist/store.d.ts +126 -0
- package/dist/store.js +266 -0
- package/options.schema.json +633 -0
- package/package.json +143 -4
- package/templates/extension.ts +45 -0
- package/templates/migrations/dokebi_oauth.ts +117 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The comparison key of a resource URI — the **one** function every place uses (authorize, token,
|
|
3
|
+
* audience, registration; XF ADR 0122): scheme and host in lower case, the default port dropped
|
|
4
|
+
* and an empty path equal to `/` (RFC 3986 §6.2.3), as the WHATWG URL parser normalizes them. The
|
|
5
|
+
* path, the query and their case stay as they are. Undefined for something that is not an
|
|
6
|
+
* absolute http(s) URI without a fragment or user information.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resourceKey(uri: string): string | undefined;
|
|
9
|
+
/** Whether two resource URIs name the same resource (`resourceKey` of both). */
|
|
10
|
+
export declare function sameResource(a: string, b: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Where the protected resource metadata of a resource lives (RFC 9728 §3.1): the well-known path
|
|
13
|
+
* inserted between the origin and the resource's path (`https://h/mcp` →
|
|
14
|
+
* `https://h/.well-known/oauth-protected-resource/mcp`; `https://h` → `…/oauth-protected-resource`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function metadataLocation(uri: string): {
|
|
17
|
+
url: string;
|
|
18
|
+
path: string;
|
|
19
|
+
};
|
|
20
|
+
/** A space-separated scope string as a list without repeats (order kept); empty for none. */
|
|
21
|
+
export declare function parseScope(value: string | undefined): string[];
|
|
22
|
+
export declare function formatScope(scopes: readonly string[]): string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether a presented redirect URI matches a registered one: the exact string, never a prefix or
|
|
25
|
+
* a normalized form (that would be an open redirect). The one exception (RFC 8252 §7.3): on a
|
|
26
|
+
* loopback host (`127.0.0.1`, `[::1]`, `localhost`) only the port may differ — the scheme, the
|
|
27
|
+
* host (`localhost` and `127.0.0.1` are different hosts), the path and the query must not.
|
|
28
|
+
*/
|
|
29
|
+
export declare function redirectMatches(registered: string, presented: string): boolean;
|
|
30
|
+
export declare function isLoopbackUri(uri: string): boolean;
|
|
31
|
+
/** The host a redirect goes to (`claude.ai`), for the consent data and logs. */
|
|
32
|
+
export declare function hostOf(uri: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Why a redirect URI cannot be registered (CIMD documents, registration, options), or undefined:
|
|
35
|
+
* an absolute https URI, or http on a loopback host; no fragment, no user information.
|
|
36
|
+
*/
|
|
37
|
+
export declare function redirectProblem(uri: unknown): string | undefined;
|
|
38
|
+
/** `uri` with the query parameters added (the existing query is kept). */
|
|
39
|
+
export declare function withQuery(uri: string, values: Readonly<Record<string, string | undefined>>): string;
|
|
40
|
+
/** BASE64URL(SHA256(verifier)): 43 characters without padding. */
|
|
41
|
+
export declare const CHALLENGE: RegExp;
|
|
42
|
+
export declare function s256(verifier: string): string;
|
|
43
|
+
/** Whether `verifier` belongs to `challenge` (S256), in constant time. */
|
|
44
|
+
export declare function verifyPkce(verifier: string, challenge: string): boolean;
|
|
45
|
+
/** Parsed `application/x-www-form-urlencoded` text: every value of every name, in order. */
|
|
46
|
+
export type ParamList = ReadonlyMap<string, readonly string[]>;
|
|
47
|
+
export type StrictParams = {
|
|
48
|
+
readonly ok: true;
|
|
49
|
+
readonly params: Readonly<Record<string, string>>;
|
|
50
|
+
} | {
|
|
51
|
+
readonly ok: false;
|
|
52
|
+
readonly reason: 'repeated' | 'encoding';
|
|
53
|
+
readonly name: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Every name and its values (repeats kept), or the name whose escape is broken. Parameters
|
|
57
|
+
* without a value count as given (for repeats) and are dropped by `strictParams`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function paramList(raw: string): ParamList | {
|
|
60
|
+
readonly broken: string;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* One value per name or a refusal (RFC 6749 §3.1: parameters MUST NOT be included more than
|
|
64
|
+
* once). A web framework silently keeps the last of `resource=A&resource=B`: the value checked and
|
|
65
|
+
* the value used could differ. Empty values are treated as omitted (§3.1), but still count as a
|
|
66
|
+
* repeat.
|
|
67
|
+
*/
|
|
68
|
+
export declare function strictParams(list: ParamList | {
|
|
69
|
+
readonly broken: string;
|
|
70
|
+
}): StrictParams;
|
|
71
|
+
/** The query text of a request URL (`/oauth/authorize?a=1` → `a=1`). */
|
|
72
|
+
export declare function queryOf(url: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* The sentence of a refused parameter list (for `error_description` and the screen): a known
|
|
75
|
+
* OAuth parameter is named, anything else the request sent is not echoed.
|
|
76
|
+
*/
|
|
77
|
+
export declare function paramsProblem(result: Exclude<StrictParams, {
|
|
78
|
+
ok: true;
|
|
79
|
+
}>): string;
|
|
80
|
+
/** sha256 of a secret value in base64url (codes; never stored in clear). */
|
|
81
|
+
export declare function sha256(text: string): string;
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/*
|
|
3
|
+
* The pure parts of the protocol (D-0091 §3): the one resource comparison, scopes, redirect URI
|
|
4
|
+
* rules, PKCE S256 and the strict parameter parser. No I/O here.
|
|
5
|
+
*/
|
|
6
|
+
// ---- Resource URIs ----------------------------------------------------------------------------
|
|
7
|
+
/**
|
|
8
|
+
* The comparison key of a resource URI — the **one** function every place uses (authorize, token,
|
|
9
|
+
* audience, registration; XF ADR 0122): scheme and host in lower case, the default port dropped
|
|
10
|
+
* and an empty path equal to `/` (RFC 3986 §6.2.3), as the WHATWG URL parser normalizes them. The
|
|
11
|
+
* path, the query and their case stay as they are. Undefined for something that is not an
|
|
12
|
+
* absolute http(s) URI without a fragment or user information.
|
|
13
|
+
*/
|
|
14
|
+
export function resourceKey(uri) {
|
|
15
|
+
if (typeof uri !== 'string' || uri.length > 2048 || uri.includes('#'))
|
|
16
|
+
return undefined;
|
|
17
|
+
let url;
|
|
18
|
+
try {
|
|
19
|
+
url = new URL(uri);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
// Not a URL: an answer of this function (the caller refuses the value), nothing to log.
|
|
23
|
+
if (!(error instanceof TypeError))
|
|
24
|
+
throw error;
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
if ((url.protocol !== 'https:' && url.protocol !== 'http:') || url.username || url.password)
|
|
28
|
+
return undefined;
|
|
29
|
+
return `${url.protocol}//${url.host}${url.pathname}${url.search}`;
|
|
30
|
+
}
|
|
31
|
+
/** Whether two resource URIs name the same resource (`resourceKey` of both). */
|
|
32
|
+
export function sameResource(a, b) {
|
|
33
|
+
const left = resourceKey(a);
|
|
34
|
+
return left !== undefined && left === resourceKey(b);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Where the protected resource metadata of a resource lives (RFC 9728 §3.1): the well-known path
|
|
38
|
+
* inserted between the origin and the resource's path (`https://h/mcp` →
|
|
39
|
+
* `https://h/.well-known/oauth-protected-resource/mcp`; `https://h` → `…/oauth-protected-resource`).
|
|
40
|
+
*/
|
|
41
|
+
export function metadataLocation(uri) {
|
|
42
|
+
const url = new URL(uri);
|
|
43
|
+
const rest = url.pathname === '/' ? '' : url.pathname;
|
|
44
|
+
const path = `/.well-known/oauth-protected-resource${rest}`;
|
|
45
|
+
return { url: `${url.origin}${path}`, path };
|
|
46
|
+
}
|
|
47
|
+
// ---- Scopes -----------------------------------------------------------------------------------
|
|
48
|
+
/** A space-separated scope string as a list without repeats (order kept); empty for none. */
|
|
49
|
+
export function parseScope(value) {
|
|
50
|
+
if (value === undefined)
|
|
51
|
+
return [];
|
|
52
|
+
const list = [];
|
|
53
|
+
for (const one of value.split(' '))
|
|
54
|
+
if (one !== '' && !list.includes(one))
|
|
55
|
+
list.push(one);
|
|
56
|
+
return list;
|
|
57
|
+
}
|
|
58
|
+
export function formatScope(scopes) {
|
|
59
|
+
return scopes.join(' ');
|
|
60
|
+
}
|
|
61
|
+
// ---- Redirect URIs ----------------------------------------------------------------------------
|
|
62
|
+
const LOOPBACK = /^(https?):\/\/(127\.0\.0\.1|\[::1\]|localhost)(?::(\d{1,5}))?([/?][^#]*)?$/;
|
|
63
|
+
function loopbackParts(uri) {
|
|
64
|
+
const match = LOOPBACK.exec(uri);
|
|
65
|
+
if (!match)
|
|
66
|
+
return undefined;
|
|
67
|
+
const port = match[3];
|
|
68
|
+
if (port !== undefined && (Number(port) < 1 || Number(port) > 65535))
|
|
69
|
+
return undefined;
|
|
70
|
+
return { scheme: match[1], host: match[2], rest: match[4] ?? '' };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Whether a presented redirect URI matches a registered one: the exact string, never a prefix or
|
|
74
|
+
* a normalized form (that would be an open redirect). The one exception (RFC 8252 §7.3): on a
|
|
75
|
+
* loopback host (`127.0.0.1`, `[::1]`, `localhost`) only the port may differ — the scheme, the
|
|
76
|
+
* host (`localhost` and `127.0.0.1` are different hosts), the path and the query must not.
|
|
77
|
+
*/
|
|
78
|
+
export function redirectMatches(registered, presented) {
|
|
79
|
+
if (registered === presented)
|
|
80
|
+
return true;
|
|
81
|
+
const left = loopbackParts(registered);
|
|
82
|
+
const right = left && loopbackParts(presented);
|
|
83
|
+
return (left !== undefined &&
|
|
84
|
+
right !== undefined &&
|
|
85
|
+
left.scheme === right.scheme &&
|
|
86
|
+
left.host === right.host &&
|
|
87
|
+
left.rest === right.rest);
|
|
88
|
+
}
|
|
89
|
+
export function isLoopbackUri(uri) {
|
|
90
|
+
return loopbackParts(uri) !== undefined;
|
|
91
|
+
}
|
|
92
|
+
/** The host a redirect goes to (`claude.ai`), for the consent data and logs. */
|
|
93
|
+
export function hostOf(uri) {
|
|
94
|
+
try {
|
|
95
|
+
return new URL(uri).host;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (!(error instanceof TypeError))
|
|
99
|
+
throw error;
|
|
100
|
+
return '';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Why a redirect URI cannot be registered (CIMD documents, registration, options), or undefined:
|
|
105
|
+
* an absolute https URI, or http on a loopback host; no fragment, no user information.
|
|
106
|
+
*/
|
|
107
|
+
export function redirectProblem(uri) {
|
|
108
|
+
if (typeof uri !== 'string' || uri.length === 0 || uri.length > 2048)
|
|
109
|
+
return 'a redirect URI must be a string of 1 to 2048 characters';
|
|
110
|
+
let url;
|
|
111
|
+
try {
|
|
112
|
+
url = new URL(uri);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (!(error instanceof TypeError))
|
|
116
|
+
throw error;
|
|
117
|
+
return 'a redirect URI must be an absolute URI';
|
|
118
|
+
}
|
|
119
|
+
if (uri.includes('#'))
|
|
120
|
+
return 'a redirect URI must not have a fragment (RFC 6749 §3.1.2)';
|
|
121
|
+
if (url.username || url.password)
|
|
122
|
+
return 'a redirect URI must not carry user information';
|
|
123
|
+
if (url.protocol === 'https:')
|
|
124
|
+
return undefined;
|
|
125
|
+
if (url.protocol === 'http:' && isLoopbackUri(uri))
|
|
126
|
+
return undefined;
|
|
127
|
+
return 'a redirect URI must use https (http only on 127.0.0.1, [::1] or localhost)';
|
|
128
|
+
}
|
|
129
|
+
/** `uri` with the query parameters added (the existing query is kept). */
|
|
130
|
+
export function withQuery(uri, values) {
|
|
131
|
+
const url = new URL(uri);
|
|
132
|
+
for (const [key, value] of Object.entries(values))
|
|
133
|
+
if (value !== undefined)
|
|
134
|
+
url.searchParams.append(key, value);
|
|
135
|
+
return url.href;
|
|
136
|
+
}
|
|
137
|
+
// ---- PKCE (S256 only) -------------------------------------------------------------------------
|
|
138
|
+
/** RFC 7636 §4.1: 43 to 128 unreserved characters. */
|
|
139
|
+
const VERIFIER = /^[A-Za-z0-9\-._~]{43,128}$/;
|
|
140
|
+
/** BASE64URL(SHA256(verifier)): 43 characters without padding. */
|
|
141
|
+
export const CHALLENGE = /^[A-Za-z0-9_-]{43}$/;
|
|
142
|
+
export function s256(verifier) {
|
|
143
|
+
return createHash('sha256').update(verifier, 'ascii').digest('base64url');
|
|
144
|
+
}
|
|
145
|
+
/** Whether `verifier` belongs to `challenge` (S256), in constant time. */
|
|
146
|
+
export function verifyPkce(verifier, challenge) {
|
|
147
|
+
if (!VERIFIER.test(verifier) || !CHALLENGE.test(challenge))
|
|
148
|
+
return false;
|
|
149
|
+
const expected = Buffer.from(challenge, 'ascii');
|
|
150
|
+
const actual = Buffer.from(s256(verifier), 'ascii');
|
|
151
|
+
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
|
152
|
+
}
|
|
153
|
+
function decode(text) {
|
|
154
|
+
try {
|
|
155
|
+
return decodeURIComponent(text.replace(/\+/g, ' '));
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
// A broken %-escape: an answer of the parser (the request is refused).
|
|
159
|
+
if (!(error instanceof URIError))
|
|
160
|
+
throw error;
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Every name and its values (repeats kept), or the name whose escape is broken. Parameters
|
|
166
|
+
* without a value count as given (for repeats) and are dropped by `strictParams`.
|
|
167
|
+
*/
|
|
168
|
+
export function paramList(raw) {
|
|
169
|
+
const list = new Map();
|
|
170
|
+
const text = raw.startsWith('?') ? raw.slice(1) : raw;
|
|
171
|
+
for (const pair of text.split('&')) {
|
|
172
|
+
if (pair === '')
|
|
173
|
+
continue;
|
|
174
|
+
const at = pair.indexOf('=');
|
|
175
|
+
const rawName = at === -1 ? pair : pair.slice(0, at);
|
|
176
|
+
const name = decode(rawName);
|
|
177
|
+
if (name === null)
|
|
178
|
+
return { broken: rawName };
|
|
179
|
+
const value = decode(at === -1 ? '' : pair.slice(at + 1));
|
|
180
|
+
if (value === null)
|
|
181
|
+
return { broken: name };
|
|
182
|
+
const values = list.get(name);
|
|
183
|
+
if (values)
|
|
184
|
+
values.push(value);
|
|
185
|
+
else
|
|
186
|
+
list.set(name, [value]);
|
|
187
|
+
}
|
|
188
|
+
return list;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* One value per name or a refusal (RFC 6749 §3.1: parameters MUST NOT be included more than
|
|
192
|
+
* once). A web framework silently keeps the last of `resource=A&resource=B`: the value checked and
|
|
193
|
+
* the value used could differ. Empty values are treated as omitted (§3.1), but still count as a
|
|
194
|
+
* repeat.
|
|
195
|
+
*/
|
|
196
|
+
export function strictParams(list) {
|
|
197
|
+
if (!(list instanceof Map))
|
|
198
|
+
return { ok: false, reason: 'encoding', name: list.broken };
|
|
199
|
+
const params = Object.create(null);
|
|
200
|
+
for (const [name, values] of list) {
|
|
201
|
+
if (values.length > 1)
|
|
202
|
+
return { ok: false, reason: 'repeated', name };
|
|
203
|
+
if (values[0] !== '')
|
|
204
|
+
params[name] = values[0];
|
|
205
|
+
}
|
|
206
|
+
return { ok: true, params };
|
|
207
|
+
}
|
|
208
|
+
/** The query text of a request URL (`/oauth/authorize?a=1` → `a=1`). */
|
|
209
|
+
export function queryOf(url) {
|
|
210
|
+
const at = url.indexOf('?');
|
|
211
|
+
if (at === -1)
|
|
212
|
+
return '';
|
|
213
|
+
const rest = url.slice(at + 1);
|
|
214
|
+
const hash = rest.indexOf('#');
|
|
215
|
+
return hash === -1 ? rest : rest.slice(0, hash);
|
|
216
|
+
}
|
|
217
|
+
/** The parameter names a refusal may name; any other name is not echoed. */
|
|
218
|
+
const KNOWN_PARAMETERS = new Set([
|
|
219
|
+
'response_type',
|
|
220
|
+
'client_id',
|
|
221
|
+
'redirect_uri',
|
|
222
|
+
'scope',
|
|
223
|
+
'state',
|
|
224
|
+
'resource',
|
|
225
|
+
'code_challenge',
|
|
226
|
+
'code_challenge_method',
|
|
227
|
+
'grant_type',
|
|
228
|
+
'code',
|
|
229
|
+
'code_verifier',
|
|
230
|
+
'refresh_token',
|
|
231
|
+
'client_secret',
|
|
232
|
+
'token',
|
|
233
|
+
'token_type_hint',
|
|
234
|
+
'request',
|
|
235
|
+
]);
|
|
236
|
+
/**
|
|
237
|
+
* The sentence of a refused parameter list (for `error_description` and the screen): a known
|
|
238
|
+
* OAuth parameter is named, anything else the request sent is not echoed.
|
|
239
|
+
*/
|
|
240
|
+
export function paramsProblem(result) {
|
|
241
|
+
const name = KNOWN_PARAMETERS.has(result.name) ? `The parameter ${result.name}` : 'A parameter';
|
|
242
|
+
return result.reason === 'repeated'
|
|
243
|
+
? `${name} is given more than once; OAuth parameters are sent once (RFC 6749 §3.1)`
|
|
244
|
+
: `${name} has a broken percent-encoding`;
|
|
245
|
+
}
|
|
246
|
+
/** sha256 of a secret value in base64url (codes; never stored in clear). */
|
|
247
|
+
export function sha256(text) {
|
|
248
|
+
return createHash('sha256').update(text, 'utf8').digest('base64url');
|
|
249
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { AuthApi, AuthRealmApi } from '@dokebi/auth';
|
|
2
|
+
import { type Logger } from '@dokebi/core';
|
|
3
|
+
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
4
|
+
import { type ClientDirectory } from './clients.js';
|
|
5
|
+
import type { OAuthHookContext, OAuthHooks } from './extension.js';
|
|
6
|
+
import { type GrantService, type OAuthGrant } from './grants.js';
|
|
7
|
+
import { type LiveTiming, type OAuthLiveAuthorization } from './live.js';
|
|
8
|
+
import { type OAuthPaths } from './options.js';
|
|
9
|
+
/** A configured resource, checked at setup. */
|
|
10
|
+
export interface Resource {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
/** The configured URI, verbatim (the metadata's `resource`). */
|
|
13
|
+
readonly uri: string;
|
|
14
|
+
readonly key: string;
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly scopes: Readonly<Record<string, string>>;
|
|
17
|
+
readonly defaultScopes: readonly string[];
|
|
18
|
+
/** Where its protected resource metadata lives. */
|
|
19
|
+
readonly metadataUrl: string;
|
|
20
|
+
readonly metadataPath: string;
|
|
21
|
+
}
|
|
22
|
+
/** The part of @dokebi/session this plugin uses, by shape. */
|
|
23
|
+
export interface PendingSession {
|
|
24
|
+
get(key: string): Promise<unknown>;
|
|
25
|
+
/** Stores a JSON value. */
|
|
26
|
+
set(key: string, value: unknown): Promise<void>;
|
|
27
|
+
delete(key: string): Promise<boolean>;
|
|
28
|
+
/** The keys of the session (the pending requests are one key each). */
|
|
29
|
+
keys(): Promise<string[]>;
|
|
30
|
+
/**
|
|
31
|
+
* @dokebi/session one-use values (setOnce / takeOnce): the decision ticket of a consent request,
|
|
32
|
+
* taken in one store step, so a decision posted twice to two workers decides once. Optional: a
|
|
33
|
+
* session store without them answers NOT_SUPPORTED, and the ticket is then this process's only.
|
|
34
|
+
*/
|
|
35
|
+
setOnce?(key: string, value: unknown, options?: {
|
|
36
|
+
readonly ttlMs?: number;
|
|
37
|
+
}): Promise<void>;
|
|
38
|
+
takeOnce?(key: string): Promise<unknown>;
|
|
39
|
+
}
|
|
40
|
+
/** The part of @dokebi/rate-limit this plugin uses, by shape. */
|
|
41
|
+
export interface TokenLimiter {
|
|
42
|
+
consume(key: string): {
|
|
43
|
+
allowed: boolean;
|
|
44
|
+
retryAfterMs: number;
|
|
45
|
+
} | Promise<{
|
|
46
|
+
allowed: boolean;
|
|
47
|
+
retryAfterMs: number;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
/** Default of `pendingMs` (D-0113 §5). */
|
|
51
|
+
export declare const PENDING_MS: 600000;
|
|
52
|
+
/** The `WWW-Authenticate` an error of a protected route carries (the onError hook writes it). */
|
|
53
|
+
export declare function challengeOf(error: unknown): string | undefined;
|
|
54
|
+
export interface ServerSettings {
|
|
55
|
+
readonly issuer: string;
|
|
56
|
+
readonly realm: string;
|
|
57
|
+
readonly paths: OAuthPaths;
|
|
58
|
+
readonly resources: ReadonlyMap<string, Resource>;
|
|
59
|
+
readonly clients: ClientDirectory;
|
|
60
|
+
readonly grants: GrantService;
|
|
61
|
+
readonly auth: AuthApi;
|
|
62
|
+
readonly realmApi: AuthRealmApi;
|
|
63
|
+
readonly session: (request: FastifyRequest) => PendingSession;
|
|
64
|
+
readonly hooks: OAuthHooks | undefined;
|
|
65
|
+
readonly overridden: ReadonlySet<string>;
|
|
66
|
+
readonly hookContext: OAuthHookContext;
|
|
67
|
+
readonly accessMs: number;
|
|
68
|
+
readonly refreshMs: number;
|
|
69
|
+
readonly grantMs: number;
|
|
70
|
+
/** D-0122: a pre-registered client's own grant lifetime (`clients.<id>.grant.maxDays`). */
|
|
71
|
+
readonly clientGrantMs?: ReadonlyMap<string, number>;
|
|
72
|
+
readonly codeMs: number;
|
|
73
|
+
readonly grantCacheMs: number;
|
|
74
|
+
readonly registration: 'closed' | 'open';
|
|
75
|
+
/** Client ID metadata documents accepted (`cimd.enabled`). */
|
|
76
|
+
readonly cimd: boolean;
|
|
77
|
+
readonly limiter: TokenLimiter | undefined;
|
|
78
|
+
readonly logger: Logger;
|
|
79
|
+
/** D-0122: how open Bearer connections are checked (`live`); default 5000 and 1000 ms. */
|
|
80
|
+
readonly liveTiming?: LiveTiming;
|
|
81
|
+
/** dokebi.yaml limits (D-0113 §5); each missing one has its default. */
|
|
82
|
+
readonly limits?: Partial<ServerLimits>;
|
|
83
|
+
}
|
|
84
|
+
/** The limits of the endpoints (dokebi-oauth options, D-0113 §5). */
|
|
85
|
+
export interface ServerLimits {
|
|
86
|
+
readonly pendingMs: number;
|
|
87
|
+
readonly maxPending: number;
|
|
88
|
+
readonly registrationLimit: number;
|
|
89
|
+
readonly registrationWindowMs: number;
|
|
90
|
+
readonly maxTrackedAddresses: number;
|
|
91
|
+
readonly maxCachedGrants: number;
|
|
92
|
+
}
|
|
93
|
+
/** The protected-route slot of a request. */
|
|
94
|
+
export interface RequestGrant {
|
|
95
|
+
readonly grant: OAuthGrant;
|
|
96
|
+
readonly resource: string;
|
|
97
|
+
/** The scopes of this request: the grant's that this access token holds. */
|
|
98
|
+
readonly scopes: readonly string[];
|
|
99
|
+
/** D-0122, D-0124: the authentication time this token chain carries (exact), or null. */
|
|
100
|
+
readonly authTime: Date | null;
|
|
101
|
+
}
|
|
102
|
+
/** The abilities of a scope-less token (a resource without scopes): no ability at all. */
|
|
103
|
+
export declare const NO_SCOPE = "dokebi-oauth:none";
|
|
104
|
+
/** OAuth scopes as dokebi-auth token abilities (the option grammar makes them fit). */
|
|
105
|
+
export declare function abilitiesOf(scopes: readonly string[]): string[];
|
|
106
|
+
export declare class OAuthServer {
|
|
107
|
+
#private;
|
|
108
|
+
constructor(settings: ServerSettings);
|
|
109
|
+
/** The one lookup of a resource by a URI a client sent (`resourceKey`). */
|
|
110
|
+
resourceByUri(uri: string): Resource | undefined;
|
|
111
|
+
authorize(request: FastifyRequest, reply: FastifyReply): Promise<unknown>;
|
|
112
|
+
decide(request: FastifyRequest, reply: FastifyReply): Promise<unknown>;
|
|
113
|
+
token(request: FastifyRequest, reply: FastifyReply): Promise<unknown>;
|
|
114
|
+
revoke(request: FastifyRequest, reply: FastifyReply): Promise<unknown>;
|
|
115
|
+
register(request: FastifyRequest, reply: FastifyReply): Promise<unknown>;
|
|
116
|
+
/** RFC 8414, derived from the options only. */
|
|
117
|
+
serverMetadata(): Record<string, unknown>;
|
|
118
|
+
/** RFC 9728 for one resource: `resource` exactly as configured. */
|
|
119
|
+
resourceMetadata(resource: Resource): Record<string, unknown>;
|
|
120
|
+
/** The resource whose metadata lives at a path, or undefined. */
|
|
121
|
+
resourceAtPath(path: string): Resource | undefined;
|
|
122
|
+
/** The informative 404 of an unknown metadata path: where this app's metadata is. */
|
|
123
|
+
metadataNotFound(): Record<string, unknown>;
|
|
124
|
+
/**
|
|
125
|
+
* The check of a protected route: a Bearer token of this resource's audience (dokebi-auth),
|
|
126
|
+
* its user still valid (stamp, active), its grant live (not revoked, not expired; read per
|
|
127
|
+
* request with the short `grantCacheMs` cache) and holding `scopes`.
|
|
128
|
+
*/
|
|
129
|
+
authenticate(request: FastifyRequest, resourceId: string, needed: readonly string[], maxAge?: number | null): Promise<RequestGrant>;
|
|
130
|
+
/**
|
|
131
|
+
* D-0122, D-0124: the live authorization of a connection this request opens with its access
|
|
132
|
+
* token (the request passed `authenticate` first). dokebi-auth's lease of the Bearer request
|
|
133
|
+
* (the token row, its user and the stamp) with the grant and `canDelegate` in the same check:
|
|
134
|
+
* the token's expiry ends it at that instant, a revocation in this process at once, anything
|
|
135
|
+
* else (an account change too) within `live.maxAgeMs`.
|
|
136
|
+
*/
|
|
137
|
+
live(request: FastifyRequest, slot: RequestGrant | undefined): Promise<OAuthLiveAuthorization>;
|
|
138
|
+
}
|
|
139
|
+
/** What a JSON body that did not parse becomes (the handlers answer it in their own format). */
|
|
140
|
+
export declare const BAD_JSON: unique symbol;
|
|
141
|
+
/**
|
|
142
|
+
* Where a form body parsed by this plugin keeps its raw parameter list (every value of every
|
|
143
|
+
* name): the object itself is what other plugins read (`_csrf` of @dokebi/csrf), the list is what
|
|
144
|
+
* the strict checks read.
|
|
145
|
+
*/
|
|
146
|
+
export declare const FORM_PARAMS: unique symbol;
|
|
147
|
+
/** The body of this plugin's urlencoded parser: fields like node:querystring, the list beside. */
|
|
148
|
+
export declare function formBody(text: string): Record<string, unknown>;
|