@anthropic-ai/sdk 0.92.0 → 0.94.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/client.d.mts +73 -0
- package/client.d.mts.map +1 -1
- package/client.d.ts +73 -0
- package/client.d.ts.map +1 -1
- package/client.js +258 -9
- package/client.js.map +1 -1
- package/client.mjs +258 -9
- package/client.mjs.map +1 -1
- package/core/credentials.d.mts +131 -0
- package/core/credentials.d.mts.map +1 -0
- package/core/credentials.d.ts +131 -0
- package/core/credentials.d.ts.map +1 -0
- package/core/credentials.js +317 -0
- package/core/credentials.js.map +1 -0
- package/core/credentials.mjs +277 -0
- package/core/credentials.mjs.map +1 -0
- package/internal/utils/time.d.mts +3 -0
- package/internal/utils/time.d.mts.map +1 -0
- package/internal/utils/time.d.ts +3 -0
- package/internal/utils/time.d.ts.map +1 -0
- package/internal/utils/time.js +8 -0
- package/internal/utils/time.js.map +1 -0
- package/internal/utils/time.mjs +5 -0
- package/internal/utils/time.mjs.map +1 -0
- package/lib/credentials/credential-chain.d.mts +39 -0
- package/lib/credentials/credential-chain.d.mts.map +1 -0
- package/lib/credentials/credential-chain.d.ts +39 -0
- package/lib/credentials/credential-chain.d.ts.map +1 -0
- package/lib/credentials/credential-chain.js +245 -0
- package/lib/credentials/credential-chain.js.map +1 -0
- package/lib/credentials/credential-chain.mjs +208 -0
- package/lib/credentials/credential-chain.mjs.map +1 -0
- package/lib/credentials/identity-token.d.mts +11 -0
- package/lib/credentials/identity-token.d.mts.map +1 -0
- package/lib/credentials/identity-token.d.ts +11 -0
- package/lib/credentials/identity-token.d.ts.map +1 -0
- package/lib/credentials/identity-token.js +72 -0
- package/lib/credentials/identity-token.js.map +1 -0
- package/lib/credentials/identity-token.mjs +35 -0
- package/lib/credentials/identity-token.mjs.map +1 -0
- package/lib/credentials/oidc-federation.d.mts +40 -0
- package/lib/credentials/oidc-federation.d.mts.map +1 -0
- package/lib/credentials/oidc-federation.d.ts +40 -0
- package/lib/credentials/oidc-federation.d.ts.map +1 -0
- package/lib/credentials/oidc-federation.js +82 -0
- package/lib/credentials/oidc-federation.js.map +1 -0
- package/lib/credentials/oidc-federation.mjs +79 -0
- package/lib/credentials/oidc-federation.mjs.map +1 -0
- package/lib/credentials/token-cache.d.mts +54 -0
- package/lib/credentials/token-cache.d.mts.map +1 -0
- package/lib/credentials/token-cache.d.ts +54 -0
- package/lib/credentials/token-cache.d.ts.map +1 -0
- package/lib/credentials/token-cache.js +112 -0
- package/lib/credentials/token-cache.js.map +1 -0
- package/lib/credentials/token-cache.mjs +108 -0
- package/lib/credentials/token-cache.mjs.map +1 -0
- package/lib/credentials/types.d.mts +96 -0
- package/lib/credentials/types.d.mts.map +1 -0
- package/lib/credentials/types.d.ts +96 -0
- package/lib/credentials/types.d.ts.map +1 -0
- package/lib/credentials/types.js +266 -0
- package/lib/credentials/types.js.map +1 -0
- package/lib/credentials/types.mjs +224 -0
- package/lib/credentials/types.mjs.map +1 -0
- package/lib/credentials/user-oauth.d.mts +21 -0
- package/lib/credentials/user-oauth.d.mts.map +1 -0
- package/lib/credentials/user-oauth.d.ts +21 -0
- package/lib/credentials/user-oauth.d.ts.map +1 -0
- package/lib/credentials/user-oauth.js +130 -0
- package/lib/credentials/user-oauth.js.map +1 -0
- package/lib/credentials/user-oauth.mjs +94 -0
- package/lib/credentials/user-oauth.mjs.map +1 -0
- package/lib/credentials.d.mts +4 -0
- package/lib/credentials.d.mts.map +1 -0
- package/lib/credentials.d.ts +4 -0
- package/lib/credentials.d.ts.map +1 -0
- package/lib/credentials.js +8 -0
- package/lib/credentials.js.map +1 -0
- package/lib/credentials.mjs +3 -0
- package/lib/credentials.mjs.map +1 -0
- package/package.json +1 -1
- package/src/client.ts +342 -14
- package/src/core/credentials.ts +379 -0
- package/src/internal/utils/time.ts +4 -0
- package/src/lib/credentials/credential-chain.ts +292 -0
- package/src/lib/credentials/identity-token.ts +37 -0
- package/src/lib/credentials/oidc-federation.ts +140 -0
- package/src/lib/credentials/token-cache.ts +130 -0
- package/src/lib/credentials/types.ts +295 -0
- package/src/lib/credentials/user-oauth.ts +144 -0
- package/src/lib/credentials.ts +3 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,245 @@
|
|
|
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
|
+
// For federation profiles workspace_id is sent in the jwt-bearer exchange
|
|
51
|
+
// body, not as a request header (the minted token is already
|
|
52
|
+
// workspace-scoped, so the header would be ignored).
|
|
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 loaded = await (0, credentials_1.loadConfigWithSource)(profile);
|
|
78
|
+
if (!loaded) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const { config, fromFile } = loaded;
|
|
82
|
+
// For file-loaded configs, default credentials_path to the per-profile
|
|
83
|
+
// location so user_oauth and federation caching work. Shallow-clone first
|
|
84
|
+
// so callers that retain a reference to the loaded config don't observe the
|
|
85
|
+
// patched-in default.
|
|
86
|
+
//
|
|
87
|
+
// Env-only credentials (no profile file on disk) skip the disk cache —
|
|
88
|
+
// matching the other SDKs. A disk cache keyed by profile path would
|
|
89
|
+
// re-serve a stale token after a change to ANTHROPIC_WORKSPACE_ID (or
|
|
90
|
+
// ANTHROPIC_ORGANIZATION_ID / ANTHROPIC_FEDERATION_RULE_ID) until the
|
|
91
|
+
// cached token expired, so the env-only chain stays in-memory only.
|
|
92
|
+
const withPath = config.authentication.credentials_path || !fromFile ?
|
|
93
|
+
config
|
|
94
|
+
: {
|
|
95
|
+
...config,
|
|
96
|
+
authentication: {
|
|
97
|
+
...config.authentication,
|
|
98
|
+
credentials_path: (await (0, credentials_1.getCredentialsPath)(config, profile)) ?? undefined,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
return resolveCredentialsFromConfig(withPath, options);
|
|
102
|
+
}
|
|
103
|
+
function buildProvider(config, credentialsPath, baseURL, options) {
|
|
104
|
+
switch (config.authentication.type) {
|
|
105
|
+
case 'oidc_federation': {
|
|
106
|
+
const auth = config.authentication;
|
|
107
|
+
const identityProvider = resolveIdentityTokenProvider(auth);
|
|
108
|
+
if (!identityProvider) {
|
|
109
|
+
throw new types_1.WorkloadIdentityError('oidc_federation config requires an identity token (set authentication.identity_token, ' +
|
|
110
|
+
'ANTHROPIC_IDENTITY_TOKEN_FILE, or ANTHROPIC_IDENTITY_TOKEN)');
|
|
111
|
+
}
|
|
112
|
+
if (!auth.federation_rule_id) {
|
|
113
|
+
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).");
|
|
114
|
+
}
|
|
115
|
+
if (!config.organization_id) {
|
|
116
|
+
throw new types_1.WorkloadIdentityError('oidc_federation config requires organization_id (set ANTHROPIC_ORGANIZATION_ID or config.organization_id)');
|
|
117
|
+
}
|
|
118
|
+
const exchange = (0, oidc_federation_1.oidcFederationProvider)({
|
|
119
|
+
identityTokenProvider: identityProvider,
|
|
120
|
+
federationRuleId: auth.federation_rule_id,
|
|
121
|
+
organizationId: config.organization_id,
|
|
122
|
+
serviceAccountId: auth.service_account_id,
|
|
123
|
+
workspaceId: config.workspace_id,
|
|
124
|
+
baseURL,
|
|
125
|
+
fetch: options.fetch,
|
|
126
|
+
userAgent: options.userAgent,
|
|
127
|
+
});
|
|
128
|
+
// If there's a credentials file path, wrap the exchange with file caching
|
|
129
|
+
// (check file for fresh token before exchanging, write back after).
|
|
130
|
+
if (credentialsPath) {
|
|
131
|
+
return cachedExchangeProvider(exchange, credentialsPath, options.onCacheWriteError, options.onSafetyWarning);
|
|
132
|
+
}
|
|
133
|
+
return exchange;
|
|
134
|
+
}
|
|
135
|
+
case 'user_oauth': {
|
|
136
|
+
if (!credentialsPath) {
|
|
137
|
+
throw new types_1.WorkloadIdentityError('user_oauth config requires authentication.credentials_path ' +
|
|
138
|
+
'(or load via a profile so it defaults to <config_dir>/credentials/<profile>.json)');
|
|
139
|
+
}
|
|
140
|
+
return (0, user_oauth_1.userOAuthProvider)({
|
|
141
|
+
credentialsPath,
|
|
142
|
+
clientId: config.authentication.client_id,
|
|
143
|
+
baseURL,
|
|
144
|
+
fetch: options.fetch,
|
|
145
|
+
userAgent: options.userAgent,
|
|
146
|
+
onSafetyWarning: options.onSafetyWarning,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
default: {
|
|
150
|
+
const t = config.authentication.type;
|
|
151
|
+
throw new types_1.WorkloadIdentityError(`authentication.type "${t}" is not a known authentication type`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Resolves the identity token provider from config fields or environment variables.
|
|
157
|
+
*
|
|
158
|
+
* Resolution order:
|
|
159
|
+
* 1. `identity_token.path` from the config (source: "file")
|
|
160
|
+
* 2. `ANTHROPIC_IDENTITY_TOKEN_FILE` env var
|
|
161
|
+
* 3. `ANTHROPIC_IDENTITY_TOKEN` env var (static value)
|
|
162
|
+
*/
|
|
163
|
+
function resolveIdentityTokenProvider(auth) {
|
|
164
|
+
if (auth.identity_token) {
|
|
165
|
+
// Cast needed to stringify an unknown source value for the error message:
|
|
166
|
+
// the on-disk JSON may contain a source this SDK version doesn't know about.
|
|
167
|
+
const source = auth.identity_token.source;
|
|
168
|
+
if (source !== 'file') {
|
|
169
|
+
throw new types_1.WorkloadIdentityError(`identity_token.source "${source}" is not supported by this SDK version (only "file")`);
|
|
170
|
+
}
|
|
171
|
+
if (!auth.identity_token.path) {
|
|
172
|
+
throw new types_1.WorkloadIdentityError(`identity_token.source "file" requires a non-empty path`);
|
|
173
|
+
}
|
|
174
|
+
return (0, identity_token_1.identityTokenFromFile)(auth.identity_token.path);
|
|
175
|
+
}
|
|
176
|
+
const tokenFile = (0, env_1.readEnv)('ANTHROPIC_IDENTITY_TOKEN_FILE');
|
|
177
|
+
if (tokenFile) {
|
|
178
|
+
return (0, identity_token_1.identityTokenFromFile)(tokenFile);
|
|
179
|
+
}
|
|
180
|
+
const tokenValue = (0, env_1.readEnv)('ANTHROPIC_IDENTITY_TOKEN');
|
|
181
|
+
if (tokenValue) {
|
|
182
|
+
return (0, identity_token_1.identityTokenFromValue)(tokenValue);
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Wraps a federation exchange provider with credential file caching.
|
|
188
|
+
* Checks the file for a fresh token before exchanging, and writes the
|
|
189
|
+
* result back after a successful exchange (best-effort, atomic replace).
|
|
190
|
+
*
|
|
191
|
+
* Note: this is not cross-process serialized — two SDK instances that
|
|
192
|
+
* miss the cache simultaneously will both perform a full exchange and
|
|
193
|
+
* the last writer wins. That is acceptable: federation exchanges are
|
|
194
|
+
* idempotent and the cache is an optimization, not a correctness gate.
|
|
195
|
+
*/
|
|
196
|
+
function cachedExchangeProvider(exchange, credentialsPath, onCacheWriteError, onSafetyWarning) {
|
|
197
|
+
return async (opts) => {
|
|
198
|
+
const fs = await Promise.resolve().then(() => __importStar(require('node:fs')));
|
|
199
|
+
await (0, types_1.checkCredentialsFileSafety)(credentialsPath, onSafetyWarning);
|
|
200
|
+
// Try cached credentials file
|
|
201
|
+
let existing;
|
|
202
|
+
try {
|
|
203
|
+
const raw = await fs.promises.readFile(credentialsPath, 'utf-8');
|
|
204
|
+
existing = JSON.parse(raw);
|
|
205
|
+
const token = existing?.['access_token'];
|
|
206
|
+
if (token && !opts?.forceRefresh) {
|
|
207
|
+
const expiresAt = existing?.['expires_at'];
|
|
208
|
+
if (expiresAt == null || (0, time_1.nowAsSeconds)() < expiresAt - types_1.MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
|
|
209
|
+
return { token, expiresAt: expiresAt ?? null };
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
// ENOENT or invalid-JSON → no usable cache, exchange fresh. Other
|
|
215
|
+
// errors (EACCES, EISDIR, …) indicate a broken cache path; surface to
|
|
216
|
+
// the optional hook so they're at least debuggable, then proceed.
|
|
217
|
+
const code = err?.code;
|
|
218
|
+
if (code !== 'ENOENT' && !(err instanceof SyntaxError)) {
|
|
219
|
+
onCacheWriteError?.(err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Exchange for a new token
|
|
223
|
+
const result = await exchange(opts);
|
|
224
|
+
// Write cache back (best-effort). Preserve any unknown keys from the
|
|
225
|
+
// existing file (notably refresh_token, in the unlikely case this path
|
|
226
|
+
// is shared with a user_oauth profile) so the federation cache writer
|
|
227
|
+
// doesn't clobber material it didn't own.
|
|
228
|
+
try {
|
|
229
|
+
await (0, types_1.writeCredentialsFileAtomic)(credentialsPath, {
|
|
230
|
+
...(existing ?? {}),
|
|
231
|
+
version: credentials_1.CREDENTIALS_FILE_VERSION,
|
|
232
|
+
type: 'oauth_token',
|
|
233
|
+
access_token: result.token,
|
|
234
|
+
expires_at: result.expiresAt,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
catch (err) {
|
|
238
|
+
// Best-effort caching: surface to the optional hook but never fail
|
|
239
|
+
// the exchange itself.
|
|
240
|
+
onCacheWriteError?.(err);
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
//# 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,gDAgCC;AA5GD,qDAAmD;AACnD,2DAKgC;AAEhC,sCAKiB;AACjB,uDAAyD;AACzD,wDAAiF;AACjF,0DAA2D;AAC3D,gDAAiD;AAqBjD,SAAgB,4BAA4B,CAC1C,MAAuB,EACv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AAE,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE,CAAC;gBACjD,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,kEAAkE;YAClE,sEAAsE;YACtE,kEAAkE;YAClE,MAAM,IAAI,GAAI,GAA6B,EAAE,IAAI,CAAC;YAClD,IAAI,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,GAAG,YAAY,WAAW,CAAC,EAAE,CAAC;gBACvD,iBAAiB,EAAE,CAAC,GAAG,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;QAED,2BAA2B;QAC3B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC;QAEpC,qEAAqE;QACrE,uEAAuE;QACvE,sEAAsE;QACtE,0CAA0C;QAC1C,IAAI,CAAC;YACH,MAAM,IAAA,kCAA0B,EAAC,eAAe,EAAE;gBAChD,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC;gBACnB,OAAO,EAAE,sCAAwB;gBACjC,IAAI,EAAE,aAAa;gBACnB,YAAY,EAAE,MAAM,CAAC,KAAK;gBAC1B,UAAU,EAAE,MAAM,CAAC,SAAS;aAC7B,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,mEAAmE;YACnE,uBAAuB;YACvB,iBAAiB,EAAE,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { readEnv } from "../../internal/utils/env.mjs";
|
|
2
|
+
import { CREDENTIALS_FILE_VERSION, loadConfigWithSource, 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
|
+
// For federation profiles workspace_id is sent in the jwt-bearer exchange
|
|
14
|
+
// body, not as a request header (the minted token is already
|
|
15
|
+
// workspace-scoped, so the header would be ignored).
|
|
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 loaded = await loadConfigWithSource(profile);
|
|
41
|
+
if (!loaded) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const { config, fromFile } = loaded;
|
|
45
|
+
// For file-loaded configs, default credentials_path to the per-profile
|
|
46
|
+
// location so user_oauth and federation caching work. Shallow-clone first
|
|
47
|
+
// so callers that retain a reference to the loaded config don't observe the
|
|
48
|
+
// patched-in default.
|
|
49
|
+
//
|
|
50
|
+
// Env-only credentials (no profile file on disk) skip the disk cache —
|
|
51
|
+
// matching the other SDKs. A disk cache keyed by profile path would
|
|
52
|
+
// re-serve a stale token after a change to ANTHROPIC_WORKSPACE_ID (or
|
|
53
|
+
// ANTHROPIC_ORGANIZATION_ID / ANTHROPIC_FEDERATION_RULE_ID) until the
|
|
54
|
+
// cached token expired, so the env-only chain stays in-memory only.
|
|
55
|
+
const withPath = config.authentication.credentials_path || !fromFile ?
|
|
56
|
+
config
|
|
57
|
+
: {
|
|
58
|
+
...config,
|
|
59
|
+
authentication: {
|
|
60
|
+
...config.authentication,
|
|
61
|
+
credentials_path: (await getCredentialsPath(config, profile)) ?? undefined,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return resolveCredentialsFromConfig(withPath, options);
|
|
65
|
+
}
|
|
66
|
+
function buildProvider(config, credentialsPath, baseURL, options) {
|
|
67
|
+
switch (config.authentication.type) {
|
|
68
|
+
case 'oidc_federation': {
|
|
69
|
+
const auth = config.authentication;
|
|
70
|
+
const identityProvider = resolveIdentityTokenProvider(auth);
|
|
71
|
+
if (!identityProvider) {
|
|
72
|
+
throw new WorkloadIdentityError('oidc_federation config requires an identity token (set authentication.identity_token, ' +
|
|
73
|
+
'ANTHROPIC_IDENTITY_TOKEN_FILE, or ANTHROPIC_IDENTITY_TOKEN)');
|
|
74
|
+
}
|
|
75
|
+
if (!auth.federation_rule_id) {
|
|
76
|
+
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).");
|
|
77
|
+
}
|
|
78
|
+
if (!config.organization_id) {
|
|
79
|
+
throw new WorkloadIdentityError('oidc_federation config requires organization_id (set ANTHROPIC_ORGANIZATION_ID or config.organization_id)');
|
|
80
|
+
}
|
|
81
|
+
const exchange = oidcFederationProvider({
|
|
82
|
+
identityTokenProvider: identityProvider,
|
|
83
|
+
federationRuleId: auth.federation_rule_id,
|
|
84
|
+
organizationId: config.organization_id,
|
|
85
|
+
serviceAccountId: auth.service_account_id,
|
|
86
|
+
workspaceId: config.workspace_id,
|
|
87
|
+
baseURL,
|
|
88
|
+
fetch: options.fetch,
|
|
89
|
+
userAgent: options.userAgent,
|
|
90
|
+
});
|
|
91
|
+
// If there's a credentials file path, wrap the exchange with file caching
|
|
92
|
+
// (check file for fresh token before exchanging, write back after).
|
|
93
|
+
if (credentialsPath) {
|
|
94
|
+
return cachedExchangeProvider(exchange, credentialsPath, options.onCacheWriteError, options.onSafetyWarning);
|
|
95
|
+
}
|
|
96
|
+
return exchange;
|
|
97
|
+
}
|
|
98
|
+
case 'user_oauth': {
|
|
99
|
+
if (!credentialsPath) {
|
|
100
|
+
throw new WorkloadIdentityError('user_oauth config requires authentication.credentials_path ' +
|
|
101
|
+
'(or load via a profile so it defaults to <config_dir>/credentials/<profile>.json)');
|
|
102
|
+
}
|
|
103
|
+
return userOAuthProvider({
|
|
104
|
+
credentialsPath,
|
|
105
|
+
clientId: config.authentication.client_id,
|
|
106
|
+
baseURL,
|
|
107
|
+
fetch: options.fetch,
|
|
108
|
+
userAgent: options.userAgent,
|
|
109
|
+
onSafetyWarning: options.onSafetyWarning,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
default: {
|
|
113
|
+
const t = config.authentication.type;
|
|
114
|
+
throw new WorkloadIdentityError(`authentication.type "${t}" is not a known authentication type`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Resolves the identity token provider from config fields or environment variables.
|
|
120
|
+
*
|
|
121
|
+
* Resolution order:
|
|
122
|
+
* 1. `identity_token.path` from the config (source: "file")
|
|
123
|
+
* 2. `ANTHROPIC_IDENTITY_TOKEN_FILE` env var
|
|
124
|
+
* 3. `ANTHROPIC_IDENTITY_TOKEN` env var (static value)
|
|
125
|
+
*/
|
|
126
|
+
function resolveIdentityTokenProvider(auth) {
|
|
127
|
+
if (auth.identity_token) {
|
|
128
|
+
// Cast needed to stringify an unknown source value for the error message:
|
|
129
|
+
// the on-disk JSON may contain a source this SDK version doesn't know about.
|
|
130
|
+
const source = auth.identity_token.source;
|
|
131
|
+
if (source !== 'file') {
|
|
132
|
+
throw new WorkloadIdentityError(`identity_token.source "${source}" is not supported by this SDK version (only "file")`);
|
|
133
|
+
}
|
|
134
|
+
if (!auth.identity_token.path) {
|
|
135
|
+
throw new WorkloadIdentityError(`identity_token.source "file" requires a non-empty path`);
|
|
136
|
+
}
|
|
137
|
+
return identityTokenFromFile(auth.identity_token.path);
|
|
138
|
+
}
|
|
139
|
+
const tokenFile = readEnv('ANTHROPIC_IDENTITY_TOKEN_FILE');
|
|
140
|
+
if (tokenFile) {
|
|
141
|
+
return identityTokenFromFile(tokenFile);
|
|
142
|
+
}
|
|
143
|
+
const tokenValue = readEnv('ANTHROPIC_IDENTITY_TOKEN');
|
|
144
|
+
if (tokenValue) {
|
|
145
|
+
return identityTokenFromValue(tokenValue);
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Wraps a federation exchange provider with credential file caching.
|
|
151
|
+
* Checks the file for a fresh token before exchanging, and writes the
|
|
152
|
+
* result back after a successful exchange (best-effort, atomic replace).
|
|
153
|
+
*
|
|
154
|
+
* Note: this is not cross-process serialized — two SDK instances that
|
|
155
|
+
* miss the cache simultaneously will both perform a full exchange and
|
|
156
|
+
* the last writer wins. That is acceptable: federation exchanges are
|
|
157
|
+
* idempotent and the cache is an optimization, not a correctness gate.
|
|
158
|
+
*/
|
|
159
|
+
function cachedExchangeProvider(exchange, credentialsPath, onCacheWriteError, onSafetyWarning) {
|
|
160
|
+
return async (opts) => {
|
|
161
|
+
const fs = await import('node:fs');
|
|
162
|
+
await checkCredentialsFileSafety(credentialsPath, onSafetyWarning);
|
|
163
|
+
// Try cached credentials file
|
|
164
|
+
let existing;
|
|
165
|
+
try {
|
|
166
|
+
const raw = await fs.promises.readFile(credentialsPath, 'utf-8');
|
|
167
|
+
existing = JSON.parse(raw);
|
|
168
|
+
const token = existing?.['access_token'];
|
|
169
|
+
if (token && !opts?.forceRefresh) {
|
|
170
|
+
const expiresAt = existing?.['expires_at'];
|
|
171
|
+
if (expiresAt == null || nowAsSeconds() < expiresAt - MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
|
|
172
|
+
return { token, expiresAt: expiresAt ?? null };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
// ENOENT or invalid-JSON → no usable cache, exchange fresh. Other
|
|
178
|
+
// errors (EACCES, EISDIR, …) indicate a broken cache path; surface to
|
|
179
|
+
// the optional hook so they're at least debuggable, then proceed.
|
|
180
|
+
const code = err?.code;
|
|
181
|
+
if (code !== 'ENOENT' && !(err instanceof SyntaxError)) {
|
|
182
|
+
onCacheWriteError?.(err);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Exchange for a new token
|
|
186
|
+
const result = await exchange(opts);
|
|
187
|
+
// Write cache back (best-effort). Preserve any unknown keys from the
|
|
188
|
+
// existing file (notably refresh_token, in the unlikely case this path
|
|
189
|
+
// is shared with a user_oauth profile) so the federation cache writer
|
|
190
|
+
// doesn't clobber material it didn't own.
|
|
191
|
+
try {
|
|
192
|
+
await writeCredentialsFileAtomic(credentialsPath, {
|
|
193
|
+
...(existing ?? {}),
|
|
194
|
+
version: CREDENTIALS_FILE_VERSION,
|
|
195
|
+
type: 'oauth_token',
|
|
196
|
+
access_token: result.token,
|
|
197
|
+
expires_at: result.expiresAt,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
// Best-effort caching: surface to the optional hook but never fail
|
|
202
|
+
// the exchange itself.
|
|
203
|
+
onCacheWriteError?.(err);
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
//# 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,oBAAoB,EACpB,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,0EAA0E;IAC1E,6DAA6D;IAC7D,qDAAqD;IACrD,IAAI,MAAM,CAAC,YAAY,IAAI,MAAM,CAAC,cAAc,CA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QAC1C,IAAI,CAAC;YACH,MAAM,0BAA0B,CAAC,eAAe,EAAE;gBAChD,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC;gBACnB,OAAO,EAAE,wBAAwB;gBACjC,IAAI,EAAE,aAAa;gBACnB,YAAY,EAAE,MAAM,CAAC,KAAK;gBAC1B,UAAU,EAAE,MAAM,CAAC,SAAS;aAC7B,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,mEAAmE;YACnE,uBAAuB;YACvB,iBAAiB,EAAE,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -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"}
|