@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/package.json CHANGED
@@ -1,6 +1,145 @@
1
1
  {
2
2
  "name": "@dokebi/oauth",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "OAuth 2.1 authorization server for @dokebi/web on @dokebi/auth tokens: authorization code with PKCE, refresh rotation, revocation, client ID metadata documents, protected resource metadata and route protection.",
5
+ "keywords": [
6
+ "dokebi",
7
+ "dokebi-plugin",
8
+ "web",
9
+ "oauth",
10
+ "oauth2",
11
+ "authorization-server",
12
+ "pkce",
13
+ "mcp"
14
+ ],
15
+ "private": false,
16
+ "type": "module",
17
+ "license": "MIT",
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./dependency": {
26
+ "types": "./dist/dependency.d.ts",
27
+ "import": "./dist/dependency.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "templates",
33
+ "options.schema.json",
34
+ "README.md",
35
+ "AI.md",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=24.0.0"
40
+ },
41
+ "scripts": {
42
+ "build": "tsc -p tsconfig.json && node -e \"import('./dist/options.js').then(async (m) => { const { renderOptionsSchema } = await import('@dokebi/core'); process.stdout.write(renderOptionsSchema(m.options)); })\" > options.schema.json",
43
+ "typecheck": "tsc -p tsconfig.test.json --noEmit",
44
+ "test": "node -e \"require('node:fs').rmSync('.tmp/tests',{recursive:true,force:true})\" && tsc -p tsconfig.test.json && node --test --test-concurrency=1 \".tmp/tests/tests/*.test.js\""
45
+ },
46
+ "peerDependencies": {
47
+ "@dokebi/core": "^0.4.2",
48
+ "@dokebi/web": "^0.4.3",
49
+ "@dokebi/auth": "^0.4.1",
50
+ "@dokebi/session": "^0.4.4",
51
+ "@dokebi/csrf": "^0.4.0",
52
+ "@dokebi/db": "^0.4.0",
53
+ "@dokebi/redis": "^0.4.0",
54
+ "@dokebi/rate-limit": "^0.1.1",
55
+ "fastify": "^5.12.7"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "@dokebi/csrf": {
59
+ "optional": true
60
+ },
61
+ "@dokebi/db": {
62
+ "optional": true
63
+ },
64
+ "@dokebi/redis": {
65
+ "optional": true
66
+ },
67
+ "@dokebi/rate-limit": {
68
+ "optional": true
69
+ }
70
+ },
71
+ "devDependencies": {
72
+ "@dokebi/auth": "0.4.1",
73
+ "@dokebi/csrf": "0.4.0",
74
+ "@dokebi/db": "0.4.1",
75
+ "@dokebi/rate-limit": "0.1.1",
76
+ "@dokebi/redis": "0.4.1",
77
+ "@dokebi/session": "0.4.4",
78
+ "@dokebi/sse": "0.4.2",
79
+ "@dokebi/web": "0.4.3"
80
+ },
81
+ "dokebi": {
82
+ "schemaVersion": 2,
83
+ "id": "dokebi-oauth",
84
+ "core": "^0.4.2",
85
+ "dependencies": [
86
+ {
87
+ "name": "dokebi-web",
88
+ "version": "^0.4.3"
89
+ },
90
+ {
91
+ "name": "dokebi-auth",
92
+ "version": "^0.4.1"
93
+ },
94
+ {
95
+ "name": "dokebi-session",
96
+ "version": "^0.4.4"
97
+ },
98
+ {
99
+ "name": "dokebi-csrf",
100
+ "version": "^0.4.0",
101
+ "optional": true
102
+ },
103
+ {
104
+ "name": "dokebi-db",
105
+ "version": "^0.4.0",
106
+ "optional": true
107
+ },
108
+ {
109
+ "name": "dokebi-redis",
110
+ "version": "^0.4.0",
111
+ "optional": true
112
+ },
113
+ {
114
+ "name": "dokebi-rate-limit",
115
+ "version": "^0.1.1",
116
+ "optional": true
117
+ }
118
+ ],
119
+ "contributes": [
120
+ "core:describe"
121
+ ],
122
+ "installModule": "dist/install.js",
123
+ "optionsSchema": "options.schema.json",
124
+ "documentation": "AI.md"
125
+ },
126
+ "dokebiCatalog": {
127
+ "kind": "plugin",
128
+ "summary": "OAuth 2.1 authorization server on @dokebi/auth tokens: authorization code with PKCE S256, refresh rotation and reuse detection, revocation, client ID metadata documents, protected resource metadata and protected routes",
129
+ "maturity": "experimental",
130
+ "provides": [
131
+ "oauth.server"
132
+ ],
133
+ "doesNot": [
134
+ "No OpenID Connect: no id_token, userinfo or openid-configuration",
135
+ "No implicit, password, client_credentials or device grants, no introspection, no JWT access tokens, no private_key_jwt",
136
+ "Does not draw the consent screen: the application renders it (renderConsent in src/infra/oauth/extension.ts)",
137
+ "Dynamic client registration is closed unless registration: open (public clients only)"
138
+ ],
139
+ "recipes": []
140
+ },
141
+ "publishConfig": {
142
+ "access": "public",
143
+ "registry": "https://registry.npmjs.org/"
144
+ }
145
+ }
@@ -0,0 +1,45 @@
1
+ // dokebi-oauth entry file (src/infra/oauth/extension.ts): user code (D-0064, D-0091).
2
+ //
3
+ // dokebi-oauth checks every OAuth request; the consent page is YOURS. Until renderConsent is
4
+ // implemented the app does not start (EXTENSION_REQUIRED).
5
+ //
6
+ // TODO: renderConsent(request, reply, consent, context) — render your consent view and send it.
7
+ // The page must show consent.client.name and consent.client.host (who asks), consent.redirectHost
8
+ // (where the answer goes), consent.warning when it is set (the answer goes to this computer), the
9
+ // resource (consent.resource.name) and the scopes (consent.scopes: { name, label }). Its form posts to
10
+ // consent.action with the fields request (consent.id), decision ('allow' or 'deny'), scope
11
+ // (optional: a subset of the requested names), kind and binding (optional, your own values) and
12
+ // the CSRF field of @dokebi/csrf.
13
+ // kind and binding are what the browser posted: without checkDecision(user, client, decision) a
14
+ // decision that carries them is refused. Validate them there (return { kind, binding } or false)
15
+ // and authorize the binding again on every use; it is never a permission by itself.
16
+ // Optional: canDelegate(user, client) (default true: may this account connect applications?),
17
+ // clientName(clientId) (a name for a client you know), scopeLabel(scope, request) (a translated
18
+ // label), dependencies() (providers such as the template engine).
19
+ // Override with `protected override`; no constructor arguments.
20
+ //
21
+ // An EJS example (src/shared/views/oauth/consent.ejs, @dokebi/template-ejs and @dokebi/csrf):
22
+ //
23
+ // import type { PluginDependency } from '@dokebi/core';
24
+ // import { dependency as csrf } from '@dokebi/csrf';
25
+ // import type { OAuthConsent, OAuthHookContext } from '@dokebi/oauth';
26
+ // import type { FastifyReply, FastifyRequest } from 'fastify';
27
+ //
28
+ // const views: PluginDependency<{ render(name: string, data: object): Promise<string> }> = {
29
+ // name: 'dokebi-template-ejs',
30
+ // version: '^0.4.0',
31
+ // };
32
+ //
33
+ // export default class AppOAuth extends OAuthExtension {
34
+ // protected override dependencies(): readonly PluginDependency[] {
35
+ // return [views, csrf];
36
+ // }
37
+ // protected override async renderConsent(request: FastifyRequest, reply: FastifyReply, consent: OAuthConsent, context: OAuthHookContext) {
38
+ // const token = await context.getDependency(csrf).token(request);
39
+ // const html = await context.getDependency(views).render('oauth/consent', { consent, csrf: token });
40
+ // return reply.type('text/html; charset=utf-8').send(html);
41
+ // }
42
+ // }
43
+ import { OAuthExtension } from '@dokebi/oauth';
44
+
45
+ export default class AppOAuth extends OAuthExtension {}
@@ -0,0 +1,117 @@
1
+ // The tables of @dokebi/oauth (grants, authorization codes, registered clients; D-0091): written by
2
+ // `dokebi add @dokebi/oauth` for store.driver: sql. Run it with `npx dokebi-migrate up`; the app
3
+ // never creates tables at start. Works on PostgreSQL (pg), MySQL (mysql2) and MariaDB (mariadb):
4
+ // the driver is told apart by the shape of one SELECT answer. Edit only before it was applied.
5
+
6
+ interface Db {
7
+ query(sql: string, values?: unknown[]): Promise<unknown>;
8
+ }
9
+
10
+ const PREFIX = 'dokebi_oauth';
11
+
12
+ const POSTGRES = [
13
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_grants (
14
+ id varchar(22) NOT NULL PRIMARY KEY,
15
+ realm varchar(32) NOT NULL,
16
+ user_id varchar(255) NOT NULL,
17
+ client_id text NOT NULL,
18
+ client_name text NOT NULL,
19
+ resource varchar(64) NOT NULL,
20
+ scopes text NOT NULL,
21
+ kind varchar(64),
22
+ binding text,
23
+ created_at bigint NOT NULL,
24
+ expires_at bigint NOT NULL,
25
+ last_used_at bigint,
26
+ revoked_at bigint
27
+ )`,
28
+ `CREATE INDEX IF NOT EXISTS ${PREFIX}_grants_user ON ${PREFIX}_grants (realm, user_id)`,
29
+ `CREATE INDEX IF NOT EXISTS ${PREFIX}_grants_expires ON ${PREFIX}_grants (expires_at)`,
30
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_codes (
31
+ code_hash varchar(43) NOT NULL PRIMARY KEY,
32
+ grant_id varchar(22) NOT NULL,
33
+ realm varchar(32) NOT NULL,
34
+ user_id varchar(255) NOT NULL,
35
+ client_id text NOT NULL,
36
+ redirect_uri text NOT NULL,
37
+ challenge varchar(43) NOT NULL,
38
+ resource varchar(64) NOT NULL,
39
+ scopes text NOT NULL,
40
+ created_at bigint NOT NULL,
41
+ expires_at bigint NOT NULL,
42
+ used_at bigint
43
+ )`,
44
+ `CREATE INDEX IF NOT EXISTS ${PREFIX}_codes_expires ON ${PREFIX}_codes (expires_at)`,
45
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_clients (
46
+ client_id varchar(64) NOT NULL PRIMARY KEY,
47
+ name text NOT NULL,
48
+ redirect_uris text NOT NULL,
49
+ created_at bigint NOT NULL,
50
+ last_used_at bigint NOT NULL
51
+ )`,
52
+ `CREATE INDEX IF NOT EXISTS ${PREFIX}_clients_used ON ${PREFIX}_clients (last_used_at)`,
53
+ ];
54
+
55
+ // MySQL and MariaDB: ids and hashes compare in binary (the default collation would match another
56
+ // letter case).
57
+ const MYSQL = [
58
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_grants (
59
+ id VARCHAR(22) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
60
+ realm VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
61
+ user_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
62
+ client_id TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
63
+ client_name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
64
+ resource VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
65
+ scopes TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
66
+ kind VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL,
67
+ binding TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL,
68
+ created_at BIGINT NOT NULL,
69
+ expires_at BIGINT NOT NULL,
70
+ last_used_at BIGINT NULL,
71
+ revoked_at BIGINT NULL,
72
+ KEY ${PREFIX}_grants_user (realm, user_id),
73
+ KEY ${PREFIX}_grants_expires (expires_at)
74
+ ) ENGINE=InnoDB`,
75
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_codes (
76
+ code_hash VARCHAR(43) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
77
+ grant_id VARCHAR(22) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
78
+ realm VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
79
+ user_id VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
80
+ client_id TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
81
+ redirect_uri TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
82
+ challenge VARCHAR(43) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
83
+ resource VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
84
+ scopes TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
85
+ created_at BIGINT NOT NULL,
86
+ expires_at BIGINT NOT NULL,
87
+ used_at BIGINT NULL,
88
+ KEY ${PREFIX}_codes_expires (expires_at)
89
+ ) ENGINE=InnoDB`,
90
+ `CREATE TABLE IF NOT EXISTS ${PREFIX}_clients (
91
+ client_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL PRIMARY KEY,
92
+ name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
93
+ redirect_uris TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
94
+ created_at BIGINT NOT NULL,
95
+ last_used_at BIGINT NOT NULL,
96
+ KEY ${PREFIX}_clients_used (last_used_at)
97
+ ) ENGINE=InnoDB`,
98
+ ];
99
+
100
+ /** pg answers `{ rows }`; mysql2 and mariadb answer arrays. */
101
+ async function statements(db: Db): Promise<string[]> {
102
+ const probe = await db.query('SELECT 1 AS one');
103
+ const postgres =
104
+ probe !== null &&
105
+ typeof probe === 'object' &&
106
+ Array.isArray((probe as { rows?: unknown }).rows);
107
+ return postgres ? POSTGRES : MYSQL;
108
+ }
109
+
110
+ export async function up(db: Db): Promise<void> {
111
+ for (const sql of await statements(db)) await db.query(sql);
112
+ }
113
+
114
+ export async function down(db: Db): Promise<void> {
115
+ for (const table of ['clients', 'codes', 'grants'])
116
+ await db.query(`DROP TABLE IF EXISTS ${PREFIX}_${table}`);
117
+ }