@anthropic-ai/sdk 0.92.0 → 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.
- package/CHANGELOG.md +8 -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 +111 -0
- package/core/credentials.d.mts.map +1 -0
- package/core/credentials.d.ts +111 -0
- package/core/credentials.d.ts.map +1 -0
- package/core/credentials.js +298 -0
- package/core/credentials.js.map +1 -0
- package/core/credentials.mjs +259 -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 +237 -0
- package/lib/credentials/credential-chain.js.map +1 -0
- package/lib/credentials/credential-chain.mjs +200 -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 +28 -0
- package/lib/credentials/oidc-federation.d.mts.map +1 -0
- package/lib/credentials/oidc-federation.d.ts +28 -0
- package/lib/credentials/oidc-federation.d.ts.map +1 -0
- package/lib/credentials/oidc-federation.js +69 -0
- package/lib/credentials/oidc-federation.js.map +1 -0
- package/lib/credentials/oidc-federation.mjs +66 -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 +349 -0
- package/src/internal/utils/time.ts +4 -0
- package/src/lib/credentials/credential-chain.ts +284 -0
- package/src/lib/credentials/identity-token.ts +37 -0
- package/src/lib/credentials/oidc-federation.ts +112 -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,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,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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"}
|
|
@@ -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"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Fetch } from "../../internal/builtin-types.js";
|
|
2
|
+
import type { AccessTokenProvider, IdentityTokenProvider } from "./types.js";
|
|
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.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oidc-federation.d.ts","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"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oidcFederationProvider = oidcFederationProvider;
|
|
4
|
+
const types_1 = require("./types.js");
|
|
5
|
+
const time_1 = require("../../internal/utils/time.js");
|
|
6
|
+
const version_1 = require("../../version.js");
|
|
7
|
+
/**
|
|
8
|
+
* Exchanges an external OIDC JWT for an Anthropic access token via the
|
|
9
|
+
* RFC 7523 jwt-bearer grant.
|
|
10
|
+
*
|
|
11
|
+
* Each invocation performs a fresh token exchange. Wrap in a
|
|
12
|
+
* {@link TokenCache} to avoid exchanging on every request.
|
|
13
|
+
*
|
|
14
|
+
* Federation grants do not return a refresh token — callers re-exchange
|
|
15
|
+
* their assertion on expiry.
|
|
16
|
+
*/
|
|
17
|
+
function oidcFederationProvider(config) {
|
|
18
|
+
return async () => {
|
|
19
|
+
(0, types_1.requireSecureTokenEndpoint)(config.baseURL);
|
|
20
|
+
const jwt = await config.identityTokenProvider();
|
|
21
|
+
// The token endpoint enforces a 16 KiB assertion limit; surface a clear
|
|
22
|
+
// client-side error so misconfigured projected-token sources are
|
|
23
|
+
// diagnosable without a server round-trip.
|
|
24
|
+
if (jwt.length > 16 * 1024) {
|
|
25
|
+
throw new types_1.WorkloadIdentityError(`Identity token is ${Math.ceil(jwt.length / 1024)} KiB, exceeds the 16 KiB assertion limit`);
|
|
26
|
+
}
|
|
27
|
+
const body = {
|
|
28
|
+
grant_type: types_1.GRANT_TYPE_JWT_BEARER,
|
|
29
|
+
assertion: jwt,
|
|
30
|
+
federation_rule_id: config.federationRuleId,
|
|
31
|
+
organization_id: config.organizationId,
|
|
32
|
+
};
|
|
33
|
+
if (config.serviceAccountId) {
|
|
34
|
+
body['service_account_id'] = config.serviceAccountId;
|
|
35
|
+
}
|
|
36
|
+
const url = `${config.baseURL}${types_1.TOKEN_ENDPOINT}`;
|
|
37
|
+
let resp;
|
|
38
|
+
try {
|
|
39
|
+
resp = await config.fetch(url, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
'anthropic-beta': `${types_1.OAUTH_API_BETA_HEADER},${types_1.FEDERATION_BETA_HEADER}`,
|
|
44
|
+
'User-Agent': config.userAgent || `anthropic-sdk-typescript/${version_1.VERSION} oidcFederationProvider`,
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify(body),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
throw new types_1.WorkloadIdentityError(`Failed to reach token endpoint ${url}: ${err}`);
|
|
51
|
+
}
|
|
52
|
+
const requestId = resp.headers.get('Request-Id');
|
|
53
|
+
if (!resp.ok) {
|
|
54
|
+
const text = await resp.text().catch(() => '');
|
|
55
|
+
const redacted = (0, types_1.redactSensitive)(text);
|
|
56
|
+
throw new types_1.WorkloadIdentityError(`Token exchange failed with status ${resp.status}${requestId ? ` (request-id ${requestId})` : ''}: ${redacted}`, resp.status, redacted, requestId);
|
|
57
|
+
}
|
|
58
|
+
const data = await (0, types_1.parseTokenResponse)(resp, requestId);
|
|
59
|
+
const expiresIn = Number(data.expires_in);
|
|
60
|
+
if (!Number.isFinite(expiresIn)) {
|
|
61
|
+
throw new types_1.WorkloadIdentityError(`Token endpoint response missing required fields: ${JSON.stringify((0, types_1.redactSensitive)(data))}`, resp.status, (0, types_1.redactSensitive)(data), requestId);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
token: data.access_token,
|
|
65
|
+
expiresAt: (0, time_1.nowAsSeconds)() + expiresIn,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=oidc-federation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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AAY,GAAE,GAAG,SAAS;SACtC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { FEDERATION_BETA_HEADER, GRANT_TYPE_JWT_BEARER, OAUTH_API_BETA_HEADER, TOKEN_ENDPOINT, WorkloadIdentityError, parseTokenResponse, redactSensitive, requireSecureTokenEndpoint, } from "./types.mjs";
|
|
2
|
+
import { nowAsSeconds } from "../../internal/utils/time.mjs";
|
|
3
|
+
import { VERSION } from "../../version.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Exchanges an external OIDC JWT for an Anthropic access token via the
|
|
6
|
+
* RFC 7523 jwt-bearer grant.
|
|
7
|
+
*
|
|
8
|
+
* Each invocation performs a fresh token exchange. Wrap in a
|
|
9
|
+
* {@link TokenCache} to avoid exchanging on every request.
|
|
10
|
+
*
|
|
11
|
+
* Federation grants do not return a refresh token — callers re-exchange
|
|
12
|
+
* their assertion on expiry.
|
|
13
|
+
*/
|
|
14
|
+
export function oidcFederationProvider(config) {
|
|
15
|
+
return async () => {
|
|
16
|
+
requireSecureTokenEndpoint(config.baseURL);
|
|
17
|
+
const jwt = await config.identityTokenProvider();
|
|
18
|
+
// The token endpoint enforces a 16 KiB assertion limit; surface a clear
|
|
19
|
+
// client-side error so misconfigured projected-token sources are
|
|
20
|
+
// diagnosable without a server round-trip.
|
|
21
|
+
if (jwt.length > 16 * 1024) {
|
|
22
|
+
throw new WorkloadIdentityError(`Identity token is ${Math.ceil(jwt.length / 1024)} KiB, exceeds the 16 KiB assertion limit`);
|
|
23
|
+
}
|
|
24
|
+
const body = {
|
|
25
|
+
grant_type: GRANT_TYPE_JWT_BEARER,
|
|
26
|
+
assertion: jwt,
|
|
27
|
+
federation_rule_id: config.federationRuleId,
|
|
28
|
+
organization_id: config.organizationId,
|
|
29
|
+
};
|
|
30
|
+
if (config.serviceAccountId) {
|
|
31
|
+
body['service_account_id'] = config.serviceAccountId;
|
|
32
|
+
}
|
|
33
|
+
const url = `${config.baseURL}${TOKEN_ENDPOINT}`;
|
|
34
|
+
let resp;
|
|
35
|
+
try {
|
|
36
|
+
resp = await config.fetch(url, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
'anthropic-beta': `${OAUTH_API_BETA_HEADER},${FEDERATION_BETA_HEADER}`,
|
|
41
|
+
'User-Agent': config.userAgent || `anthropic-sdk-typescript/${VERSION} oidcFederationProvider`,
|
|
42
|
+
},
|
|
43
|
+
body: JSON.stringify(body),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
throw new WorkloadIdentityError(`Failed to reach token endpoint ${url}: ${err}`);
|
|
48
|
+
}
|
|
49
|
+
const requestId = resp.headers.get('Request-Id');
|
|
50
|
+
if (!resp.ok) {
|
|
51
|
+
const text = await resp.text().catch(() => '');
|
|
52
|
+
const redacted = redactSensitive(text);
|
|
53
|
+
throw new WorkloadIdentityError(`Token exchange failed with status ${resp.status}${requestId ? ` (request-id ${requestId})` : ''}: ${redacted}`, resp.status, redacted, requestId);
|
|
54
|
+
}
|
|
55
|
+
const data = await parseTokenResponse(resp, requestId);
|
|
56
|
+
const expiresIn = Number(data.expires_in);
|
|
57
|
+
if (!Number.isFinite(expiresIn)) {
|
|
58
|
+
throw new WorkloadIdentityError(`Token endpoint response missing required fields: ${JSON.stringify(redactSensitive(data))}`, resp.status, redactSensitive(data), requestId);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
token: data.access_token,
|
|
62
|
+
expiresAt: nowAsSeconds() + expiresIn,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=oidc-federation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oidc-federation.mjs","sourceRoot":"","sources":["../../src/lib/credentials/oidc-federation.ts"],"names":[],"mappings":"OAEO,EACL,sBAAsB,E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SACtC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AccessTokenProvider } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an {@link AccessTokenProvider} with two-tier proactive refresh
|
|
4
|
+
* and concurrent deduplication.
|
|
5
|
+
*
|
|
6
|
+
* Refresh policy on each {@link getToken} call:
|
|
7
|
+
*
|
|
8
|
+
* - No cached token → call provider (blocking), cache, return.
|
|
9
|
+
* - Cached with `expiresAt == null` → return cached forever.
|
|
10
|
+
* - More than 120s remaining → return cached.
|
|
11
|
+
* - 30–120s remaining (advisory window) → return stale token immediately,
|
|
12
|
+
* kick off background refresh. On failure, log and keep stale.
|
|
13
|
+
* - Less than 30s remaining or expired (mandatory) → block and refresh.
|
|
14
|
+
* On failure, throw.
|
|
15
|
+
*
|
|
16
|
+
* Concurrent mandatory callers coalesce into a single provider call.
|
|
17
|
+
*/
|
|
18
|
+
export declare class TokenCache {
|
|
19
|
+
private provider;
|
|
20
|
+
private cached;
|
|
21
|
+
private pendingRefresh;
|
|
22
|
+
private nextForce;
|
|
23
|
+
private lastAdvisoryError;
|
|
24
|
+
private onAdvisoryRefreshError;
|
|
25
|
+
constructor(provider: AccessTokenProvider, onAdvisoryRefreshError?: (err: unknown) => void);
|
|
26
|
+
getToken(): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Clears the cached token and marks the next {@link getToken} as a forced
|
|
29
|
+
* refresh, so the underlying provider bypasses any on-disk freshness check.
|
|
30
|
+
* Called after a 401 — the server has just told us the token is bad even
|
|
31
|
+
* if its `expires_at` still looks fresh.
|
|
32
|
+
*/
|
|
33
|
+
invalidate(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Mandatory refresh. Joins any in-flight refresh unless forced — a forced
|
|
36
|
+
* refresh must not coalesce into a non-forced one that may re-serve the
|
|
37
|
+
* same stale disk token.
|
|
38
|
+
*/
|
|
39
|
+
private refresh;
|
|
40
|
+
/**
|
|
41
|
+
* Advisory background refresh. Shares the same in-flight promise as
|
|
42
|
+
* mandatory refreshes for deduplication, but swallows errors so the
|
|
43
|
+
* stale cached token keeps being served. Backs off for
|
|
44
|
+
* {@link ADVISORY_REFRESH_BACKOFF_IN_SECONDS} after a failure so an
|
|
45
|
+
* outage during the advisory window doesn't hammer the token endpoint.
|
|
46
|
+
*/
|
|
47
|
+
private backgroundRefresh;
|
|
48
|
+
/**
|
|
49
|
+
* Core refresh. Sets {@link pendingRefresh} so concurrent callers
|
|
50
|
+
* (both advisory and mandatory) coalesce into a single provider call.
|
|
51
|
+
*/
|
|
52
|
+
private doRefresh;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=token-cache.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-cache.d.mts","sourceRoot":"","sources":["../../src/lib/credentials/token-cache.ts"],"names":[],"mappings":"OAAO,KAAK,EAAe,mBAAmB,EAAE;AAQhD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,MAAM,CAA4B;IAC1C,OAAO,CAAC,cAAc,CAAqC;IAC3D,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,sBAAsB,CAAuC;gBAEzD,QAAQ,EAAE,mBAAmB,EAAE,sBAAsB,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI;IAKpF,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC;IA6BjC;;;;;OAKG;IACH,UAAU,IAAI,IAAI;IAKlB;;;;OAIG;IACH,OAAO,CAAC,OAAO;IAOf;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IAezB;;;OAGG;IACH,OAAO,CAAC,SAAS;CAclB"}
|