@cloudflare/workers-auth 0.1.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 +31 -0
- package/dist/chunk-PAWJFY3S.mjs +6 -0
- package/dist/chunk-PAWJFY3S.mjs.map +1 -0
- package/dist/index.d.mts +475 -0
- package/dist/index.mjs +2154 -0
- package/dist/index.mjs.map +1 -0
- package/dist/metafile-esm.json +1 -0
- package/dist/test-helpers/index.d.mts +7 -0
- package/dist/test-helpers/index.mjs +62 -0
- package/dist/test-helpers/index.mjs.map +1 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @cloudflare/workers-auth
|
|
2
|
+
|
|
3
|
+
Internal OAuth 2.0 + PKCE flow for Cloudflare CLIs (wrangler and friends).
|
|
4
|
+
|
|
5
|
+
> **Not intended for external use.** APIs may change without notice. This package
|
|
6
|
+
> is consumed only by other packages inside this monorepo.
|
|
7
|
+
|
|
8
|
+
## What's in this package
|
|
9
|
+
|
|
10
|
+
- OAuth 2.0 Authorization Code flow with PKCE (RFC 6749 + RFC 7636)
|
|
11
|
+
- Cloudflare-flavored OAuth endpoints (`dash.cloudflare.com` / staging / `WRANGLER_*` overrides)
|
|
12
|
+
- Cloudflare Access detection + service-token / `cloudflared` integration for staging auth domains
|
|
13
|
+
- Persistent auth state stored as TOML in `<globalWranglerConfigPath>/config/<env>.toml`
|
|
14
|
+
- `login`, `logout`, `loginOrRefreshIfRequired`, `getOauthToken`, `getOAuthTokenFromLocalState`
|
|
15
|
+
|
|
16
|
+
What's **not** here (lives in wrangler):
|
|
17
|
+
|
|
18
|
+
- yargs `login` / `logout` / `whoami` / `auth token` commands
|
|
19
|
+
- Environment-based credential resolution (`CLOUDFLARE_API_TOKEN`, etc.)
|
|
20
|
+
- Cloudflare account selection (`requireAuth`, `getOrSelectAccountId`)
|
|
21
|
+
- The scope catalog (passed in as `string[]`)
|
|
22
|
+
- `whoami` / account fetching
|
|
23
|
+
|
|
24
|
+
## Attribution
|
|
25
|
+
|
|
26
|
+
Portions of this package are derived from
|
|
27
|
+
[BitySA/oauth2-auth-code-pkce](https://github.com/BitySA/oauth2-auth-code-pkce),
|
|
28
|
+
licensed under the Apache License 2.0. See the individual file headers for
|
|
29
|
+
the attribution and `LICENSE` for the full text.
|
|
30
|
+
|
|
31
|
+
The rest of the package is MIT-licensed.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"chunk-PAWJFY3S.mjs"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { UserError, ComplianceConfig } from '@cloudflare/workers-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The data that may be read from the on-disk user auth config file.
|
|
5
|
+
*/
|
|
6
|
+
interface UserAuthConfig {
|
|
7
|
+
oauth_token?: string;
|
|
8
|
+
refresh_token?: string;
|
|
9
|
+
expiration_time?: string;
|
|
10
|
+
scopes?: string[];
|
|
11
|
+
/** @deprecated - this field was only provided by the deprecated v1 `wrangler config` command. */
|
|
12
|
+
api_token?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns the absolute path to the auth config TOML file.
|
|
16
|
+
*
|
|
17
|
+
* The file lives under the global Wrangler config directory and is named
|
|
18
|
+
* `default.toml` in production, or `<environment>.toml` for the staging /
|
|
19
|
+
* other Cloudflare API environments.
|
|
20
|
+
*/
|
|
21
|
+
declare function getAuthConfigFilePath(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Writes the user auth config to disk.
|
|
24
|
+
*
|
|
25
|
+
* No in-memory cache to invalidate — auth state is read on demand by every call
|
|
26
|
+
* site that needs it. Callers are responsible for any consumer-side cache
|
|
27
|
+
* purging (e.g. via the {@link OAuthFlowContext.purgeOnLoginOrLogout} hook).
|
|
28
|
+
*/
|
|
29
|
+
declare function writeAuthConfigFile(config: UserAuthConfig): void;
|
|
30
|
+
/**
|
|
31
|
+
* Reads the user auth config from disk.
|
|
32
|
+
*
|
|
33
|
+
* @throws if the file does not exist or cannot be parsed as TOML. Callers
|
|
34
|
+
* typically catch this and treat the failure as "not logged in via local OAuth".
|
|
35
|
+
*/
|
|
36
|
+
declare function readAuthConfigFile(): UserAuthConfig;
|
|
37
|
+
|
|
38
|
+
interface GenerateAuthUrlProps {
|
|
39
|
+
authUrl: string;
|
|
40
|
+
clientId: string;
|
|
41
|
+
scopes: string[];
|
|
42
|
+
stateQueryParam: string;
|
|
43
|
+
codeChallenge: string;
|
|
44
|
+
}
|
|
45
|
+
declare const OAUTH_CALLBACK_URL = "http://localhost:8976/oauth/callback";
|
|
46
|
+
/**
|
|
47
|
+
* Build the OAuth 2.0 authorize URL for the Cloudflare auth endpoint.
|
|
48
|
+
*
|
|
49
|
+
* Extracted from the rest of the OAuth flow so consumers (or tests) can
|
|
50
|
+
* substitute a deterministic implementation when a stable URL is needed
|
|
51
|
+
* (e.g. for snapshot testing).
|
|
52
|
+
*/
|
|
53
|
+
declare const generateAuthUrl: ({ authUrl, clientId, scopes, stateQueryParam, codeChallenge, }: GenerateAuthUrlProps) => string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generates random state to be passed for anti-csrf.
|
|
57
|
+
*
|
|
58
|
+
* Extracted from the rest of the OAuth flow so consumers (or tests) can
|
|
59
|
+
* substitute a deterministic implementation when a stable state value is
|
|
60
|
+
* needed (e.g. for snapshot testing).
|
|
61
|
+
*/
|
|
62
|
+
declare function generateRandomState(lengthOfState: number): string;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Subset of the wrangler `logger` singleton used by the OAuth flow.
|
|
66
|
+
* Consumers pass in an implementation that maps to their own logging surface.
|
|
67
|
+
*/
|
|
68
|
+
interface OAuthFlowLogger {
|
|
69
|
+
debug(...args: unknown[]): void;
|
|
70
|
+
info(...args: unknown[]): void;
|
|
71
|
+
log(...args: unknown[]): void;
|
|
72
|
+
warn(...args: unknown[]): void;
|
|
73
|
+
error(...args: unknown[]): void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Dependency-injection surface for {@link createOAuthFlow}.
|
|
77
|
+
*
|
|
78
|
+
* The OAuth flow only talks to OAuth endpoints (`/oauth2/auth`, `/oauth2/token`,
|
|
79
|
+
* `/oauth2/revoke`) using `undici`'s `fetch` directly — there is no Cloudflare
|
|
80
|
+
* API client wired into this context.
|
|
81
|
+
*/
|
|
82
|
+
interface OAuthFlowContext {
|
|
83
|
+
logger: OAuthFlowLogger;
|
|
84
|
+
/**
|
|
85
|
+
* Whether the process should not prompt the user. The OAuth flow uses this to
|
|
86
|
+
* decide whether to short-circuit interactive login attempts.
|
|
87
|
+
*/
|
|
88
|
+
isNonInteractiveOrCI: () => boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Open the given URL in the user's default browser. Called during the
|
|
91
|
+
* interactive OAuth login flow with the authorize URL.
|
|
92
|
+
*/
|
|
93
|
+
openInBrowser: (url: string) => Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Whether environment-based credentials are present. When `true`, the OAuth
|
|
96
|
+
* flow short-circuits because the env credentials take priority over stored
|
|
97
|
+
* OAuth tokens:
|
|
98
|
+
* - `login` refuses to start
|
|
99
|
+
* - `logout` is a no-op (it cannot revoke env credentials)
|
|
100
|
+
* - the refresh check returns `false` so an expired stored OAuth token does
|
|
101
|
+
* not trigger a needless refresh attempt
|
|
102
|
+
*/
|
|
103
|
+
hasEnvCredentials: () => boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Called after a successful `login` or `logout` so the consumer can invalidate
|
|
106
|
+
* any caches that depend on the active token (e.g. wrangler's selected-account
|
|
107
|
+
* cache).
|
|
108
|
+
*/
|
|
109
|
+
purgeOnLoginOrLogout?: () => void;
|
|
110
|
+
/**
|
|
111
|
+
* Override the OAuth authorize URL generator. Used by tests to produce a
|
|
112
|
+
* deterministic URL for snapshot testing. Defaults to the standard
|
|
113
|
+
* implementation.
|
|
114
|
+
*/
|
|
115
|
+
generateAuthUrl?: typeof generateAuthUrl;
|
|
116
|
+
/**
|
|
117
|
+
* Override the random state generator. Used by tests to produce a
|
|
118
|
+
* deterministic state value for snapshot testing. Defaults to the standard
|
|
119
|
+
* implementation.
|
|
120
|
+
*/
|
|
121
|
+
generateRandomState?: typeof generateRandomState;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Clear internal caches. Exported for use in tests only.
|
|
126
|
+
*/
|
|
127
|
+
declare function clearAccessCaches(): void;
|
|
128
|
+
/**
|
|
129
|
+
* Probe a domain to detect whether it is sitting behind Cloudflare Access.
|
|
130
|
+
*
|
|
131
|
+
* A 302 to `cloudflareaccess.com` is the canonical signal. Service-auth-only
|
|
132
|
+
* Access applications return a hard 403 instead and are therefore not detected
|
|
133
|
+
* here — see {@link getAccessHeaders} for how this is handled.
|
|
134
|
+
*/
|
|
135
|
+
declare function domainUsesAccess(domain: string, logger: OAuthFlowLogger): Promise<boolean>;
|
|
136
|
+
/**
|
|
137
|
+
* Get the headers needed to authenticate with an Access-protected domain.
|
|
138
|
+
*
|
|
139
|
+
* @param domain The hostname of the Access-protected domain (e.g. `"example.com"`).
|
|
140
|
+
* @param options logger + an `isNonInteractiveOrCI` predicate used to
|
|
141
|
+
* produce an actionable error in CI; both default to no-op / `false`.
|
|
142
|
+
* @returns
|
|
143
|
+
* - Service token headers (`CF-Access-Client-Id` + `CF-Access-Client-Secret`) if env vars are set
|
|
144
|
+
* - A `Cookie: CF_Authorization=...` header if obtained via `cloudflared` (interactive only)
|
|
145
|
+
* - An empty object if the domain is not behind Access
|
|
146
|
+
* @throws {UserError} If the response does not contain a `CF_Authorization` cookie,
|
|
147
|
+
* indicating the service token is invalid, expired, or lacks a Service Auth policy.
|
|
148
|
+
* Also throws in non-interactive environments when the domain is behind Access
|
|
149
|
+
* but no service token credentials are configured.
|
|
150
|
+
*/
|
|
151
|
+
declare function getAccessHeaders(domain: string, options: {
|
|
152
|
+
logger: OAuthFlowLogger;
|
|
153
|
+
isNonInteractiveOrCI: () => boolean;
|
|
154
|
+
}): Promise<Record<string, string>>;
|
|
155
|
+
/**
|
|
156
|
+
* Get headers needed to authenticate with the Cloudflare OAuth auth domain
|
|
157
|
+
* (the OAuth `WRANGLER_AUTH_DOMAIN`, which is `dash.cloudflare.com` by default
|
|
158
|
+
* and `dash.staging.cloudflare.com` in staging).
|
|
159
|
+
*
|
|
160
|
+
* Checks `WRANGLER_CF_AUTHORIZATION_TOKEN` first, then falls back to
|
|
161
|
+
* {@link getAccessHeaders} against the configured auth domain.
|
|
162
|
+
*/
|
|
163
|
+
declare function getCloudflareAccessHeaders(options: {
|
|
164
|
+
logger: OAuthFlowLogger;
|
|
165
|
+
isNonInteractiveOrCI: () => boolean;
|
|
166
|
+
}): Promise<Record<string, string>>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* `WRANGLER_CLIENT_ID` is a UUID that is used to identify Wrangler
|
|
170
|
+
* to the Cloudflare APIs.
|
|
171
|
+
*
|
|
172
|
+
* Normally you should not need to set this explicitly.
|
|
173
|
+
* If you want to switch to the staging environment set the
|
|
174
|
+
* `WRANGLER_API_ENVIRONMENT=staging` environment variable instead.
|
|
175
|
+
*/
|
|
176
|
+
declare const getClientIdFromEnv: () => string;
|
|
177
|
+
/**
|
|
178
|
+
* `WRANGLER_AUTH_DOMAIN` is the URL base domain that is used
|
|
179
|
+
* to access OAuth URLs for the Cloudflare APIs.
|
|
180
|
+
*
|
|
181
|
+
* Normally you should not need to set this explicitly.
|
|
182
|
+
* If you want to switch to the staging environment set the
|
|
183
|
+
* `WRANGLER_API_ENVIRONMENT=staging` environment variable instead.
|
|
184
|
+
*/
|
|
185
|
+
declare const getAuthDomainFromEnv: () => string;
|
|
186
|
+
/**
|
|
187
|
+
* `WRANGLER_AUTH_URL` is the path that is used to access OAuth
|
|
188
|
+
* for the Cloudflare APIs.
|
|
189
|
+
*
|
|
190
|
+
* Normally you should not need to set this explicitly.
|
|
191
|
+
* If you want to switch to the staging environment set the
|
|
192
|
+
* `WRANGLER_API_ENVIRONMENT=staging` environment variable instead.
|
|
193
|
+
*/
|
|
194
|
+
declare const getAuthUrlFromEnv: () => string;
|
|
195
|
+
/**
|
|
196
|
+
* `WRANGLER_TOKEN_URL` is the path that is used to exchange an OAuth
|
|
197
|
+
* token for an API token.
|
|
198
|
+
*
|
|
199
|
+
* Normally you should not need to set this explicitly.
|
|
200
|
+
* If you want to switch to the staging environment set the
|
|
201
|
+
* `WRANGLER_API_ENVIRONMENT=staging` environment variable instead.
|
|
202
|
+
*/
|
|
203
|
+
declare const getTokenUrlFromEnv: () => string;
|
|
204
|
+
/**
|
|
205
|
+
* `WRANGLER_REVOKE_URL` is the path that is used to exchange an OAuth
|
|
206
|
+
* refresh token for a new OAuth token.
|
|
207
|
+
*
|
|
208
|
+
* Normally you should not need to set this explicitly.
|
|
209
|
+
* If you want to switch to the staging environment set the
|
|
210
|
+
* `WRANGLER_API_ENVIRONMENT=staging` environment variable instead.
|
|
211
|
+
*/
|
|
212
|
+
declare const getRevokeUrlFromEnv: () => string;
|
|
213
|
+
/**
|
|
214
|
+
* `CLOUDFLARE_ACCESS_CLIENT_ID` is the Client ID of a Cloudflare Access Service Token.
|
|
215
|
+
* Used together with `CLOUDFLARE_ACCESS_CLIENT_SECRET` to authenticate with
|
|
216
|
+
* Access-protected domains in non-interactive environments (e.g. CI).
|
|
217
|
+
*
|
|
218
|
+
* @see https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/
|
|
219
|
+
*/
|
|
220
|
+
declare const getAccessClientIdFromEnv: () => string | undefined;
|
|
221
|
+
/**
|
|
222
|
+
* `CLOUDFLARE_ACCESS_CLIENT_SECRET` is the Client Secret of a Cloudflare Access Service Token.
|
|
223
|
+
* Used together with `CLOUDFLARE_ACCESS_CLIENT_ID` to authenticate with
|
|
224
|
+
* Access-protected domains in non-interactive environments (e.g. CI).
|
|
225
|
+
*
|
|
226
|
+
* @see https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/
|
|
227
|
+
*/
|
|
228
|
+
declare const getAccessClientSecretFromEnv: () => string | undefined;
|
|
229
|
+
/**
|
|
230
|
+
* `WRANGLER_CF_AUTHORIZATION_TOKEN` is an explicit `CF_Authorization` cookie value
|
|
231
|
+
* used to authenticate against the OAuth auth domain when it is Access-protected
|
|
232
|
+
* (typically staging). When set, the OAuth flow skips Access detection and uses
|
|
233
|
+
* this token directly.
|
|
234
|
+
*/
|
|
235
|
+
declare const getCfAuthorizationTokenFromEnv: () => string | undefined;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* A list of OAuth2AuthCodePKCE errors.
|
|
239
|
+
*/
|
|
240
|
+
declare class ErrorOAuth2 extends UserError {
|
|
241
|
+
toString(): string;
|
|
242
|
+
}
|
|
243
|
+
declare class ErrorUnknown extends UserError {
|
|
244
|
+
toString(): string;
|
|
245
|
+
}
|
|
246
|
+
declare class ErrorNoAuthCode extends ErrorOAuth2 {
|
|
247
|
+
toString(): string;
|
|
248
|
+
}
|
|
249
|
+
declare class ErrorInvalidReturnedStateParam extends ErrorOAuth2 {
|
|
250
|
+
toString(): string;
|
|
251
|
+
}
|
|
252
|
+
declare class ErrorInvalidJson extends ErrorOAuth2 {
|
|
253
|
+
toString(): string;
|
|
254
|
+
}
|
|
255
|
+
declare class ErrorInvalidScope extends ErrorOAuth2 {
|
|
256
|
+
toString(): string;
|
|
257
|
+
}
|
|
258
|
+
declare class ErrorInvalidRequest extends ErrorOAuth2 {
|
|
259
|
+
toString(): string;
|
|
260
|
+
}
|
|
261
|
+
declare class ErrorInvalidToken extends ErrorOAuth2 {
|
|
262
|
+
toString(): string;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Possible authorization grant errors given by the redirection from the
|
|
266
|
+
* authorization server.
|
|
267
|
+
*/
|
|
268
|
+
declare class ErrorAuthenticationGrant extends ErrorOAuth2 {
|
|
269
|
+
toString(): string;
|
|
270
|
+
}
|
|
271
|
+
declare class ErrorUnauthorizedClient extends ErrorAuthenticationGrant {
|
|
272
|
+
toString(): string;
|
|
273
|
+
}
|
|
274
|
+
declare class ErrorAccessDenied extends ErrorAuthenticationGrant {
|
|
275
|
+
toString(): string;
|
|
276
|
+
}
|
|
277
|
+
declare class ErrorUnsupportedResponseType extends ErrorAuthenticationGrant {
|
|
278
|
+
toString(): string;
|
|
279
|
+
}
|
|
280
|
+
declare class ErrorServerError extends ErrorAuthenticationGrant {
|
|
281
|
+
toString(): string;
|
|
282
|
+
}
|
|
283
|
+
declare class ErrorTemporarilyUnavailable extends ErrorAuthenticationGrant {
|
|
284
|
+
toString(): string;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* A list of possible access token response errors.
|
|
288
|
+
*/
|
|
289
|
+
declare class ErrorAccessTokenResponse extends ErrorOAuth2 {
|
|
290
|
+
toString(): string;
|
|
291
|
+
}
|
|
292
|
+
declare class ErrorInvalidClient extends ErrorAccessTokenResponse {
|
|
293
|
+
toString(): string;
|
|
294
|
+
}
|
|
295
|
+
declare class ErrorInvalidGrant extends ErrorAccessTokenResponse {
|
|
296
|
+
toString(): string;
|
|
297
|
+
}
|
|
298
|
+
declare class ErrorUnsupportedGrantType extends ErrorAccessTokenResponse {
|
|
299
|
+
toString(): string;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Translate the raw error strings returned from the server into error classes.
|
|
303
|
+
*/
|
|
304
|
+
declare function toErrorClass(rawError: string): ErrorOAuth2 | ErrorUnknown;
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Options for an interactive OAuth login.
|
|
308
|
+
*/
|
|
309
|
+
interface LoginProps {
|
|
310
|
+
complianceConfig: ComplianceConfig;
|
|
311
|
+
/**
|
|
312
|
+
* The OAuth scopes to request. The catalog of valid scope keys is
|
|
313
|
+
* consumer-defined; this package treats scopes as opaque strings.
|
|
314
|
+
*/
|
|
315
|
+
scopes: string[];
|
|
316
|
+
/**
|
|
317
|
+
* Whether to open the authorize URL in a browser. Defaults to `true`.
|
|
318
|
+
* When `false` the URL is printed for the user to copy/paste.
|
|
319
|
+
*/
|
|
320
|
+
browser?: boolean;
|
|
321
|
+
/** Host the local callback server listens on. Defaults to `localhost`. */
|
|
322
|
+
callbackHost?: string;
|
|
323
|
+
/** Port the local callback server listens on. Defaults to `8976`. */
|
|
324
|
+
callbackPort?: number;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Public surface returned by {@link createOAuthFlow}.
|
|
328
|
+
*/
|
|
329
|
+
interface OAuthFlowAPI {
|
|
330
|
+
/**
|
|
331
|
+
* Open the authorize URL in the user's browser, wait for the callback to be
|
|
332
|
+
* hit on the local HTTP server, exchange the code for an access token, and
|
|
333
|
+
* persist the result to disk.
|
|
334
|
+
*
|
|
335
|
+
* Refuses to start when `ctx.hasEnvCredentials()` returns `true`.
|
|
336
|
+
* Refuses to start when the compliance region is `fedramp_high`.
|
|
337
|
+
*
|
|
338
|
+
* @returns `true` on success, `false` when env credentials are present.
|
|
339
|
+
*/
|
|
340
|
+
login(props: LoginProps): Promise<boolean>;
|
|
341
|
+
/**
|
|
342
|
+
* Revoke the stored refresh token at the Cloudflare OAuth endpoint and
|
|
343
|
+
* delete the on-disk auth config file.
|
|
344
|
+
*
|
|
345
|
+
* No-op when `ctx.hasEnvCredentials()` returns `true` (env credentials
|
|
346
|
+
* cannot be revoked).
|
|
347
|
+
*/
|
|
348
|
+
logout(): Promise<void>;
|
|
349
|
+
/**
|
|
350
|
+
* If the user has no stored OAuth token, attempt an interactive login.
|
|
351
|
+
* If they have one but it is expired, attempt a refresh; if refresh fails,
|
|
352
|
+
* fall back to an interactive login.
|
|
353
|
+
*
|
|
354
|
+
* Scopes are required in case an interactive login is triggered — the
|
|
355
|
+
* consumer's scope catalog lives outside this package.
|
|
356
|
+
*
|
|
357
|
+
* @returns `true` when the user is logged in (or env credentials are
|
|
358
|
+
* present), `false` when interactive login was needed but skipped (e.g.
|
|
359
|
+
* non-interactive environment).
|
|
360
|
+
*/
|
|
361
|
+
loginOrRefreshIfRequired(props: LoginProps): Promise<boolean>;
|
|
362
|
+
/**
|
|
363
|
+
* Read the OAuth access token from local state, refreshing it first if
|
|
364
|
+
* needed. Returns `undefined` when there is no stored OAuth token or the
|
|
365
|
+
* refresh fails.
|
|
366
|
+
*
|
|
367
|
+
* This intentionally does NOT consult env credentials — callers that want
|
|
368
|
+
* env-or-OAuth resolution should check env first themselves.
|
|
369
|
+
*/
|
|
370
|
+
getOAuthTokenFromLocalState(): Promise<string | undefined>;
|
|
371
|
+
/**
|
|
372
|
+
* Whether the stored OAuth access token has expired and a refresh is
|
|
373
|
+
* required before it can be used. Returns `false` when env credentials are
|
|
374
|
+
* present (per `ctx.hasEnvCredentials`), because the stored OAuth state is
|
|
375
|
+
* not consulted in that case.
|
|
376
|
+
*/
|
|
377
|
+
isRefreshNeeded(): boolean;
|
|
378
|
+
/**
|
|
379
|
+
* Trigger an OAuth refresh-token rotation. Persists the new access/refresh
|
|
380
|
+
* tokens to disk on success. Returns `false` on any failure.
|
|
381
|
+
*/
|
|
382
|
+
refreshToken(): Promise<boolean>;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Build an instance of the OAuth flow bound to the given context.
|
|
386
|
+
*
|
|
387
|
+
* The returned object owns module-private state (the transient OAuth flow
|
|
388
|
+
* state and the deprecated-v1 warning latch). In practice consumers create
|
|
389
|
+
* exactly one instance per process.
|
|
390
|
+
*/
|
|
391
|
+
declare function createOAuthFlow(ctx: OAuthFlowContext): OAuthFlowAPI;
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* The maximum length for a code verifier for the best security we can offer.
|
|
395
|
+
* Please note the NOTE section of RFC 7636 § 4.1 - the length must be >= 43,
|
|
396
|
+
* but <= 128, **after** base64 url encoding. This means 32 code verifier bytes
|
|
397
|
+
* encoded will be 43 bytes, or 96 bytes encoded will be 128 bytes. So 96 bytes
|
|
398
|
+
* is the highest valid value that can be used.
|
|
399
|
+
*/
|
|
400
|
+
declare const RECOMMENDED_CODE_VERIFIER_LENGTH = 96;
|
|
401
|
+
/**
|
|
402
|
+
* A sensible length for the state's length, for anti-csrf.
|
|
403
|
+
*/
|
|
404
|
+
declare const RECOMMENDED_STATE_LENGTH = 32;
|
|
405
|
+
/**
|
|
406
|
+
* Character set to generate code verifier defined in rfc7636.
|
|
407
|
+
*/
|
|
408
|
+
declare const PKCE_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
409
|
+
interface PKCECodes {
|
|
410
|
+
codeChallenge: string;
|
|
411
|
+
codeVerifier: string;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Implements *base64url-encode* (RFC 4648 § 5) without padding, which is NOT
|
|
415
|
+
* the same as regular base64 encoding.
|
|
416
|
+
*/
|
|
417
|
+
declare function base64urlEncode(value: string): string;
|
|
418
|
+
/**
|
|
419
|
+
* Generates a code_verifier and code_challenge, as specified in rfc7636.
|
|
420
|
+
*/
|
|
421
|
+
declare function generatePKCECodes(): Promise<PKCECodes>;
|
|
422
|
+
|
|
423
|
+
interface RefreshToken {
|
|
424
|
+
value: string;
|
|
425
|
+
}
|
|
426
|
+
interface AccessToken {
|
|
427
|
+
value: string;
|
|
428
|
+
expiry: string;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Transient state that is shared across the steps of a single OAuth login flow
|
|
432
|
+
* within one Wrangler command. This state is not file-backed; it lives only for
|
|
433
|
+
* the duration of an interactive login.
|
|
434
|
+
*/
|
|
435
|
+
interface OAuthFlowState {
|
|
436
|
+
authorizationCode?: string;
|
|
437
|
+
codeChallenge?: string;
|
|
438
|
+
codeVerifier?: string;
|
|
439
|
+
hasAuthCodeBeenExchangedForAccessToken?: boolean;
|
|
440
|
+
stateQueryParam?: string;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* The auth state that is stored on disk in the user auth config file (TOML).
|
|
444
|
+
* Read on demand by {@link readStoredAuthState} — never cached at module scope
|
|
445
|
+
* so that environment variables loaded after import (e.g. from `.env`) take
|
|
446
|
+
* priority correctly.
|
|
447
|
+
*/
|
|
448
|
+
interface StoredAuthState {
|
|
449
|
+
accessToken?: AccessToken;
|
|
450
|
+
refreshToken?: RefreshToken;
|
|
451
|
+
scopes?: string[];
|
|
452
|
+
/** @deprecated - this field was only provided by the deprecated v1 `wrangler config` command. */
|
|
453
|
+
deprecatedApiToken?: string;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Read the on-disk auth state. Called on demand from every site that needs the
|
|
457
|
+
* stored OAuth tokens or the deprecated v1 `api_token`, rather than being
|
|
458
|
+
* cached at module scope, so that environment-based credentials loaded after
|
|
459
|
+
* module import are honoured by the rest of wrangler.
|
|
460
|
+
*
|
|
461
|
+
* @return an empty object when no auth config file exists or the file cannot
|
|
462
|
+
* be parsed — the caller treats this as "not logged in via local OAuth".
|
|
463
|
+
*
|
|
464
|
+
* @param options.configOverride seed the state from an in-memory config (used by
|
|
465
|
+
* the OAuth login flow before it writes to disk).
|
|
466
|
+
* @param options.warningLogger if provided, a one-time warning is emitted when a
|
|
467
|
+
* deprecated v1 `api_token` is found on disk. Pass the consumer's logger (e.g.
|
|
468
|
+
* wrangler's logger singleton) to surface this to the user.
|
|
469
|
+
*/
|
|
470
|
+
declare function readStoredAuthState(options?: {
|
|
471
|
+
configOverride?: UserAuthConfig;
|
|
472
|
+
warningLogger?: Pick<OAuthFlowLogger, "warn">;
|
|
473
|
+
}): StoredAuthState;
|
|
474
|
+
|
|
475
|
+
export { type AccessToken, ErrorAccessDenied, ErrorAccessTokenResponse, ErrorAuthenticationGrant, ErrorInvalidClient, ErrorInvalidGrant, ErrorInvalidJson, ErrorInvalidRequest, ErrorInvalidReturnedStateParam, ErrorInvalidScope, ErrorInvalidToken, ErrorNoAuthCode, ErrorOAuth2, ErrorServerError, ErrorTemporarilyUnavailable, ErrorUnauthorizedClient, ErrorUnknown, ErrorUnsupportedGrantType, ErrorUnsupportedResponseType, type LoginProps, OAUTH_CALLBACK_URL, type OAuthFlowAPI, type OAuthFlowContext, type OAuthFlowLogger, type OAuthFlowState, type PKCECodes, PKCE_CHARSET, RECOMMENDED_CODE_VERIFIER_LENGTH, RECOMMENDED_STATE_LENGTH, type RefreshToken, type StoredAuthState, type UserAuthConfig, base64urlEncode, clearAccessCaches, createOAuthFlow, domainUsesAccess, generateAuthUrl, generatePKCECodes, generateRandomState, getAccessClientIdFromEnv, getAccessClientSecretFromEnv, getAccessHeaders, getAuthConfigFilePath, getAuthDomainFromEnv, getAuthUrlFromEnv, getCfAuthorizationTokenFromEnv, getClientIdFromEnv, getCloudflareAccessHeaders, getRevokeUrlFromEnv, getTokenUrlFromEnv, readAuthConfigFile, readStoredAuthState, toErrorClass, writeAuthConfigFile };
|