Skip to content

Auth

Defined in: auth/types.ts:155

User denied access during OAuth flow

new AccessDeniedError(): AccessDeniedError

Defined in: auth/types.ts:156

AccessDeniedError

AuthError.constructor

readonly optional code: string

Defined in: errors.ts:21

Machine-readable error code. Prefer values from the ErrorCode enum.

AuthError.code

readonly optional httpStatus: number

Defined in: errors.ts:25

HTTP status code hint for error classification

AuthError.httpStatus

readonly retryable: boolean

Defined in: errors.ts:23

Whether this error is safe to retry

AuthError.retryable

static is(error): error is AgentSDKError

Defined in: errors.ts:36

Check if an error is an AgentSDKError (works across bundled copies)

unknown

error is AgentSDKError

AuthError.is


Defined in: auth/types.ts:139

Base error for auth operations.

Error description

Standard ErrorOptions (e.g. cause)

new AuthError(message, options?): AuthError

Defined in: auth/types.ts:140

string

ErrorOptions

AuthError

AgentSDKError.constructor

readonly optional code: string

Defined in: errors.ts:21

Machine-readable error code. Prefer values from the ErrorCode enum.

AgentSDKError.code

readonly optional httpStatus: number

Defined in: errors.ts:25

HTTP status code hint for error classification

AgentSDKError.httpStatus

readonly retryable: boolean

Defined in: errors.ts:23

Whether this error is safe to retry

AgentSDKError.retryable

static is(error): error is AgentSDKError

Defined in: errors.ts:36

Check if an error is an AgentSDKError (works across bundled copies)

unknown

error is AgentSDKError

AgentSDKError.is


Defined in: auth/claude-auth.ts:33

Programmatic OAuth+PKCE authentication for Claude SDK.

const auth = new ClaudeAuth();
const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
redirectUri: "https://platform.claude.com/oauth/code/callback",
});
// Open authorizeUrl in browser, get code from redirect
const token = await completeAuth(code);
// Use token with ClaudeBackendOptions: env.CLAUDE_CODE_OAUTH_TOKEN

new ClaudeAuth(options?): ClaudeAuth

Defined in: auth/claude-auth.ts:40

Optional configuration with custom fetch and random bytes for testing

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

RandomBytesFn

ClaudeAuth

static extractCode(input): string

Defined in: auth/claude-auth.ts:103

Extract an authorization code from user input. Accepts a raw code string or a full redirect URL containing a code query parameter.

string

Raw authorization code or redirect URL

string

The extracted authorization code

ClaudeAuth.extractCode("abc123"); // "abc123"
ClaudeAuth.extractCode("https://platform.claude.com/oauth/code/callback?code=abc123&state=xyz"); // "abc123"

refreshToken(refreshToken): Promise<ClaudeAuthToken>

Defined in: auth/claude-auth.ts:133

Refresh an expired Claude token.

string

The refresh token from a previous authentication

Promise<ClaudeAuthToken>

New auth token with refreshed access token

If the refresh request fails

const auth = new ClaudeAuth();
const newToken = await auth.refreshToken(oldToken.refreshToken);

startOAuthFlow(options?): OAuthFlowResult

Defined in: auth/claude-auth.ts:67

Start the Claude OAuth+PKCE flow. Generates PKCE code verifier/challenge and returns an authorize URL plus a completeAuth(code) function for token exchange.

OAuthFlowOptions

Redirect URI and optional scopes

OAuthFlowResult

OAuth flow result with authorize URL and completeAuth function

If PKCE generation fails

const auth = new ClaudeAuth();
const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
redirectUri: "https://platform.claude.com/oauth/code/callback",
});
console.log(`Open: ${authorizeUrl}`);
const token = await completeAuth(authorizationCode);

Defined in: auth/copilot-auth.ts:54

Programmatic GitHub Device Flow authentication for Copilot SDK.

const auth = new CopilotAuth();
const flow = await auth.startDeviceFlow();
console.log(`Open ${flow.verificationUrl} and enter ${flow.userCode}`);
const token = await flow.waitForToken();
// Use token.accessToken with CopilotBackendOptions.githubToken

new CopilotAuth(options?): CopilotAuth

Defined in: auth/copilot-auth.ts:58

Optional configuration with custom fetch for testing

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

CopilotAuth

refreshToken(refreshToken, signal?): Promise<CopilotAuthToken>

Defined in: auth/copilot-auth.ts:232

Refresh an expired Copilot token using a refresh token. Only works for GitHub App tokens that include a refresh_token.

string

The refresh token from the original auth flow

AbortSignal

Optional abort signal

Promise<CopilotAuthToken>

Fresh CopilotAuthToken with new access and refresh tokens

If the refresh request fails

const auth = new CopilotAuth();
const newToken = await auth.refreshToken(oldToken.refreshToken!);

startDeviceFlow(options?): Promise<DeviceFlowResult>

Defined in: auth/copilot-auth.ts:81

Start the GitHub Device Flow. Returns a device code result with user code, verification URL, and a waitForToken() function that polls until the user authorizes.

Optional scopes and abort signal

string

AbortSignal

Promise<DeviceFlowResult>

Device flow result with user code, verification URL, and waitForToken poller

If the device code request fails

If the device code expires before user authorizes

If the user denies access

const auth = new CopilotAuth();
const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
const token = await waitForToken();

Defined in: auth/types.ts:147

Device code expired before user authorized

new DeviceCodeExpiredError(): DeviceCodeExpiredError

Defined in: auth/types.ts:148

DeviceCodeExpiredError

AuthError.constructor

readonly optional code: string

Defined in: errors.ts:21

Machine-readable error code. Prefer values from the ErrorCode enum.

AuthError.code

readonly optional httpStatus: number

Defined in: errors.ts:25

HTTP status code hint for error classification

AuthError.httpStatus

readonly retryable: boolean

Defined in: errors.ts:23

Whether this error is safe to retry

AuthError.retryable

static is(error): error is AgentSDKError

Defined in: errors.ts:36

Check if an error is an AgentSDKError (works across bundled copies)

unknown

error is AgentSDKError

AuthError.is


Defined in: auth/types.ts:166

Token exchange or refresh failed.

Error description

Standard ErrorOptions (e.g. cause)

new TokenExchangeError(message, options?): TokenExchangeError

Defined in: auth/types.ts:167

string

ErrorOptions

TokenExchangeError

AuthError.constructor

readonly optional code: string

Defined in: errors.ts:21

Machine-readable error code. Prefer values from the ErrorCode enum.

AuthError.code

readonly optional httpStatus: number

Defined in: errors.ts:25

HTTP status code hint for error classification

AuthError.httpStatus

readonly retryable: boolean

Defined in: errors.ts:23

Whether this error is safe to retry

AuthError.retryable

static is(error): error is AgentSDKError

Defined in: errors.ts:36

Check if an error is an AgentSDKError (works across bundled copies)

unknown

error is AgentSDKError

AuthError.is


Defined in: auth/refresh-manager.ts:116

Background token refresh manager with event emission and retry logic.

Lifecycle: newstart() → (auto-refreshes) → stop() or dispose()

new TokenRefreshManager(options): TokenRefreshManager

Defined in: auth/refresh-manager.ts:135

TokenRefreshOptions

TokenRefreshManager

get isDisposed(): boolean

Defined in: auth/refresh-manager.ts:169

Whether the manager has been disposed

boolean

get isRunning(): boolean

Defined in: auth/refresh-manager.ts:164

Whether the manager is currently running

boolean

get token(): AuthToken

Defined in: auth/refresh-manager.ts:159

Current token managed by this instance

AuthToken

dispose(): void

Defined in: auth/refresh-manager.ts:205

Stop and clean up all resources

void

off<K>(event, listener): this

Defined in: auth/refresh-manager.ts:152

Remove an event listener

K extends keyof TokenRefreshEvents

K

TokenRefreshEvents[K]

this

on<K>(event, listener): this

Defined in: auth/refresh-manager.ts:145

Register an event listener

K extends keyof TokenRefreshEvents

K

TokenRefreshEvents[K]

this

start(): void

Defined in: auth/refresh-manager.ts:178

Start automatic refresh scheduling. If the token is already expired, emits “expired” immediately. If the token has no expiresIn, does nothing (long-lived token).

void

stop(): void

Defined in: auth/refresh-manager.ts:186

Stop automatic refresh (can be restarted with start())

void

updateToken(token): void

Defined in: auth/refresh-manager.ts:195

Update the managed token (e.g. after manual refresh). Reschedules automatic refresh if running.

AuthToken

void

Defined in: auth/types.ts:20

Base auth token returned by all auth providers.

import type { AuthToken } from "@witqq/agent-sdk/auth";
const token: AuthToken = {
accessToken: "gho_abc123...",
tokenType: "bearer",
obtainedAt: Date.now(),
};

accessToken: string

Defined in: auth/types.ts:22

The access token string

optional expiresIn: number

Defined in: auth/types.ts:26

Seconds until token expires (undefined = long-lived)

obtainedAt: number

Defined in: auth/types.ts:28

Timestamp when the token was obtained

tokenType: string

Defined in: auth/types.ts:24

Token type (e.g. “bearer”)


Defined in: auth/types.ts:70

Claude-specific token (OAuth+PKCE, expires in 8h).

import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
const token: ClaudeAuthToken = {
accessToken: "sk-ant-oat01-...",
tokenType: "bearer",
expiresIn: 28800,
obtainedAt: Date.now(),
refreshToken: "sk-ant-rt01-...",
scopes: ["user:inference", "user:profile"],
};

accessToken: string

Defined in: auth/types.ts:22

The access token string

AuthToken.accessToken

optional expiresIn: number

Defined in: auth/types.ts:26

Seconds until token expires (undefined = long-lived)

AuthToken.expiresIn

obtainedAt: number

Defined in: auth/types.ts:28

Timestamp when the token was obtained

AuthToken.obtainedAt

refreshToken: string

Defined in: auth/types.ts:72

Refresh token for obtaining new access tokens

scopes: string[]

Defined in: auth/types.ts:74

OAuth scopes granted

tokenType: string

Defined in: auth/types.ts:24

Token type (e.g. “bearer”)

AuthToken.tokenType


Defined in: auth/types.ts:46

Copilot-specific token (GitHub OAuth, long-lived).

import type { CopilotAuthToken } from "@witqq/agent-sdk/auth";
const token: CopilotAuthToken = {
accessToken: "gho_abc123...",
tokenType: "bearer",
obtainedAt: Date.now(),
login: "octocat",
};

accessToken: string

Defined in: auth/types.ts:22

The access token string

AuthToken.accessToken

optional expiresIn: number

Defined in: auth/types.ts:26

Seconds until token expires (undefined = long-lived)

AuthToken.expiresIn

optional login: string

Defined in: auth/types.ts:48

GitHub user login associated with the token

obtainedAt: number

Defined in: auth/types.ts:28

Timestamp when the token was obtained

AuthToken.obtainedAt

optional refreshToken: string

Defined in: auth/types.ts:50

Refresh token for obtaining new access tokens (present when GitHub App has expiring tokens)

tokenType: string

Defined in: auth/types.ts:24

Token type (e.g. “bearer”)

AuthToken.tokenType


Defined in: auth/types.ts:92

Result of initiating a GitHub Device Flow.

import { CopilotAuth } from "@witqq/agent-sdk/auth";
const auth = new CopilotAuth();
const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
const token = await waitForToken();

userCode: string

Defined in: auth/types.ts:94

The code the user must enter at the verification URL

verificationUrl: string

Defined in: auth/types.ts:96

URL where the user enters the code

waitForToken: (signal?) => Promise<CopilotAuthToken>

Defined in: auth/types.ts:98

Polls GitHub until user authorizes; resolves with token

AbortSignal

Promise<CopilotAuthToken>


Defined in: auth/types.ts:104

Options for starting a Claude OAuth flow

optional redirectUri: string

Defined in: auth/types.ts:106

The redirect URI registered with the OAuth app

optional scopes: string

Defined in: auth/types.ts:108

OAuth scopes to request (defaults to user:profile user:inference)


Defined in: auth/types.ts:126

Result of initiating a Claude OAuth flow.

import type { OAuthFlowResult } from "@witqq/agent-sdk/auth";
const result: OAuthFlowResult = {
authorizeUrl: "https://claude.ai/oauth/authorize?...",
completeAuth: async (code) => ({ ... }),
};
// Open result.authorizeUrl in browser, get code from redirect
const token = await result.completeAuth(code);

authorizeUrl: string

Defined in: auth/types.ts:128

URL to open in browser for user authorization

completeAuth: (codeOrUrl) => Promise<ClaudeAuthToken>

Defined in: auth/types.ts:130

Exchange the authorization code (or full redirect URL) for tokens

string

Promise<ClaudeAuthToken>


Defined in: auth/refresh-manager.ts:67

Events emitted by TokenRefreshManager

disposed: () => void

Defined in: auth/refresh-manager.ts:75

Emitted when manager is disposed

void

error: (error, attempt) => void

Defined in: auth/refresh-manager.ts:71

Emitted when refresh attempt failed (may retry)

Error

number

void

expired: () => void

Defined in: auth/refresh-manager.ts:73

Emitted when token expired and could not be refreshed

void

refreshed: (token) => void

Defined in: auth/refresh-manager.ts:69

Emitted when token was successfully refreshed

AuthToken

void


Defined in: auth/refresh-manager.ts:79

Configuration for TokenRefreshManager

optional maxRetries: number

Defined in: auth/refresh-manager.ts:95

Maximum retry attempts on refresh failure. Default: 3

optional minDelayMs: number

Defined in: auth/refresh-manager.ts:103

Minimum schedule delay in ms (prevents scheduling in the past). Default: 1000

refresh: (token) => Promise<AuthToken>

Defined in: auth/refresh-manager.ts:86

Function that performs the actual token refresh. Receives the current token and returns a new one.

AuthToken

Promise<AuthToken>

optional refreshThreshold: number

Defined in: auth/refresh-manager.ts:91

Fraction of token lifetime at which to trigger refresh (0-1). Default: 0.8 (refresh at 80% of lifetime, i.e. with 20% remaining)

optional retryDelayMs: number

Defined in: auth/refresh-manager.ts:99

Base delay between retries in ms. Exponential backoff applied. Default: 1000

token: AuthToken

Defined in: auth/refresh-manager.ts:81

Current token with expiresIn and obtainedAt