kavachos 0.0.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.
@@ -0,0 +1,351 @@
1
+ import { z } from 'zod';
2
+
3
+ interface KavachError {
4
+ code: string;
5
+ message: string;
6
+ details?: Record<string, unknown>;
7
+ }
8
+ type Result<T> = {
9
+ success: true;
10
+ data: T;
11
+ } | {
12
+ success: false;
13
+ error: KavachError;
14
+ };
15
+ interface McpConfig {
16
+ /** Enable MCP authorization server */
17
+ enabled: boolean;
18
+ /** Enforce auth on all MCP requests */
19
+ enforceAuth?: boolean;
20
+ /** Custom scopes supported by this server */
21
+ scopes?: string[];
22
+ /** Issuer URL (typically your base URL) */
23
+ issuer?: string;
24
+ /** Base URL for MCP endpoints */
25
+ baseUrl?: string;
26
+ /** Secret key for signing JWTs (must be >= 32 chars) */
27
+ signingSecret?: string;
28
+ /** Access token TTL in seconds (default: 3600) */
29
+ accessTokenTtl?: number;
30
+ /** Refresh token TTL in seconds (default: 604800 = 7 days) */
31
+ refreshTokenTtl?: number;
32
+ /** Authorization code TTL in seconds (default: 600 = 10 minutes) */
33
+ codeTtl?: number;
34
+ /** Allowed resource URIs for RFC 8707 */
35
+ allowedResources?: string[];
36
+ /** Login page URL - where users are redirected to authenticate */
37
+ loginPage?: string;
38
+ /** Consent page URL - where users approve scopes */
39
+ consentPage?: string;
40
+ /** Custom token claims generator */
41
+ getAdditionalClaims?: (userId: string, scopes: string[]) => Promise<Record<string, unknown>>;
42
+ }
43
+ interface McpServerMetadata {
44
+ issuer: string;
45
+ authorization_endpoint: string;
46
+ token_endpoint: string;
47
+ registration_endpoint: string;
48
+ jwks_uri?: string;
49
+ scopes_supported: string[];
50
+ response_types_supported: string[];
51
+ response_modes_supported: string[];
52
+ grant_types_supported: string[];
53
+ token_endpoint_auth_methods_supported: string[];
54
+ code_challenge_methods_supported: string[];
55
+ service_documentation?: string;
56
+ revocation_endpoint?: string;
57
+ }
58
+ interface McpProtectedResourceMetadata {
59
+ resource: string;
60
+ authorization_servers: string[];
61
+ jwks_uri?: string;
62
+ scopes_supported: string[];
63
+ bearer_methods_supported: string[];
64
+ resource_signing_alg_values_supported?: string[];
65
+ }
66
+ interface McpClient {
67
+ clientId: string;
68
+ clientSecret: string | null;
69
+ clientName: string | null;
70
+ clientUri: string | null;
71
+ logoUri: string | null;
72
+ redirectUris: string[];
73
+ grantTypes: string[];
74
+ responseTypes: string[];
75
+ tokenEndpointAuthMethod: string;
76
+ scope: string | null;
77
+ contacts: string[] | null;
78
+ tosUri: string | null;
79
+ policyUri: string | null;
80
+ softwareId: string | null;
81
+ softwareVersion: string | null;
82
+ clientType: "public" | "confidential";
83
+ disabled: boolean;
84
+ userId: string | null;
85
+ createdAt: Date;
86
+ updatedAt: Date;
87
+ }
88
+ interface McpClientRegistrationRequest {
89
+ redirect_uris: string[];
90
+ token_endpoint_auth_method?: string;
91
+ grant_types?: string[];
92
+ response_types?: string[];
93
+ client_name?: string;
94
+ client_uri?: string;
95
+ logo_uri?: string;
96
+ scope?: string;
97
+ contacts?: string[];
98
+ tos_uri?: string;
99
+ policy_uri?: string;
100
+ software_id?: string;
101
+ software_version?: string;
102
+ }
103
+ interface McpClientRegistrationResponse {
104
+ client_id: string;
105
+ client_secret?: string;
106
+ client_id_issued_at: number;
107
+ client_secret_expires_at?: number;
108
+ redirect_uris: string[];
109
+ token_endpoint_auth_method: string;
110
+ grant_types: string[];
111
+ response_types: string[];
112
+ client_name?: string;
113
+ client_uri?: string;
114
+ logo_uri?: string;
115
+ scope?: string;
116
+ contacts?: string[];
117
+ tos_uri?: string;
118
+ policy_uri?: string;
119
+ software_id?: string;
120
+ software_version?: string;
121
+ }
122
+ interface McpAuthorizationCode {
123
+ code: string;
124
+ clientId: string;
125
+ userId: string;
126
+ redirectUri: string;
127
+ scope: string[];
128
+ codeChallenge: string;
129
+ codeChallengeMethod: "S256";
130
+ resource: string | null;
131
+ expiresAt: Date;
132
+ createdAt: Date;
133
+ }
134
+ interface McpAuthorizeRequest {
135
+ response_type: string;
136
+ client_id: string;
137
+ redirect_uri: string;
138
+ scope?: string;
139
+ state?: string;
140
+ code_challenge: string;
141
+ code_challenge_method: string;
142
+ resource?: string;
143
+ }
144
+ interface McpAuthorizeResult {
145
+ redirectUri: string;
146
+ code: string;
147
+ state: string | null;
148
+ }
149
+ interface McpAccessToken {
150
+ accessToken: string;
151
+ refreshToken: string | null;
152
+ tokenType: "Bearer";
153
+ expiresIn: number;
154
+ scope: string[];
155
+ clientId: string;
156
+ userId: string;
157
+ resource: string | null;
158
+ expiresAt: Date;
159
+ createdAt: Date;
160
+ }
161
+ interface McpTokenRequest {
162
+ grant_type: string;
163
+ code?: string;
164
+ redirect_uri?: string;
165
+ client_id?: string;
166
+ client_secret?: string;
167
+ code_verifier?: string;
168
+ refresh_token?: string;
169
+ resource?: string;
170
+ scope?: string;
171
+ }
172
+ interface McpTokenResponse {
173
+ access_token: string;
174
+ token_type: "Bearer";
175
+ expires_in: number;
176
+ refresh_token?: string;
177
+ scope: string;
178
+ }
179
+ interface McpTokenPayload {
180
+ sub: string;
181
+ iss: string;
182
+ aud: string | string[];
183
+ exp: number;
184
+ iat: number;
185
+ jti: string;
186
+ scope: string;
187
+ client_id: string;
188
+ }
189
+ interface McpSession {
190
+ userId: string;
191
+ clientId: string;
192
+ scopes: string[];
193
+ resource: string | null;
194
+ expiresAt: Date;
195
+ tokenId: string;
196
+ }
197
+ interface McpAuthContext {
198
+ config: Required<Pick<McpConfig, "issuer" | "baseUrl" | "accessTokenTtl" | "refreshTokenTtl" | "codeTtl">> & McpConfig;
199
+ /** Store a client registration */
200
+ storeClient: (client: McpClient) => Promise<void>;
201
+ /** Find a client by ID */
202
+ findClient: (clientId: string) => Promise<McpClient | null>;
203
+ /** Store an authorization code */
204
+ storeAuthorizationCode: (code: McpAuthorizationCode) => Promise<void>;
205
+ /** Find and consume an authorization code (must delete after finding) */
206
+ consumeAuthorizationCode: (code: string) => Promise<McpAuthorizationCode | null>;
207
+ /** Store an access token record */
208
+ storeToken: (token: McpAccessToken) => Promise<void>;
209
+ /** Find a token by refresh token value */
210
+ findTokenByRefreshToken: (refreshToken: string) => Promise<McpAccessToken | null>;
211
+ /** Revoke a token (by access token value) */
212
+ revokeToken: (accessToken: string) => Promise<void>;
213
+ /** Resolve the authenticated user ID (e.g., from session cookie) */
214
+ resolveUserId: (request: Request) => Promise<string | null>;
215
+ }
216
+ interface McpAuthModule {
217
+ /** Get OAuth 2.0 Authorization Server Metadata */
218
+ getMetadata: () => McpServerMetadata;
219
+ /** Get Protected Resource Metadata (RFC 9728) */
220
+ getProtectedResourceMetadata: () => McpProtectedResourceMetadata;
221
+ /** Register a new OAuth client (RFC 7591) */
222
+ registerClient: (body: McpClientRegistrationRequest) => Promise<Result<McpClientRegistrationResponse>>;
223
+ /** Handle authorization request */
224
+ authorize: (request: Request) => Promise<Result<McpAuthorizeResult>>;
225
+ /** Handle token exchange */
226
+ token: (request: Request) => Promise<Result<McpTokenResponse>>;
227
+ /** Validate an access token */
228
+ validateToken: (token: string, requiredScopes?: string[]) => Promise<Result<McpSession>>;
229
+ /** Middleware that validates Bearer tokens and returns session */
230
+ middleware: (request: Request) => Promise<Result<McpSession>>;
231
+ }
232
+ declare const McpClientRegistrationSchema: z.ZodObject<{
233
+ redirect_uris: z.ZodArray<z.ZodString, "many">;
234
+ token_endpoint_auth_method: z.ZodOptional<z.ZodDefault<z.ZodEnum<["none", "client_secret_basic", "client_secret_post"]>>>;
235
+ grant_types: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodEnum<["authorization_code", "refresh_token"]>, "many">>>;
236
+ response_types: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodEnum<["code"]>, "many">>>;
237
+ client_name: z.ZodOptional<z.ZodString>;
238
+ client_uri: z.ZodOptional<z.ZodString>;
239
+ logo_uri: z.ZodOptional<z.ZodString>;
240
+ scope: z.ZodOptional<z.ZodString>;
241
+ contacts: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
242
+ tos_uri: z.ZodOptional<z.ZodString>;
243
+ policy_uri: z.ZodOptional<z.ZodString>;
244
+ software_id: z.ZodOptional<z.ZodString>;
245
+ software_version: z.ZodOptional<z.ZodString>;
246
+ }, "strip", z.ZodTypeAny, {
247
+ redirect_uris: string[];
248
+ client_name?: string | undefined;
249
+ client_uri?: string | undefined;
250
+ grant_types?: ("authorization_code" | "refresh_token")[] | undefined;
251
+ response_types?: "code"[] | undefined;
252
+ token_endpoint_auth_method?: "client_secret_basic" | "none" | "client_secret_post" | undefined;
253
+ logo_uri?: string | undefined;
254
+ scope?: string | undefined;
255
+ contacts?: string[] | undefined;
256
+ tos_uri?: string | undefined;
257
+ policy_uri?: string | undefined;
258
+ software_id?: string | undefined;
259
+ software_version?: string | undefined;
260
+ }, {
261
+ redirect_uris: string[];
262
+ client_name?: string | undefined;
263
+ client_uri?: string | undefined;
264
+ grant_types?: ("authorization_code" | "refresh_token")[] | undefined;
265
+ response_types?: "code"[] | undefined;
266
+ token_endpoint_auth_method?: "client_secret_basic" | "none" | "client_secret_post" | undefined;
267
+ logo_uri?: string | undefined;
268
+ scope?: string | undefined;
269
+ contacts?: string[] | undefined;
270
+ tos_uri?: string | undefined;
271
+ policy_uri?: string | undefined;
272
+ software_id?: string | undefined;
273
+ software_version?: string | undefined;
274
+ }>;
275
+ declare const McpAuthorizeRequestSchema: z.ZodObject<{
276
+ response_type: z.ZodLiteral<"code">;
277
+ client_id: z.ZodString;
278
+ redirect_uri: z.ZodString;
279
+ scope: z.ZodOptional<z.ZodString>;
280
+ state: z.ZodOptional<z.ZodString>;
281
+ code_challenge: z.ZodString;
282
+ code_challenge_method: z.ZodLiteral<"S256">;
283
+ resource: z.ZodOptional<z.ZodString>;
284
+ }, "strip", z.ZodTypeAny, {
285
+ client_id: string;
286
+ redirect_uri: string;
287
+ code_challenge: string;
288
+ code_challenge_method: "S256";
289
+ response_type: "code";
290
+ resource?: string | undefined;
291
+ scope?: string | undefined;
292
+ state?: string | undefined;
293
+ }, {
294
+ client_id: string;
295
+ redirect_uri: string;
296
+ code_challenge: string;
297
+ code_challenge_method: "S256";
298
+ response_type: "code";
299
+ resource?: string | undefined;
300
+ scope?: string | undefined;
301
+ state?: string | undefined;
302
+ }>;
303
+ declare const McpTokenRequestSchema: z.ZodDiscriminatedUnion<"grant_type", [z.ZodObject<{
304
+ grant_type: z.ZodLiteral<"authorization_code">;
305
+ code: z.ZodString;
306
+ redirect_uri: z.ZodString;
307
+ client_id: z.ZodString;
308
+ client_secret: z.ZodOptional<z.ZodString>;
309
+ code_verifier: z.ZodString;
310
+ resource: z.ZodOptional<z.ZodString>;
311
+ }, "strip", z.ZodTypeAny, {
312
+ client_id: string;
313
+ code: string;
314
+ redirect_uri: string;
315
+ grant_type: "authorization_code";
316
+ code_verifier: string;
317
+ resource?: string | undefined;
318
+ client_secret?: string | undefined;
319
+ }, {
320
+ client_id: string;
321
+ code: string;
322
+ redirect_uri: string;
323
+ grant_type: "authorization_code";
324
+ code_verifier: string;
325
+ resource?: string | undefined;
326
+ client_secret?: string | undefined;
327
+ }>, z.ZodObject<{
328
+ grant_type: z.ZodLiteral<"refresh_token">;
329
+ refresh_token: z.ZodString;
330
+ client_id: z.ZodString;
331
+ client_secret: z.ZodOptional<z.ZodString>;
332
+ scope: z.ZodOptional<z.ZodString>;
333
+ resource: z.ZodOptional<z.ZodString>;
334
+ }, "strip", z.ZodTypeAny, {
335
+ client_id: string;
336
+ refresh_token: string;
337
+ grant_type: "refresh_token";
338
+ resource?: string | undefined;
339
+ client_secret?: string | undefined;
340
+ scope?: string | undefined;
341
+ }, {
342
+ client_id: string;
343
+ refresh_token: string;
344
+ grant_type: "refresh_token";
345
+ resource?: string | undefined;
346
+ client_secret?: string | undefined;
347
+ scope?: string | undefined;
348
+ }>]>;
349
+ type McpTokenRequestParsed = z.infer<typeof McpTokenRequestSchema>;
350
+
351
+ export { type KavachError as K, type McpConfig as M, type Result as R, type McpAuthContext as a, type McpAuthorizeResult as b, type McpServerMetadata as c, type McpProtectedResourceMetadata as d, type McpClientRegistrationResponse as e, type McpAuthModule as f, type McpTokenResponse as g, type McpSession as h, type McpAccessToken as i, type McpAuthorizationCode as j, type McpAuthorizeRequest as k, McpAuthorizeRequestSchema as l, type McpClient as m, type McpClientRegistrationRequest as n, McpClientRegistrationSchema as o, type McpTokenPayload as p, type McpTokenRequest as q, type McpTokenRequestParsed as r, McpTokenRequestSchema as s };