@mcp-z/oauth 1.0.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.
Files changed (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -0
  3. package/dist/cjs/account-utils.d.cts +107 -0
  4. package/dist/cjs/account-utils.d.ts +107 -0
  5. package/dist/cjs/account-utils.js +481 -0
  6. package/dist/cjs/account-utils.js.map +1 -0
  7. package/dist/cjs/index.d.cts +19 -0
  8. package/dist/cjs/index.d.ts +19 -0
  9. package/dist/cjs/index.js +149 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/jwt-auth.d.cts +53 -0
  12. package/dist/cjs/jwt-auth.d.ts +53 -0
  13. package/dist/cjs/jwt-auth.js +417 -0
  14. package/dist/cjs/jwt-auth.js.map +1 -0
  15. package/dist/cjs/key-utils.d.cts +131 -0
  16. package/dist/cjs/key-utils.d.ts +131 -0
  17. package/dist/cjs/key-utils.js +421 -0
  18. package/dist/cjs/key-utils.js.map +1 -0
  19. package/dist/cjs/lib/account-server/index.d.cts +45 -0
  20. package/dist/cjs/lib/account-server/index.d.ts +45 -0
  21. package/dist/cjs/lib/account-server/index.js +67 -0
  22. package/dist/cjs/lib/account-server/index.js.map +1 -0
  23. package/dist/cjs/lib/account-server/loopback.d.cts +22 -0
  24. package/dist/cjs/lib/account-server/loopback.d.ts +22 -0
  25. package/dist/cjs/lib/account-server/loopback.js +778 -0
  26. package/dist/cjs/lib/account-server/loopback.js.map +1 -0
  27. package/dist/cjs/lib/account-server/me.d.cts +23 -0
  28. package/dist/cjs/lib/account-server/me.d.ts +23 -0
  29. package/dist/cjs/lib/account-server/me.js +412 -0
  30. package/dist/cjs/lib/account-server/me.js.map +1 -0
  31. package/dist/cjs/lib/account-server/shared-utils.d.cts +6 -0
  32. package/dist/cjs/lib/account-server/shared-utils.d.ts +6 -0
  33. package/dist/cjs/lib/account-server/shared-utils.js +235 -0
  34. package/dist/cjs/lib/account-server/shared-utils.js.map +1 -0
  35. package/dist/cjs/lib/account-server/stateless.d.cts +20 -0
  36. package/dist/cjs/lib/account-server/stateless.d.ts +20 -0
  37. package/dist/cjs/lib/account-server/stateless.js +32 -0
  38. package/dist/cjs/lib/account-server/stateless.js.map +1 -0
  39. package/dist/cjs/lib/account-server/types.d.cts +32 -0
  40. package/dist/cjs/lib/account-server/types.d.ts +32 -0
  41. package/dist/cjs/lib/account-server/types.js +7 -0
  42. package/dist/cjs/lib/account-server/types.js.map +1 -0
  43. package/dist/cjs/lib/dcr-types.d.cts +126 -0
  44. package/dist/cjs/lib/dcr-types.d.ts +126 -0
  45. package/dist/cjs/lib/dcr-types.js +12 -0
  46. package/dist/cjs/lib/dcr-types.js.map +1 -0
  47. package/dist/cjs/lib/rfc-metadata-types.d.cts +46 -0
  48. package/dist/cjs/lib/rfc-metadata-types.d.ts +46 -0
  49. package/dist/cjs/lib/rfc-metadata-types.js +8 -0
  50. package/dist/cjs/lib/rfc-metadata-types.js.map +1 -0
  51. package/dist/cjs/package.json +1 -0
  52. package/dist/cjs/pkce.d.cts +36 -0
  53. package/dist/cjs/pkce.d.ts +36 -0
  54. package/dist/cjs/pkce.js +25 -0
  55. package/dist/cjs/pkce.js.map +1 -0
  56. package/dist/cjs/sanitizer.d.cts +37 -0
  57. package/dist/cjs/sanitizer.d.ts +37 -0
  58. package/dist/cjs/sanitizer.js +407 -0
  59. package/dist/cjs/sanitizer.js.map +1 -0
  60. package/dist/cjs/schemas/index.d.cts +36 -0
  61. package/dist/cjs/schemas/index.d.ts +36 -0
  62. package/dist/cjs/schemas/index.js +28 -0
  63. package/dist/cjs/schemas/index.js.map +1 -0
  64. package/dist/cjs/session-auth.d.cts +79 -0
  65. package/dist/cjs/session-auth.d.ts +79 -0
  66. package/dist/cjs/session-auth.js +354 -0
  67. package/dist/cjs/session-auth.js.map +1 -0
  68. package/dist/cjs/templates.d.cts +18 -0
  69. package/dist/cjs/templates.d.ts +18 -0
  70. package/dist/cjs/templates.js +38 -0
  71. package/dist/cjs/templates.js.map +1 -0
  72. package/dist/cjs/types.d.cts +343 -0
  73. package/dist/cjs/types.d.ts +343 -0
  74. package/dist/cjs/types.js +210 -0
  75. package/dist/cjs/types.js.map +1 -0
  76. package/dist/esm/account-utils.d.ts +107 -0
  77. package/dist/esm/account-utils.js +179 -0
  78. package/dist/esm/account-utils.js.map +1 -0
  79. package/dist/esm/index.d.ts +19 -0
  80. package/dist/esm/index.js +23 -0
  81. package/dist/esm/index.js.map +1 -0
  82. package/dist/esm/jwt-auth.d.ts +53 -0
  83. package/dist/esm/jwt-auth.js +164 -0
  84. package/dist/esm/jwt-auth.js.map +1 -0
  85. package/dist/esm/key-utils.d.ts +131 -0
  86. package/dist/esm/key-utils.js +143 -0
  87. package/dist/esm/key-utils.js.map +1 -0
  88. package/dist/esm/lib/account-server/index.d.ts +45 -0
  89. package/dist/esm/lib/account-server/index.js +41 -0
  90. package/dist/esm/lib/account-server/index.js.map +1 -0
  91. package/dist/esm/lib/account-server/loopback.d.ts +22 -0
  92. package/dist/esm/lib/account-server/loopback.js +372 -0
  93. package/dist/esm/lib/account-server/loopback.js.map +1 -0
  94. package/dist/esm/lib/account-server/me.d.ts +23 -0
  95. package/dist/esm/lib/account-server/me.js +170 -0
  96. package/dist/esm/lib/account-server/me.js.map +1 -0
  97. package/dist/esm/lib/account-server/shared-utils.d.ts +6 -0
  98. package/dist/esm/lib/account-server/shared-utils.js +24 -0
  99. package/dist/esm/lib/account-server/shared-utils.js.map +1 -0
  100. package/dist/esm/lib/account-server/stateless.d.ts +20 -0
  101. package/dist/esm/lib/account-server/stateless.js +25 -0
  102. package/dist/esm/lib/account-server/stateless.js.map +1 -0
  103. package/dist/esm/lib/account-server/types.d.ts +32 -0
  104. package/dist/esm/lib/account-server/types.js +6 -0
  105. package/dist/esm/lib/account-server/types.js.map +1 -0
  106. package/dist/esm/lib/dcr-types.d.ts +126 -0
  107. package/dist/esm/lib/dcr-types.js +13 -0
  108. package/dist/esm/lib/dcr-types.js.map +1 -0
  109. package/dist/esm/lib/rfc-metadata-types.d.ts +46 -0
  110. package/dist/esm/lib/rfc-metadata-types.js +7 -0
  111. package/dist/esm/lib/rfc-metadata-types.js.map +1 -0
  112. package/dist/esm/package.json +1 -0
  113. package/dist/esm/pkce.d.ts +36 -0
  114. package/dist/esm/pkce.js +33 -0
  115. package/dist/esm/pkce.js.map +1 -0
  116. package/dist/esm/sanitizer.d.ts +37 -0
  117. package/dist/esm/sanitizer.js +256 -0
  118. package/dist/esm/sanitizer.js.map +1 -0
  119. package/dist/esm/schemas/index.d.ts +36 -0
  120. package/dist/esm/schemas/index.js +19 -0
  121. package/dist/esm/schemas/index.js.map +1 -0
  122. package/dist/esm/session-auth.d.ts +79 -0
  123. package/dist/esm/session-auth.js +141 -0
  124. package/dist/esm/session-auth.js.map +1 -0
  125. package/dist/esm/templates.d.ts +18 -0
  126. package/dist/esm/templates.js +132 -0
  127. package/dist/esm/templates.js.map +1 -0
  128. package/dist/esm/types.d.ts +343 -0
  129. package/dist/esm/types.js +34 -0
  130. package/dist/esm/types.js.map +1 -0
  131. package/package.json +82 -0
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Key generation utilities for consistent storage key format
3
+ *
4
+ * Key format: {accountId}:{service}:{type}
5
+ * Example: work@gmail.com:gmail:token
6
+ */
7
+ import type { Keyv } from 'keyv';
8
+ /**
9
+ * Key types for account-scoped data (requires accountId)
10
+ */
11
+ export type AccountKeyType = 'token' | 'metadata' | 'dcr-client';
12
+ /**
13
+ * Key types for service-scoped data (no accountId)
14
+ */
15
+ export type ServiceKeyType = 'active' | 'linked';
16
+ /**
17
+ * Parameters for account-scoped keys.
18
+ * All fields are required - no silent defaults.
19
+ */
20
+ export interface AccountKeyParams {
21
+ /** Account identifier - typically an email address */
22
+ accountId: string;
23
+ /** Service name (e.g., 'gmail', 'sheets', 'drive', 'outlook') */
24
+ service: string;
25
+ }
26
+ /**
27
+ * Parameters for service-scoped keys.
28
+ * These keys don't include accountId.
29
+ */
30
+ export interface ServiceKeyParams {
31
+ /** Service name */
32
+ service: string;
33
+ }
34
+ /**
35
+ * Create account-scoped storage key.
36
+ *
37
+ * These keys are scoped to a specific account (email address) and store
38
+ * account-specific data like OAuth tokens and account metadata.
39
+ *
40
+ * @param type - Key type ('token' for OAuth tokens, 'metadata' for account details, 'dcr-client' for DCR registration)
41
+ * @param params - Account key parameters with explicit field names
42
+ * @returns Storage key in format: "{accountId}:{service}:{type}"
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // Store OAuth token
47
+ * const tokenKey = createAccountKey('token', {
48
+ * accountId: 'alice@gmail.com',
49
+ * service: 'gmail'
50
+ * });
51
+ * // Returns: "alice@gmail.com:gmail:token"
52
+ *
53
+ * // Store account metadata (alias, timestamps, profile)
54
+ * const metadataKey = createAccountKey('metadata', {
55
+ * accountId: 'alice@gmail.com',
56
+ * service: 'gmail'
57
+ * });
58
+ * // Returns: "alice@gmail.com:gmail:metadata"
59
+ *
60
+ * // Store DCR client registration info
61
+ * const dcrKey = createAccountKey('dcr-client', {
62
+ * accountId: 'alice@outlook.com',
63
+ * service: 'outlook'
64
+ * });
65
+ * // Returns: "alice@outlook.com:outlook:dcr-client"
66
+ * ```
67
+ */
68
+ export declare function createAccountKey(type: AccountKeyType, params: AccountKeyParams): string;
69
+ /**
70
+ * Create service-scoped storage key.
71
+ *
72
+ * These keys are scoped to a service (not a specific account) and store
73
+ * service-level data like which account is active or the list of linked accounts.
74
+ *
75
+ * @param type - Key type ('active' for active account, 'linked' for account list)
76
+ * @param params - Service key parameters
77
+ * @returns Storage key in format: "{service}:{type}"
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * // Track active account
82
+ * const activeKey = createServiceKey('active', {
83
+ * service: 'gmail'
84
+ * });
85
+ * // Returns: "gmail:active"
86
+ *
87
+ * // Store list of linked accounts
88
+ * const linkedKey = createServiceKey('linked', {
89
+ * service: 'gmail'
90
+ * });
91
+ * // Returns: "gmail:linked"
92
+ * ```
93
+ */
94
+ export declare function createServiceKey(type: ServiceKeyType, params: ServiceKeyParams): string;
95
+ /**
96
+ * Parse token key to extract components
97
+ *
98
+ * @param key - Storage key to parse
99
+ * @returns Object with accountId and service, or undefined if invalid format
100
+ *
101
+ * @example
102
+ * const parsed = parseTokenKey('user@gmail.com:gmail:token');
103
+ * // Returns: { accountId: 'user@gmail.com', service: 'gmail' }
104
+ *
105
+ * const invalid = parseTokenKey('invalid-key');
106
+ * // Returns: undefined
107
+ */
108
+ export declare function parseTokenKey(key: string): {
109
+ accountId: string;
110
+ service: string;
111
+ } | undefined;
112
+ /**
113
+ * List all account IDs for a service
114
+ *
115
+ * Iterates token keys and returns all accountIds that match the service.
116
+ * Encapsulates key format details for forward compatibility.
117
+ *
118
+ * @param store - Keyv store to iterate
119
+ * @param service - Service name
120
+ * @returns Array of account IDs (e.g., email addresses)
121
+ *
122
+ * @example
123
+ * const accounts = await listAccountIds(store, 'gmail');
124
+ * // Returns: ['alice@gmail.com', 'bob@gmail.com']
125
+ *
126
+ * @example
127
+ * // Empty array if no accounts found
128
+ * const empty = await listAccountIds(store, 'unknown-service');
129
+ * // Returns: []
130
+ */
131
+ export declare function listAccountIds(store: Keyv, service: string): Promise<string[]>;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Key generation utilities for consistent storage key format
3
+ *
4
+ * Key format: {accountId}:{service}:{type}
5
+ * Example: work@gmail.com:gmail:token
6
+ */
7
+ import type { Keyv } from 'keyv';
8
+ /**
9
+ * Key types for account-scoped data (requires accountId)
10
+ */
11
+ export type AccountKeyType = 'token' | 'metadata' | 'dcr-client';
12
+ /**
13
+ * Key types for service-scoped data (no accountId)
14
+ */
15
+ export type ServiceKeyType = 'active' | 'linked';
16
+ /**
17
+ * Parameters for account-scoped keys.
18
+ * All fields are required - no silent defaults.
19
+ */
20
+ export interface AccountKeyParams {
21
+ /** Account identifier - typically an email address */
22
+ accountId: string;
23
+ /** Service name (e.g., 'gmail', 'sheets', 'drive', 'outlook') */
24
+ service: string;
25
+ }
26
+ /**
27
+ * Parameters for service-scoped keys.
28
+ * These keys don't include accountId.
29
+ */
30
+ export interface ServiceKeyParams {
31
+ /** Service name */
32
+ service: string;
33
+ }
34
+ /**
35
+ * Create account-scoped storage key.
36
+ *
37
+ * These keys are scoped to a specific account (email address) and store
38
+ * account-specific data like OAuth tokens and account metadata.
39
+ *
40
+ * @param type - Key type ('token' for OAuth tokens, 'metadata' for account details, 'dcr-client' for DCR registration)
41
+ * @param params - Account key parameters with explicit field names
42
+ * @returns Storage key in format: "{accountId}:{service}:{type}"
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // Store OAuth token
47
+ * const tokenKey = createAccountKey('token', {
48
+ * accountId: 'alice@gmail.com',
49
+ * service: 'gmail'
50
+ * });
51
+ * // Returns: "alice@gmail.com:gmail:token"
52
+ *
53
+ * // Store account metadata (alias, timestamps, profile)
54
+ * const metadataKey = createAccountKey('metadata', {
55
+ * accountId: 'alice@gmail.com',
56
+ * service: 'gmail'
57
+ * });
58
+ * // Returns: "alice@gmail.com:gmail:metadata"
59
+ *
60
+ * // Store DCR client registration info
61
+ * const dcrKey = createAccountKey('dcr-client', {
62
+ * accountId: 'alice@outlook.com',
63
+ * service: 'outlook'
64
+ * });
65
+ * // Returns: "alice@outlook.com:outlook:dcr-client"
66
+ * ```
67
+ */
68
+ export declare function createAccountKey(type: AccountKeyType, params: AccountKeyParams): string;
69
+ /**
70
+ * Create service-scoped storage key.
71
+ *
72
+ * These keys are scoped to a service (not a specific account) and store
73
+ * service-level data like which account is active or the list of linked accounts.
74
+ *
75
+ * @param type - Key type ('active' for active account, 'linked' for account list)
76
+ * @param params - Service key parameters
77
+ * @returns Storage key in format: "{service}:{type}"
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * // Track active account
82
+ * const activeKey = createServiceKey('active', {
83
+ * service: 'gmail'
84
+ * });
85
+ * // Returns: "gmail:active"
86
+ *
87
+ * // Store list of linked accounts
88
+ * const linkedKey = createServiceKey('linked', {
89
+ * service: 'gmail'
90
+ * });
91
+ * // Returns: "gmail:linked"
92
+ * ```
93
+ */
94
+ export declare function createServiceKey(type: ServiceKeyType, params: ServiceKeyParams): string;
95
+ /**
96
+ * Parse token key to extract components
97
+ *
98
+ * @param key - Storage key to parse
99
+ * @returns Object with accountId and service, or undefined if invalid format
100
+ *
101
+ * @example
102
+ * const parsed = parseTokenKey('user@gmail.com:gmail:token');
103
+ * // Returns: { accountId: 'user@gmail.com', service: 'gmail' }
104
+ *
105
+ * const invalid = parseTokenKey('invalid-key');
106
+ * // Returns: undefined
107
+ */
108
+ export declare function parseTokenKey(key: string): {
109
+ accountId: string;
110
+ service: string;
111
+ } | undefined;
112
+ /**
113
+ * List all account IDs for a service
114
+ *
115
+ * Iterates token keys and returns all accountIds that match the service.
116
+ * Encapsulates key format details for forward compatibility.
117
+ *
118
+ * @param store - Keyv store to iterate
119
+ * @param service - Service name
120
+ * @returns Array of account IDs (e.g., email addresses)
121
+ *
122
+ * @example
123
+ * const accounts = await listAccountIds(store, 'gmail');
124
+ * // Returns: ['alice@gmail.com', 'bob@gmail.com']
125
+ *
126
+ * @example
127
+ * // Empty array if no accounts found
128
+ * const empty = await listAccountIds(store, 'unknown-service');
129
+ * // Returns: []
130
+ */
131
+ export declare function listAccountIds(store: Keyv, service: string): Promise<string[]>;
@@ -0,0 +1,421 @@
1
+ /**
2
+ * Key generation utilities for consistent storage key format
3
+ *
4
+ * Key format: {accountId}:{service}:{type}
5
+ * Example: work@gmail.com:gmail:token
6
+ */ "use strict";
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ function _export(target, all) {
11
+ for(var name in all)Object.defineProperty(target, name, {
12
+ enumerable: true,
13
+ get: Object.getOwnPropertyDescriptor(all, name).get
14
+ });
15
+ }
16
+ _export(exports, {
17
+ get createAccountKey () {
18
+ return createAccountKey;
19
+ },
20
+ get createServiceKey () {
21
+ return createServiceKey;
22
+ },
23
+ get listAccountIds () {
24
+ return listAccountIds;
25
+ },
26
+ get parseTokenKey () {
27
+ return parseTokenKey;
28
+ }
29
+ });
30
+ function _array_like_to_array(arr, len) {
31
+ if (len == null || len > arr.length) len = arr.length;
32
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
33
+ return arr2;
34
+ }
35
+ function _array_with_holes(arr) {
36
+ if (Array.isArray(arr)) return arr;
37
+ }
38
+ function _async_iterator(iterable) {
39
+ var method, async, sync, retry = 2;
40
+ for("undefined" != typeof Symbol && (async = Symbol.asyncIterator, sync = Symbol.iterator); retry--;){
41
+ if (async && null != (method = iterable[async])) return method.call(iterable);
42
+ if (sync && null != (method = iterable[sync])) return new AsyncFromSyncIterator(method.call(iterable));
43
+ async = "@@asyncIterator", sync = "@@iterator";
44
+ }
45
+ throw new TypeError("Object is not async iterable");
46
+ }
47
+ function AsyncFromSyncIterator(s) {
48
+ function AsyncFromSyncIteratorContinuation(r) {
49
+ if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object."));
50
+ var done = r.done;
51
+ return Promise.resolve(r.value).then(function(value) {
52
+ return {
53
+ value: value,
54
+ done: done
55
+ };
56
+ });
57
+ }
58
+ return AsyncFromSyncIterator = function(s) {
59
+ this.s = s, this.n = s.next;
60
+ }, AsyncFromSyncIterator.prototype = {
61
+ s: null,
62
+ n: null,
63
+ next: function() {
64
+ return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));
65
+ },
66
+ return: function(value) {
67
+ var ret = this.s.return;
68
+ return void 0 === ret ? Promise.resolve({
69
+ value: value,
70
+ done: !0
71
+ }) : AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));
72
+ },
73
+ throw: function(value) {
74
+ var thr = this.s.return;
75
+ return void 0 === thr ? Promise.reject(value) : AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));
76
+ }
77
+ }, new AsyncFromSyncIterator(s);
78
+ }
79
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
80
+ try {
81
+ var info = gen[key](arg);
82
+ var value = info.value;
83
+ } catch (error) {
84
+ reject(error);
85
+ return;
86
+ }
87
+ if (info.done) {
88
+ resolve(value);
89
+ } else {
90
+ Promise.resolve(value).then(_next, _throw);
91
+ }
92
+ }
93
+ function _async_to_generator(fn) {
94
+ return function() {
95
+ var self = this, args = arguments;
96
+ return new Promise(function(resolve, reject) {
97
+ var gen = fn.apply(self, args);
98
+ function _next(value) {
99
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
100
+ }
101
+ function _throw(err) {
102
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
103
+ }
104
+ _next(undefined);
105
+ });
106
+ };
107
+ }
108
+ function _iterable_to_array_limit(arr, i) {
109
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
110
+ if (_i == null) return;
111
+ var _arr = [];
112
+ var _n = true;
113
+ var _d = false;
114
+ var _s, _e;
115
+ try {
116
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
117
+ _arr.push(_s.value);
118
+ if (i && _arr.length === i) break;
119
+ }
120
+ } catch (err) {
121
+ _d = true;
122
+ _e = err;
123
+ } finally{
124
+ try {
125
+ if (!_n && _i["return"] != null) _i["return"]();
126
+ } finally{
127
+ if (_d) throw _e;
128
+ }
129
+ }
130
+ return _arr;
131
+ }
132
+ function _non_iterable_rest() {
133
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
134
+ }
135
+ function _sliced_to_array(arr, i) {
136
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
137
+ }
138
+ function _type_of(obj) {
139
+ "@swc/helpers - typeof";
140
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
141
+ }
142
+ function _unsupported_iterable_to_array(o, minLen) {
143
+ if (!o) return;
144
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
145
+ var n = Object.prototype.toString.call(o).slice(8, -1);
146
+ if (n === "Object" && o.constructor) n = o.constructor.name;
147
+ if (n === "Map" || n === "Set") return Array.from(n);
148
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
149
+ }
150
+ function _ts_generator(thisArg, body) {
151
+ var f, y, t, _ = {
152
+ label: 0,
153
+ sent: function() {
154
+ if (t[0] & 1) throw t[1];
155
+ return t[1];
156
+ },
157
+ trys: [],
158
+ ops: []
159
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
160
+ return d(g, "next", {
161
+ value: verb(0)
162
+ }), d(g, "throw", {
163
+ value: verb(1)
164
+ }), d(g, "return", {
165
+ value: verb(2)
166
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
167
+ value: function() {
168
+ return this;
169
+ }
170
+ }), g;
171
+ function verb(n) {
172
+ return function(v) {
173
+ return step([
174
+ n,
175
+ v
176
+ ]);
177
+ };
178
+ }
179
+ function step(op) {
180
+ if (f) throw new TypeError("Generator is already executing.");
181
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
182
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
183
+ if (y = 0, t) op = [
184
+ op[0] & 2,
185
+ t.value
186
+ ];
187
+ switch(op[0]){
188
+ case 0:
189
+ case 1:
190
+ t = op;
191
+ break;
192
+ case 4:
193
+ _.label++;
194
+ return {
195
+ value: op[1],
196
+ done: false
197
+ };
198
+ case 5:
199
+ _.label++;
200
+ y = op[1];
201
+ op = [
202
+ 0
203
+ ];
204
+ continue;
205
+ case 7:
206
+ op = _.ops.pop();
207
+ _.trys.pop();
208
+ continue;
209
+ default:
210
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
211
+ _ = 0;
212
+ continue;
213
+ }
214
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
215
+ _.label = op[1];
216
+ break;
217
+ }
218
+ if (op[0] === 6 && _.label < t[1]) {
219
+ _.label = t[1];
220
+ t = op;
221
+ break;
222
+ }
223
+ if (t && _.label < t[2]) {
224
+ _.label = t[2];
225
+ _.ops.push(op);
226
+ break;
227
+ }
228
+ if (t[2]) _.ops.pop();
229
+ _.trys.pop();
230
+ continue;
231
+ }
232
+ op = body.call(thisArg, _);
233
+ } catch (e) {
234
+ op = [
235
+ 6,
236
+ e
237
+ ];
238
+ y = 0;
239
+ } finally{
240
+ f = t = 0;
241
+ }
242
+ if (op[0] & 5) throw op[1];
243
+ return {
244
+ value: op[0] ? op[1] : void 0,
245
+ done: true
246
+ };
247
+ }
248
+ }
249
+ /**
250
+ * Validate key parameters don't contain colon delimiter
251
+ */ function validateKeyParams(params) {
252
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
253
+ try {
254
+ for(var _iterator = Object.entries(params)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
255
+ var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
256
+ if (typeof value !== 'string') {
257
+ throw new Error("Key parameter '".concat(key, "' must be a string, got: ").concat(typeof value === "undefined" ? "undefined" : _type_of(value)));
258
+ }
259
+ if (value.includes(':')) {
260
+ throw new Error("Key parameter '".concat(key, "' cannot contain colon character: ").concat(value));
261
+ }
262
+ }
263
+ } catch (err) {
264
+ _didIteratorError = true;
265
+ _iteratorError = err;
266
+ } finally{
267
+ try {
268
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
269
+ _iterator.return();
270
+ }
271
+ } finally{
272
+ if (_didIteratorError) {
273
+ throw _iteratorError;
274
+ }
275
+ }
276
+ }
277
+ }
278
+ function createAccountKey(type, params) {
279
+ validateKeyParams(params);
280
+ return "".concat(params.accountId, ":").concat(params.service, ":").concat(type);
281
+ }
282
+ function createServiceKey(type, params) {
283
+ validateKeyParams(params);
284
+ return "".concat(params.service, ":").concat(type);
285
+ }
286
+ function parseTokenKey(key) {
287
+ var parts = key.split(':');
288
+ if (parts.length !== 3 || parts[2] !== 'token' || !parts[0] || !parts[1]) {
289
+ return undefined;
290
+ }
291
+ return {
292
+ accountId: parts[0],
293
+ service: parts[1]
294
+ };
295
+ }
296
+ function listAccountIds(store, service) {
297
+ return _async_to_generator(function() {
298
+ var accountIds, _store_iterator, iterator, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, _value1, key, parsed, err, _error;
299
+ return _ts_generator(this, function(_state) {
300
+ switch(_state.label){
301
+ case 0:
302
+ accountIds = [];
303
+ _state.label = 1;
304
+ case 1:
305
+ _state.trys.push([
306
+ 1,
307
+ 14,
308
+ ,
309
+ 15
310
+ ]);
311
+ iterator = (_store_iterator = store.iterator) === null || _store_iterator === void 0 ? void 0 : _store_iterator.call(store, undefined);
312
+ if (!iterator) {
313
+ return [
314
+ 2,
315
+ accountIds
316
+ ];
317
+ }
318
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
319
+ _state.label = 2;
320
+ case 2:
321
+ _state.trys.push([
322
+ 2,
323
+ 7,
324
+ 8,
325
+ 13
326
+ ]);
327
+ _iterator = _async_iterator(iterator);
328
+ _state.label = 3;
329
+ case 3:
330
+ return [
331
+ 4,
332
+ _iterator.next()
333
+ ];
334
+ case 4:
335
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
336
+ 3,
337
+ 6
338
+ ];
339
+ _value = _step.value;
340
+ _value1 = _sliced_to_array(_value, 1), key = _value1[0];
341
+ parsed = parseTokenKey(key);
342
+ if (parsed && parsed.service === service) {
343
+ accountIds.push(parsed.accountId);
344
+ }
345
+ _state.label = 5;
346
+ case 5:
347
+ _iteratorAbruptCompletion = false;
348
+ return [
349
+ 3,
350
+ 3
351
+ ];
352
+ case 6:
353
+ return [
354
+ 3,
355
+ 13
356
+ ];
357
+ case 7:
358
+ err = _state.sent();
359
+ _didIteratorError = true;
360
+ _iteratorError = err;
361
+ return [
362
+ 3,
363
+ 13
364
+ ];
365
+ case 8:
366
+ _state.trys.push([
367
+ 8,
368
+ ,
369
+ 11,
370
+ 12
371
+ ]);
372
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
373
+ 3,
374
+ 10
375
+ ];
376
+ return [
377
+ 4,
378
+ _iterator.return()
379
+ ];
380
+ case 9:
381
+ _state.sent();
382
+ _state.label = 10;
383
+ case 10:
384
+ return [
385
+ 3,
386
+ 12
387
+ ];
388
+ case 11:
389
+ if (_didIteratorError) {
390
+ throw _iteratorError;
391
+ }
392
+ return [
393
+ 7
394
+ ];
395
+ case 12:
396
+ return [
397
+ 7
398
+ ];
399
+ case 13:
400
+ return [
401
+ 3,
402
+ 15
403
+ ];
404
+ case 14:
405
+ _error = _state.sent();
406
+ // If iteration fails, return empty array (fail gracefully)
407
+ // This handles stores that don't support iteration
408
+ return [
409
+ 2,
410
+ accountIds
411
+ ];
412
+ case 15:
413
+ return [
414
+ 2,
415
+ accountIds
416
+ ];
417
+ }
418
+ });
419
+ })();
420
+ }
421
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/Projects/ai/mcp-z/oauth/oauth/src/key-utils.ts"],"sourcesContent":["/**\n * Key generation utilities for consistent storage key format\n *\n * Key format: {accountId}:{service}:{type}\n * Example: work@gmail.com:gmail:token\n */\n\nimport type { Keyv } from 'keyv';\n\n/**\n * Key types for account-scoped data (requires accountId)\n */\nexport type AccountKeyType = 'token' | 'metadata' | 'dcr-client';\n\n/**\n * Key types for service-scoped data (no accountId)\n */\nexport type ServiceKeyType = 'active' | 'linked';\n\n/**\n * Parameters for account-scoped keys.\n * All fields are required - no silent defaults.\n */\nexport interface AccountKeyParams {\n /** Account identifier - typically an email address */\n accountId: string;\n\n /** Service name (e.g., 'gmail', 'sheets', 'drive', 'outlook') */\n service: string;\n}\n\n/**\n * Parameters for service-scoped keys.\n * These keys don't include accountId.\n */\nexport interface ServiceKeyParams {\n /** Service name */\n service: string;\n}\n\n/**\n * Validate key parameters don't contain colon delimiter\n */\nfunction validateKeyParams(params: AccountKeyParams | ServiceKeyParams): void {\n for (const [key, value] of Object.entries(params)) {\n if (typeof value !== 'string') {\n throw new Error(`Key parameter '${key}' must be a string, got: ${typeof value}`);\n }\n if (value.includes(':')) {\n throw new Error(`Key parameter '${key}' cannot contain colon character: ${value}`);\n }\n }\n}\n\n/**\n * Create account-scoped storage key.\n *\n * These keys are scoped to a specific account (email address) and store\n * account-specific data like OAuth tokens and account metadata.\n *\n * @param type - Key type ('token' for OAuth tokens, 'metadata' for account details, 'dcr-client' for DCR registration)\n * @param params - Account key parameters with explicit field names\n * @returns Storage key in format: \"{accountId}:{service}:{type}\"\n *\n * @example\n * ```typescript\n * // Store OAuth token\n * const tokenKey = createAccountKey('token', {\n * accountId: 'alice@gmail.com',\n * service: 'gmail'\n * });\n * // Returns: \"alice@gmail.com:gmail:token\"\n *\n * // Store account metadata (alias, timestamps, profile)\n * const metadataKey = createAccountKey('metadata', {\n * accountId: 'alice@gmail.com',\n * service: 'gmail'\n * });\n * // Returns: \"alice@gmail.com:gmail:metadata\"\n *\n * // Store DCR client registration info\n * const dcrKey = createAccountKey('dcr-client', {\n * accountId: 'alice@outlook.com',\n * service: 'outlook'\n * });\n * // Returns: \"alice@outlook.com:outlook:dcr-client\"\n * ```\n */\nexport function createAccountKey(type: AccountKeyType, params: AccountKeyParams): string {\n validateKeyParams(params);\n return `${params.accountId}:${params.service}:${type}`;\n}\n\n/**\n * Create service-scoped storage key.\n *\n * These keys are scoped to a service (not a specific account) and store\n * service-level data like which account is active or the list of linked accounts.\n *\n * @param type - Key type ('active' for active account, 'linked' for account list)\n * @param params - Service key parameters\n * @returns Storage key in format: \"{service}:{type}\"\n *\n * @example\n * ```typescript\n * // Track active account\n * const activeKey = createServiceKey('active', {\n * service: 'gmail'\n * });\n * // Returns: \"gmail:active\"\n *\n * // Store list of linked accounts\n * const linkedKey = createServiceKey('linked', {\n * service: 'gmail'\n * });\n * // Returns: \"gmail:linked\"\n * ```\n */\nexport function createServiceKey(type: ServiceKeyType, params: ServiceKeyParams): string {\n validateKeyParams(params);\n return `${params.service}:${type}`;\n}\n\n/**\n * Parse token key to extract components\n *\n * @param key - Storage key to parse\n * @returns Object with accountId and service, or undefined if invalid format\n *\n * @example\n * const parsed = parseTokenKey('user@gmail.com:gmail:token');\n * // Returns: { accountId: 'user@gmail.com', service: 'gmail' }\n *\n * const invalid = parseTokenKey('invalid-key');\n * // Returns: undefined\n */\nexport function parseTokenKey(key: string): { accountId: string; service: string } | undefined {\n const parts = key.split(':');\n if (parts.length !== 3 || parts[2] !== 'token' || !parts[0] || !parts[1]) {\n return undefined;\n }\n return {\n accountId: parts[0],\n service: parts[1],\n };\n}\n\n/**\n * List all account IDs for a service\n *\n * Iterates token keys and returns all accountIds that match the service.\n * Encapsulates key format details for forward compatibility.\n *\n * @param store - Keyv store to iterate\n * @param service - Service name\n * @returns Array of account IDs (e.g., email addresses)\n *\n * @example\n * const accounts = await listAccountIds(store, 'gmail');\n * // Returns: ['alice@gmail.com', 'bob@gmail.com']\n *\n * @example\n * // Empty array if no accounts found\n * const empty = await listAccountIds(store, 'unknown-service');\n * // Returns: []\n */\nexport async function listAccountIds(store: Keyv, service: string): Promise<string[]> {\n const accountIds: string[] = [];\n\n try {\n const iterator = store.iterator?.(undefined);\n if (!iterator) {\n return accountIds;\n }\n\n for await (const [key] of iterator) {\n const parsed = parseTokenKey(key);\n if (parsed && parsed.service === service) {\n accountIds.push(parsed.accountId);\n }\n }\n } catch (_error) {\n // If iteration fails, return empty array (fail gracefully)\n // This handles stores that don't support iteration\n return accountIds;\n }\n\n return accountIds;\n}\n"],"names":["createAccountKey","createServiceKey","listAccountIds","parseTokenKey","validateKeyParams","params","Object","entries","key","value","Error","includes","type","accountId","service","parts","split","length","undefined","store","accountIds","iterator","parsed","_error","push"],"mappings":"AAAA;;;;;CAKC;;;;;;;;;;;QAmFeA;eAAAA;;QA8BAC;eAAAA;;QAgDMC;eAAAA;;QA9BNC;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAhGhB;;CAEC,GACD,SAASC,kBAAkBC,MAA2C;QAC/D,kCAAA,2BAAA;;QAAL,QAAK,YAAsBC,OAAOC,OAAO,CAACF,4BAArC,SAAA,6BAAA,QAAA,yBAAA,iCAA8C;YAA9C,mCAAA,iBAAOG,sBAAKC;YACf,IAAI,OAAOA,UAAU,UAAU;gBAC7B,MAAM,IAAIC,MAAM,AAAC,kBAAgD,OAA/BF,KAAI,6BAAwC,OAAb,OAAOC,sCAAP,SAAOA;YAC1E;YACA,IAAIA,MAAME,QAAQ,CAAC,MAAM;gBACvB,MAAM,IAAID,MAAM,AAAC,kBAAyDD,OAAxCD,KAAI,sCAA0C,OAANC;YAC5E;QACF;;QAPK;QAAA;;;iBAAA,6BAAA;gBAAA;;;gBAAA;sBAAA;;;;AAQP;AAoCO,SAAST,iBAAiBY,IAAoB,EAAEP,MAAwB;IAC7ED,kBAAkBC;IAClB,OAAO,AAAC,GAAsBA,OAApBA,OAAOQ,SAAS,EAAC,KAAqBD,OAAlBP,OAAOS,OAAO,EAAC,KAAQ,OAALF;AAClD;AA2BO,SAASX,iBAAiBW,IAAoB,EAAEP,MAAwB;IAC7ED,kBAAkBC;IAClB,OAAO,AAAC,GAAoBO,OAAlBP,OAAOS,OAAO,EAAC,KAAQ,OAALF;AAC9B;AAeO,SAAST,cAAcK,GAAW;IACvC,IAAMO,QAAQP,IAAIQ,KAAK,CAAC;IACxB,IAAID,MAAME,MAAM,KAAK,KAAKF,KAAK,CAAC,EAAE,KAAK,WAAW,CAACA,KAAK,CAAC,EAAE,IAAI,CAACA,KAAK,CAAC,EAAE,EAAE;QACxE,OAAOG;IACT;IACA,OAAO;QACLL,WAAWE,KAAK,CAAC,EAAE;QACnBD,SAASC,KAAK,CAAC,EAAE;IACnB;AACF;AAqBO,SAAeb,eAAeiB,KAAW,EAAEL,OAAe;;YACzDM,YAGaD,iBAAXE,2GAKYb,KACVc,aAKDC;;;;oBAdHH;;;;;;;;;oBAGEC,YAAWF,kBAAAA,MAAME,QAAQ,cAAdF,sCAAAA,qBAAAA,OAAiBD;oBAClC,IAAI,CAACG,UAAU;wBACb;;4BAAOD;;oBACT;;;;;;;;;;gDAE0BC;;;;;;;;;;;;;2DAARb;oBACVc,SAASnB,cAAcK;oBAC7B,IAAIc,UAAUA,OAAOR,OAAO,KAAKA,SAAS;wBACxCM,WAAWI,IAAI,CAACF,OAAOT,SAAS;oBAClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oBAEKU;oBACP,2DAA2D;oBAC3D,mDAAmD;oBACnD;;wBAAOH;;;oBAGT;;wBAAOA;;;;IACT"}