@witqq/agent-sdk 0.3.1 → 0.5.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/README.md +117 -1
- package/dist/auth/index.cjs +359 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/index.d.cts +271 -0
- package/dist/auth/index.d.ts +271 -0
- package/dist/auth/index.js +352 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/backends/claude.cjs +120 -13
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +6 -1
- package/dist/backends/claude.d.ts +6 -1
- package/dist/backends/claude.js +120 -13
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +101 -13
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +2 -1
- package/dist/backends/copilot.d.ts +2 -1
- package/dist/backends/copilot.js +101 -13
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/vercel-ai.cjs +24 -1
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +1 -1
- package/dist/backends/vercel-ai.d.ts +1 -1
- package/dist/backends/vercel-ai.js +24 -1
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/index.cjs +8 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/{types-ChCH6zvp.d.cts → types-CQEyww7V.d.cts} +21 -0
- package/dist/{types-ChCH6zvp.d.ts → types-CQEyww7V.d.ts} +21 -0
- package/package.json +11 -1
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base auth token returned by all auth providers.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import type { AuthToken } from "@witqq/agent-sdk/auth";
|
|
7
|
+
*
|
|
8
|
+
* const token: AuthToken = {
|
|
9
|
+
* accessToken: "gho_abc123...",
|
|
10
|
+
* tokenType: "bearer",
|
|
11
|
+
* obtainedAt: Date.now(),
|
|
12
|
+
* };
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
interface AuthToken {
|
|
16
|
+
/** The access token string */
|
|
17
|
+
accessToken: string;
|
|
18
|
+
/** Token type (e.g. "bearer") */
|
|
19
|
+
tokenType: string;
|
|
20
|
+
/** Seconds until token expires (undefined = long-lived) */
|
|
21
|
+
expiresIn?: number;
|
|
22
|
+
/** Timestamp when the token was obtained */
|
|
23
|
+
obtainedAt: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Copilot-specific token (GitHub OAuth, long-lived).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import type { CopilotAuthToken } from "@witqq/agent-sdk/auth";
|
|
31
|
+
*
|
|
32
|
+
* const token: CopilotAuthToken = {
|
|
33
|
+
* accessToken: "gho_abc123...",
|
|
34
|
+
* tokenType: "bearer",
|
|
35
|
+
* obtainedAt: Date.now(),
|
|
36
|
+
* login: "octocat",
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
interface CopilotAuthToken extends AuthToken {
|
|
41
|
+
/** GitHub user login associated with the token */
|
|
42
|
+
login?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Claude-specific token (OAuth+PKCE, expires in 8h).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
|
|
50
|
+
*
|
|
51
|
+
* const token: ClaudeAuthToken = {
|
|
52
|
+
* accessToken: "sk-ant-oat01-...",
|
|
53
|
+
* tokenType: "bearer",
|
|
54
|
+
* expiresIn: 28800,
|
|
55
|
+
* obtainedAt: Date.now(),
|
|
56
|
+
* refreshToken: "sk-ant-rt01-...",
|
|
57
|
+
* scopes: ["user:inference", "user:profile"],
|
|
58
|
+
* };
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
interface ClaudeAuthToken extends AuthToken {
|
|
62
|
+
/** Refresh token for obtaining new access tokens */
|
|
63
|
+
refreshToken: string;
|
|
64
|
+
/** OAuth scopes granted */
|
|
65
|
+
scopes: string[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Result of initiating a GitHub Device Flow.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { CopilotAuth } from "@witqq/agent-sdk/auth";
|
|
73
|
+
*
|
|
74
|
+
* const auth = new CopilotAuth();
|
|
75
|
+
* const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
|
|
76
|
+
* console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
|
|
77
|
+
* const token = await waitForToken();
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
interface DeviceFlowResult {
|
|
81
|
+
/** The code the user must enter at the verification URL */
|
|
82
|
+
userCode: string;
|
|
83
|
+
/** URL where the user enters the code */
|
|
84
|
+
verificationUrl: string;
|
|
85
|
+
/** Polls GitHub until user authorizes; resolves with token */
|
|
86
|
+
waitForToken: (signal?: AbortSignal) => Promise<CopilotAuthToken>;
|
|
87
|
+
}
|
|
88
|
+
/** Options for starting a Claude OAuth flow */
|
|
89
|
+
interface OAuthFlowOptions {
|
|
90
|
+
/** The redirect URI registered with the OAuth app */
|
|
91
|
+
redirectUri?: string;
|
|
92
|
+
/** OAuth scopes to request (defaults to user:profile user:inference) */
|
|
93
|
+
scopes?: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Result of initiating a Claude OAuth flow.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import type { OAuthFlowResult } from "@witqq/agent-sdk/auth";
|
|
101
|
+
*
|
|
102
|
+
* const result: OAuthFlowResult = {
|
|
103
|
+
* authorizeUrl: "https://claude.ai/oauth/authorize?...",
|
|
104
|
+
* completeAuth: async (code) => ({ ... }),
|
|
105
|
+
* };
|
|
106
|
+
* // Open result.authorizeUrl in browser, get code from redirect
|
|
107
|
+
* const token = await result.completeAuth(code);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
interface OAuthFlowResult {
|
|
111
|
+
/** URL to open in browser for user authorization */
|
|
112
|
+
authorizeUrl: string;
|
|
113
|
+
/** Exchange the authorization code (or full redirect URL) for tokens */
|
|
114
|
+
completeAuth: (codeOrUrl: string) => Promise<ClaudeAuthToken>;
|
|
115
|
+
}
|
|
116
|
+
/** Base error for auth operations.
|
|
117
|
+
* @param message - Error description
|
|
118
|
+
* @param options - Standard ErrorOptions (e.g. cause)
|
|
119
|
+
*/
|
|
120
|
+
declare class AuthError extends Error {
|
|
121
|
+
constructor(message: string, options?: ErrorOptions);
|
|
122
|
+
}
|
|
123
|
+
/** Device code expired before user authorized */
|
|
124
|
+
declare class DeviceCodeExpiredError extends AuthError {
|
|
125
|
+
constructor();
|
|
126
|
+
}
|
|
127
|
+
/** User denied access during OAuth flow */
|
|
128
|
+
declare class AccessDeniedError extends AuthError {
|
|
129
|
+
constructor();
|
|
130
|
+
}
|
|
131
|
+
/** Token exchange or refresh failed.
|
|
132
|
+
* @param message - Error description
|
|
133
|
+
* @param options - Standard ErrorOptions (e.g. cause)
|
|
134
|
+
*/
|
|
135
|
+
declare class TokenExchangeError extends AuthError {
|
|
136
|
+
constructor(message: string, options?: ErrorOptions);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Fetch function type for dependency injection in tests */
|
|
140
|
+
type FetchFn$1 = typeof globalThis.fetch;
|
|
141
|
+
/**
|
|
142
|
+
* Programmatic GitHub Device Flow authentication for Copilot SDK.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const auth = new CopilotAuth();
|
|
147
|
+
* const flow = await auth.startDeviceFlow();
|
|
148
|
+
* console.log(`Open ${flow.verificationUrl} and enter ${flow.userCode}`);
|
|
149
|
+
* const token = await flow.waitForToken();
|
|
150
|
+
* // Use token.accessToken with CopilotBackendOptions.githubToken
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
declare class CopilotAuth {
|
|
154
|
+
private readonly fetchFn;
|
|
155
|
+
/** @param options - Optional configuration with custom fetch for testing */
|
|
156
|
+
constructor(options?: {
|
|
157
|
+
fetch?: FetchFn$1;
|
|
158
|
+
});
|
|
159
|
+
/**
|
|
160
|
+
* Start the GitHub Device Flow.
|
|
161
|
+
* Returns a device code result with user code, verification URL,
|
|
162
|
+
* and a `waitForToken()` function that polls until the user authorizes.
|
|
163
|
+
*
|
|
164
|
+
* @param options - Optional scopes and abort signal
|
|
165
|
+
* @returns Device flow result with user code, verification URL, and waitForToken poller
|
|
166
|
+
* @throws {AuthError} If the device code request fails
|
|
167
|
+
* @throws {DeviceCodeExpiredError} If the device code expires before user authorizes
|
|
168
|
+
* @throws {AccessDeniedError} If the user denies access
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* const auth = new CopilotAuth();
|
|
173
|
+
* const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
|
|
174
|
+
* console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
|
|
175
|
+
* const token = await waitForToken();
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
startDeviceFlow(options?: {
|
|
179
|
+
scopes?: string;
|
|
180
|
+
signal?: AbortSignal;
|
|
181
|
+
}): Promise<DeviceFlowResult>;
|
|
182
|
+
private pollForToken;
|
|
183
|
+
private fetchUserLogin;
|
|
184
|
+
private delay;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Fetch function type for dependency injection in tests */
|
|
188
|
+
type FetchFn = typeof globalThis.fetch;
|
|
189
|
+
/** Random bytes generator for dependency injection in tests */
|
|
190
|
+
type RandomBytesFn = (size: number) => Uint8Array;
|
|
191
|
+
/**
|
|
192
|
+
* Programmatic OAuth+PKCE authentication for Claude SDK.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const auth = new ClaudeAuth();
|
|
197
|
+
* const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
|
|
198
|
+
* redirectUri: "https://platform.claude.com/oauth/code/callback",
|
|
199
|
+
* });
|
|
200
|
+
* // Open authorizeUrl in browser, get code from redirect
|
|
201
|
+
* const token = await completeAuth(code);
|
|
202
|
+
* // Use token with ClaudeBackendOptions: env.CLAUDE_CODE_OAUTH_TOKEN
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
declare class ClaudeAuth {
|
|
206
|
+
private readonly fetchFn;
|
|
207
|
+
private readonly randomBytes;
|
|
208
|
+
/**
|
|
209
|
+
* @param options - Optional configuration with custom fetch and random bytes for testing
|
|
210
|
+
*/
|
|
211
|
+
constructor(options?: {
|
|
212
|
+
fetch?: FetchFn;
|
|
213
|
+
randomBytes?: RandomBytesFn;
|
|
214
|
+
});
|
|
215
|
+
/**
|
|
216
|
+
* Start the Claude OAuth+PKCE flow.
|
|
217
|
+
* Generates PKCE code verifier/challenge and returns an authorize URL
|
|
218
|
+
* plus a `completeAuth(code)` function for token exchange.
|
|
219
|
+
*
|
|
220
|
+
* @param options - Redirect URI and optional scopes
|
|
221
|
+
* @returns OAuth flow result with authorize URL and completeAuth function
|
|
222
|
+
* @throws {AuthError} If PKCE generation fails
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const auth = new ClaudeAuth();
|
|
227
|
+
* const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
|
|
228
|
+
* redirectUri: "https://platform.claude.com/oauth/code/callback",
|
|
229
|
+
* });
|
|
230
|
+
* console.log(`Open: ${authorizeUrl}`);
|
|
231
|
+
* const token = await completeAuth(authorizationCode);
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
startOAuthFlow(options?: OAuthFlowOptions): OAuthFlowResult;
|
|
235
|
+
/**
|
|
236
|
+
* Extract an authorization code from user input.
|
|
237
|
+
* Accepts a raw code string or a full redirect URL containing a `code` query parameter.
|
|
238
|
+
*
|
|
239
|
+
* @param input - Raw authorization code or redirect URL
|
|
240
|
+
* @returns The extracted authorization code
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* ClaudeAuth.extractCode("abc123"); // "abc123"
|
|
245
|
+
* ClaudeAuth.extractCode("https://platform.claude.com/oauth/code/callback?code=abc123&state=xyz"); // "abc123"
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
static extractCode(input: string): string;
|
|
249
|
+
/**
|
|
250
|
+
* Refresh an expired Claude token.
|
|
251
|
+
*
|
|
252
|
+
* @param refreshToken - The refresh token from a previous authentication
|
|
253
|
+
* @returns New auth token with refreshed access token
|
|
254
|
+
* @throws {TokenExchangeError} If the refresh request fails
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* const auth = new ClaudeAuth();
|
|
259
|
+
* const newToken = await auth.refreshToken(oldToken.refreshToken);
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
refreshToken(refreshToken: string): Promise<ClaudeAuthToken>;
|
|
263
|
+
private generateCodeVerifier;
|
|
264
|
+
private generateState;
|
|
265
|
+
private buildAuthorizeUrl;
|
|
266
|
+
private generateCodeChallengeSync;
|
|
267
|
+
private exchangeCode;
|
|
268
|
+
private mapTokenResponse;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export { AccessDeniedError, AuthError, type AuthToken, ClaudeAuth, type ClaudeAuthToken, CopilotAuth, type CopilotAuthToken, DeviceCodeExpiredError, type DeviceFlowResult, type OAuthFlowOptions, type OAuthFlowResult, TokenExchangeError };
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base auth token returned by all auth providers.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import type { AuthToken } from "@witqq/agent-sdk/auth";
|
|
7
|
+
*
|
|
8
|
+
* const token: AuthToken = {
|
|
9
|
+
* accessToken: "gho_abc123...",
|
|
10
|
+
* tokenType: "bearer",
|
|
11
|
+
* obtainedAt: Date.now(),
|
|
12
|
+
* };
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
interface AuthToken {
|
|
16
|
+
/** The access token string */
|
|
17
|
+
accessToken: string;
|
|
18
|
+
/** Token type (e.g. "bearer") */
|
|
19
|
+
tokenType: string;
|
|
20
|
+
/** Seconds until token expires (undefined = long-lived) */
|
|
21
|
+
expiresIn?: number;
|
|
22
|
+
/** Timestamp when the token was obtained */
|
|
23
|
+
obtainedAt: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Copilot-specific token (GitHub OAuth, long-lived).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import type { CopilotAuthToken } from "@witqq/agent-sdk/auth";
|
|
31
|
+
*
|
|
32
|
+
* const token: CopilotAuthToken = {
|
|
33
|
+
* accessToken: "gho_abc123...",
|
|
34
|
+
* tokenType: "bearer",
|
|
35
|
+
* obtainedAt: Date.now(),
|
|
36
|
+
* login: "octocat",
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
interface CopilotAuthToken extends AuthToken {
|
|
41
|
+
/** GitHub user login associated with the token */
|
|
42
|
+
login?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Claude-specific token (OAuth+PKCE, expires in 8h).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
|
|
50
|
+
*
|
|
51
|
+
* const token: ClaudeAuthToken = {
|
|
52
|
+
* accessToken: "sk-ant-oat01-...",
|
|
53
|
+
* tokenType: "bearer",
|
|
54
|
+
* expiresIn: 28800,
|
|
55
|
+
* obtainedAt: Date.now(),
|
|
56
|
+
* refreshToken: "sk-ant-rt01-...",
|
|
57
|
+
* scopes: ["user:inference", "user:profile"],
|
|
58
|
+
* };
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
interface ClaudeAuthToken extends AuthToken {
|
|
62
|
+
/** Refresh token for obtaining new access tokens */
|
|
63
|
+
refreshToken: string;
|
|
64
|
+
/** OAuth scopes granted */
|
|
65
|
+
scopes: string[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Result of initiating a GitHub Device Flow.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { CopilotAuth } from "@witqq/agent-sdk/auth";
|
|
73
|
+
*
|
|
74
|
+
* const auth = new CopilotAuth();
|
|
75
|
+
* const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
|
|
76
|
+
* console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
|
|
77
|
+
* const token = await waitForToken();
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
interface DeviceFlowResult {
|
|
81
|
+
/** The code the user must enter at the verification URL */
|
|
82
|
+
userCode: string;
|
|
83
|
+
/** URL where the user enters the code */
|
|
84
|
+
verificationUrl: string;
|
|
85
|
+
/** Polls GitHub until user authorizes; resolves with token */
|
|
86
|
+
waitForToken: (signal?: AbortSignal) => Promise<CopilotAuthToken>;
|
|
87
|
+
}
|
|
88
|
+
/** Options for starting a Claude OAuth flow */
|
|
89
|
+
interface OAuthFlowOptions {
|
|
90
|
+
/** The redirect URI registered with the OAuth app */
|
|
91
|
+
redirectUri?: string;
|
|
92
|
+
/** OAuth scopes to request (defaults to user:profile user:inference) */
|
|
93
|
+
scopes?: string;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Result of initiating a Claude OAuth flow.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import type { OAuthFlowResult } from "@witqq/agent-sdk/auth";
|
|
101
|
+
*
|
|
102
|
+
* const result: OAuthFlowResult = {
|
|
103
|
+
* authorizeUrl: "https://claude.ai/oauth/authorize?...",
|
|
104
|
+
* completeAuth: async (code) => ({ ... }),
|
|
105
|
+
* };
|
|
106
|
+
* // Open result.authorizeUrl in browser, get code from redirect
|
|
107
|
+
* const token = await result.completeAuth(code);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
interface OAuthFlowResult {
|
|
111
|
+
/** URL to open in browser for user authorization */
|
|
112
|
+
authorizeUrl: string;
|
|
113
|
+
/** Exchange the authorization code (or full redirect URL) for tokens */
|
|
114
|
+
completeAuth: (codeOrUrl: string) => Promise<ClaudeAuthToken>;
|
|
115
|
+
}
|
|
116
|
+
/** Base error for auth operations.
|
|
117
|
+
* @param message - Error description
|
|
118
|
+
* @param options - Standard ErrorOptions (e.g. cause)
|
|
119
|
+
*/
|
|
120
|
+
declare class AuthError extends Error {
|
|
121
|
+
constructor(message: string, options?: ErrorOptions);
|
|
122
|
+
}
|
|
123
|
+
/** Device code expired before user authorized */
|
|
124
|
+
declare class DeviceCodeExpiredError extends AuthError {
|
|
125
|
+
constructor();
|
|
126
|
+
}
|
|
127
|
+
/** User denied access during OAuth flow */
|
|
128
|
+
declare class AccessDeniedError extends AuthError {
|
|
129
|
+
constructor();
|
|
130
|
+
}
|
|
131
|
+
/** Token exchange or refresh failed.
|
|
132
|
+
* @param message - Error description
|
|
133
|
+
* @param options - Standard ErrorOptions (e.g. cause)
|
|
134
|
+
*/
|
|
135
|
+
declare class TokenExchangeError extends AuthError {
|
|
136
|
+
constructor(message: string, options?: ErrorOptions);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Fetch function type for dependency injection in tests */
|
|
140
|
+
type FetchFn$1 = typeof globalThis.fetch;
|
|
141
|
+
/**
|
|
142
|
+
* Programmatic GitHub Device Flow authentication for Copilot SDK.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const auth = new CopilotAuth();
|
|
147
|
+
* const flow = await auth.startDeviceFlow();
|
|
148
|
+
* console.log(`Open ${flow.verificationUrl} and enter ${flow.userCode}`);
|
|
149
|
+
* const token = await flow.waitForToken();
|
|
150
|
+
* // Use token.accessToken with CopilotBackendOptions.githubToken
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
declare class CopilotAuth {
|
|
154
|
+
private readonly fetchFn;
|
|
155
|
+
/** @param options - Optional configuration with custom fetch for testing */
|
|
156
|
+
constructor(options?: {
|
|
157
|
+
fetch?: FetchFn$1;
|
|
158
|
+
});
|
|
159
|
+
/**
|
|
160
|
+
* Start the GitHub Device Flow.
|
|
161
|
+
* Returns a device code result with user code, verification URL,
|
|
162
|
+
* and a `waitForToken()` function that polls until the user authorizes.
|
|
163
|
+
*
|
|
164
|
+
* @param options - Optional scopes and abort signal
|
|
165
|
+
* @returns Device flow result with user code, verification URL, and waitForToken poller
|
|
166
|
+
* @throws {AuthError} If the device code request fails
|
|
167
|
+
* @throws {DeviceCodeExpiredError} If the device code expires before user authorizes
|
|
168
|
+
* @throws {AccessDeniedError} If the user denies access
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* const auth = new CopilotAuth();
|
|
173
|
+
* const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
|
|
174
|
+
* console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
|
|
175
|
+
* const token = await waitForToken();
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
startDeviceFlow(options?: {
|
|
179
|
+
scopes?: string;
|
|
180
|
+
signal?: AbortSignal;
|
|
181
|
+
}): Promise<DeviceFlowResult>;
|
|
182
|
+
private pollForToken;
|
|
183
|
+
private fetchUserLogin;
|
|
184
|
+
private delay;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Fetch function type for dependency injection in tests */
|
|
188
|
+
type FetchFn = typeof globalThis.fetch;
|
|
189
|
+
/** Random bytes generator for dependency injection in tests */
|
|
190
|
+
type RandomBytesFn = (size: number) => Uint8Array;
|
|
191
|
+
/**
|
|
192
|
+
* Programmatic OAuth+PKCE authentication for Claude SDK.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const auth = new ClaudeAuth();
|
|
197
|
+
* const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
|
|
198
|
+
* redirectUri: "https://platform.claude.com/oauth/code/callback",
|
|
199
|
+
* });
|
|
200
|
+
* // Open authorizeUrl in browser, get code from redirect
|
|
201
|
+
* const token = await completeAuth(code);
|
|
202
|
+
* // Use token with ClaudeBackendOptions: env.CLAUDE_CODE_OAUTH_TOKEN
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
declare class ClaudeAuth {
|
|
206
|
+
private readonly fetchFn;
|
|
207
|
+
private readonly randomBytes;
|
|
208
|
+
/**
|
|
209
|
+
* @param options - Optional configuration with custom fetch and random bytes for testing
|
|
210
|
+
*/
|
|
211
|
+
constructor(options?: {
|
|
212
|
+
fetch?: FetchFn;
|
|
213
|
+
randomBytes?: RandomBytesFn;
|
|
214
|
+
});
|
|
215
|
+
/**
|
|
216
|
+
* Start the Claude OAuth+PKCE flow.
|
|
217
|
+
* Generates PKCE code verifier/challenge and returns an authorize URL
|
|
218
|
+
* plus a `completeAuth(code)` function for token exchange.
|
|
219
|
+
*
|
|
220
|
+
* @param options - Redirect URI and optional scopes
|
|
221
|
+
* @returns OAuth flow result with authorize URL and completeAuth function
|
|
222
|
+
* @throws {AuthError} If PKCE generation fails
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const auth = new ClaudeAuth();
|
|
227
|
+
* const { authorizeUrl, completeAuth } = auth.startOAuthFlow({
|
|
228
|
+
* redirectUri: "https://platform.claude.com/oauth/code/callback",
|
|
229
|
+
* });
|
|
230
|
+
* console.log(`Open: ${authorizeUrl}`);
|
|
231
|
+
* const token = await completeAuth(authorizationCode);
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
startOAuthFlow(options?: OAuthFlowOptions): OAuthFlowResult;
|
|
235
|
+
/**
|
|
236
|
+
* Extract an authorization code from user input.
|
|
237
|
+
* Accepts a raw code string or a full redirect URL containing a `code` query parameter.
|
|
238
|
+
*
|
|
239
|
+
* @param input - Raw authorization code or redirect URL
|
|
240
|
+
* @returns The extracted authorization code
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* ClaudeAuth.extractCode("abc123"); // "abc123"
|
|
245
|
+
* ClaudeAuth.extractCode("https://platform.claude.com/oauth/code/callback?code=abc123&state=xyz"); // "abc123"
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
static extractCode(input: string): string;
|
|
249
|
+
/**
|
|
250
|
+
* Refresh an expired Claude token.
|
|
251
|
+
*
|
|
252
|
+
* @param refreshToken - The refresh token from a previous authentication
|
|
253
|
+
* @returns New auth token with refreshed access token
|
|
254
|
+
* @throws {TokenExchangeError} If the refresh request fails
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* const auth = new ClaudeAuth();
|
|
259
|
+
* const newToken = await auth.refreshToken(oldToken.refreshToken);
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
refreshToken(refreshToken: string): Promise<ClaudeAuthToken>;
|
|
263
|
+
private generateCodeVerifier;
|
|
264
|
+
private generateState;
|
|
265
|
+
private buildAuthorizeUrl;
|
|
266
|
+
private generateCodeChallengeSync;
|
|
267
|
+
private exchangeCode;
|
|
268
|
+
private mapTokenResponse;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export { AccessDeniedError, AuthError, type AuthToken, ClaudeAuth, type ClaudeAuthToken, CopilotAuth, type CopilotAuthToken, DeviceCodeExpiredError, type DeviceFlowResult, type OAuthFlowOptions, type OAuthFlowResult, TokenExchangeError };
|