@dokebi/oauth 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/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
package/dist/options.js
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import { defineOptions, option as o } from '@dokebi/core';
|
|
2
|
+
import { appOption, routeIdsOption } from '@dokebi/web/points';
|
|
3
|
+
/*
|
|
4
|
+
* dokebi-oauth options (D-0091 §5, D-0063 §3, D-0071 vocabulary): this declaration is both the
|
|
5
|
+
* parser definePlugin runs and options.schema.json, which the build writes from it. The
|
|
6
|
+
* authorization server metadata is derived from these options only (never written by hand).
|
|
7
|
+
*/
|
|
8
|
+
const DAY_MS = 86_400_000;
|
|
9
|
+
/** Default of `grant.clientMaxDays`: the ceiling of a client's own `grant.maxDays` (D-0122). */
|
|
10
|
+
export const GRANT_CLIENT_MAX_DAYS = 365;
|
|
11
|
+
/** Defaults of `live` (D-0122), the same bound as dokebi-auth's `live` for sessions. */
|
|
12
|
+
export const LIVE_DEFAULT = Object.freeze({ maxAgeMs: 5_000, renewBeforeMs: 1_000 });
|
|
13
|
+
/**
|
|
14
|
+
* Defaults of the D-0113 §5 limits (dokebi.yaml options); constructors used directly (tests) fall
|
|
15
|
+
* back to them.
|
|
16
|
+
*/
|
|
17
|
+
export const LIMITS_DEFAULT = Object.freeze({
|
|
18
|
+
logRepeatWindowMs: 10_000,
|
|
19
|
+
storeRetryAfterMs: 1_000,
|
|
20
|
+
storeFullRetryAfterMs: 5_000,
|
|
21
|
+
grantTouchMs: 60_000,
|
|
22
|
+
maxCachedGrants: 10_000,
|
|
23
|
+
codeGraceMs: 600_000,
|
|
24
|
+
clientUnusedMs: 30 * DAY_MS,
|
|
25
|
+
refreshMs: 30 * DAY_MS,
|
|
26
|
+
registrationLimit: 20,
|
|
27
|
+
registrationWindowMs: 3_600_000,
|
|
28
|
+
maxTrackedAddresses: 10_000,
|
|
29
|
+
pendingMs: 600_000,
|
|
30
|
+
maxPending: 5,
|
|
31
|
+
maxBodyBytes: 65_536,
|
|
32
|
+
maxCodes: 100_000,
|
|
33
|
+
maxClients: 100_000,
|
|
34
|
+
maxPerClient: 20,
|
|
35
|
+
});
|
|
36
|
+
/** Defaults of the `cimd` limits (D-0113 §5). */
|
|
37
|
+
export const CIMD_LIMITS_DEFAULT = Object.freeze({
|
|
38
|
+
maxFetches: 16,
|
|
39
|
+
maxFetchesPerAddress: 2,
|
|
40
|
+
refusalCacheMs: 300_000,
|
|
41
|
+
maxRefused: 1_000,
|
|
42
|
+
maxBytes: 16 * 1024,
|
|
43
|
+
maxHeaderBytes: 8 * 1024,
|
|
44
|
+
minCacheMs: 300_000,
|
|
45
|
+
maxCacheMs: DAY_MS,
|
|
46
|
+
defaultCacheMs: 3_600_000,
|
|
47
|
+
maxCached: 1_000,
|
|
48
|
+
});
|
|
49
|
+
/** An origin without a path: `https://example.com`, `http://localhost:3000`. */
|
|
50
|
+
export const ISSUER = /^https?:\/\/[a-z0-9.-]{1,253}(?::[0-9]{1,5})?$/;
|
|
51
|
+
/** A realm of dokebi-auth. */
|
|
52
|
+
export const REALM_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
53
|
+
/** A resource id: also the audience of its tokens (dokebi-auth `aud:<id>`). */
|
|
54
|
+
export const RESOURCE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
55
|
+
/**
|
|
56
|
+
* A scope name: an RFC 6749 §3.3 scope-token that is also a dokebi-auth token ability (the
|
|
57
|
+
* tokens carry their scopes as abilities): letters, digits, `_`, `:`, `-`, dot-separated parts.
|
|
58
|
+
*/
|
|
59
|
+
export const SCOPE_TOKEN = /^(?=.{1,128}$)[A-Za-z0-9_:-]+(?:\.[A-Za-z0-9_:-]+)*$/;
|
|
60
|
+
/** A pre-registered client id: no URL (those are client ID metadata documents). */
|
|
61
|
+
export const CLIENT_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
|
62
|
+
const CONNECTION = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
63
|
+
const NAMESPACE = /^[A-Za-z0-9][A-Za-z0-9:._-]{0,127}$/;
|
|
64
|
+
/** An SQL table prefix: lower case letters, digits and `_` (optionally schema-qualified). */
|
|
65
|
+
const TABLE_PREFIX = /^[a-z_][a-z0-9_]{0,40}(?:\.[a-z_][a-z0-9_]{0,40})?$/;
|
|
66
|
+
const HOST = /^[a-z0-9.-]{1,253}$/;
|
|
67
|
+
/** A route path of the plugin's endpoints. */
|
|
68
|
+
const PATH = /^\/(?![/\\])[A-Za-z0-9._~!$&'()*+,;=:@%/-]{0,255}$/;
|
|
69
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
70
|
+
const POLICY = /^[a-z][a-z0-9-]{0,31}$/;
|
|
71
|
+
const resource = o.object({
|
|
72
|
+
uri: o.string({
|
|
73
|
+
minLength: 8,
|
|
74
|
+
maxLength: 2048,
|
|
75
|
+
required: true,
|
|
76
|
+
description: 'The resource URI (RFC 8707), exactly as clients send it and as the protected resource metadata says it: https://example.com/mcp. Compared with one function (scheme and host case, default port, empty path = /)',
|
|
77
|
+
}),
|
|
78
|
+
name: o.string({
|
|
79
|
+
minLength: 1,
|
|
80
|
+
maxLength: 200,
|
|
81
|
+
description: 'Human name of the resource (resource_name in its metadata, the consent data)',
|
|
82
|
+
}),
|
|
83
|
+
scopes: o.record(o.string({ minLength: 1, maxLength: 200 }), {
|
|
84
|
+
default: {},
|
|
85
|
+
keys: { pattern: SCOPE_TOKEN },
|
|
86
|
+
maxProperties: 64,
|
|
87
|
+
description: 'Scopes of this resource and their labels: { "tools:read": "Read your projects" }. Requests may ask only for these',
|
|
88
|
+
}),
|
|
89
|
+
defaultScopes: o.array(o.string({ pattern: SCOPE_TOKEN }), {
|
|
90
|
+
default: [],
|
|
91
|
+
maxItems: 64,
|
|
92
|
+
uniqueItems: true,
|
|
93
|
+
description: 'Scopes a request without scope gets (also the scope of the 401 challenge); each one must be in scopes',
|
|
94
|
+
}),
|
|
95
|
+
app: appOption('The app or apps of @dokebi/app whose hosts serve this resource metadata (RFC 9728, at the origin of uri; D-0132): the app of the uri host, for example mcp. Default dokebi-oauth.app; neither set: every host'),
|
|
96
|
+
}, { description: 'One protected resource of this application' });
|
|
97
|
+
const client = o.object({
|
|
98
|
+
name: o.string({
|
|
99
|
+
minLength: 1,
|
|
100
|
+
maxLength: 200,
|
|
101
|
+
required: true,
|
|
102
|
+
description: 'The name the consent data shows (after the client id)',
|
|
103
|
+
}),
|
|
104
|
+
redirectUris: o.array(o.string({ minLength: 10, maxLength: 2048 }), {
|
|
105
|
+
minItems: 1,
|
|
106
|
+
maxItems: 16,
|
|
107
|
+
uniqueItems: true,
|
|
108
|
+
required: true,
|
|
109
|
+
description: 'Exact redirect URIs (https, or http on 127.0.0.1, [::1] or localhost where only the port may differ)',
|
|
110
|
+
}),
|
|
111
|
+
secretSha256: o.string({
|
|
112
|
+
pattern: SHA256_HEX,
|
|
113
|
+
secret: true,
|
|
114
|
+
description: 'sha256 of the client secret in lower-case hex (the secret itself is never stored): a confidential client authenticating with client_secret_basic or client_secret_post. Absent: a public client (none)',
|
|
115
|
+
}),
|
|
116
|
+
grant: o.object({
|
|
117
|
+
maxDays: o.integer({
|
|
118
|
+
minimum: 1,
|
|
119
|
+
maximum: 3_650,
|
|
120
|
+
required: true,
|
|
121
|
+
description: "Absolute lifetime of this client's grants in days, instead of grant.maxDays (at most grant.clientMaxDays; D-0122)",
|
|
122
|
+
}),
|
|
123
|
+
}, { description: "This client's own grant lifetime (absent: grant.maxDays)" }),
|
|
124
|
+
}, { description: 'A pre-registered client' });
|
|
125
|
+
const memoryStore = o.object({
|
|
126
|
+
driver: o.literal('memory', {
|
|
127
|
+
default: 'memory',
|
|
128
|
+
description: 'One process, in memory (the default); lost on restart',
|
|
129
|
+
}),
|
|
130
|
+
maxGrants: o.integer({
|
|
131
|
+
minimum: 1,
|
|
132
|
+
maximum: 10_000_000,
|
|
133
|
+
default: 100_000,
|
|
134
|
+
description: 'Grants kept at once; expired and revoked ones make room, live ones are never dropped (a new grant is refused when full)',
|
|
135
|
+
}),
|
|
136
|
+
maxGrantsPerUser: o.integer({
|
|
137
|
+
minimum: 1,
|
|
138
|
+
maximum: 100_000,
|
|
139
|
+
default: 100,
|
|
140
|
+
description: 'Live grants one user may hold; beyond it a new grant is refused',
|
|
141
|
+
}),
|
|
142
|
+
maxCodes: o.integer({
|
|
143
|
+
minimum: 1,
|
|
144
|
+
maximum: 10_000_000,
|
|
145
|
+
default: LIMITS_DEFAULT.maxCodes,
|
|
146
|
+
description: 'Authorization codes kept at once (each lives codeMs); beyond it the oldest go first (D-0113)',
|
|
147
|
+
}),
|
|
148
|
+
maxClients: o.integer({
|
|
149
|
+
minimum: 1,
|
|
150
|
+
maximum: 10_000_000,
|
|
151
|
+
default: LIMITS_DEFAULT.maxClients,
|
|
152
|
+
description: 'Dynamically registered clients kept at once; beyond it a registration is refused with 503 STORE_FULL (D-0113)',
|
|
153
|
+
}),
|
|
154
|
+
});
|
|
155
|
+
const sqlStore = o.object({
|
|
156
|
+
driver: o.literal('sql', { required: true }),
|
|
157
|
+
connection: o.string({
|
|
158
|
+
pattern: CONNECTION,
|
|
159
|
+
required: true,
|
|
160
|
+
description: 'Connection name in dokebi-db.connections (a pg, mysql2 or mariadb pool); the tables come from a migration',
|
|
161
|
+
}),
|
|
162
|
+
tablePrefix: o.string({
|
|
163
|
+
pattern: TABLE_PREFIX,
|
|
164
|
+
default: 'dokebi_oauth',
|
|
165
|
+
description: 'Prefix of the three tables <prefix>_grants, <prefix>_codes and <prefix>_clients',
|
|
166
|
+
}),
|
|
167
|
+
});
|
|
168
|
+
const redisStore = o.object({
|
|
169
|
+
driver: o.literal('redis', { required: true }),
|
|
170
|
+
connection: o.string({
|
|
171
|
+
pattern: CONNECTION,
|
|
172
|
+
required: true,
|
|
173
|
+
description: 'Connection name in dokebi-redis.connections',
|
|
174
|
+
}),
|
|
175
|
+
namespace: o.string({ pattern: NAMESPACE, default: 'dokebi' }),
|
|
176
|
+
});
|
|
177
|
+
const declaration = defineOptions({
|
|
178
|
+
issuer: o.string({
|
|
179
|
+
pattern: ISSUER,
|
|
180
|
+
required: true,
|
|
181
|
+
description: 'The authorization server identifier: an origin without a path (https://example.com). http only on localhost and *.localhost',
|
|
182
|
+
}),
|
|
183
|
+
realm: o.string({
|
|
184
|
+
pattern: REALM_NAME,
|
|
185
|
+
default: 'default',
|
|
186
|
+
description: 'The dokebi-auth realm whose users grant access',
|
|
187
|
+
}),
|
|
188
|
+
app: appOption('The app or apps of @dokebi/app whose hosts answer the authorization server (D-0132): its metadata, authorize (the consent page), token, revoke and register, at the issuer origin, for example studio. Other hosts get the 404 of an unknown route. Unset: every host'),
|
|
189
|
+
resources: o.record(resource, {
|
|
190
|
+
keys: { pattern: RESOURCE_ID },
|
|
191
|
+
minProperties: 1,
|
|
192
|
+
maxProperties: 32,
|
|
193
|
+
required: true,
|
|
194
|
+
description: 'Protected resources by id (the id is the audience of their tokens and the resource of config.oauth): { mcp: { uri, name?, scopes, defaultScopes } }',
|
|
195
|
+
}),
|
|
196
|
+
clients: o.record(client, {
|
|
197
|
+
default: {},
|
|
198
|
+
keys: { pattern: CLIENT_ID },
|
|
199
|
+
maxProperties: 256,
|
|
200
|
+
description: 'Pre-registered clients by client_id',
|
|
201
|
+
}),
|
|
202
|
+
cimd: o.object({
|
|
203
|
+
enabled: o.boolean({
|
|
204
|
+
default: true,
|
|
205
|
+
description: 'Accept client ID metadata documents: an https client_id URL whose JSON document this server fetches',
|
|
206
|
+
}),
|
|
207
|
+
allowHosts: o.array(o.string({ pattern: HOST }), {
|
|
208
|
+
default: [],
|
|
209
|
+
maxItems: 64,
|
|
210
|
+
uniqueItems: true,
|
|
211
|
+
description: 'Hosts whose documents are accepted (lower case); empty: every public https host',
|
|
212
|
+
}),
|
|
213
|
+
timeoutMs: o.integer({
|
|
214
|
+
minimum: 500,
|
|
215
|
+
maximum: 10_000,
|
|
216
|
+
default: 3_000,
|
|
217
|
+
description: 'The whole fetch of one document (headers and body)',
|
|
218
|
+
}),
|
|
219
|
+
maxFetches: o.integer({
|
|
220
|
+
minimum: 1,
|
|
221
|
+
maximum: 1_024,
|
|
222
|
+
default: CIMD_LIMITS_DEFAULT.maxFetches,
|
|
223
|
+
description: 'Document fetches running at once per process (each an outbound request to a host the client named); beyond it the authorize request answers busy',
|
|
224
|
+
}),
|
|
225
|
+
maxFetchesPerAddress: o.integer({
|
|
226
|
+
minimum: 1,
|
|
227
|
+
maximum: 1_024,
|
|
228
|
+
default: CIMD_LIMITS_DEFAULT.maxFetchesPerAddress,
|
|
229
|
+
description: 'Document fetches one client address (IPv6 by /64) may start at once',
|
|
230
|
+
}),
|
|
231
|
+
refusalCacheMs: o.integer({
|
|
232
|
+
minimum: 0,
|
|
233
|
+
maximum: DAY_MS,
|
|
234
|
+
default: CIMD_LIMITS_DEFAULT.refusalCacheMs,
|
|
235
|
+
description: 'A refused document URL is not fetched again for this long (0: every request fetches again)',
|
|
236
|
+
}),
|
|
237
|
+
maxRefused: o.integer({
|
|
238
|
+
minimum: 1,
|
|
239
|
+
maximum: 1_000_000,
|
|
240
|
+
default: CIMD_LIMITS_DEFAULT.maxRefused,
|
|
241
|
+
description: 'Refused document URLs remembered at once; beyond it the oldest go',
|
|
242
|
+
}),
|
|
243
|
+
maxBytes: o.integer({
|
|
244
|
+
minimum: 1_024,
|
|
245
|
+
maximum: 1_048_576,
|
|
246
|
+
default: CIMD_LIMITS_DEFAULT.maxBytes,
|
|
247
|
+
description: 'Largest document body in bytes; a larger one is refused',
|
|
248
|
+
}),
|
|
249
|
+
maxHeaderBytes: o.integer({
|
|
250
|
+
minimum: 1_024,
|
|
251
|
+
maximum: 65_536,
|
|
252
|
+
default: CIMD_LIMITS_DEFAULT.maxHeaderBytes,
|
|
253
|
+
description: 'Largest response head of a document fetch in bytes',
|
|
254
|
+
}),
|
|
255
|
+
minCacheMs: o.integer({
|
|
256
|
+
minimum: 0,
|
|
257
|
+
maximum: 7 * DAY_MS,
|
|
258
|
+
default: CIMD_LIMITS_DEFAULT.minCacheMs,
|
|
259
|
+
description: 'A fetched document is kept at least this long, also with no-store or a shorter max-age',
|
|
260
|
+
}),
|
|
261
|
+
maxCacheMs: o.integer({
|
|
262
|
+
minimum: 60_000,
|
|
263
|
+
maximum: 7 * DAY_MS,
|
|
264
|
+
default: CIMD_LIMITS_DEFAULT.maxCacheMs,
|
|
265
|
+
description: 'A fetched document is kept at most this long, whatever its max-age says',
|
|
266
|
+
}),
|
|
267
|
+
defaultCacheMs: o.integer({
|
|
268
|
+
minimum: 0,
|
|
269
|
+
maximum: 7 * DAY_MS,
|
|
270
|
+
default: CIMD_LIMITS_DEFAULT.defaultCacheMs,
|
|
271
|
+
description: 'How long a document without Cache-Control max-age is kept',
|
|
272
|
+
}),
|
|
273
|
+
maxCached: o.integer({
|
|
274
|
+
minimum: 1,
|
|
275
|
+
maximum: 1_000_000,
|
|
276
|
+
default: CIMD_LIMITS_DEFAULT.maxCached,
|
|
277
|
+
description: 'Fetched documents kept at once; beyond it the oldest go',
|
|
278
|
+
}),
|
|
279
|
+
}, {
|
|
280
|
+
description: 'Client ID metadata documents (CIMD)',
|
|
281
|
+
checks: [
|
|
282
|
+
{
|
|
283
|
+
path: 'minCacheMs',
|
|
284
|
+
message: 'must be at most defaultCacheMs and maxCacheMs',
|
|
285
|
+
test: (cimd) => cimd.minCacheMs <= cimd.defaultCacheMs && cimd.minCacheMs <= cimd.maxCacheMs,
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
path: 'defaultCacheMs',
|
|
289
|
+
message: 'must be at most maxCacheMs',
|
|
290
|
+
test: (cimd) => cimd.defaultCacheMs <= cimd.maxCacheMs,
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
}),
|
|
294
|
+
registration: o.enum(['closed', 'open'], {
|
|
295
|
+
default: 'closed',
|
|
296
|
+
description: 'Dynamic client registration (RFC 7591): closed (POST /oauth/register answers 404) or open (public clients only, registrationLimit per address and registrationWindowMs, removed after clientUnusedMs unused)',
|
|
297
|
+
}),
|
|
298
|
+
registrationLimit: o.integer({
|
|
299
|
+
minimum: 1,
|
|
300
|
+
maximum: 1_000_000,
|
|
301
|
+
default: LIMITS_DEFAULT.registrationLimit,
|
|
302
|
+
description: 'Registrations one client address (IPv6 by /64) may make per registrationWindowMs; beyond it 429 with Retry-After (D-0113)',
|
|
303
|
+
}),
|
|
304
|
+
registrationWindowMs: o.integer({
|
|
305
|
+
minimum: 1_000,
|
|
306
|
+
maximum: DAY_MS,
|
|
307
|
+
default: LIMITS_DEFAULT.registrationWindowMs,
|
|
308
|
+
description: 'The window of registrationLimit (default one hour)',
|
|
309
|
+
}),
|
|
310
|
+
clientUnusedMs: o.integer({
|
|
311
|
+
minimum: DAY_MS,
|
|
312
|
+
maximum: 3_650 * DAY_MS,
|
|
313
|
+
default: LIMITS_DEFAULT.clientUnusedMs,
|
|
314
|
+
description: 'A registered client unused this long is removed by purge (sql, memory) or expires (redis); default 30 days. Never less than refreshMs: a client stays while a refresh token it was given may still run',
|
|
315
|
+
}),
|
|
316
|
+
maxTrackedAddresses: o.integer({
|
|
317
|
+
minimum: 1,
|
|
318
|
+
maximum: 10_000_000,
|
|
319
|
+
default: LIMITS_DEFAULT.maxTrackedAddresses,
|
|
320
|
+
description: 'Client addresses whose registrations and document fetches this process counts; beyond it the least recently seen are forgotten',
|
|
321
|
+
}),
|
|
322
|
+
accessMs: o.integer({
|
|
323
|
+
minimum: 360_000,
|
|
324
|
+
maximum: DAY_MS,
|
|
325
|
+
default: 900_000,
|
|
326
|
+
description: 'Access token lifetime (default 15 minutes; more than 5 minutes, or Claude Code refreshes before every call)',
|
|
327
|
+
}),
|
|
328
|
+
refreshMs: o.integer({
|
|
329
|
+
minimum: 3_600_000,
|
|
330
|
+
maximum: 365 * DAY_MS,
|
|
331
|
+
default: LIMITS_DEFAULT.refreshMs,
|
|
332
|
+
description: 'Refresh token lifetime (default 30 days; one use each). A registered client is kept at least this long after its last use (clientUnusedMs)',
|
|
333
|
+
}),
|
|
334
|
+
grant: o.object({
|
|
335
|
+
maxDays: o.integer({
|
|
336
|
+
minimum: 1,
|
|
337
|
+
maximum: 3_650,
|
|
338
|
+
default: 90,
|
|
339
|
+
description: 'Absolute lifetime of a grant from the consent; refreshes never extend it (consent again after it for a new grant)',
|
|
340
|
+
}),
|
|
341
|
+
clientMaxDays: o.integer({
|
|
342
|
+
minimum: 1,
|
|
343
|
+
maximum: 3_650,
|
|
344
|
+
default: GRANT_CLIENT_MAX_DAYS,
|
|
345
|
+
description: 'The longest clients.<id>.grant.maxDays a pre-registered client may set (default 365); a longer one is refused at start (D-0122)',
|
|
346
|
+
}),
|
|
347
|
+
maxPerClient: o.integer({
|
|
348
|
+
minimum: 1,
|
|
349
|
+
maximum: 1_000,
|
|
350
|
+
default: LIMITS_DEFAULT.maxPerClient,
|
|
351
|
+
description: 'Live grants (connected devices) one user may hold for one client (D-0124). A new authorization beyond it revokes the least recently used one, those without a live token first, and logs warn OAUTH_GRANT_EVICTED; it never refuses the new device. With the memory store at most store.maxGrantsPerUser',
|
|
352
|
+
}),
|
|
353
|
+
}, { description: 'Grants (one per authorization: every device that connects gets its own)' }),
|
|
354
|
+
live: o.object({
|
|
355
|
+
maxAgeMs: o.integer({
|
|
356
|
+
minimum: 100,
|
|
357
|
+
maximum: 60_000,
|
|
358
|
+
default: LIVE_DEFAULT.maxAgeMs,
|
|
359
|
+
description: 'api.live(): an SSE or WebSocket connection opened with an access token ends at most this long after the token or its grant was revoked elsewhere (D-0122); its expiry ends it at once. Each open connection rereads its token, grant and user about every maxAgeMs - renewBeforeMs',
|
|
360
|
+
}),
|
|
361
|
+
renewBeforeMs: o.integer({
|
|
362
|
+
minimum: 10,
|
|
363
|
+
maximum: 30_000,
|
|
364
|
+
default: LIVE_DEFAULT.renewBeforeMs,
|
|
365
|
+
description: 'How long before its deadline a live connection checks its token again (at most half of maxAgeMs); keep it above the slowest token, grant and user read',
|
|
366
|
+
}),
|
|
367
|
+
}, {
|
|
368
|
+
description: 'Live authorization of Bearer SSE and WebSocket connections (api.live)',
|
|
369
|
+
checks: [
|
|
370
|
+
{
|
|
371
|
+
path: 'renewBeforeMs',
|
|
372
|
+
message: 'must be smaller than maxAgeMs',
|
|
373
|
+
test: (live) => live.renewBeforeMs < live.maxAgeMs,
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
}),
|
|
377
|
+
codeMs: o.integer({
|
|
378
|
+
minimum: 10_000,
|
|
379
|
+
maximum: 600_000,
|
|
380
|
+
default: 60_000,
|
|
381
|
+
description: 'Authorization code lifetime (default 60 seconds; one use)',
|
|
382
|
+
}),
|
|
383
|
+
grantCacheMs: o.integer({
|
|
384
|
+
minimum: 0,
|
|
385
|
+
maximum: 60_000,
|
|
386
|
+
default: 5_000,
|
|
387
|
+
description: 'Protected routes keep a looked-up grant this long in process memory (a revocation through this process applies at once, elsewhere within it)',
|
|
388
|
+
}),
|
|
389
|
+
maxCachedGrants: o.integer({
|
|
390
|
+
minimum: 1,
|
|
391
|
+
maximum: 10_000_000,
|
|
392
|
+
default: LIMITS_DEFAULT.maxCachedGrants,
|
|
393
|
+
description: 'Grants the grantCacheMs cache, the delegation check and the last-use memo keep at once; beyond it the oldest go',
|
|
394
|
+
}),
|
|
395
|
+
grantTouchMs: o.integer({
|
|
396
|
+
minimum: 1_000,
|
|
397
|
+
maximum: DAY_MS,
|
|
398
|
+
default: LIMITS_DEFAULT.grantTouchMs,
|
|
399
|
+
description: 'The lastUsedAt of a grant is written at most this often (default one minute)',
|
|
400
|
+
}),
|
|
401
|
+
codeGraceMs: o.integer({
|
|
402
|
+
minimum: 0,
|
|
403
|
+
maximum: DAY_MS,
|
|
404
|
+
default: LIMITS_DEFAULT.codeGraceMs,
|
|
405
|
+
description: 'An expired code is kept this long before purge removes it, so a replay is still recognized and revokes its grant',
|
|
406
|
+
}),
|
|
407
|
+
pendingMs: o.integer({
|
|
408
|
+
minimum: 10_000,
|
|
409
|
+
maximum: 3_600_000,
|
|
410
|
+
default: LIMITS_DEFAULT.pendingMs,
|
|
411
|
+
description: 'How long an authorization request waits in the session for login and consent (default 10 minutes)',
|
|
412
|
+
}),
|
|
413
|
+
maxPending: o.integer({
|
|
414
|
+
minimum: 1,
|
|
415
|
+
maximum: 100,
|
|
416
|
+
default: LIMITS_DEFAULT.maxPending,
|
|
417
|
+
description: 'Authorization requests one session keeps waiting at once (tabs); beyond it the oldest is dropped',
|
|
418
|
+
}),
|
|
419
|
+
maxBodyBytes: o.integer({
|
|
420
|
+
minimum: 1_024,
|
|
421
|
+
maximum: 1_048_576,
|
|
422
|
+
default: LIMITS_DEFAULT.maxBodyBytes,
|
|
423
|
+
description: 'Largest body of POST /oauth/token, /oauth/revoke and /oauth/register in bytes',
|
|
424
|
+
}),
|
|
425
|
+
storeRetryAfterMs: o.integer({
|
|
426
|
+
minimum: 1,
|
|
427
|
+
maximum: 60_000,
|
|
428
|
+
default: LIMITS_DEFAULT.storeRetryAfterMs,
|
|
429
|
+
description: 'data.retryAfterMs (Retry-After) of 503 STORE_FAILED: a store that did not answer',
|
|
430
|
+
}),
|
|
431
|
+
storeFullRetryAfterMs: o.integer({
|
|
432
|
+
minimum: 1,
|
|
433
|
+
maximum: 600_000,
|
|
434
|
+
default: LIMITS_DEFAULT.storeFullRetryAfterMs,
|
|
435
|
+
description: 'data.retryAfterMs (Retry-After) of 503 STORE_FULL: a full memory store',
|
|
436
|
+
}),
|
|
437
|
+
logRepeatWindowMs: o.integer({
|
|
438
|
+
minimum: 1_000,
|
|
439
|
+
maximum: 3_600_000,
|
|
440
|
+
default: LIMITS_DEFAULT.logRepeatWindowMs,
|
|
441
|
+
description: 'Window of the repeated OAuth logs (OAUTH_STORE_FAILED, OAUTH_STORE_FULL, OAUTH_TOUCH_FAILED, OAUTH_HOOK_FAILED): the first of each in full, the rest of the window counted into one summary with repeated (D-0113)',
|
|
442
|
+
}),
|
|
443
|
+
store: o.union([memoryStore, sqlStore, redisStore], {
|
|
444
|
+
description: 'Grants, codes and registered clients: memory (default), sql (a @dokebi/db connection) or redis (a @dokebi/redis connection)',
|
|
445
|
+
}),
|
|
446
|
+
paths: o.object({
|
|
447
|
+
authorize: o.string({ pattern: PATH, default: '/oauth/authorize', plain: true }),
|
|
448
|
+
token: o.string({ pattern: PATH, default: '/oauth/token', plain: true }),
|
|
449
|
+
revoke: o.string({ pattern: PATH, default: '/oauth/revoke', plain: true }),
|
|
450
|
+
register: o.string({ pattern: PATH, default: '/oauth/register', plain: true }),
|
|
451
|
+
}, { description: 'Endpoint paths (the metadata follows them)' }),
|
|
452
|
+
routeIds: routeIdsOption('dokebi-oauth', {
|
|
453
|
+
serverMetadata: 'GET /.well-known/oauth-authorization-server',
|
|
454
|
+
resourceMetadata: 'GET /.well-known/oauth-protected-resource and /.well-known/oauth-protected-resource/* (one pair per app)',
|
|
455
|
+
authorize: 'GET and POST paths.authorize (the consent page and its decision)',
|
|
456
|
+
token: 'POST paths.token',
|
|
457
|
+
revoke: 'POST paths.revoke',
|
|
458
|
+
register: 'POST paths.register',
|
|
459
|
+
}),
|
|
460
|
+
rateLimit: o.object({
|
|
461
|
+
policy: o.string({
|
|
462
|
+
pattern: POLICY,
|
|
463
|
+
required: true,
|
|
464
|
+
description: 'A @dokebi/rate-limit policy applied to POST /oauth/token, keyed by client and credential',
|
|
465
|
+
}),
|
|
466
|
+
}, { description: 'Limit the token endpoint with @dokebi/rate-limit (absent: no limit)' }),
|
|
467
|
+
}, {
|
|
468
|
+
checks: [
|
|
469
|
+
{
|
|
470
|
+
path: 'clients',
|
|
471
|
+
message: 'a client grant.maxDays must be at most grant.clientMaxDays (default 365)',
|
|
472
|
+
test: (value) => {
|
|
473
|
+
const ceiling = value.grant?.clientMaxDays ?? GRANT_CLIENT_MAX_DAYS;
|
|
474
|
+
return Object.values(value.clients).every((client) => client.grant === undefined || client.grant.maxDays <= ceiling);
|
|
475
|
+
},
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
path: 'grant',
|
|
479
|
+
message: 'grant.maxPerClient must be at most store.maxGrantsPerUser (default 100) with the memory store: it would refuse the grants the bound keeps',
|
|
480
|
+
test: (value) => {
|
|
481
|
+
const store = value.store ?? { driver: 'memory', maxGrantsPerUser: 100 };
|
|
482
|
+
if (store.driver !== 'memory')
|
|
483
|
+
return true;
|
|
484
|
+
return ((value.grant?.maxPerClient ?? LIMITS_DEFAULT.maxPerClient) <= store.maxGrantsPerUser);
|
|
485
|
+
},
|
|
486
|
+
},
|
|
487
|
+
],
|
|
488
|
+
});
|
|
489
|
+
/** The routes dokebi-oauth adds, by the key of `routeIds` (D-0142). */
|
|
490
|
+
export const OAUTH_ROUTE_KEYS = [
|
|
491
|
+
'serverMetadata',
|
|
492
|
+
'resourceMetadata',
|
|
493
|
+
'authorize',
|
|
494
|
+
'token',
|
|
495
|
+
'revoke',
|
|
496
|
+
'register',
|
|
497
|
+
];
|
|
498
|
+
const same = true;
|
|
499
|
+
void same;
|
|
500
|
+
/** The `dokebi-oauth` options declaration (parser and options.schema.json). */
|
|
501
|
+
export const options = declaration;
|
|
502
|
+
export const CIMD_DEFAULT = Object.freeze({
|
|
503
|
+
enabled: true,
|
|
504
|
+
allowHosts: Object.freeze([]),
|
|
505
|
+
timeoutMs: 3_000,
|
|
506
|
+
...CIMD_LIMITS_DEFAULT,
|
|
507
|
+
});
|
|
508
|
+
export const PATHS_DEFAULT = Object.freeze({
|
|
509
|
+
authorize: '/oauth/authorize',
|
|
510
|
+
token: '/oauth/token',
|
|
511
|
+
revoke: '/oauth/revoke',
|
|
512
|
+
register: '/oauth/register',
|
|
513
|
+
});
|
|
514
|
+
export const STORE_DEFAULT = Object.freeze({
|
|
515
|
+
driver: 'memory',
|
|
516
|
+
maxGrants: 100_000,
|
|
517
|
+
maxGrantsPerUser: 100,
|
|
518
|
+
maxCodes: LIMITS_DEFAULT.maxCodes,
|
|
519
|
+
maxClients: LIMITS_DEFAULT.maxClients,
|
|
520
|
+
});
|
|
521
|
+
export const GRANT_MAX_DAYS = 90;
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type PluginFactory } from '@dokebi/core';
|
|
2
|
+
import type { FastifyRequest } from 'fastify';
|
|
3
|
+
import { type CimdFetch } from './cimd.js';
|
|
4
|
+
import { type OAuthGrant, type OAuthGrants } from './grants.js';
|
|
5
|
+
import type { OAuthLiveAuthorization } from './live.js';
|
|
6
|
+
/** What a protected request knows: `request.oauth`. */
|
|
7
|
+
export interface RequestOAuth {
|
|
8
|
+
/** The live grant of a `config.oauth` route's request; undefined elsewhere. */
|
|
9
|
+
readonly grant: OAuthGrant | undefined;
|
|
10
|
+
/** The resource id of the route; undefined elsewhere. */
|
|
11
|
+
readonly resource: string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* The scopes of this request: the grant's that this access token holds (a refresh may have
|
|
14
|
+
* narrowed the token). Empty elsewhere.
|
|
15
|
+
*/
|
|
16
|
+
readonly scopes: readonly string[];
|
|
17
|
+
/** Whether this request holds `scope` (in the grant and in the token). */
|
|
18
|
+
has(scope: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* D-0122, D-0124: when the user last authenticated before the consent of this token chain
|
|
21
|
+
* (dokebi-auth `authenticatedAt()`, whole seconds), or null when that was not known (a session
|
|
22
|
+
* restored from a remember cookie issued before D-0124, tokens issued before D-0122).
|
|
23
|
+
*/
|
|
24
|
+
readonly authTime: Date | null;
|
|
25
|
+
}
|
|
26
|
+
/** The route setting `config.oauth` (D-0091 §5). */
|
|
27
|
+
export interface OAuthRouteSetting {
|
|
28
|
+
/** The resource id (`resources.<id>`). */
|
|
29
|
+
readonly resource: string;
|
|
30
|
+
/** Scopes the grant must hold (each declared by the resource); default none beyond a grant. */
|
|
31
|
+
readonly scopes?: readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* D-0122 (RFC 9470): the token's sign-in must be at most this many seconds old; otherwise 401
|
|
34
|
+
* INSUFFICIENT_USER_AUTHENTICATION with `error="insufficient_user_authentication", max_age`.
|
|
35
|
+
*/
|
|
36
|
+
readonly maxAge?: number;
|
|
37
|
+
}
|
|
38
|
+
/** The plugin API: `app.get(plugin)` or `context.getDependency(dependency)`. */
|
|
39
|
+
export interface OAuthApi {
|
|
40
|
+
readonly issuer: string;
|
|
41
|
+
/** The dokebi-auth realm of the grants. */
|
|
42
|
+
readonly realm: string;
|
|
43
|
+
/** The resource ids. */
|
|
44
|
+
readonly resources: readonly string[];
|
|
45
|
+
/** Grants of the realm's users: `list`, `revoke`, `revokeAll`, `purge`. */
|
|
46
|
+
readonly grants: OAuthGrants;
|
|
47
|
+
/**
|
|
48
|
+
* The check of a protected route, for a route that cannot use `config.oauth` (a WebSocket
|
|
49
|
+
* upgrade, a route of a scope added before this plugin): resolves to the live grant, or throws
|
|
50
|
+
* 401 UNAUTHENTICATED / 403 INSUFFICIENT_SCOPE carrying `WWW-Authenticate: Bearer
|
|
51
|
+
* resource_metadata=…`. Call it before anything reads `request.auth` on that request.
|
|
52
|
+
*/
|
|
53
|
+
authenticate(request: FastifyRequest, settings: {
|
|
54
|
+
readonly resource: string;
|
|
55
|
+
readonly scopes?: readonly string[];
|
|
56
|
+
readonly maxAge?: number;
|
|
57
|
+
}): Promise<OAuthGrant>;
|
|
58
|
+
/**
|
|
59
|
+
* D-0122, D-0124: the live authorization of an SSE stream or WebSocket opened with this
|
|
60
|
+
* request's access token, for `identity.authorization`: dokebi-auth's Bearer lease with the
|
|
61
|
+
* grant in the same check. Call it after `authenticate` (or on a `config.oauth` route). The
|
|
62
|
+
* token's expiry (or the grant's end) ends it at that instant; a revocation through this process
|
|
63
|
+
* at once; a revocation elsewhere, a password change or `logoutOtherDevices` elsewhere, a
|
|
64
|
+
* disabled account or a lost `canDelegate` within `live.maxAgeMs`. INVALID_ARGUMENT on a request
|
|
65
|
+
* without such a token; 401 UNAUTHENTICATED when it stopped being valid since the route check.
|
|
66
|
+
*/
|
|
67
|
+
live(request: FastifyRequest): Promise<OAuthLiveAuthorization>;
|
|
68
|
+
/** `request.oauth` of a request. */
|
|
69
|
+
of(request: FastifyRequest): RequestOAuth;
|
|
70
|
+
/** The protected resource metadata URL of a resource (`WWW-Authenticate`'s resource_metadata). */
|
|
71
|
+
metadataUrl(resource: string): string;
|
|
72
|
+
/**
|
|
73
|
+
* A resource as configured (for a plugin serving it, such as @dokebi/mcp): its URI verbatim, its
|
|
74
|
+
* name, the declared scopes with their labels, the default scopes and the metadata URL. Throws
|
|
75
|
+
* INVALID_ARGUMENT for an unknown resource.
|
|
76
|
+
*/
|
|
77
|
+
resourceInfo(resource: string): OAuthResourceInfo;
|
|
78
|
+
}
|
|
79
|
+
/** `api.resourceInfo(id)`: one resource of `options.dokebi-oauth.resources`, resolved. */
|
|
80
|
+
export interface OAuthResourceInfo {
|
|
81
|
+
readonly id: string;
|
|
82
|
+
/** The configured URI, verbatim (the metadata's `resource`, the audience clients ask for). */
|
|
83
|
+
readonly uri: string;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
/** Declared scope names and their labels. */
|
|
86
|
+
readonly scopes: Readonly<Record<string, string>>;
|
|
87
|
+
readonly defaultScopes: readonly string[];
|
|
88
|
+
/** Where its protected resource metadata lives (RFC 9728). */
|
|
89
|
+
readonly metadataUrl: string;
|
|
90
|
+
}
|
|
91
|
+
/** The user entry module (`src/infra/oauth/extension.ts`), or an extension class. */
|
|
92
|
+
export interface OAuthEntry {
|
|
93
|
+
/** `export default class AppOAuth extends OAuthExtension<UserRow> { … }` */
|
|
94
|
+
readonly default?: unknown;
|
|
95
|
+
}
|
|
96
|
+
export interface OAuthPluginSettings {
|
|
97
|
+
/**
|
|
98
|
+
* Replaces the fetch of client ID metadata documents (tests, an egress proxy). The response and
|
|
99
|
+
* document checks still run; the address checks are then the replacement's job.
|
|
100
|
+
*/
|
|
101
|
+
readonly cimdFetch?: CimdFetch;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The dokebi-oauth plugin factory with settings: `defineOAuthPlugin({ cimdFetch })`.
|
|
105
|
+
* `createPlugin` is `defineOAuthPlugin()`.
|
|
106
|
+
*/
|
|
107
|
+
export declare function defineOAuthPlugin(settings?: OAuthPluginSettings): PluginFactory<OAuthApi>;
|
|
108
|
+
/**
|
|
109
|
+
* Factory input: `{ app, options?, entry }`. Options default to `app.config['dokebi-oauth']`;
|
|
110
|
+
* `entry` is the module of src/infra/oauth/extension.ts (D-0064), required: without
|
|
111
|
+
* `renderConsent` the plugin refuses to start (EXTENSION_REQUIRED).
|
|
112
|
+
*/
|
|
113
|
+
export declare const createPlugin: PluginFactory<OAuthApi>;
|