@cloudflare/workers-auth 0.4.2 → 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.
@@ -0,0 +1,189 @@
1
+ import { l as OAuthConsentPages, F as FileFormat, b as OAuthFlowContext, C as CredentialStore, d as LoginOrRefreshResult, U as UserAuthConfig } from './file-format-CWtMNfzC.mjs';
2
+ import { ApiCredentials, ComplianceConfig } from '@cloudflare/workers-utils';
3
+
4
+ /** Minimal shape of the interactive `select` prompt's options for account selection. */
5
+ interface AccountSelectOptions {
6
+ choices: {
7
+ title: string;
8
+ value: string;
9
+ }[];
10
+ }
11
+ /**
12
+ * Dependency-injection surface for {@link createCloudflareAuth}.
13
+ *
14
+ * Everything here is a consumer primitive that genuinely can't live in this
15
+ * package: the logger, the interactive prompts (`prompt` / `select`, whose
16
+ * implementations live in the consuming CLI, e.g. wrangler's `dialogs.ts`), and
17
+ * the User-Agent string. All the auth *logic* lives in the package and is
18
+ * parameterised by the {@link CliDescriptor} descriptor instead.
19
+ */
20
+ interface AuthContext {
21
+ /** The consumer's logger (drop-in for wrangler's logger singleton). */
22
+ logger: OAuthFlowContext["logger"];
23
+ /**
24
+ * User-Agent header sent with the account/membership REST calls
25
+ * (e.g. `wrangler/<version>` or `cf/<version>`).
26
+ */
27
+ userAgent: string;
28
+ /** The interactive text prompt, used for the temporary-preview-account terms. */
29
+ prompt: (question: string) => Promise<string>;
30
+ /** The interactive selection prompt. */
31
+ select: (text: string, options: AccountSelectOptions) => Promise<string>;
32
+ /**
33
+ * Whether the given error is the "no default value / non-interactive" signal
34
+ * thrown by {@link AuthContext.select} (wrangler's `NoDefaultValueProvided`).
35
+ */
36
+ isNoDefaultValueProvidedError: (error: unknown) => boolean;
37
+ }
38
+ /**
39
+ * Everything that varies between the Cloudflare CLIs that share this auth layer
40
+ * (wrangler, cf, …). The CLI-agnostic {@link createCloudflareAuth} factory
41
+ * reads every consumer-specific value from here, so a new CLI is a descriptor
42
+ * rather than a fork of the factory.
43
+ */
44
+ interface CliDescriptor {
45
+ /** CLI name used in user-facing messaging and keyring install-dir scoping (e.g. `"wrangler"`). */
46
+ cliName: string;
47
+ /** Commands surfaced in auth guidance. */
48
+ commands: {
49
+ login: string;
50
+ whoami: string;
51
+ createProfile?: string;
52
+ };
53
+ /**
54
+ * OS-keyring service identifier. Becomes the `-s` arg to macOS `security`,
55
+ * the `service` attribute for Linux `secret-tool`, and the `service` arg to
56
+ * `@napi-rs/keyring` on Windows. Distinct per CLI so credentials don't collide.
57
+ */
58
+ keyringServiceName: string;
59
+ /** The CLI's registered OAuth app client ID (or a lazy env-driven resolver). */
60
+ clientId: string | (() => string);
61
+ /** The CLI's branded OAuth consent pages. */
62
+ consent: OAuthConsentPages;
63
+ /** The `redirect_uri` registered on the CLI's OAuth app; also the local callback URL. */
64
+ redirectUri: string;
65
+ /**
66
+ * Whether Cloudflare Global API Key + email credentials are accepted.
67
+ * Defaults to `true`.
68
+ */
69
+ allowGlobalAuthKey?: boolean;
70
+ /** The CLI's global config directory, resolved lazily (re-read per call for tests). */
71
+ getConfigPath: () => string;
72
+ /** Absolute path to the temporary-preview-account cache file. */
73
+ getTemporaryAccountConfigPath: () => string;
74
+ /** On-disk file format for credentials / temporary-account / profile files. */
75
+ fileFormat: FileFormat;
76
+ /**
77
+ * Filename prefix for the cached account selection in the config cache
78
+ * (e.g. `"wrangler-account"` → `wrangler-account.json`). Always JSON.
79
+ */
80
+ accountCachePrefix: string;
81
+ /**
82
+ * Namespace for the config-cache directory, isolating each CLI's cache so
83
+ * one CLI's login/logout purge never wipes another's. Omit to use wrangler's
84
+ * shared cache dir (the historical default).
85
+ */
86
+ cacheNamespace?: string;
87
+ /**
88
+ * Label for the CLI's config file, used in "set `account_id` in your <file>"
89
+ * hints. A getter so it can reflect runtime config state (wrangler resolves
90
+ * this from `configFileName(undefined)`).
91
+ */
92
+ getConfigFileLabel: () => string;
93
+ /** The live default OAuth scope keys (a getter so any reassignment of the mutable `DefaultScopeKeys` binding is observed). */
94
+ getDefaultScopeKeys: () => string[];
95
+ }
96
+
97
+ /** Details for one of the user's accounts. */
98
+ type Account = {
99
+ id: string;
100
+ name: string;
101
+ };
102
+ /** The config surface {@link CloudflareAuth.requireAuth} needs: compliance settings plus an optional `account_id`. */
103
+ type RequireAuthConfig = ComplianceConfig & {
104
+ account_id?: string;
105
+ };
106
+ /** Overrides accepted by {@link CloudflareAuth.login} / {@link CloudflareAuth.loginOrRefreshIfRequired}. */
107
+ interface CloudflareLoginProps {
108
+ scopes?: string[];
109
+ browser?: boolean;
110
+ callbackHost?: string;
111
+ callbackPort?: number;
112
+ profile?: string;
113
+ }
114
+ /** A Cloudflare CLI's auth layer, returned by {@link createCloudflareAuth}. */
115
+ interface CloudflareAuth {
116
+ /** Set the active auth profile for all subsequent credential lookups. */
117
+ setProfile: (profile: string) => void;
118
+ /** Return the active auth profile name. */
119
+ getActiveProfile: () => string;
120
+ /** The currently-active credential store for the active profile. */
121
+ getCredentialStore: () => CredentialStore;
122
+ /** Mark whether `--temporary` is permitted for the current invocation. */
123
+ setTemporaryAllowed: (allowed: boolean) => void;
124
+ /** Resolve API credentials (env / temporary account / stored OAuth token). */
125
+ getAPIToken: () => ApiCredentials | undefined;
126
+ /** Throw an error if there is no API token available. */
127
+ requireApiToken: () => ApiCredentials;
128
+ /** Open the browser, complete the OAuth login, and persist the token. */
129
+ login: (complianceConfig: ComplianceConfig, props?: CloudflareLoginProps) => Promise<boolean>;
130
+ /** Revoke and delete the stored OAuth token. */
131
+ logout: (profile?: string) => Promise<void>;
132
+ /** Ensure the user is authenticated, refreshing or prompting as needed. */
133
+ loginOrRefreshIfRequired: (complianceConfig: ComplianceConfig, props?: CloudflareLoginProps) => Promise<LoginOrRefreshResult>;
134
+ /** Read the OAuth access token from local state, refreshing first if needed. */
135
+ getOAuthTokenFromLocalState: () => Promise<string | undefined>;
136
+ /** Scopes granted to the stored OAuth token, or `undefined` when not OAuth-logged-in. */
137
+ getScopes: () => string[] | undefined;
138
+ /** Read stored OAuth credentials via the active credential store. */
139
+ readAuthCredentials: () => UserAuthConfig | undefined;
140
+ /** Persist OAuth credentials via the active credential store. */
141
+ writeAuthCredentials: (config: UserAuthConfig) => void;
142
+ /** Returns the active account ID without side effects (config → env → cache). */
143
+ getActiveAccountId: (config: {
144
+ account_id?: string;
145
+ }) => string | undefined;
146
+ /** Resolve the account ID to use for API requests (fetching / prompting if needed). */
147
+ getOrSelectAccountId: (config: RequireAuthConfig) => Promise<string>;
148
+ /** Retrieve the cached account for the active profile, if any. */
149
+ getAccountFromCache: () => Account | undefined;
150
+ /** Fetch the accounts the current login auth can actually use. */
151
+ fetchAllAccounts: (complianceConfig: ComplianceConfig, options?: {
152
+ throwOnEmpty?: boolean;
153
+ }) => Promise<Account[]>;
154
+ /** Ensure the user is logged in and resolve a valid account ID. */
155
+ requireAuth: (config: RequireAuthConfig) => Promise<string>;
156
+ }
157
+
158
+ /**
159
+ * Persistent, user-level preferences for the authentication subsystem.
160
+ *
161
+ * Stored as `<global-config-path>/preferences.json` (always JSON, regardless of
162
+ * the CLI's credential file format). The preference is only consulted when
163
+ * the `CLOUDFLARE_AUTH_USE_KEYRING` environment variable is unset.
164
+ */
165
+ interface UserPreferences {
166
+ /**
167
+ * When `true`, OAuth credentials are stored in the OS keychain rather than
168
+ * the plaintext file. Set by `<cli> login --use-keyring` and cleared by
169
+ * `<cli> login --no-use-keyring`.
170
+ */
171
+ keyring_enabled?: boolean;
172
+ }
173
+
174
+ /**
175
+ * Result of {@link KeyringPreference.setKeyringPreference}: the preference
176
+ * actually persisted, which can differ from the requested value when enabling
177
+ * was rolled back because the keyring backend isn't usable on this host.
178
+ */
179
+ interface SetKeyringPreferenceResult {
180
+ enabled: boolean;
181
+ }
182
+ /** Consumer primitives {@link KeyringPreference.setKeyringPreference} needs. */
183
+ interface KeyringPreferenceContext {
184
+ logger: OAuthFlowContext["logger"];
185
+ /** The currently-active credential store for the active profile. */
186
+ getCredentialStore: () => CredentialStore;
187
+ }
188
+
189
+ export type { AuthContext as A, CliDescriptor as C, KeyringPreferenceContext as K, SetKeyringPreferenceResult as S, UserPreferences as U, CloudflareAuth as a, CloudflareLoginProps as b };