@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,144 @@
|
|
|
1
|
+
import type { Fetch } from '../../internal/builtin-types';
|
|
2
|
+
import { CREDENTIALS_FILE_VERSION, type AnthropicCredentials } from '../../core/credentials';
|
|
3
|
+
import type { AccessTokenProvider } from './types';
|
|
4
|
+
import {
|
|
5
|
+
GRANT_TYPE_REFRESH_TOKEN,
|
|
6
|
+
MANDATORY_REFRESH_THRESHOLD_IN_SECONDS,
|
|
7
|
+
OAUTH_API_BETA_HEADER,
|
|
8
|
+
TOKEN_ENDPOINT,
|
|
9
|
+
WorkloadIdentityError,
|
|
10
|
+
checkCredentialsFileSafety,
|
|
11
|
+
parseTokenResponse,
|
|
12
|
+
redactSensitive,
|
|
13
|
+
requireSecureTokenEndpoint,
|
|
14
|
+
writeCredentialsFileAtomic,
|
|
15
|
+
} from './types';
|
|
16
|
+
import { nowAsSeconds } from '../../internal/utils/time';
|
|
17
|
+
import { VERSION } from '../../version';
|
|
18
|
+
|
|
19
|
+
export type UserOAuthConfig = {
|
|
20
|
+
credentialsPath: string;
|
|
21
|
+
clientId?: string | undefined;
|
|
22
|
+
baseURL: string;
|
|
23
|
+
fetch: Fetch;
|
|
24
|
+
userAgent?: string | undefined;
|
|
25
|
+
onSafetyWarning?: ((msg: string) => void) | undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Reads a user-oauth credential file. Returns the cached access token while
|
|
30
|
+
* fresh; on expiry performs a `refresh_token` grant and writes the new
|
|
31
|
+
* tokens back to the credentials file (atomic replace, fsync'd).
|
|
32
|
+
*
|
|
33
|
+
* If `clientId` is empty, the access token is treated as static — the
|
|
34
|
+
* credentials file is read on every call but no refresh is attempted, and
|
|
35
|
+
* an expired token without a `refresh_token` raises.
|
|
36
|
+
*/
|
|
37
|
+
export function userOAuthProvider(config: UserOAuthConfig): AccessTokenProvider {
|
|
38
|
+
return async (opts) => {
|
|
39
|
+
const fs = await import('node:fs');
|
|
40
|
+
|
|
41
|
+
await checkCredentialsFileSafety(config.credentialsPath, config.onSafetyWarning);
|
|
42
|
+
|
|
43
|
+
let raw: string;
|
|
44
|
+
try {
|
|
45
|
+
raw = await fs.promises.readFile(config.credentialsPath, 'utf-8');
|
|
46
|
+
} catch (err) {
|
|
47
|
+
throw new WorkloadIdentityError(`Credentials file not found at ${config.credentialsPath}: ${err}`);
|
|
48
|
+
}
|
|
49
|
+
let creds: AnthropicCredentials;
|
|
50
|
+
try {
|
|
51
|
+
creds = JSON.parse(raw);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
throw new WorkloadIdentityError(
|
|
54
|
+
`Credentials file at ${config.credentialsPath} is not valid JSON: ${err}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const accessToken = creds.access_token;
|
|
59
|
+
if (!accessToken) {
|
|
60
|
+
throw new WorkloadIdentityError(
|
|
61
|
+
`Credentials file at ${config.credentialsPath} must include 'access_token'`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Return cached token if still fresh (or no expiry info), unless the
|
|
66
|
+
// caller is forcing a refresh after a 401 — then go straight to refresh
|
|
67
|
+
// even if the file's expires_at still looks valid.
|
|
68
|
+
const expiresAt = creds.expires_at;
|
|
69
|
+
if (
|
|
70
|
+
!opts?.forceRefresh &&
|
|
71
|
+
(expiresAt == null || nowAsSeconds() < expiresAt - MANDATORY_REFRESH_THRESHOLD_IN_SECONDS)
|
|
72
|
+
) {
|
|
73
|
+
return { token: accessToken, expiresAt: expiresAt ?? null };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const refreshToken = creds.refresh_token;
|
|
77
|
+
if (!config.clientId || !refreshToken) {
|
|
78
|
+
throw new WorkloadIdentityError(
|
|
79
|
+
`Access token at ${config.credentialsPath} has expired and no refresh is available ` +
|
|
80
|
+
`(client_id ${config.clientId ? 'set' : 'empty'}, refresh_token ${refreshToken ? 'set' : 'empty'})`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
requireSecureTokenEndpoint(config.baseURL);
|
|
85
|
+
|
|
86
|
+
const body: Record<string, string> = {
|
|
87
|
+
grant_type: GRANT_TYPE_REFRESH_TOKEN,
|
|
88
|
+
refresh_token: refreshToken,
|
|
89
|
+
client_id: config.clientId,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const url = `${config.baseURL}${TOKEN_ENDPOINT}`;
|
|
93
|
+
let resp: Response;
|
|
94
|
+
try {
|
|
95
|
+
resp = await config.fetch(url, {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
'Content-Type': 'application/json',
|
|
99
|
+
'anthropic-beta': OAUTH_API_BETA_HEADER,
|
|
100
|
+
'User-Agent': config.userAgent || `anthropic-sdk-typescript/${VERSION} userOAuthProvider`,
|
|
101
|
+
},
|
|
102
|
+
body: JSON.stringify(body),
|
|
103
|
+
});
|
|
104
|
+
} catch (err) {
|
|
105
|
+
throw new WorkloadIdentityError(`User OAuth refresh failed to reach token endpoint: ${err}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const requestId = resp.headers.get('Request-Id');
|
|
109
|
+
|
|
110
|
+
if (!resp.ok) {
|
|
111
|
+
const text = await resp.text().catch(() => '');
|
|
112
|
+
throw new WorkloadIdentityError(
|
|
113
|
+
`User OAuth refresh failed (HTTP ${resp.status}): ${redactSensitive(text)}`,
|
|
114
|
+
resp.status,
|
|
115
|
+
redactSensitive(text),
|
|
116
|
+
requestId,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const data = await parseTokenResponse(resp, requestId);
|
|
121
|
+
const expiresIn = Number(data.expires_in);
|
|
122
|
+
if (!Number.isFinite(expiresIn)) {
|
|
123
|
+
throw new WorkloadIdentityError(
|
|
124
|
+
`User OAuth refresh response missing or invalid expires_in: ${JSON.stringify(redactSensitive(data))}`,
|
|
125
|
+
resp.status,
|
|
126
|
+
redactSensitive(data),
|
|
127
|
+
requestId,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const newExpiresAt = nowAsSeconds() + expiresIn;
|
|
131
|
+
const newRefreshToken = data.refresh_token || refreshToken;
|
|
132
|
+
|
|
133
|
+
await writeCredentialsFileAtomic(config.credentialsPath, {
|
|
134
|
+
...creds,
|
|
135
|
+
version: CREDENTIALS_FILE_VERSION,
|
|
136
|
+
type: 'oauth_token',
|
|
137
|
+
access_token: data.access_token,
|
|
138
|
+
expires_at: newExpiresAt,
|
|
139
|
+
refresh_token: newRefreshToken,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return { token: data.access_token, expiresAt: newExpiresAt };
|
|
143
|
+
};
|
|
144
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.94.0'; // x-release-please-version
|
package/version.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.94.0";
|
|
2
2
|
//# sourceMappingURL=version.d.mts.map
|
package/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.94.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/version.js
CHANGED
package/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.94.0'; // x-release-please-version
|
|
2
2
|
//# sourceMappingURL=version.mjs.map
|