@anthropic-ai/sdk 0.91.1 → 0.93.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 (167) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/client.d.mts +73 -0
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +73 -0
  5. package/client.d.ts.map +1 -1
  6. package/client.js +269 -9
  7. package/client.js.map +1 -1
  8. package/client.mjs +269 -9
  9. package/client.mjs.map +1 -1
  10. package/core/credentials.d.mts +111 -0
  11. package/core/credentials.d.mts.map +1 -0
  12. package/core/credentials.d.ts +111 -0
  13. package/core/credentials.d.ts.map +1 -0
  14. package/core/credentials.js +298 -0
  15. package/core/credentials.js.map +1 -0
  16. package/core/credentials.mjs +259 -0
  17. package/core/credentials.mjs.map +1 -0
  18. package/internal/types.d.mts +12 -6
  19. package/internal/types.d.ts +12 -6
  20. package/internal/utils/time.d.mts +3 -0
  21. package/internal/utils/time.d.mts.map +1 -0
  22. package/internal/utils/time.d.ts +3 -0
  23. package/internal/utils/time.d.ts.map +1 -0
  24. package/internal/utils/time.js +8 -0
  25. package/internal/utils/time.js.map +1 -0
  26. package/internal/utils/time.mjs +5 -0
  27. package/internal/utils/time.mjs.map +1 -0
  28. package/lib/credentials/credential-chain.d.mts +39 -0
  29. package/lib/credentials/credential-chain.d.mts.map +1 -0
  30. package/lib/credentials/credential-chain.d.ts +39 -0
  31. package/lib/credentials/credential-chain.d.ts.map +1 -0
  32. package/lib/credentials/credential-chain.js +237 -0
  33. package/lib/credentials/credential-chain.js.map +1 -0
  34. package/lib/credentials/credential-chain.mjs +200 -0
  35. package/lib/credentials/credential-chain.mjs.map +1 -0
  36. package/lib/credentials/identity-token.d.mts +11 -0
  37. package/lib/credentials/identity-token.d.mts.map +1 -0
  38. package/lib/credentials/identity-token.d.ts +11 -0
  39. package/lib/credentials/identity-token.d.ts.map +1 -0
  40. package/lib/credentials/identity-token.js +72 -0
  41. package/lib/credentials/identity-token.js.map +1 -0
  42. package/lib/credentials/identity-token.mjs +35 -0
  43. package/lib/credentials/identity-token.mjs.map +1 -0
  44. package/lib/credentials/oidc-federation.d.mts +28 -0
  45. package/lib/credentials/oidc-federation.d.mts.map +1 -0
  46. package/lib/credentials/oidc-federation.d.ts +28 -0
  47. package/lib/credentials/oidc-federation.d.ts.map +1 -0
  48. package/lib/credentials/oidc-federation.js +69 -0
  49. package/lib/credentials/oidc-federation.js.map +1 -0
  50. package/lib/credentials/oidc-federation.mjs +66 -0
  51. package/lib/credentials/oidc-federation.mjs.map +1 -0
  52. package/lib/credentials/token-cache.d.mts +54 -0
  53. package/lib/credentials/token-cache.d.mts.map +1 -0
  54. package/lib/credentials/token-cache.d.ts +54 -0
  55. package/lib/credentials/token-cache.d.ts.map +1 -0
  56. package/lib/credentials/token-cache.js +112 -0
  57. package/lib/credentials/token-cache.js.map +1 -0
  58. package/lib/credentials/token-cache.mjs +108 -0
  59. package/lib/credentials/token-cache.mjs.map +1 -0
  60. package/lib/credentials/types.d.mts +96 -0
  61. package/lib/credentials/types.d.mts.map +1 -0
  62. package/lib/credentials/types.d.ts +96 -0
  63. package/lib/credentials/types.d.ts.map +1 -0
  64. package/lib/credentials/types.js +266 -0
  65. package/lib/credentials/types.js.map +1 -0
  66. package/lib/credentials/types.mjs +224 -0
  67. package/lib/credentials/types.mjs.map +1 -0
  68. package/lib/credentials/user-oauth.d.mts +21 -0
  69. package/lib/credentials/user-oauth.d.mts.map +1 -0
  70. package/lib/credentials/user-oauth.d.ts +21 -0
  71. package/lib/credentials/user-oauth.d.ts.map +1 -0
  72. package/lib/credentials/user-oauth.js +130 -0
  73. package/lib/credentials/user-oauth.js.map +1 -0
  74. package/lib/credentials/user-oauth.mjs +94 -0
  75. package/lib/credentials/user-oauth.mjs.map +1 -0
  76. package/lib/credentials.d.mts +4 -0
  77. package/lib/credentials.d.mts.map +1 -0
  78. package/lib/credentials.d.ts +4 -0
  79. package/lib/credentials.d.ts.map +1 -0
  80. package/lib/credentials.js +8 -0
  81. package/lib/credentials.js.map +1 -0
  82. package/lib/credentials.mjs +3 -0
  83. package/lib/credentials.mjs.map +1 -0
  84. package/package.json +1 -1
  85. package/resources/beta/environments.d.mts +1 -1
  86. package/resources/beta/environments.d.ts +1 -1
  87. package/resources/beta/index.d.mts +1 -1
  88. package/resources/beta/index.d.mts.map +1 -1
  89. package/resources/beta/index.d.ts +1 -1
  90. package/resources/beta/index.d.ts.map +1 -1
  91. package/resources/beta/index.js.map +1 -1
  92. package/resources/beta/index.mjs.map +1 -1
  93. package/resources/beta/memory-stores/memories.d.mts +126 -11
  94. package/resources/beta/memory-stores/memories.d.mts.map +1 -1
  95. package/resources/beta/memory-stores/memories.d.ts +126 -11
  96. package/resources/beta/memory-stores/memories.d.ts.map +1 -1
  97. package/resources/beta/memory-stores/memories.js +5 -5
  98. package/resources/beta/memory-stores/memories.mjs +5 -5
  99. package/resources/beta/memory-stores/memory-stores.d.mts +58 -14
  100. package/resources/beta/memory-stores/memory-stores.d.mts.map +1 -1
  101. package/resources/beta/memory-stores/memory-stores.d.ts +58 -14
  102. package/resources/beta/memory-stores/memory-stores.d.ts.map +1 -1
  103. package/resources/beta/memory-stores/memory-stores.js +6 -6
  104. package/resources/beta/memory-stores/memory-stores.js.map +1 -1
  105. package/resources/beta/memory-stores/memory-stores.mjs +6 -6
  106. package/resources/beta/memory-stores/memory-stores.mjs.map +1 -1
  107. package/resources/beta/memory-stores/memory-versions.d.mts +88 -5
  108. package/resources/beta/memory-stores/memory-versions.d.mts.map +1 -1
  109. package/resources/beta/memory-stores/memory-versions.d.ts +88 -5
  110. package/resources/beta/memory-stores/memory-versions.d.ts.map +1 -1
  111. package/resources/beta/memory-stores/memory-versions.js +3 -3
  112. package/resources/beta/memory-stores/memory-versions.mjs +3 -3
  113. package/resources/beta/messages/batches.d.mts +4 -0
  114. package/resources/beta/messages/batches.d.mts.map +1 -1
  115. package/resources/beta/messages/batches.d.ts +4 -0
  116. package/resources/beta/messages/batches.d.ts.map +1 -1
  117. package/resources/beta/messages/index.d.mts +1 -1
  118. package/resources/beta/messages/index.d.mts.map +1 -1
  119. package/resources/beta/messages/index.d.ts +1 -1
  120. package/resources/beta/messages/index.d.ts.map +1 -1
  121. package/resources/beta/messages/index.js.map +1 -1
  122. package/resources/beta/messages/index.mjs.map +1 -1
  123. package/resources/beta/messages/messages.d.mts +5 -1
  124. package/resources/beta/messages/messages.d.mts.map +1 -1
  125. package/resources/beta/messages/messages.d.ts +5 -1
  126. package/resources/beta/messages/messages.d.ts.map +1 -1
  127. package/resources/beta/messages/messages.js +1 -1
  128. package/resources/beta/messages/messages.js.map +1 -1
  129. package/resources/beta/messages/messages.mjs +1 -1
  130. package/resources/beta/messages/messages.mjs.map +1 -1
  131. package/resources/beta/sessions/sessions.d.mts +5 -0
  132. package/resources/beta/sessions/sessions.d.mts.map +1 -1
  133. package/resources/beta/sessions/sessions.d.ts +5 -0
  134. package/resources/beta/sessions/sessions.d.ts.map +1 -1
  135. package/resources/beta/sessions/sessions.js.map +1 -1
  136. package/resources/beta/sessions/sessions.mjs.map +1 -1
  137. package/resources/messages/messages.d.mts +4 -0
  138. package/resources/messages/messages.d.mts.map +1 -1
  139. package/resources/messages/messages.d.ts +4 -0
  140. package/resources/messages/messages.d.ts.map +1 -1
  141. package/resources/messages/messages.js.map +1 -1
  142. package/resources/messages/messages.mjs.map +1 -1
  143. package/src/client.ts +354 -14
  144. package/src/core/credentials.ts +349 -0
  145. package/src/internal/utils/time.ts +4 -0
  146. package/src/lib/credentials/credential-chain.ts +284 -0
  147. package/src/lib/credentials/identity-token.ts +37 -0
  148. package/src/lib/credentials/oidc-federation.ts +112 -0
  149. package/src/lib/credentials/token-cache.ts +130 -0
  150. package/src/lib/credentials/types.ts +295 -0
  151. package/src/lib/credentials/user-oauth.ts +144 -0
  152. package/src/lib/credentials.ts +3 -0
  153. package/src/resources/beta/environments.ts +1 -1
  154. package/src/resources/beta/index.ts +1 -0
  155. package/src/resources/beta/memory-stores/memories.ts +126 -11
  156. package/src/resources/beta/memory-stores/memory-stores.ts +58 -14
  157. package/src/resources/beta/memory-stores/memory-versions.ts +88 -5
  158. package/src/resources/beta/messages/batches.ts +4 -0
  159. package/src/resources/beta/messages/index.ts +1 -0
  160. package/src/resources/beta/messages/messages.ts +5 -1
  161. package/src/resources/beta/sessions/sessions.ts +6 -0
  162. package/src/resources/messages/messages.ts +4 -0
  163. package/src/version.ts +1 -1
  164. package/version.d.mts +1 -1
  165. package/version.d.ts +1 -1
  166. package/version.js +1 -1
  167. package/version.mjs +1 -1
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.resolveCredentialsFromConfig = resolveCredentialsFromConfig;
37
+ exports.defaultCredentials = defaultCredentials;
38
+ const env_1 = require("../../internal/utils/env.js");
39
+ const credentials_1 = require("../../core/credentials.js");
40
+ const types_1 = require("./types.js");
41
+ const time_1 = require("../../internal/utils/time.js");
42
+ const identity_token_1 = require("./identity-token.js");
43
+ const oidc_federation_1 = require("./oidc-federation.js");
44
+ const user_oauth_1 = require("./user-oauth.js");
45
+ function resolveCredentialsFromConfig(config, options) {
46
+ const credentialsPath = config.authentication.credentials_path ?? null;
47
+ const effectiveBaseURL = (config.base_url || options.baseURL).replace(/\/+$/, '');
48
+ const provider = buildProvider(config, credentialsPath, effectiveBaseURL, options);
49
+ const extraHeaders = {};
50
+ // Workspace scoping for oidc_federation is server-side (the federation rule
51
+ // encodes the workspace and the minted token is workspace-scoped), so the
52
+ // header is only meaningful for user_oauth.
53
+ if (config.workspace_id && config.authentication.type === 'user_oauth') {
54
+ extraHeaders['anthropic-workspace-id'] = config.workspace_id;
55
+ }
56
+ // Surface the profile's own base_url (not the options.baseURL fallback) so
57
+ // the client can adopt it for outbound API requests when the caller didn't
58
+ // pin one explicitly. Echoing options.baseURL back would defeat precedence.
59
+ return { provider, extraHeaders, baseURL: config.base_url || undefined };
60
+ }
61
+ /**
62
+ * Resolves a {@link CredentialResult} from the environment. Returns `null`
63
+ * when no credentials can be resolved.
64
+ *
65
+ * Resolution order:
66
+ *
67
+ * 1. Config file for the active profile (or the explicit `profile` argument)
68
+ * → dispatch on `authentication.type` (`oidc_federation`, `user_oauth`)
69
+ * 2. Environment variables `ANTHROPIC_FEDERATION_RULE_ID` +
70
+ * `ANTHROPIC_ORGANIZATION_ID` (+ identity token) → OIDC federation
71
+ * 3. Nothing matches → `null`
72
+ *
73
+ * Passing `profile` selects `<config_dir>/configs/<profile>.json` directly,
74
+ * skipping `ANTHROPIC_PROFILE` / `active_config` resolution.
75
+ */
76
+ async function defaultCredentials(options, profile) {
77
+ const config = await (0, credentials_1.loadConfig)(profile);
78
+ if (!config) {
79
+ return null;
80
+ }
81
+ // For env/file-loaded configs, default credentials_path to the
82
+ // per-profile location so user_oauth and federation caching work.
83
+ // Shallow-clone first so callers that retain a reference to the loaded
84
+ // config don't observe the patched-in default.
85
+ const withPath = config.authentication.credentials_path ?
86
+ config
87
+ : {
88
+ ...config,
89
+ authentication: {
90
+ ...config.authentication,
91
+ credentials_path: (await (0, credentials_1.getCredentialsPath)(config, profile)) ?? undefined,
92
+ },
93
+ };
94
+ return resolveCredentialsFromConfig(withPath, options);
95
+ }
96
+ function buildProvider(config, credentialsPath, baseURL, options) {
97
+ switch (config.authentication.type) {
98
+ case 'oidc_federation': {
99
+ const auth = config.authentication;
100
+ const identityProvider = resolveIdentityTokenProvider(auth);
101
+ if (!identityProvider) {
102
+ throw new types_1.WorkloadIdentityError('oidc_federation config requires an identity token (set authentication.identity_token, ' +
103
+ 'ANTHROPIC_IDENTITY_TOKEN_FILE, or ANTHROPIC_IDENTITY_TOKEN)');
104
+ }
105
+ if (!auth.federation_rule_id) {
106
+ throw new types_1.WorkloadIdentityError("oidc_federation config requires 'federation_rule_id'. Set it in authentication.federation_rule_id in your profile, or via ANTHROPIC_FEDERATION_RULE_ID (profile takes precedence).");
107
+ }
108
+ if (!config.organization_id) {
109
+ throw new types_1.WorkloadIdentityError('oidc_federation config requires organization_id (set ANTHROPIC_ORGANIZATION_ID or config.organization_id)');
110
+ }
111
+ const exchange = (0, oidc_federation_1.oidcFederationProvider)({
112
+ identityTokenProvider: identityProvider,
113
+ federationRuleId: auth.federation_rule_id,
114
+ organizationId: config.organization_id,
115
+ serviceAccountId: auth.service_account_id,
116
+ baseURL,
117
+ fetch: options.fetch,
118
+ userAgent: options.userAgent,
119
+ });
120
+ // If there's a credentials file path, wrap the exchange with file caching
121
+ // (check file for fresh token before exchanging, write back after).
122
+ if (credentialsPath) {
123
+ return cachedExchangeProvider(exchange, credentialsPath, options.onCacheWriteError, options.onSafetyWarning);
124
+ }
125
+ return exchange;
126
+ }
127
+ case 'user_oauth': {
128
+ if (!credentialsPath) {
129
+ throw new types_1.WorkloadIdentityError('user_oauth config requires authentication.credentials_path ' +
130
+ '(or load via a profile so it defaults to <config_dir>/credentials/<profile>.json)');
131
+ }
132
+ return (0, user_oauth_1.userOAuthProvider)({
133
+ credentialsPath,
134
+ clientId: config.authentication.client_id,
135
+ baseURL,
136
+ fetch: options.fetch,
137
+ userAgent: options.userAgent,
138
+ onSafetyWarning: options.onSafetyWarning,
139
+ });
140
+ }
141
+ default: {
142
+ const t = config.authentication.type;
143
+ throw new types_1.WorkloadIdentityError(`authentication.type "${t}" is not a known authentication type`);
144
+ }
145
+ }
146
+ }
147
+ /**
148
+ * Resolves the identity token provider from config fields or environment variables.
149
+ *
150
+ * Resolution order:
151
+ * 1. `identity_token.path` from the config (source: "file")
152
+ * 2. `ANTHROPIC_IDENTITY_TOKEN_FILE` env var
153
+ * 3. `ANTHROPIC_IDENTITY_TOKEN` env var (static value)
154
+ */
155
+ function resolveIdentityTokenProvider(auth) {
156
+ if (auth.identity_token) {
157
+ // Cast needed to stringify an unknown source value for the error message:
158
+ // the on-disk JSON may contain a source this SDK version doesn't know about.
159
+ const source = auth.identity_token.source;
160
+ if (source !== 'file') {
161
+ throw new types_1.WorkloadIdentityError(`identity_token.source "${source}" is not supported by this SDK version (only "file")`);
162
+ }
163
+ if (!auth.identity_token.path) {
164
+ throw new types_1.WorkloadIdentityError(`identity_token.source "file" requires a non-empty path`);
165
+ }
166
+ return (0, identity_token_1.identityTokenFromFile)(auth.identity_token.path);
167
+ }
168
+ const tokenFile = (0, env_1.readEnv)('ANTHROPIC_IDENTITY_TOKEN_FILE');
169
+ if (tokenFile) {
170
+ return (0, identity_token_1.identityTokenFromFile)(tokenFile);
171
+ }
172
+ const tokenValue = (0, env_1.readEnv)('ANTHROPIC_IDENTITY_TOKEN');
173
+ if (tokenValue) {
174
+ return (0, identity_token_1.identityTokenFromValue)(tokenValue);
175
+ }
176
+ return null;
177
+ }
178
+ /**
179
+ * Wraps a federation exchange provider with credential file caching.
180
+ * Checks the file for a fresh token before exchanging, and writes the
181
+ * result back after a successful exchange (best-effort, atomic replace).
182
+ *
183
+ * Note: this is not cross-process serialized — two SDK instances that
184
+ * miss the cache simultaneously will both perform a full exchange and
185
+ * the last writer wins. That is acceptable: federation exchanges are
186
+ * idempotent and the cache is an optimization, not a correctness gate.
187
+ */
188
+ function cachedExchangeProvider(exchange, credentialsPath, onCacheWriteError, onSafetyWarning) {
189
+ return async (opts) => {
190
+ const fs = await Promise.resolve().then(() => __importStar(require('node:fs')));
191
+ await (0, types_1.checkCredentialsFileSafety)(credentialsPath, onSafetyWarning);
192
+ // Try cached credentials file
193
+ let existing;
194
+ try {
195
+ const raw = await fs.promises.readFile(credentialsPath, 'utf-8');
196
+ existing = JSON.parse(raw);
197
+ const token = existing?.['access_token'];
198
+ if (token && !opts?.forceRefresh) {
199
+ const expiresAt = existing?.['expires_at'];
200
+ if (expiresAt == null || (0, time_1.nowAsSeconds)() < expiresAt - types_1.MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
201
+ return { token, expiresAt: expiresAt ?? null };
202
+ }
203
+ }
204
+ }
205
+ catch (err) {
206
+ // ENOENT or invalid-JSON → no usable cache, exchange fresh. Other
207
+ // errors (EACCES, EISDIR, …) indicate a broken cache path; surface to
208
+ // the optional hook so they're at least debuggable, then proceed.
209
+ const code = err?.code;
210
+ if (code !== 'ENOENT' && !(err instanceof SyntaxError)) {
211
+ onCacheWriteError?.(err);
212
+ }
213
+ }
214
+ // Exchange for a new token
215
+ const result = await exchange(opts);
216
+ // Write cache back (best-effort). Preserve any unknown keys from the
217
+ // existing file (notably refresh_token, in the unlikely case this path
218
+ // is shared with a user_oauth profile) so the federation cache writer
219
+ // doesn't clobber material it didn't own.
220
+ try {
221
+ await (0, types_1.writeCredentialsFileAtomic)(credentialsPath, {
222
+ ...(existing ?? {}),
223
+ version: credentials_1.CREDENTIALS_FILE_VERSION,
224
+ type: 'oauth_token',
225
+ access_token: result.token,
226
+ expires_at: result.expiresAt,
227
+ });
228
+ }
229
+ catch (err) {
230
+ // Best-effort caching: surface to the optional hook but never fail
231
+ // the exchange itself.
232
+ onCacheWriteError?.(err);
233
+ }
234
+ return result;
235
+ };
236
+ }
237
+ //# sourceMappingURL=credential-chain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credential-chain.js","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,oEAqBC;AAiBD,gDAyBC;AArGD,qDAAmD;AACnD,2DAKgC;AAEhC,sCAKiB;AACjB,uDAAyD;AACzD,wDAAiF;AACjF,0DAA2D;AAC3D,gDAAiD;AAqBjD,SAAgB,4BAA4B,CAC1C,MAAuB,EACvB,OAAwB;IAExB,MAAM,eAAe,GAAG,MAAM,CAAC,cAAc,CAAC,gBAAgB,IAAI,IAAI,CAAC;IACvE,MAAM,gBAAgB,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAElF,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAEnF,MAAM,YAAY,GAA2B,EAAE,CAAC;IAChD,4EAA4E;IAC5E,0EAA0E;IAC1E,4CAA4C;IAC5C,IAAI,MAAM,CAAC,YAAY,IAAI,MAAM,CAAC,cAAc,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACvE,YAAY,CAAC,wBAAwB,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC;IAC/D,CAAC;IAED,2EAA2E;IAC3E,2EAA2E;IAC3E,4EAA4E;IAC5E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC,QAAQ,IAAI,S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M,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,200 @@
1
+ import { readEnv } from "../../internal/utils/env.mjs";
2
+ import { CREDENTIALS_FILE_VERSION, loadConfig, getCredentialsPath, } from "../../core/credentials.mjs";
3
+ import { MANDATORY_REFRESH_THRESHOLD_IN_SECONDS, WorkloadIdentityError, checkCredentialsFileSafety, writeCredentialsFileAtomic, } from "./types.mjs";
4
+ import { nowAsSeconds } from "../../internal/utils/time.mjs";
5
+ import { identityTokenFromFile, identityTokenFromValue } from "./identity-token.mjs";
6
+ import { oidcFederationProvider } from "./oidc-federation.mjs";
7
+ import { userOAuthProvider } from "./user-oauth.mjs";
8
+ export function resolveCredentialsFromConfig(config, options) {
9
+ const credentialsPath = config.authentication.credentials_path ?? null;
10
+ const effectiveBaseURL = (config.base_url || options.baseURL).replace(/\/+$/, '');
11
+ const provider = buildProvider(config, credentialsPath, effectiveBaseURL, options);
12
+ const extraHeaders = {};
13
+ // Workspace scoping for oidc_federation is server-side (the federation rule
14
+ // encodes the workspace and the minted token is workspace-scoped), so the
15
+ // header is only meaningful for user_oauth.
16
+ if (config.workspace_id && config.authentication.type === 'user_oauth') {
17
+ extraHeaders['anthropic-workspace-id'] = config.workspace_id;
18
+ }
19
+ // Surface the profile's own base_url (not the options.baseURL fallback) so
20
+ // the client can adopt it for outbound API requests when the caller didn't
21
+ // pin one explicitly. Echoing options.baseURL back would defeat precedence.
22
+ return { provider, extraHeaders, baseURL: config.base_url || undefined };
23
+ }
24
+ /**
25
+ * Resolves a {@link CredentialResult} from the environment. Returns `null`
26
+ * when no credentials can be resolved.
27
+ *
28
+ * Resolution order:
29
+ *
30
+ * 1. Config file for the active profile (or the explicit `profile` argument)
31
+ * → dispatch on `authentication.type` (`oidc_federation`, `user_oauth`)
32
+ * 2. Environment variables `ANTHROPIC_FEDERATION_RULE_ID` +
33
+ * `ANTHROPIC_ORGANIZATION_ID` (+ identity token) → OIDC federation
34
+ * 3. Nothing matches → `null`
35
+ *
36
+ * Passing `profile` selects `<config_dir>/configs/<profile>.json` directly,
37
+ * skipping `ANTHROPIC_PROFILE` / `active_config` resolution.
38
+ */
39
+ export async function defaultCredentials(options, profile) {
40
+ const config = await loadConfig(profile);
41
+ if (!config) {
42
+ return null;
43
+ }
44
+ // For env/file-loaded configs, default credentials_path to the
45
+ // per-profile location so user_oauth and federation caching work.
46
+ // Shallow-clone first so callers that retain a reference to the loaded
47
+ // config don't observe the patched-in default.
48
+ const withPath = config.authentication.credentials_path ?
49
+ config
50
+ : {
51
+ ...config,
52
+ authentication: {
53
+ ...config.authentication,
54
+ credentials_path: (await getCredentialsPath(config, profile)) ?? undefined,
55
+ },
56
+ };
57
+ return resolveCredentialsFromConfig(withPath, options);
58
+ }
59
+ function buildProvider(config, credentialsPath, baseURL, options) {
60
+ switch (config.authentication.type) {
61
+ case 'oidc_federation': {
62
+ const auth = config.authentication;
63
+ const identityProvider = resolveIdentityTokenProvider(auth);
64
+ if (!identityProvider) {
65
+ throw new WorkloadIdentityError('oidc_federation config requires an identity token (set authentication.identity_token, ' +
66
+ 'ANTHROPIC_IDENTITY_TOKEN_FILE, or ANTHROPIC_IDENTITY_TOKEN)');
67
+ }
68
+ if (!auth.federation_rule_id) {
69
+ throw new WorkloadIdentityError("oidc_federation config requires 'federation_rule_id'. Set it in authentication.federation_rule_id in your profile, or via ANTHROPIC_FEDERATION_RULE_ID (profile takes precedence).");
70
+ }
71
+ if (!config.organization_id) {
72
+ throw new WorkloadIdentityError('oidc_federation config requires organization_id (set ANTHROPIC_ORGANIZATION_ID or config.organization_id)');
73
+ }
74
+ const exchange = oidcFederationProvider({
75
+ identityTokenProvider: identityProvider,
76
+ federationRuleId: auth.federation_rule_id,
77
+ organizationId: config.organization_id,
78
+ serviceAccountId: auth.service_account_id,
79
+ baseURL,
80
+ fetch: options.fetch,
81
+ userAgent: options.userAgent,
82
+ });
83
+ // If there's a credentials file path, wrap the exchange with file caching
84
+ // (check file for fresh token before exchanging, write back after).
85
+ if (credentialsPath) {
86
+ return cachedExchangeProvider(exchange, credentialsPath, options.onCacheWriteError, options.onSafetyWarning);
87
+ }
88
+ return exchange;
89
+ }
90
+ case 'user_oauth': {
91
+ if (!credentialsPath) {
92
+ throw new WorkloadIdentityError('user_oauth config requires authentication.credentials_path ' +
93
+ '(or load via a profile so it defaults to <config_dir>/credentials/<profile>.json)');
94
+ }
95
+ return userOAuthProvider({
96
+ credentialsPath,
97
+ clientId: config.authentication.client_id,
98
+ baseURL,
99
+ fetch: options.fetch,
100
+ userAgent: options.userAgent,
101
+ onSafetyWarning: options.onSafetyWarning,
102
+ });
103
+ }
104
+ default: {
105
+ const t = config.authentication.type;
106
+ throw new WorkloadIdentityError(`authentication.type "${t}" is not a known authentication type`);
107
+ }
108
+ }
109
+ }
110
+ /**
111
+ * Resolves the identity token provider from config fields or environment variables.
112
+ *
113
+ * Resolution order:
114
+ * 1. `identity_token.path` from the config (source: "file")
115
+ * 2. `ANTHROPIC_IDENTITY_TOKEN_FILE` env var
116
+ * 3. `ANTHROPIC_IDENTITY_TOKEN` env var (static value)
117
+ */
118
+ function resolveIdentityTokenProvider(auth) {
119
+ if (auth.identity_token) {
120
+ // Cast needed to stringify an unknown source value for the error message:
121
+ // the on-disk JSON may contain a source this SDK version doesn't know about.
122
+ const source = auth.identity_token.source;
123
+ if (source !== 'file') {
124
+ throw new WorkloadIdentityError(`identity_token.source "${source}" is not supported by this SDK version (only "file")`);
125
+ }
126
+ if (!auth.identity_token.path) {
127
+ throw new WorkloadIdentityError(`identity_token.source "file" requires a non-empty path`);
128
+ }
129
+ return identityTokenFromFile(auth.identity_token.path);
130
+ }
131
+ const tokenFile = readEnv('ANTHROPIC_IDENTITY_TOKEN_FILE');
132
+ if (tokenFile) {
133
+ return identityTokenFromFile(tokenFile);
134
+ }
135
+ const tokenValue = readEnv('ANTHROPIC_IDENTITY_TOKEN');
136
+ if (tokenValue) {
137
+ return identityTokenFromValue(tokenValue);
138
+ }
139
+ return null;
140
+ }
141
+ /**
142
+ * Wraps a federation exchange provider with credential file caching.
143
+ * Checks the file for a fresh token before exchanging, and writes the
144
+ * result back after a successful exchange (best-effort, atomic replace).
145
+ *
146
+ * Note: this is not cross-process serialized — two SDK instances that
147
+ * miss the cache simultaneously will both perform a full exchange and
148
+ * the last writer wins. That is acceptable: federation exchanges are
149
+ * idempotent and the cache is an optimization, not a correctness gate.
150
+ */
151
+ function cachedExchangeProvider(exchange, credentialsPath, onCacheWriteError, onSafetyWarning) {
152
+ return async (opts) => {
153
+ const fs = await import('node:fs');
154
+ await checkCredentialsFileSafety(credentialsPath, onSafetyWarning);
155
+ // Try cached credentials file
156
+ let existing;
157
+ try {
158
+ const raw = await fs.promises.readFile(credentialsPath, 'utf-8');
159
+ existing = JSON.parse(raw);
160
+ const token = existing?.['access_token'];
161
+ if (token && !opts?.forceRefresh) {
162
+ const expiresAt = existing?.['expires_at'];
163
+ if (expiresAt == null || nowAsSeconds() < expiresAt - MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
164
+ return { token, expiresAt: expiresAt ?? null };
165
+ }
166
+ }
167
+ }
168
+ catch (err) {
169
+ // ENOENT or invalid-JSON → no usable cache, exchange fresh. Other
170
+ // errors (EACCES, EISDIR, …) indicate a broken cache path; surface to
171
+ // the optional hook so they're at least debuggable, then proceed.
172
+ const code = err?.code;
173
+ if (code !== 'ENOENT' && !(err instanceof SyntaxError)) {
174
+ onCacheWriteError?.(err);
175
+ }
176
+ }
177
+ // Exchange for a new token
178
+ const result = await exchange(opts);
179
+ // Write cache back (best-effort). Preserve any unknown keys from the
180
+ // existing file (notably refresh_token, in the unlikely case this path
181
+ // is shared with a user_oauth profile) so the federation cache writer
182
+ // doesn't clobber material it didn't own.
183
+ try {
184
+ await writeCredentialsFileAtomic(credentialsPath, {
185
+ ...(existing ?? {}),
186
+ version: CREDENTIALS_FILE_VERSION,
187
+ type: 'oauth_token',
188
+ access_token: result.token,
189
+ expires_at: result.expiresAt,
190
+ });
191
+ }
192
+ catch (err) {
193
+ // Best-effort caching: surface to the optional hook but never fail
194
+ // the exchange itself.
195
+ onCacheWriteError?.(err);
196
+ }
197
+ return result;
198
+ };
199
+ }
200
+ //# sourceMappingURL=credential-chain.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credential-chain.mjs","sourceRoot":"","sources":["../../src/lib/credentials/credential-chain.ts"],"names":[],"mappings":"OACO,EAAE,OAAO,EAAE;OACX,EACL,wBAAwB,EACxB,UAAU,EACV,kBAAkB,GAEnB;OAEM,EACL,sCAAsC,EACtC,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,GAC3B;OACM,EAAE,YAAY,EAAE;OAChB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE;OACjD,EAAE,sBAAsB,EAAE;OAC1B,EAAE,iBAAiB,EAAE;AAqB5B,MAAM,UAAU,4BAA4B,CAC1C,MAAuB,EACvB,OAAwB;IAExB,MAAM,eAAe,GAAG,MAAM,CAAC,cAAc,CAAC,gBAAgB,IAAI,IAAI,CAAC;IACvE,MAAM,gBAAgB,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAElF,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,EAAE,eAAe,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAEnF,MAAM,YAAY,GAA2B,EAAE,CAAC;IAChD,4EAA4E;IAC5E,0EAA0E;IAC1E,4CAA4C;IAC5C,IAAI,MAAM,CAAC,YAAY,IAAI,MAAM,CAAC,cAAc,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACvE,YAAY,CAAC,wBAAwB,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC;IAC/D,CAAC;IAED,2EAA2E;IAC3E,2EAA2E;IAC3E,4EAA4E;IAC5E,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC,QAAQ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 -0,0 +1,11 @@
1
+ import type { IdentityTokenProvider } from "./types.mjs";
2
+ /**
3
+ * Reads a JWT from a file on every call. Supports automatic rotation
4
+ * (e.g. Kubernetes projected service-account tokens).
5
+ */
6
+ export declare function identityTokenFromFile(path: string): IdentityTokenProvider;
7
+ /**
8
+ * Wraps a static JWT string as an {@link IdentityTokenProvider}.
9
+ */
10
+ export declare function identityTokenFromValue(token: string): IdentityTokenProvider;
11
+ //# sourceMappingURL=identity-token.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity-token.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/identity-token.ts"],"names":[],"mappings":"OACO,KAAK,EAAE,qBAAqB,EAAE;AAErC;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,qBAAqB,CAmBzE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAK3E"}
@@ -0,0 +1,11 @@
1
+ import type { IdentityTokenProvider } from "./types.js";
2
+ /**
3
+ * Reads a JWT from a file on every call. Supports automatic rotation
4
+ * (e.g. Kubernetes projected service-account tokens).
5
+ */
6
+ export declare function identityTokenFromFile(path: string): IdentityTokenProvider;
7
+ /**
8
+ * Wraps a static JWT string as an {@link IdentityTokenProvider}.
9
+ */
10
+ export declare function identityTokenFromValue(token: string): IdentityTokenProvider;
11
+ //# sourceMappingURL=identity-token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity-token.d.ts","sourceRoot":"","sources":["../../src/lib/credentials/identity-token.ts"],"names":[],"mappings":"OACO,KAAK,EAAE,qBAAqB,EAAE;AAErC;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,qBAAqB,CAmBzE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAK3E"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.identityTokenFromFile = identityTokenFromFile;
37
+ exports.identityTokenFromValue = identityTokenFromValue;
38
+ const error_1 = require("../../core/error.js");
39
+ /**
40
+ * Reads a JWT from a file on every call. Supports automatic rotation
41
+ * (e.g. Kubernetes projected service-account tokens).
42
+ */
43
+ function identityTokenFromFile(path) {
44
+ if (!path) {
45
+ throw new error_1.AnthropicError('Identity token file path is empty');
46
+ }
47
+ return async () => {
48
+ const fs = await Promise.resolve().then(() => __importStar(require('node:fs')));
49
+ let content;
50
+ try {
51
+ content = await fs.promises.readFile(path, 'utf-8');
52
+ }
53
+ catch (err) {
54
+ throw new error_1.AnthropicError(`Failed to read identity token file at ${path}: ${err}`);
55
+ }
56
+ const token = content.trim();
57
+ if (!token) {
58
+ throw new error_1.AnthropicError(`Identity token file at ${path} is empty`);
59
+ }
60
+ return token;
61
+ };
62
+ }
63
+ /**
64
+ * Wraps a static JWT string as an {@link IdentityTokenProvider}.
65
+ */
66
+ function identityTokenFromValue(token) {
67
+ if (!token) {
68
+ throw new error_1.AnthropicError('Identity token value is empty');
69
+ }
70
+ return () => token;
71
+ }
72
+ //# sourceMappingURL=identity-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity-token.js","sourceRoot":"","sources":["../../src/lib/credentials/identity-token.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,sDAmBC;AAKD,wDAKC;AApCD,+CAAkD;AAGlD;;;GAGG;AACH,SAAgB,qBAAqB,CAAC,IAAY;IAChD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,sBAAc,CAAC,mCAAmC,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,KAAK,IAAI,EAAE;QAChB,MAAM,EAAE,GAAG,wDAAa,SAAS,GAAC,CAAC;QACnC,IAAI,OAAe,CAAC;QACpB,IAAI,CAAC;YACH,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,sBAAc,CAAC,yCAAyC,IAAI,KAAK,GAAG,EAAE,CAAC,CAAC;QACpF,CAAC;QACD,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,sBAAc,CAAC,0BAA0B,IAAI,WAAW,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,sBAAsB,CAAC,KAAa;IAClD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,sBAAc,CAAC,+BAA+B,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACrB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { AnthropicError } from "../../core/error.mjs";
2
+ /**
3
+ * Reads a JWT from a file on every call. Supports automatic rotation
4
+ * (e.g. Kubernetes projected service-account tokens).
5
+ */
6
+ export function identityTokenFromFile(path) {
7
+ if (!path) {
8
+ throw new AnthropicError('Identity token file path is empty');
9
+ }
10
+ return async () => {
11
+ const fs = await import('node:fs');
12
+ let content;
13
+ try {
14
+ content = await fs.promises.readFile(path, 'utf-8');
15
+ }
16
+ catch (err) {
17
+ throw new AnthropicError(`Failed to read identity token file at ${path}: ${err}`);
18
+ }
19
+ const token = content.trim();
20
+ if (!token) {
21
+ throw new AnthropicError(`Identity token file at ${path} is empty`);
22
+ }
23
+ return token;
24
+ };
25
+ }
26
+ /**
27
+ * Wraps a static JWT string as an {@link IdentityTokenProvider}.
28
+ */
29
+ export function identityTokenFromValue(token) {
30
+ if (!token) {
31
+ throw new AnthropicError('Identity token value is empty');
32
+ }
33
+ return () => token;
34
+ }
35
+ //# sourceMappingURL=identity-token.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity-token.mjs","sourceRoot":"","sources":["../../src/lib/credentials/identity-token.ts"],"names":[],"mappings":"OAAO,EAAE,cAAc,EAAE;AAGzB;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,cAAc,CAAC,mCAAmC,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,KAAK,IAAI,EAAE;QAChB,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,CAAC;QACnC,IAAI,OAAe,CAAC;QACpB,IAAI,CAAC;YACH,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,cAAc,CAAC,yCAAyC,IAAI,KAAK,GAAG,EAAE,CAAC,CAAC;QACpF,CAAC;QACD,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,cAAc,CAAC,0BAA0B,IAAI,WAAW,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAa;IAClD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,cAAc,CAAC,+BAA+B,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACrB,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { Fetch } from "../../internal/builtin-types.mjs";
2
+ import type { AccessTokenProvider, IdentityTokenProvider } from "./types.mjs";
3
+ export type OIDCFederationConfig = {
4
+ identityTokenProvider: IdentityTokenProvider;
5
+ federationRuleId: string;
6
+ organizationId: string;
7
+ serviceAccountId?: string | undefined;
8
+ baseURL: string;
9
+ fetch: Fetch;
10
+ /**
11
+ * Overrides the outgoing User-Agent header on the token exchange. When
12
+ * empty, sends an SDK-identified UA so the token endpoint's access logs
13
+ * identify the caller.
14
+ */
15
+ userAgent?: string | undefined;
16
+ };
17
+ /**
18
+ * Exchanges an external OIDC JWT for an Anthropic access token via the
19
+ * RFC 7523 jwt-bearer grant.
20
+ *
21
+ * Each invocation performs a fresh token exchange. Wrap in a
22
+ * {@link TokenCache} to avoid exchanging on every request.
23
+ *
24
+ * Federation grants do not return a refresh token — callers re-exchange
25
+ * their assertion on expiry.
26
+ */
27
+ export declare function oidcFederationProvider(config: OIDCFederationConfig): AccessTokenProvider;
28
+ //# sourceMappingURL=oidc-federation.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oidc-federation.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/oidc-federation.ts"],"names":[],"mappings":"OAAO,KAAK,EAAE,KAAK,EAAE;OACd,KAAK,EAAE,mBAAmB,EAAE,qBAAqB,EAAE;AAc1D,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,qBAAqB,CAAC;IAC7C,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,KAAK,CAAC;IACb;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,oBAAoB,GAAG,mBAAmB,CAuExF"}