@zgeoff/mcp-auth 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/admin/build-admin.d.ts +4 -0
- package/dist/admin/build-client-admin.d.ts +4 -0
- package/dist/admin/build-grant-admin.d.ts +3 -0
- package/dist/admin/build-passkey-admin.d.ts +3 -0
- package/dist/admin/check-redirect-uris.d.ts +1 -0
- package/dist/admin/create-enrollment-token.d.ts +2 -0
- package/dist/admin/read-client-name.d.ts +1 -0
- package/dist/admin/read-sign-in-grants.d.ts +3 -0
- package/dist/admin/remove-expired.d.ts +2 -0
- package/dist/admin/to-client-view.d.ts +3 -0
- package/dist/default-paths.d.ts +18 -0
- package/dist/elysia/create-mcp-http-app.d.ts +17 -0
- package/dist/elysia/find-bearer-token.d.ts +1 -0
- package/dist/elysia/index.d.ts +2 -0
- package/dist/elysia/is-beside-prefix.d.ts +1 -0
- package/dist/grants/collect-granted-scopes.d.ts +7 -0
- package/dist/grants/read-access-payload.d.ts +9 -0
- package/dist/grants/read-client-scopes.d.ts +2 -0
- package/dist/grants/verify-oauth-access.d.ts +13 -0
- package/dist/index.d.ts +7 -0
- package/dist/mcp-auth-error.d.ts +5 -0
- package/dist/open-mcp-auth-admin.d.ts +2 -0
- package/dist/open-mcp-auth.d.ts +2 -0
- package/dist/passkeys/answer-enroll-request.d.ts +3 -0
- package/dist/passkeys/create-enrollment-state.d.ts +6 -0
- package/dist/passkeys/create-passkey.d.ts +10 -0
- package/dist/passkeys/passkey-script.d.ts +4 -0
- package/dist/passkeys/read-enrollment-token.d.ts +2 -0
- package/dist/passkeys/read-passkey-rows.d.ts +13 -0
- package/dist/passkeys/render-enroll-page.d.ts +11 -0
- package/dist/passkeys/render-login-page.d.ts +11 -0
- package/dist/passkeys/update-passkey-use.d.ts +9 -0
- package/dist/passkeys/verify-passkey-assertion.d.ts +10 -0
- package/dist/passkeys/verify-passkey-registration.d.ts +15 -0
- package/dist/passkeys/verify-session-passkey.d.ts +8 -0
- package/dist/routes/build-routes.d.ts +3 -0
- package/dist/routes/with-form-origin.d.ts +4 -0
- package/dist/service-tokens/build-service-token-verifier.d.ts +13 -0
- package/dist/service-tokens/parse-service-token-file.d.ts +7 -0
- package/dist/sign-in/answer-authorize-request.d.ts +3 -0
- package/dist/sign-in/answer-consent-request.d.ts +3 -0
- package/dist/sign-in/answer-login-request.d.ts +3 -0
- package/dist/sign-in/build-consent-binding.d.ts +1 -0
- package/dist/sign-in/create-sign-in-state.d.ts +43 -0
- package/dist/sign-in/pick-consent-choice.d.ts +8 -0
- package/dist/sign-in/pick-consent-outcome.d.ts +4 -0
- package/dist/sign-in/read-session-id.d.ts +2 -0
- package/dist/sign-in/read-stored-client-name.d.ts +3 -0
- package/dist/sign-in/read-url-field.d.ts +1 -0
- package/dist/sign-in/render-consent-page.d.ts +11 -0
- package/dist/sign-in/route-context.d.ts +17 -0
- package/dist/sign-in/verify-oauth-query.d.ts +1 -0
- package/dist/store/check-cascade-keys.d.ts +2 -0
- package/dist/store/check-foreign-keys.d.ts +2 -0
- package/dist/store/check-table-names.d.ts +2 -0
- package/dist/store/check-user-columns.d.ts +2 -0
- package/dist/store/create-auth-server.d.ts +103 -0
- package/dist/store/create-grant-plugin.d.ts +28 -0
- package/dist/store/create-owned-tables.d.ts +3 -0
- package/dist/store/open-store.d.ts +15 -0
- package/dist/store/read-table-columns.d.ts +2 -0
- package/dist/store/remove-grant-rows.d.ts +3 -0
- package/dist/store/store-schema.d.ts +63 -0
- package/dist/test-utils/authorize-test-client.d.ts +12 -0
- package/dist/test-utils/build-form-request.d.ts +1 -0
- package/dist/test-utils/build-seeder.d.ts +13 -0
- package/dist/test-utils/build-stub-authenticator.d.ts +49 -0
- package/dist/test-utils/build-stub-clock.d.ts +5 -0
- package/dist/test-utils/build-test-requester.d.ts +2 -0
- package/dist/test-utils/create-test-database.d.ts +2 -0
- package/dist/test-utils/hook-sqlite-statements.d.ts +2 -0
- package/dist/test-utils/index.d.ts +12 -0
- package/dist/test-utils/open-test-auth.d.ts +21 -0
- package/dist/test-utils/parse-passkey-page.d.ts +21 -0
- package/dist/test-utils/read-json-record.d.ts +5 -0
- package/dist/test-utils/run-sign-in-to-consent.d.ts +13 -0
- package/dist/test-utils/seed-client.d.ts +7 -0
- package/dist/test-utils/seed-enrollment-token.d.ts +2 -0
- package/dist/test-utils/seed-passkey.d.ts +7 -0
- package/dist/tokens/answer-error-request.d.ts +2 -0
- package/dist/tokens/answer-metadata-request.d.ts +2 -0
- package/dist/tokens/answer-revoke-request.d.ts +2 -0
- package/dist/tokens/answer-token-request.d.ts +3 -0
- package/dist/tokens/build-better-auth-request.d.ts +6 -0
- package/dist/tokens/build-oauth-error.d.ts +1 -0
- package/dist/tokens/read-error-code.d.ts +1 -0
- package/dist/tokens/read-error-description.d.ts +1 -0
- package/dist/tokens/record-token-exchange.d.ts +10 -0
- package/dist/tokens/remove-token-sessions.d.ts +2 -0
- package/dist/types.d.ts +185 -0
- package/dist/utils/build-page-response.d.ts +5 -0
- package/dist/utils/derive-token-hash.d.ts +1 -0
- package/dist/utils/is-allowed-redirect-uri.d.ts +1 -0
- package/dist/utils/is-loopback-host.d.ts +1 -0
- package/dist/utils/normalize-client-name.d.ts +1 -0
- package/dist/utils/parse-approved-by.d.ts +2 -0
- package/dist/utils/parse-json-strings.d.ts +1 -0
- package/dist/utils/read-body-text.d.ts +1 -0
- package/dist/utils/read-form.d.ts +2 -0
- package/dist/utils/to-html-text.d.ts +1 -0
- package/dist/utils/to-script-json.d.ts +1 -0
- package/package.json +73 -4
- package/src/admin/build-admin.ts +22 -0
- package/src/admin/build-client-admin.ts +112 -0
- package/src/admin/build-grant-admin.ts +62 -0
- package/src/admin/build-passkey-admin.ts +53 -0
- package/src/admin/check-redirect-uris.ts +17 -0
- package/src/admin/create-enrollment-token.ts +18 -0
- package/src/admin/read-client-name.ts +13 -0
- package/src/admin/read-sign-in-grants.ts +22 -0
- package/src/admin/remove-expired.ts +47 -0
- package/src/admin/to-client-view.ts +12 -0
- package/src/default-paths.ts +21 -0
- package/src/elysia/create-mcp-http-app.ts +218 -0
- package/src/elysia/find-bearer-token.ts +6 -0
- package/src/elysia/index.ts +2 -0
- package/src/elysia/is-beside-prefix.ts +7 -0
- package/src/grants/collect-granted-scopes.ts +19 -0
- package/src/grants/read-access-payload.ts +37 -0
- package/src/grants/read-client-scopes.ts +36 -0
- package/src/grants/verify-oauth-access.ts +113 -0
- package/src/index.ts +7 -0
- package/src/mcp-auth-error.ts +17 -0
- package/src/open-mcp-auth-admin.ts +14 -0
- package/src/open-mcp-auth.ts +89 -0
- package/src/passkeys/answer-enroll-request.ts +179 -0
- package/src/passkeys/create-enrollment-state.ts +54 -0
- package/src/passkeys/create-passkey.ts +39 -0
- package/src/passkeys/passkey-script.ts +48 -0
- package/src/passkeys/read-enrollment-token.ts +21 -0
- package/src/passkeys/read-passkey-rows.ts +30 -0
- package/src/passkeys/render-enroll-page.ts +58 -0
- package/src/passkeys/render-login-page.ts +59 -0
- package/src/passkeys/update-passkey-use.ts +24 -0
- package/src/passkeys/verify-passkey-assertion.ts +120 -0
- package/src/passkeys/verify-passkey-registration.ts +116 -0
- package/src/passkeys/verify-session-passkey.ts +42 -0
- package/src/routes/build-routes.ts +31 -0
- package/src/routes/with-form-origin.ts +18 -0
- package/src/service-tokens/build-service-token-verifier.ts +56 -0
- package/src/service-tokens/parse-service-token-file.ts +104 -0
- package/src/sign-in/answer-authorize-request.ts +133 -0
- package/src/sign-in/answer-consent-request.ts +220 -0
- package/src/sign-in/answer-login-request.ts +243 -0
- package/src/sign-in/build-consent-binding.ts +11 -0
- package/src/sign-in/create-sign-in-state.ts +229 -0
- package/src/sign-in/pick-consent-choice.ts +27 -0
- package/src/sign-in/pick-consent-outcome.ts +14 -0
- package/src/sign-in/read-session-id.ts +11 -0
- package/src/sign-in/read-stored-client-name.ts +15 -0
- package/src/sign-in/read-url-field.ts +12 -0
- package/src/sign-in/render-consent-page.ts +57 -0
- package/src/sign-in/route-context.ts +25 -0
- package/src/sign-in/verify-oauth-query.ts +39 -0
- package/src/store/check-cascade-keys.ts +36 -0
- package/src/store/check-foreign-keys.ts +20 -0
- package/src/store/check-table-names.ts +13 -0
- package/src/store/check-user-columns.ts +22 -0
- package/src/store/create-auth-server.ts +97 -0
- package/src/store/create-grant-plugin.ts +68 -0
- package/src/store/create-owned-tables.ts +45 -0
- package/src/store/open-store.ts +72 -0
- package/src/store/read-table-columns.ts +12 -0
- package/src/store/remove-grant-rows.ts +27 -0
- package/src/store/store-schema.ts +61 -0
- package/src/test-utils/authorize-test-client.ts +93 -0
- package/src/test-utils/build-form-request.ts +12 -0
- package/src/test-utils/build-seeder.ts +19 -0
- package/src/test-utils/build-stub-authenticator.ts +244 -0
- package/src/test-utils/build-stub-clock.ts +21 -0
- package/src/test-utils/build-test-requester.ts +5 -0
- package/src/test-utils/create-test-database.ts +7 -0
- package/src/test-utils/hook-sqlite-statements.ts +17 -0
- package/src/test-utils/index.ts +12 -0
- package/src/test-utils/open-test-auth.ts +72 -0
- package/src/test-utils/parse-passkey-page.ts +25 -0
- package/src/test-utils/read-json-record.ts +15 -0
- package/src/test-utils/run-sign-in-to-consent.ts +100 -0
- package/src/test-utils/seed-client.ts +16 -0
- package/src/test-utils/seed-enrollment-token.ts +5 -0
- package/src/test-utils/seed-passkey.ts +51 -0
- package/src/tokens/answer-error-request.ts +26 -0
- package/src/tokens/answer-metadata-request.ts +51 -0
- package/src/tokens/answer-revoke-request.ts +47 -0
- package/src/tokens/answer-token-request.ts +133 -0
- package/src/tokens/build-better-auth-request.ts +23 -0
- package/src/tokens/build-oauth-error.ts +7 -0
- package/src/tokens/read-error-code.ts +12 -0
- package/src/tokens/read-error-description.ts +11 -0
- package/src/tokens/record-token-exchange.ts +73 -0
- package/src/tokens/remove-token-sessions.ts +32 -0
- package/src/types.ts +247 -0
- package/src/utils/build-page-response.ts +24 -0
- package/src/utils/derive-token-hash.ts +5 -0
- package/src/utils/is-allowed-redirect-uri.ts +17 -0
- package/src/utils/is-loopback-host.ts +8 -0
- package/src/utils/normalize-client-name.ts +20 -0
- package/src/utils/parse-approved-by.ts +9 -0
- package/src/utils/parse-json-strings.ts +13 -0
- package/src/utils/read-body-text.ts +23 -0
- package/src/utils/read-form.ts +16 -0
- package/src/utils/to-html-text.ts +8 -0
- package/src/utils/to-script-json.ts +6 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { Database } from 'bun:sqlite';
|
|
2
|
+
import type { Tagged } from 'type-fest';
|
|
3
|
+
export type GrantID = Tagged<string, 'GrantID'>;
|
|
4
|
+
export type OAuthClientID = Tagged<string, 'OAuthClientID'>;
|
|
5
|
+
export type PasskeyID = Tagged<string, 'PasskeyID'>;
|
|
6
|
+
export type CorrelationID = Tagged<string, 'CorrelationID'>;
|
|
7
|
+
export type ApprovedBy = 'passkey' | `token:${string}`;
|
|
8
|
+
export interface MCPAuthPaths {
|
|
9
|
+
readonly browser: {
|
|
10
|
+
readonly authorize: string;
|
|
11
|
+
readonly login: string;
|
|
12
|
+
readonly enroll: string;
|
|
13
|
+
readonly consent: string;
|
|
14
|
+
readonly error: string;
|
|
15
|
+
};
|
|
16
|
+
readonly machine: {
|
|
17
|
+
readonly mcp: string;
|
|
18
|
+
readonly root: string;
|
|
19
|
+
readonly token: string;
|
|
20
|
+
readonly revoke: string;
|
|
21
|
+
readonly authorizationServerMetadata: string;
|
|
22
|
+
readonly resourceMetadata: string;
|
|
23
|
+
readonly mcpResourceMetadata: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface MCPAuthTables {
|
|
27
|
+
readonly passkey: string;
|
|
28
|
+
readonly enrollmentToken: string;
|
|
29
|
+
}
|
|
30
|
+
export interface Clock {
|
|
31
|
+
readonly now: () => number;
|
|
32
|
+
}
|
|
33
|
+
export interface Requester {
|
|
34
|
+
readonly socketAddress: string | null;
|
|
35
|
+
readonly connectingIP: {
|
|
36
|
+
readonly address: string;
|
|
37
|
+
readonly provenance: 'trusted' | 'unverified';
|
|
38
|
+
} | null;
|
|
39
|
+
readonly userAgent: string | null;
|
|
40
|
+
}
|
|
41
|
+
export interface SecurityEvent {
|
|
42
|
+
readonly kind: 'authorize_started' | 'authorize_invalid' | 'owner_auth' | 'consent' | 'token_exchange' | 'bearer' | 'bad_host' | 'bad_origin';
|
|
43
|
+
readonly outcome: 'accepted' | 'rejected' | 'succeeded' | 'failed' | 'allowed' | 'denied';
|
|
44
|
+
readonly reason?: string;
|
|
45
|
+
readonly path: string;
|
|
46
|
+
readonly requester: Requester;
|
|
47
|
+
readonly client?: {
|
|
48
|
+
readonly id: OAuthClientID;
|
|
49
|
+
readonly name: string;
|
|
50
|
+
};
|
|
51
|
+
readonly correlationID?: CorrelationID;
|
|
52
|
+
}
|
|
53
|
+
export interface SecuritySink {
|
|
54
|
+
readonly record: (event: SecurityEvent) => void;
|
|
55
|
+
}
|
|
56
|
+
export interface MCPAuthAdminOptions<Scope extends string> {
|
|
57
|
+
readonly database: Database;
|
|
58
|
+
readonly scopes: readonly Scope[];
|
|
59
|
+
readonly tables: MCPAuthTables;
|
|
60
|
+
readonly clock: Clock;
|
|
61
|
+
readonly onGrantRevoked: (grantID: GrantID) => void;
|
|
62
|
+
}
|
|
63
|
+
export interface ServiceClient<Scope extends string> {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly scopes: readonly Scope[];
|
|
66
|
+
readonly tokens: readonly string[];
|
|
67
|
+
}
|
|
68
|
+
export interface ServiceTokenFile<Scope extends string> {
|
|
69
|
+
readonly resource: string;
|
|
70
|
+
readonly clients: readonly ServiceClient<Scope>[];
|
|
71
|
+
}
|
|
72
|
+
export type ParsedServiceTokenFile<Scope extends string> = {
|
|
73
|
+
readonly ok: true;
|
|
74
|
+
readonly file: ServiceTokenFile<Scope>;
|
|
75
|
+
} | {
|
|
76
|
+
readonly ok: false;
|
|
77
|
+
readonly reason: string;
|
|
78
|
+
};
|
|
79
|
+
export interface MCPAuthOptions<Scope extends string, Limits> extends MCPAuthAdminOptions<Scope> {
|
|
80
|
+
readonly origin: string;
|
|
81
|
+
readonly productName: string;
|
|
82
|
+
readonly paths: MCPAuthPaths;
|
|
83
|
+
readonly serviceTokens?: {
|
|
84
|
+
readonly prefix: string;
|
|
85
|
+
readonly file: ServiceTokenFile<Scope>;
|
|
86
|
+
};
|
|
87
|
+
readonly grantLimits?: GrantLimits<Limits>;
|
|
88
|
+
readonly security: SecuritySink;
|
|
89
|
+
}
|
|
90
|
+
export interface ClientView {
|
|
91
|
+
readonly clientID: OAuthClientID;
|
|
92
|
+
readonly name: string;
|
|
93
|
+
readonly redirectURIs: readonly string[];
|
|
94
|
+
readonly createdAt: string;
|
|
95
|
+
}
|
|
96
|
+
export interface GrantView {
|
|
97
|
+
readonly grantID: GrantID;
|
|
98
|
+
readonly clientID: OAuthClientID;
|
|
99
|
+
readonly clientName: string;
|
|
100
|
+
readonly scopes: readonly string[];
|
|
101
|
+
readonly approvedBy: ApprovedBy;
|
|
102
|
+
readonly createdAt: string;
|
|
103
|
+
readonly lastUsedAt: string | null;
|
|
104
|
+
}
|
|
105
|
+
export interface PasskeyView {
|
|
106
|
+
readonly id: PasskeyID;
|
|
107
|
+
readonly createdAt: string;
|
|
108
|
+
readonly lastUsedAt: string | null;
|
|
109
|
+
}
|
|
110
|
+
export interface MCPAuthAdmin {
|
|
111
|
+
readonly clients: {
|
|
112
|
+
readonly list: () => Promise<ClientView[]>;
|
|
113
|
+
readonly add: (name: string, redirectURIs: readonly string[]) => Promise<ClientView>;
|
|
114
|
+
readonly update: (name: string, redirectURIs: readonly string[]) => Promise<ClientView>;
|
|
115
|
+
readonly remove: (name: string) => Promise<number>;
|
|
116
|
+
};
|
|
117
|
+
readonly grants: {
|
|
118
|
+
readonly list: () => Promise<GrantView[]>;
|
|
119
|
+
readonly revoke: (grantID: GrantID) => Promise<boolean>;
|
|
120
|
+
};
|
|
121
|
+
readonly passkeys: {
|
|
122
|
+
readonly createEnrollmentToken: () => Promise<string>;
|
|
123
|
+
readonly list: () => Promise<PasskeyView[]>;
|
|
124
|
+
readonly remove: (id: PasskeyID) => Promise<void>;
|
|
125
|
+
readonly reset: () => Promise<void>;
|
|
126
|
+
};
|
|
127
|
+
readonly removeExpired: () => Promise<void>;
|
|
128
|
+
readonly close: () => Promise<void>;
|
|
129
|
+
}
|
|
130
|
+
export type MCPAuthRouteName = 'authorize' | 'login' | 'enroll' | 'consent' | 'error' | 'token' | 'revoke' | 'authorizationServerMetadata' | 'resourceMetadata';
|
|
131
|
+
export type MCPAuthRoutes = Record<MCPAuthRouteName, (request: Request, requester: Requester) => Promise<Response>>;
|
|
132
|
+
export type Access<Scope extends string, Limits> = {
|
|
133
|
+
readonly kind: 'oauth';
|
|
134
|
+
readonly grantID: GrantID;
|
|
135
|
+
readonly clientID: OAuthClientID;
|
|
136
|
+
readonly clientName: string;
|
|
137
|
+
readonly scopes: readonly Scope[];
|
|
138
|
+
readonly limits: Limits | null;
|
|
139
|
+
} | {
|
|
140
|
+
readonly kind: 'service';
|
|
141
|
+
readonly clientID: `service:${string}`;
|
|
142
|
+
readonly clientName: `service:${string}`;
|
|
143
|
+
readonly scopes: readonly Scope[];
|
|
144
|
+
};
|
|
145
|
+
export interface MCPAuth<Scope extends string, Limits = null> extends MCPAuthAdmin {
|
|
146
|
+
readonly resource: string;
|
|
147
|
+
readonly issuer: string;
|
|
148
|
+
readonly paths: MCPAuthPaths;
|
|
149
|
+
readonly routes: MCPAuthRoutes;
|
|
150
|
+
readonly verify: (bearer: string) => Promise<Access<Scope, Limits> | null>;
|
|
151
|
+
readonly findClientScopes: (clientID: string) => Promise<readonly Scope[] | null>;
|
|
152
|
+
}
|
|
153
|
+
export interface ConsentRequest {
|
|
154
|
+
readonly clientID: OAuthClientID;
|
|
155
|
+
readonly clientName: string;
|
|
156
|
+
readonly scopes: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
export interface StoredGrant<Limits> {
|
|
159
|
+
readonly grantID: GrantID;
|
|
160
|
+
readonly clientID: OAuthClientID;
|
|
161
|
+
readonly scopes: readonly string[];
|
|
162
|
+
readonly approvedBy: ApprovedBy;
|
|
163
|
+
readonly limits: Limits | null;
|
|
164
|
+
readonly createdAt: number;
|
|
165
|
+
readonly lastUsedAt: number | null;
|
|
166
|
+
}
|
|
167
|
+
export type GrantCheck = {
|
|
168
|
+
readonly kind: 'allow';
|
|
169
|
+
readonly scopes: readonly string[];
|
|
170
|
+
} | {
|
|
171
|
+
readonly kind: 'revoke';
|
|
172
|
+
readonly reason: string;
|
|
173
|
+
};
|
|
174
|
+
export interface GrantLimits<Limits> {
|
|
175
|
+
readonly renderConsentFields: (request: ConsentRequest) => string;
|
|
176
|
+
readonly parseConsentFields: (form: URLSearchParams) => {
|
|
177
|
+
ok: true;
|
|
178
|
+
limits: Limits;
|
|
179
|
+
} | {
|
|
180
|
+
ok: false;
|
|
181
|
+
error: string;
|
|
182
|
+
};
|
|
183
|
+
readonly parseStored: (json: unknown) => Limits | null;
|
|
184
|
+
readonly check: (grant: StoredGrant<Limits>) => Promise<GrantCheck>;
|
|
185
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deriveTokenHash(token: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isAllowedRedirectURI(raw: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isLoopbackHost(host: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normalizeClientName(raw: unknown, fallback?: string, maxLength?: number): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseJSONStrings(raw: string): string[] | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function readBodyText(request: Request, maxBytes: number): Promise<string | null>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toHTMLText(text: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toScriptJSON(value: unknown): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/mcp-auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A shared OAuth server for the MCP route of an app, built on better-auth, with passkey sign-in for the owner",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"better-auth",
|
|
7
|
+
"mcp",
|
|
8
|
+
"oauth",
|
|
9
|
+
"passkey"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/zgeoff/tools.git",
|
|
15
|
+
"directory": "packages/mcp-auth"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"!src/**/*.test.ts",
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./src/index.ts"
|
|
27
|
+
},
|
|
28
|
+
"./elysia": {
|
|
29
|
+
"types": "./dist/elysia/index.d.ts",
|
|
30
|
+
"default": "./src/elysia/index.ts"
|
|
31
|
+
},
|
|
32
|
+
"./testing": {
|
|
33
|
+
"types": "./dist/test-utils/index.d.ts",
|
|
34
|
+
"default": "./src/test-utils/index.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
42
|
+
"typecheck": "tsrs"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@better-auth/mcp": "1.7.15",
|
|
46
|
+
"@better-auth/oauth-provider": "1.7.15",
|
|
47
|
+
"@simplewebauthn/server": "14.0.3",
|
|
48
|
+
"@zgeoff/kysely-bun-sqlite": "0.3.2",
|
|
49
|
+
"@zgeoff/std": "0.4.3",
|
|
50
|
+
"better-auth": "1.7.15",
|
|
51
|
+
"ts-pattern": "5.9.2",
|
|
52
|
+
"type-fest": "5.10.0",
|
|
53
|
+
"zod": "4.6.7"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/bun": "1.4.4",
|
|
57
|
+
"elysia": "2.0.0-rc.3",
|
|
58
|
+
"kysely": "0.29.8",
|
|
59
|
+
"tiny-invariant": "1.3.3",
|
|
60
|
+
"tsrs": "0.18.0-ts7.1.0-dev.20260929",
|
|
61
|
+
"typescript": "7.0.2"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"elysia": ">=2.0.0-rc.3 <3.0.0",
|
|
65
|
+
"kysely": ">=0.29.5 <0.30.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"elysia": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"engines": {
|
|
73
|
+
"bun": ">=1.3"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SignInState } from '../sign-in/create-sign-in-state.ts';
|
|
2
|
+
import type { Store } from '../store/open-store.ts';
|
|
3
|
+
import type { MCPAuthAdmin } from '../types.ts';
|
|
4
|
+
import { buildClientAdmin } from './build-client-admin.ts';
|
|
5
|
+
import { buildGrantAdmin } from './build-grant-admin.ts';
|
|
6
|
+
import { buildPasskeyAdmin } from './build-passkey-admin.ts';
|
|
7
|
+
import { removeExpired } from './remove-expired.ts';
|
|
8
|
+
|
|
9
|
+
export function buildAdmin(
|
|
10
|
+
store: Store,
|
|
11
|
+
signIns: Pick<SignInState, 'removeClient'> | null,
|
|
12
|
+
): MCPAuthAdmin {
|
|
13
|
+
return {
|
|
14
|
+
clients: buildClientAdmin(store, signIns),
|
|
15
|
+
grants: buildGrantAdmin(store),
|
|
16
|
+
passkeys: buildPasskeyAdmin(store),
|
|
17
|
+
removeExpired: () => removeExpired(store),
|
|
18
|
+
close: async () => {
|
|
19
|
+
await store.db.destroy();
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { toTagged } from '@zgeoff/std';
|
|
2
|
+
import { generateRandomString } from 'better-auth/crypto';
|
|
3
|
+
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
4
|
+
import type { SignInState } from '../sign-in/create-sign-in-state.ts';
|
|
5
|
+
import type { Store } from '../store/open-store.ts';
|
|
6
|
+
import { removeGrantRows } from '../store/remove-grant-rows.ts';
|
|
7
|
+
import type { GrantID, MCPAuthAdmin, OAuthClientID } from '../types.ts';
|
|
8
|
+
import { checkRedirectURIs } from './check-redirect-uris.ts';
|
|
9
|
+
import { readClientName } from './read-client-name.ts';
|
|
10
|
+
import { toClientView } from './to-client-view.ts';
|
|
11
|
+
|
|
12
|
+
export function buildClientAdmin(
|
|
13
|
+
store: Store,
|
|
14
|
+
signIns: Pick<SignInState, 'removeClient'> | null,
|
|
15
|
+
): MCPAuthAdmin['clients'] {
|
|
16
|
+
const readClient = (name: string) =>
|
|
17
|
+
store.db.selectFrom('oauthClient').selectAll().where('name', '=', name).executeTakeFirst();
|
|
18
|
+
|
|
19
|
+
const requireClient = async (name: string) => {
|
|
20
|
+
const client = await readClient(name);
|
|
21
|
+
|
|
22
|
+
if (client === undefined) {
|
|
23
|
+
throw new MCPAuthError('not-found', 'no client has that name');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return client;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
list: async () => {
|
|
31
|
+
const rows = await store.db.selectFrom('oauthClient').selectAll().orderBy('name').execute();
|
|
32
|
+
|
|
33
|
+
return rows.map((row) => toClientView(row));
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
add: async (name, redirectURIs) => {
|
|
37
|
+
const cleaned = readClientName(name);
|
|
38
|
+
|
|
39
|
+
checkRedirectURIs(redirectURIs);
|
|
40
|
+
|
|
41
|
+
if ((await readClient(cleaned)) !== undefined) {
|
|
42
|
+
throw new MCPAuthError('conflict', 'a client already has that name');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const now = new Date(store.options.clock.now()).toISOString();
|
|
46
|
+
|
|
47
|
+
const clientID = toTagged<OAuthClientID>(generateRandomString(32, 'a-z', 'A-Z', '0-9'));
|
|
48
|
+
|
|
49
|
+
const row = await store.db
|
|
50
|
+
.insertInto('oauthClient')
|
|
51
|
+
.values({
|
|
52
|
+
id: crypto.randomUUID(),
|
|
53
|
+
clientId: clientID,
|
|
54
|
+
name: cleaned,
|
|
55
|
+
redirectUris: JSON.stringify(redirectURIs),
|
|
56
|
+
scopes: JSON.stringify([...store.options.scopes, 'offline_access']),
|
|
57
|
+
clientCredentialsScopes: '[]',
|
|
58
|
+
tokenEndpointAuthMethod: 'none',
|
|
59
|
+
grantTypes: JSON.stringify(['authorization_code', 'refresh_token']),
|
|
60
|
+
responseTypes: JSON.stringify(['code']),
|
|
61
|
+
applicationType: redirectURIs.every((uri) => uri.startsWith('https:')) ? 'web' : 'native',
|
|
62
|
+
requirePKCE: 1,
|
|
63
|
+
disabled: 0,
|
|
64
|
+
createdAt: now,
|
|
65
|
+
updatedAt: now,
|
|
66
|
+
})
|
|
67
|
+
.returningAll()
|
|
68
|
+
.executeTakeFirstOrThrow();
|
|
69
|
+
|
|
70
|
+
return toClientView(row);
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
update: async (name, redirectURIs) => {
|
|
74
|
+
const client = await requireClient(readClientName(name));
|
|
75
|
+
|
|
76
|
+
checkRedirectURIs(redirectURIs);
|
|
77
|
+
|
|
78
|
+
const row = await store.db
|
|
79
|
+
.updateTable('oauthClient')
|
|
80
|
+
.set({
|
|
81
|
+
redirectUris: JSON.stringify(redirectURIs),
|
|
82
|
+
updatedAt: new Date(store.options.clock.now()).toISOString(),
|
|
83
|
+
})
|
|
84
|
+
.where('clientId', '=', client.clientId)
|
|
85
|
+
.returningAll()
|
|
86
|
+
.executeTakeFirstOrThrow();
|
|
87
|
+
|
|
88
|
+
return toClientView(row);
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
remove: async (name) => {
|
|
92
|
+
const client = await requireClient(readClientName(name));
|
|
93
|
+
|
|
94
|
+
const grants = await store.db
|
|
95
|
+
.selectFrom('user')
|
|
96
|
+
.select('id')
|
|
97
|
+
.where('clientId', '=', client.clientId)
|
|
98
|
+
.execute();
|
|
99
|
+
|
|
100
|
+
const revoked = await removeGrantRows(
|
|
101
|
+
store,
|
|
102
|
+
grants.map((grant): GrantID => grant.id),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
await store.db.deleteFrom('oauthClient').where('clientId', '=', client.clientId).execute();
|
|
106
|
+
|
|
107
|
+
signIns?.removeClient(client.clientId);
|
|
108
|
+
|
|
109
|
+
return revoked.length;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Store } from '../store/open-store.ts';
|
|
2
|
+
import { removeGrantRows } from '../store/remove-grant-rows.ts';
|
|
3
|
+
import type { MCPAuthAdmin } from '../types.ts';
|
|
4
|
+
import { parseApprovedBy } from '../utils/parse-approved-by.ts';
|
|
5
|
+
import { parseJSONStrings } from '../utils/parse-json-strings.ts';
|
|
6
|
+
|
|
7
|
+
export function buildGrantAdmin(store: Store): MCPAuthAdmin['grants'] {
|
|
8
|
+
return {
|
|
9
|
+
list: async () => {
|
|
10
|
+
const at = new Date(store.options.clock.now()).toISOString();
|
|
11
|
+
|
|
12
|
+
const rows = await store.db
|
|
13
|
+
.selectFrom('user')
|
|
14
|
+
.leftJoin('oauthClient', 'oauthClient.clientId', 'user.clientId')
|
|
15
|
+
.select([
|
|
16
|
+
'user.id',
|
|
17
|
+
'user.clientId',
|
|
18
|
+
'user.scopes',
|
|
19
|
+
'user.approvedBy',
|
|
20
|
+
'user.createdAt',
|
|
21
|
+
'user.lastUsedAt',
|
|
22
|
+
'oauthClient.name as clientName',
|
|
23
|
+
])
|
|
24
|
+
.where((eb) =>
|
|
25
|
+
eb.exists(
|
|
26
|
+
eb
|
|
27
|
+
.selectFrom('oauthRefreshToken')
|
|
28
|
+
.select('oauthRefreshToken.id')
|
|
29
|
+
.whereRef('oauthRefreshToken.userId', '=', 'user.id')
|
|
30
|
+
.where('oauthRefreshToken.revoked', 'is', null)
|
|
31
|
+
.where('oauthRefreshToken.expiresAt', '>', at),
|
|
32
|
+
),
|
|
33
|
+
)
|
|
34
|
+
.orderBy('user.createdAt')
|
|
35
|
+
.execute();
|
|
36
|
+
|
|
37
|
+
return rows.flatMap((row) => {
|
|
38
|
+
const approvedBy = parseApprovedBy(row.approvedBy);
|
|
39
|
+
|
|
40
|
+
if (approvedBy === null) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
grantID: row.id,
|
|
46
|
+
clientID: row.clientId,
|
|
47
|
+
clientName: row.clientName ?? row.clientId,
|
|
48
|
+
scopes: parseJSONStrings(row.scopes ?? '[]') ?? [],
|
|
49
|
+
approvedBy,
|
|
50
|
+
createdAt: row.createdAt,
|
|
51
|
+
lastUsedAt: row.lastUsedAt,
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
revoke: async (grantID) => {
|
|
57
|
+
const removed = await removeGrantRows(store, [grantID]);
|
|
58
|
+
|
|
59
|
+
return removed.length > 0;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { toTagged } from '@zgeoff/std';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
import { readPasskeyRows } from '../passkeys/read-passkey-rows.ts';
|
|
4
|
+
import type { Store } from '../store/open-store.ts';
|
|
5
|
+
import { removeGrantRows } from '../store/remove-grant-rows.ts';
|
|
6
|
+
import type { MCPAuthAdmin, PasskeyID } from '../types.ts';
|
|
7
|
+
import { createEnrollmentToken } from './create-enrollment-token.ts';
|
|
8
|
+
import { readSignInGrants } from './read-sign-in-grants.ts';
|
|
9
|
+
|
|
10
|
+
export function buildPasskeyAdmin(store: Store): MCPAuthAdmin['passkeys'] {
|
|
11
|
+
const passkeyTable = sql.table(store.options.tables.passkey);
|
|
12
|
+
const enrollmentTable = sql.table(store.options.tables.enrollmentToken);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
createEnrollmentToken: () => createEnrollmentToken(store),
|
|
16
|
+
|
|
17
|
+
list: async () => {
|
|
18
|
+
const rows = await readPasskeyRows(store.db, store.options.tables.passkey, null);
|
|
19
|
+
|
|
20
|
+
return rows.map((row) => ({
|
|
21
|
+
id: toTagged<PasskeyID>(row.id),
|
|
22
|
+
createdAt: row.created_at,
|
|
23
|
+
lastUsedAt: row.last_used_at,
|
|
24
|
+
}));
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
// Every session goes, so no sign-in the removed passkey made carries on to
|
|
28
|
+
// a consent.
|
|
29
|
+
remove: async (id) => {
|
|
30
|
+
await store.db.transaction().execute(async (trx) => {
|
|
31
|
+
const removed = await sql`delete from ${passkeyTable} where id = ${id}`.execute(trx);
|
|
32
|
+
|
|
33
|
+
const removedCount = removed.numAffectedRows ?? 0n;
|
|
34
|
+
|
|
35
|
+
if (removedCount > 0n) {
|
|
36
|
+
await trx.deleteFrom('session').execute();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
reset: async () => {
|
|
42
|
+
await store.db.transaction().execute(async (trx) => {
|
|
43
|
+
await sql`delete from ${passkeyTable}`.execute(trx);
|
|
44
|
+
await sql`delete from ${enrollmentTable}`.execute(trx);
|
|
45
|
+
await trx.deleteFrom('session').execute();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const signIns = await readSignInGrants(store);
|
|
49
|
+
|
|
50
|
+
await removeGrantRows(store, signIns);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
2
|
+
import { isAllowedRedirectURI } from '../utils/is-allowed-redirect-uri.ts';
|
|
3
|
+
|
|
4
|
+
export function checkRedirectURIs(redirectURIs: readonly string[]): void {
|
|
5
|
+
if (redirectURIs.length === 0) {
|
|
6
|
+
throw new MCPAuthError('invalid-redirect-uri', 'a client needs at least one redirect URI');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
for (const uri of redirectURIs) {
|
|
10
|
+
if (!isAllowedRedirectURI(uri)) {
|
|
11
|
+
throw new MCPAuthError(
|
|
12
|
+
'invalid-redirect-uri',
|
|
13
|
+
'a redirect URI must use https, or http on a loopback host, with no fragment or user info',
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
import type { Store } from '../store/open-store.ts';
|
|
4
|
+
import { deriveTokenHash } from '../utils/derive-token-hash.ts';
|
|
5
|
+
|
|
6
|
+
// How long an enrollment token stays usable after it is made.
|
|
7
|
+
const ENROLLMENT_TOKEN_MS = 15 * 60_000;
|
|
8
|
+
|
|
9
|
+
export async function createEnrollmentToken(store: Store): Promise<string> {
|
|
10
|
+
const token = `enroll-${randomBytes(32).toString('base64url')}`;
|
|
11
|
+
const now = store.options.clock.now();
|
|
12
|
+
|
|
13
|
+
await sql`insert into ${sql.table(store.options.tables.enrollmentToken)} (token_hash, created_at, expires_at) values (${deriveTokenHash(token)}, ${new Date(now).toISOString()}, ${new Date(now + ENROLLMENT_TOKEN_MS).toISOString()})`.execute(
|
|
14
|
+
store.db,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return token;
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MCPAuthError } from '../mcp-auth-error.ts';
|
|
2
|
+
import { normalizeClientName } from '../utils/normalize-client-name.ts';
|
|
3
|
+
|
|
4
|
+
// A name the core stores or looks up: cleaned, and never empty.
|
|
5
|
+
export function readClientName(raw: string): string {
|
|
6
|
+
const cleaned = normalizeClientName(raw, '');
|
|
7
|
+
|
|
8
|
+
if (cleaned === '') {
|
|
9
|
+
throw new MCPAuthError('invalid-client-name', 'the client name is empty after cleaning');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return cleaned;
|
|
13
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Store } from '../store/open-store.ts';
|
|
2
|
+
import type { GrantID } from '../types.ts';
|
|
3
|
+
|
|
4
|
+
// A grant row with no refresh token is a sign-in that has not finished.
|
|
5
|
+
export async function readSignInGrants(store: Store): Promise<GrantID[]> {
|
|
6
|
+
const rows = await store.db
|
|
7
|
+
.selectFrom('user')
|
|
8
|
+
.select('user.id')
|
|
9
|
+
.where((eb) =>
|
|
10
|
+
eb.not(
|
|
11
|
+
eb.exists(
|
|
12
|
+
eb
|
|
13
|
+
.selectFrom('oauthRefreshToken')
|
|
14
|
+
.select('oauthRefreshToken.id')
|
|
15
|
+
.whereRef('oauthRefreshToken.userId', '=', 'user.id'),
|
|
16
|
+
),
|
|
17
|
+
),
|
|
18
|
+
)
|
|
19
|
+
.execute();
|
|
20
|
+
|
|
21
|
+
return rows.map((row) => row.id);
|
|
22
|
+
}
|