@kya-os/contracts 1.7.1 → 1.7.3

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.
@@ -9,13 +9,15 @@
9
9
  * @module @kya-os/contracts/tool-protection
10
10
  */
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DelegationRequiredErrorDataSchema = exports.ToolProtectionResponseSchema = exports.ToolProtectionMapSchema = exports.ToolProtectionSchema = exports.AuthorizationRequirementSchema = void 0;
12
+ exports.CONSENT_PROVIDER_TYPES = exports.DelegationRequiredErrorDataSchema = exports.ToolProtectionResponseSchema = exports.ToolProtectionMapSchema = exports.ToolProtectionSchema = exports.AuthorizationRequirementSchema = exports.AUTHORIZATION_TYPES = void 0;
13
13
  exports.isToolProtection = isToolProtection;
14
14
  exports.isToolProtectionMap = isToolProtectionMap;
15
15
  exports.isToolProtectionResponse = isToolProtectionResponse;
16
16
  exports.isDelegationRequiredErrorData = isDelegationRequiredErrorData;
17
17
  exports.isAuthorizationRequirement = isAuthorizationRequirement;
18
18
  exports.hasOAuthAuthorization = hasOAuthAuthorization;
19
+ exports.hasPasswordAuthorization = hasPasswordAuthorization;
20
+ exports.hasVerifiableCredentialAuthorization = hasVerifiableCredentialAuthorization;
19
21
  exports.validateToolProtection = validateToolProtection;
20
22
  exports.validateToolProtectionMap = validateToolProtectionMap;
21
23
  exports.validateToolProtectionResponse = validateToolProtectionResponse;
@@ -25,7 +27,31 @@ exports.getToolRequiredScopes = getToolRequiredScopes;
25
27
  exports.getToolRiskLevel = getToolRiskLevel;
26
28
  exports.createDelegationRequiredError = createDelegationRequiredError;
27
29
  exports.normalizeToolProtection = normalizeToolProtection;
30
+ exports.determineConsentProviderType = determineConsentProviderType;
31
+ exports.normalizeAuthorizationType = normalizeAuthorizationType;
32
+ exports.getAuthorizationTypeLabel = getAuthorizationTypeLabel;
33
+ exports.getAuthorizationTypeKey = getAuthorizationTypeKey;
28
34
  const zod_1 = require("zod");
35
+ /**
36
+ * Canonical authorization type values
37
+ * Use these constants instead of string literals for type safety
38
+ */
39
+ exports.AUTHORIZATION_TYPES = {
40
+ /** OAuth 2.0 provider authentication */
41
+ OAUTH: 'oauth',
42
+ /** Username/password or API key authentication */
43
+ PASSWORD: 'password',
44
+ /** Mobile Driver's License (ISO 18013-5) */
45
+ MDL: 'mdl',
46
+ /** Identity Verification provider */
47
+ IDV: 'idv',
48
+ /** W3C Verifiable Credential requirement (preferred) */
49
+ VERIFIABLE_CREDENTIAL: 'verifiable_credential',
50
+ /** @deprecated Use VERIFIABLE_CREDENTIAL instead */
51
+ CREDENTIAL: 'credential',
52
+ /** Consent-only (clickwrap agreement) */
53
+ NONE: 'none',
54
+ };
29
55
  /**
30
56
  * Zod Schemas for Validation
31
57
  */
@@ -35,6 +61,10 @@ exports.AuthorizationRequirementSchema = zod_1.z.discriminatedUnion('type', [
35
61
  provider: zod_1.z.string(),
36
62
  requiredScopes: zod_1.z.array(zod_1.z.string()).optional(),
37
63
  }),
64
+ zod_1.z.object({
65
+ type: zod_1.z.literal('password'),
66
+ provider: zod_1.z.string(),
67
+ }),
38
68
  zod_1.z.object({
39
69
  type: zod_1.z.literal('mdl'),
40
70
  issuer: zod_1.z.string(),
@@ -45,6 +75,12 @@ exports.AuthorizationRequirementSchema = zod_1.z.discriminatedUnion('type', [
45
75
  provider: zod_1.z.string(),
46
76
  verificationLevel: zod_1.z.enum(['basic', 'enhanced', 'loa3']).optional(),
47
77
  }),
78
+ zod_1.z.object({
79
+ type: zod_1.z.literal('verifiable_credential'),
80
+ credentialType: zod_1.z.string(),
81
+ issuer: zod_1.z.string().optional(),
82
+ }),
83
+ // Backward compatibility: 'credential' is an alias for 'verifiable_credential'
48
84
  zod_1.z.object({
49
85
  type: zod_1.z.literal('credential'),
50
86
  credentialType: zod_1.z.string(),
@@ -104,6 +140,20 @@ function isAuthorizationRequirement(obj) {
104
140
  function hasOAuthAuthorization(protection) {
105
141
  return protection.authorization?.type === 'oauth';
106
142
  }
143
+ /**
144
+ * Type guard to check if a ToolProtection has password authorization
145
+ */
146
+ function hasPasswordAuthorization(protection) {
147
+ return protection.authorization?.type === 'password';
148
+ }
149
+ /**
150
+ * Type guard to check if a ToolProtection requires a Verifiable Credential
151
+ * Handles both 'verifiable_credential' and legacy 'credential' types
152
+ */
153
+ function hasVerifiableCredentialAuthorization(protection) {
154
+ const type = protection.authorization?.type;
155
+ return type === 'verifiable_credential' || type === 'credential';
156
+ }
107
157
  /**
108
158
  * Validation Functions
109
159
  */
@@ -198,3 +248,133 @@ function normalizeToolProtection(raw) {
198
248
  }
199
249
  return normalized;
200
250
  }
251
+ // =============================================================================
252
+ // CONSENT PROVIDER TYPES
253
+ // =============================================================================
254
+ /**
255
+ * Consent Provider Types
256
+ *
257
+ * These constants define the authentication method used during consent:
258
+ * - NONE: Consent-only mode (clickwrap) - user agrees without authentication
259
+ * - OAUTH2: OAuth provider authentication (GitHub, Google, etc.)
260
+ * - PASSWORD: Password-based authentication (email/password, username/password)
261
+ * - CREDENTIAL: Alias for PASSWORD (legacy compatibility)
262
+ * - MAGIC_LINK: Email magic link authentication
263
+ * - OTP: One-time password authentication
264
+ *
265
+ * NOTE: This is distinct from AUTHORIZATION_TYPES which define what a TOOL requires.
266
+ * CONSENT_PROVIDER_TYPES define what authentication method the USER used.
267
+ */
268
+ exports.CONSENT_PROVIDER_TYPES = {
269
+ /** Consent-only mode - no authentication, just clickwrap agreement */
270
+ NONE: 'none',
271
+ /** OAuth2 provider authentication */
272
+ OAUTH2: 'oauth2',
273
+ /** Password-based authentication (email/password, username/password) */
274
+ PASSWORD: 'password',
275
+ /** @deprecated Use PASSWORD instead. Alias for backward compatibility */
276
+ CREDENTIAL: 'credential',
277
+ /** Email magic link authentication */
278
+ MAGIC_LINK: 'magic_link',
279
+ /** One-time password (SMS/email code) */
280
+ OTP: 'otp',
281
+ };
282
+ /**
283
+ * Determine consent provider type based on available identity information
284
+ *
285
+ * This is the single source of truth for consent provider type determination.
286
+ *
287
+ * @param hasOAuthIdentity - Whether OAuth identity is available
288
+ * @param isPasswordFlow - Whether this is a password/credential flow
289
+ * @param isMagicLinkFlow - Whether this is a magic link flow
290
+ * @param isOtpFlow - Whether this is an OTP flow
291
+ * @returns The consent provider type
292
+ */
293
+ function determineConsentProviderType(hasOAuthIdentity, isPasswordFlow = false, isMagicLinkFlow = false, isOtpFlow = false) {
294
+ if (isPasswordFlow) {
295
+ return exports.CONSENT_PROVIDER_TYPES.PASSWORD;
296
+ }
297
+ if (isMagicLinkFlow) {
298
+ return exports.CONSENT_PROVIDER_TYPES.MAGIC_LINK;
299
+ }
300
+ if (isOtpFlow) {
301
+ return exports.CONSENT_PROVIDER_TYPES.OTP;
302
+ }
303
+ if (hasOAuthIdentity) {
304
+ return exports.CONSENT_PROVIDER_TYPES.OAUTH2;
305
+ }
306
+ return exports.CONSENT_PROVIDER_TYPES.NONE;
307
+ }
308
+ // =============================================================================
309
+ // AUTHORIZATION TYPE NORMALIZATION
310
+ // =============================================================================
311
+ /**
312
+ * Normalize authorization requirement to use canonical type names
313
+ *
314
+ * Migrates legacy 'credential' type to 'verifiable_credential'
315
+ * This ensures consistent type usage across the codebase.
316
+ *
317
+ * @param auth - The authorization requirement (may have legacy type)
318
+ * @returns Normalized authorization requirement with canonical type
319
+ */
320
+ function normalizeAuthorizationType(auth) {
321
+ // Migrate legacy 'credential' to 'verifiable_credential'
322
+ if (auth.type === 'credential') {
323
+ return {
324
+ type: 'verifiable_credential',
325
+ credentialType: auth.credentialType,
326
+ issuer: auth.issuer,
327
+ };
328
+ }
329
+ return auth;
330
+ }
331
+ /**
332
+ * Get a human-readable label for an authorization requirement type
333
+ */
334
+ function getAuthorizationTypeLabel(auth) {
335
+ switch (auth.type) {
336
+ case 'oauth':
337
+ return auth.provider
338
+ ? auth.provider.charAt(0).toUpperCase() + auth.provider.slice(1)
339
+ : 'OAuth Provider';
340
+ case 'password':
341
+ return 'Password Authentication';
342
+ case 'mdl':
343
+ return "Mobile Driver's License";
344
+ case 'idv':
345
+ return 'Identity Verification';
346
+ case 'verifiable_credential':
347
+ case 'credential':
348
+ return auth.credentialType || 'Verifiable Credential';
349
+ case 'none':
350
+ return 'Consent Only';
351
+ default:
352
+ // TypeScript exhaustiveness check
353
+ const _exhaustive = auth;
354
+ return 'Unknown';
355
+ }
356
+ }
357
+ /**
358
+ * Get a unique key for an authorization requirement (for React keys, caching, etc.)
359
+ */
360
+ function getAuthorizationTypeKey(auth) {
361
+ switch (auth.type) {
362
+ case 'oauth':
363
+ return `oauth:${auth.provider}`;
364
+ case 'password':
365
+ return `password:${auth.provider}`;
366
+ case 'mdl':
367
+ return `mdl:${auth.issuer}:${auth.credentialType || ''}`;
368
+ case 'idv':
369
+ return `idv:${auth.provider}:${auth.verificationLevel || ''}`;
370
+ case 'verifiable_credential':
371
+ case 'credential':
372
+ return `vc:${auth.issuer || 'any'}:${auth.credentialType}`;
373
+ case 'none':
374
+ return 'none';
375
+ default:
376
+ // TypeScript exhaustiveness check
377
+ const _exhaustive = auth;
378
+ return 'unknown';
379
+ }
380
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kya-os/contracts",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "Shared contracts, types, and schemas for MCP-I framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",