@cortexkit/anthropic-auth-core 1.2.2 → 1.2.3

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/dist/accounts.js CHANGED
@@ -380,9 +380,17 @@ export function buildRefreshOperationError(input) {
380
380
  ? (input.previous.retryCount ?? 0)
381
381
  : 0;
382
382
  const retryCount = previousRetryCount + 1;
383
- const delay = isTransientRefreshError(input.error)
384
- ? Math.min(MAX_REFRESH_RETRY_DELAY_MS, MIN_REFRESH_RETRY_DELAY_MS * 2 ** Math.min(retryCount - 1, 6))
385
- : NON_TRANSIENT_REFRESH_RETRY_DELAY_MS;
383
+ let delay;
384
+ if (input.error instanceof ClaudeOAuthRefreshError &&
385
+ input.error.retryAfter) {
386
+ delay = input.error.retryAfter * 1000;
387
+ }
388
+ else if (isTransientRefreshError(input.error)) {
389
+ delay = Math.min(MAX_REFRESH_RETRY_DELAY_MS, MIN_REFRESH_RETRY_DELAY_MS * 2 ** Math.min(retryCount - 1, 6));
390
+ }
391
+ else {
392
+ delay = NON_TRANSIENT_REFRESH_RETRY_DELAY_MS;
393
+ }
386
394
  return {
387
395
  message: formatErrorMessage(input.error),
388
396
  checkedAt: input.now,
package/dist/auth.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  export declare class ClaudeOAuthRefreshError extends Error {
2
2
  readonly status: number;
3
3
  readonly body: string;
4
- constructor(status: number, body: string);
4
+ /** Parsed Retry-After value in seconds, if the server provided one. */
5
+ readonly retryAfter: number | undefined;
6
+ constructor(status: number, body: string, retryAfterHeader?: string | null);
5
7
  }
6
8
  export type ClaudeOAuthRefreshResult = {
7
9
  access: string;
package/dist/auth.js CHANGED
@@ -1,13 +1,29 @@
1
1
  import { AUTHORIZE_URLS, CLIENT_ID, CODE_CALLBACK_URL, OAUTH_SCOPES, TOKEN_URL, } from "./constants.js";
2
2
  import { generatePKCE } from "./pkce.js";
3
+ function parseRetryAfterHeader(value) {
4
+ if (!value)
5
+ return undefined;
6
+ const seconds = Number(value);
7
+ if (Number.isFinite(seconds) && seconds > 0)
8
+ return Math.ceil(seconds);
9
+ const date = Date.parse(value);
10
+ if (Number.isFinite(date)) {
11
+ const delta = Math.ceil((date - Date.now()) / 1000);
12
+ return delta > 0 ? delta : undefined;
13
+ }
14
+ return undefined;
15
+ }
3
16
  export class ClaudeOAuthRefreshError extends Error {
4
17
  status;
5
18
  body;
6
- constructor(status, body) {
19
+ /** Parsed Retry-After value in seconds, if the server provided one. */
20
+ retryAfter;
21
+ constructor(status, body, retryAfterHeader) {
7
22
  super(`Claude OAuth refresh failed: ${status} — ${body}`);
8
23
  this.status = status;
9
24
  this.body = body;
10
25
  this.name = 'ClaudeOAuthRefreshError';
26
+ this.retryAfter = parseRetryAfterHeader(retryAfterHeader ?? undefined);
11
27
  }
12
28
  }
13
29
  function isTransientNetworkError(error) {
@@ -35,6 +51,7 @@ export async function refreshClaudeOAuthToken(input) {
35
51
  headers: {
36
52
  'Content-Type': 'application/json',
37
53
  Accept: 'application/json',
54
+ 'User-Agent': 'axios/1.13.6',
38
55
  },
39
56
  body: JSON.stringify({
40
57
  grant_type: 'refresh_token',
@@ -48,7 +65,7 @@ export async function refreshClaudeOAuthToken(input) {
48
65
  continue;
49
66
  }
50
67
  const body = await response.text().catch(() => '');
51
- throw new ClaudeOAuthRefreshError(response.status, body);
68
+ throw new ClaudeOAuthRefreshError(response.status, body, response.headers.get('retry-after'));
52
69
  }
53
70
  const json = (await response.json());
54
71
  const refreshedAt = input.now?.() ?? Date.now();
@@ -4,7 +4,7 @@ export declare const AUTHORIZE_URLS: {
4
4
  readonly max: "https://claude.ai/oauth/authorize";
5
5
  };
6
6
  export declare const CODE_CALLBACK_URL = "https://platform.claude.com/oauth/code/callback";
7
- export declare const TOKEN_URL = "https://api.anthropic.com/v1/oauth/token";
7
+ export declare const TOKEN_URL = "https://platform.claude.com/v1/oauth/token";
8
8
  export declare const OAUTH_SCOPES: string[];
9
9
  export declare const TOOL_PREFIX = "mcp_";
10
10
  export declare const REQUIRED_BETAS: string[];
package/dist/constants.js CHANGED
@@ -4,7 +4,7 @@ export const AUTHORIZE_URLS = {
4
4
  max: 'https://claude.ai/oauth/authorize',
5
5
  };
6
6
  export const CODE_CALLBACK_URL = 'https://platform.claude.com/oauth/code/callback';
7
- export const TOKEN_URL = 'https://api.anthropic.com/v1/oauth/token';
7
+ export const TOKEN_URL = 'https://platform.claude.com/v1/oauth/token';
8
8
  export const OAUTH_SCOPES = [
9
9
  'org:create_api_key',
10
10
  'user:profile',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/anthropic-auth-core",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",