@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,85 @@
1
+ import { a as ConfigStorage, A as AuthConfigStorage, U as UserAuthConfig, b as OAuthFlowContext, k as ProfileStore } from '../file-format-CWtMNfzC.mjs';
2
+ import { C as CliDescriptor, U as UserPreferences, A as AuthContext, a as CloudflareAuth, K as KeyringPreferenceContext, S as SetKeyringPreferenceResult } from '../keyring-preference-DMsAtawt.mjs';
3
+ export { b as CloudflareLoginProps } from '../keyring-preference-DMsAtawt.mjs';
4
+ import '@cloudflare/workers-utils';
5
+
6
+ /**
7
+ * A JSON-file-on-disk storage backend (cf's on-disk format), a thin
8
+ * `JSON`-bound wrapper over the CLI-agnostic {@link createFileStorage}.
9
+ */
10
+ declare function createJsonFileStorage<T extends object>(getPath: () => string): ConfigStorage<T>;
11
+ declare const getAuthConfigFilePath: (profile?: string) => string;
12
+ declare const getEncryptedAuthConfigFilePath: (profile?: string) => string;
13
+ declare const defaultAuthConfigStorage: (profile?: string) => AuthConfigStorage;
14
+ declare const readAuthConfigFile: (profile?: string) => UserAuthConfig | undefined;
15
+ declare const writeAuthConfigFile: (config: UserAuthConfig, profile?: string) => void;
16
+
17
+ declare const CF_SCOPES: readonly ["openid", "offline", "user:read", "account:read", "access:read", "access:write", "agw:read", "agw:run", "agw:write", "ai:read", "ai:write", "ai-search:read", "ai-search:run", "ai-search:write", "aiaudit:read", "aiaudit:write", "aig:read", "aig:write", "auditlogs:read", "browser:read", "browser:write", "cfone:read", "cfone:write", "cloudchamber:write", "connectivity:admin", "connectivity:bind", "connectivity:read", "constellation:write", "containers:write", "d1:write", "dex:read", "dex:write", "dns_analytics:read", "dns_records:edit", "dns_records:read", "dns_settings:read", "firstpartytags:write", "images:read", "images:write", "lb:edit", "lb:read", "logpush:read", "logpush:write", "mcp_portals:read", "mcp_portals:write", "notebook-examples:read", "notification:read", "notification:write", "pages:read", "pages:write", "pipelines:read", "pipelines:setup", "pipelines:write", "query_cache:write", "queues:write", "r2_catalog:write", "radar:read", "rag:read", "rag:write", "registrar:read", "registrar:write", "secrets_store:read", "secrets_store:write", "sso-connector:read", "sso-connector:write", "ssl_certs:write", "teams:pii", "teams:read", "teams:secure_location", "teams:write", "url_scanner:read", "url_scanner:write", "vectorize:write", "workers:read", "workers:write", "workers_builds:read", "workers_builds:write", "workers_deployments:read", "workers_kv:write", "workers_observability:read", "workers_observability:write", "workers_observability_telemetry:write", "workers_routes:write", "workers_scripts:write", "workers_tail:read", "zone:read"];
18
+ /**
19
+ * The possible keys for a cf Scope.
20
+ *
21
+ * "offline_access" is automatically included.
22
+ */
23
+ type Scope = (typeof CF_SCOPES)[number];
24
+ declare let DefaultScopeKeys: Scope[];
25
+ declare function setLoginScopeKeys(scopes: Scope[]): void;
26
+ declare function validateScopeKeys(scopes: string[]): scopes is typeof DefaultScopeKeys;
27
+
28
+ /**
29
+ * `CLOUDFLARE_CLIENT_ID` is the UUID of cf's registered OAuth app, used to
30
+ * identify the `cf` CLI to the Cloudflare OAuth server.
31
+ *
32
+ * Normally you should not need to set this explicitly. cf has a single
33
+ * registered OAuth app (there is no separate staging app), so the default is
34
+ * the same regardless of `WRANGLER_API_ENVIRONMENT`; override via the env var
35
+ * if a different app is ever needed.
36
+ */
37
+ declare const getClientIdFromEnv: () => string;
38
+
39
+ /**
40
+ * OS-keyring service identifier for the `cf` CLI. Distinct from wrangler's so
41
+ * the two CLIs' credentials never collide in the OS keychain. Becomes the `-s`
42
+ * arg to macOS `security`, the `service` attribute for Linux `secret-tool`, and
43
+ * the `service` arg to `@napi-rs/keyring` on Windows.
44
+ */
45
+ declare const CF_KEYRING_SERVICE_NAME = "cloudflare";
46
+ /**
47
+ * The `redirect_uri` registered on cf's OAuth app; also the local callback URL.
48
+ * cf uses the fixed local callback port 8877 (from its historical 8877–8886
49
+ * range).
50
+ */
51
+ declare const CF_OAUTH_CALLBACK_URL = "http://localhost:8877/oauth/callback";
52
+
53
+ /**
54
+ * The `cf` CLI's global config directory: `~/.config/cloudflare` (XDG) — no
55
+ * leading dot (unlike wrangler's `.wrangler`) and no legacy `~/.cloudflare`
56
+ * fallback, since `cf` is new and has no historical home-dir layout to be
57
+ * compatible with. Resolved lazily so `runInTempDir()` fixtures (which re-stub
58
+ * `HOME` / `XDG_CONFIG_HOME`) are honoured per call.
59
+ */
60
+ declare function getCfConfigPath(): string;
61
+
62
+ /**
63
+ * The cf {@link CliDescriptor}: JSON files under `~/.config/cloudflare`, cf's
64
+ * OAuth app / consent pages, the `"cloudflare"` keyring service, and cf-worded
65
+ * copy.
66
+ */
67
+ declare const CF_CLI: CliDescriptor;
68
+ declare const readUserPreferences: () => UserPreferences;
69
+ declare const updateUserPreferences: (update: Partial<UserPreferences>) => void;
70
+ /**
71
+ * Build cf's auth layer, injecting the few consumer primitives that can't move
72
+ * into the shared package (logger, interactive `prompt` / `select`, and the
73
+ * User-Agent string).
74
+ */
75
+ declare function createCfAuth(ctx: AuthContext): CloudflareAuth;
76
+ /** Consumer primitives {@link createCfProfileStore} needs. */
77
+ interface CfProfileStoreContext {
78
+ logger: OAuthFlowContext["logger"];
79
+ }
80
+ /** Build cf's {@link ProfileStore} (JSON `.json` / `.enc` files under `~/.config/cloudflare`). */
81
+ declare function createCfProfileStore(ctx: CfProfileStoreContext): ProfileStore;
82
+ /** Apply and persist cf's global keyring-storage preference. */
83
+ declare function setKeyringPreference(enabled: boolean, ctx: KeyringPreferenceContext): SetKeyringPreferenceResult;
84
+
85
+ export { AuthContext, CF_CLI, CF_KEYRING_SERVICE_NAME, CF_OAUTH_CALLBACK_URL, type CfProfileStoreContext, CliDescriptor, CloudflareAuth, DefaultScopeKeys, KeyringPreferenceContext, type Scope, SetKeyringPreferenceResult, UserPreferences, createCfAuth, createCfProfileStore, createJsonFileStorage, defaultAuthConfigStorage, getAuthConfigFilePath, getCfConfigPath, getClientIdFromEnv, getEncryptedAuthConfigFilePath, readAuthConfigFile, readUserPreferences, setKeyringPreference, setLoginScopeKeys, updateUserPreferences, validateScopeKeys, writeAuthConfigFile };
@@ -0,0 +1,212 @@
1
+ import { createAuthConfigFileHelpers, createPreferences, createKeyringPreference, createFileStorage, createCloudflareAuth, createCloudflareProfileStore } from '../chunk-WJE5I5WS.mjs';
2
+ import '../chunk-FX4KV7FS.mjs';
3
+ import { __name } from '../chunk-O6YSETKJ.mjs';
4
+ import { getEnvironmentVariableFactory, getGlobalConfigPath, getCloudflareApiEnvironmentFromEnv } from '@cloudflare/workers-utils';
5
+ import path from 'node:path';
6
+
7
+ // src/cf/constants.ts
8
+ var CF_KEYRING_SERVICE_NAME = "cloudflare";
9
+ var CF_CLI_NAME = "cf";
10
+ var CF_OAUTH_CALLBACK_URL = "http://localhost:8877/oauth/callback";
11
+ var CF_CONSENT_PAGES = {
12
+ granted: {
13
+ url: "https://welcome.developers.workers.dev/cf-oauth-consent-granted"
14
+ },
15
+ denied: {
16
+ url: "https://welcome.developers.workers.dev/cf-oauth-consent-denied",
17
+ error: "Error: Consent denied. You must grant consent to cf in order to login.\nIf you don't want to do this consider passing an API token via the `CLOUDFLARE_API_TOKEN` environment variable"
18
+ }
19
+ };
20
+ var getClientIdFromEnv = getEnvironmentVariableFactory({
21
+ variableName: "CLOUDFLARE_CLIENT_ID",
22
+ defaultValue: /* @__PURE__ */ __name(() => "cbca97e7-c331-4cdd-8fd8-e25a451b98bf", "defaultValue")
23
+ });
24
+ function getCfConfigPath() {
25
+ return getGlobalConfigPath({
26
+ appName: "cloudflare",
27
+ leadingDot: false,
28
+ useLegacyHomeDir: false
29
+ });
30
+ }
31
+ __name(getCfConfigPath, "getCfConfigPath");
32
+
33
+ // src/cf/scopes.ts
34
+ var CF_SCOPES = [
35
+ "openid",
36
+ "offline",
37
+ "user:read",
38
+ "account:read",
39
+ "access:read",
40
+ "access:write",
41
+ "agw:read",
42
+ "agw:run",
43
+ "agw:write",
44
+ "ai:read",
45
+ "ai:write",
46
+ "ai-search:read",
47
+ "ai-search:run",
48
+ "ai-search:write",
49
+ "aiaudit:read",
50
+ "aiaudit:write",
51
+ "aig:read",
52
+ "aig:write",
53
+ "auditlogs:read",
54
+ "browser:read",
55
+ "browser:write",
56
+ "cfone:read",
57
+ "cfone:write",
58
+ "cloudchamber:write",
59
+ "connectivity:admin",
60
+ "connectivity:bind",
61
+ "connectivity:read",
62
+ "constellation:write",
63
+ "containers:write",
64
+ "d1:write",
65
+ "dex:read",
66
+ "dex:write",
67
+ "dns_analytics:read",
68
+ "dns_records:edit",
69
+ "dns_records:read",
70
+ "dns_settings:read",
71
+ "firstpartytags:write",
72
+ "images:read",
73
+ "images:write",
74
+ "lb:edit",
75
+ "lb:read",
76
+ "logpush:read",
77
+ "logpush:write",
78
+ "mcp_portals:read",
79
+ "mcp_portals:write",
80
+ "notebook-examples:read",
81
+ "notification:read",
82
+ "notification:write",
83
+ "pages:read",
84
+ "pages:write",
85
+ "pipelines:read",
86
+ "pipelines:setup",
87
+ "pipelines:write",
88
+ "query_cache:write",
89
+ "queues:write",
90
+ "r2_catalog:write",
91
+ "radar:read",
92
+ "rag:read",
93
+ "rag:write",
94
+ "registrar:read",
95
+ "registrar:write",
96
+ "secrets_store:read",
97
+ "secrets_store:write",
98
+ "sso-connector:read",
99
+ "sso-connector:write",
100
+ "ssl_certs:write",
101
+ "teams:pii",
102
+ "teams:read",
103
+ "teams:secure_location",
104
+ "teams:write",
105
+ "url_scanner:read",
106
+ "url_scanner:write",
107
+ "vectorize:write",
108
+ "workers:read",
109
+ "workers:write",
110
+ "workers_builds:read",
111
+ "workers_builds:write",
112
+ "workers_deployments:read",
113
+ "workers_kv:write",
114
+ "workers_observability:read",
115
+ "workers_observability:write",
116
+ "workers_observability_telemetry:write",
117
+ "workers_routes:write",
118
+ "workers_scripts:write",
119
+ "workers_tail:read",
120
+ "zone:read"
121
+ ];
122
+ var DefaultScopeKeys = [...CF_SCOPES];
123
+ function setLoginScopeKeys(scopes) {
124
+ DefaultScopeKeys = scopes;
125
+ }
126
+ __name(setLoginScopeKeys, "setLoginScopeKeys");
127
+ var CF_SCOPE_SET = new Set(CF_SCOPES);
128
+ function validateScopeKeys(scopes) {
129
+ return scopes.every((scope) => CF_SCOPE_SET.has(scope));
130
+ }
131
+ __name(validateScopeKeys, "validateScopeKeys");
132
+ function getTemporaryAccountConfigFile() {
133
+ const environment = getCloudflareApiEnvironmentFromEnv();
134
+ return environment === "production" ? "cloudflare-temporary-account.json" : `cloudflare-temporary-account.${environment}.json`;
135
+ }
136
+ __name(getTemporaryAccountConfigFile, "getTemporaryAccountConfigFile");
137
+ function getCfTemporaryAccountConfigPath() {
138
+ return path.join(getCfConfigPath(), getTemporaryAccountConfigFile());
139
+ }
140
+ __name(getCfTemporaryAccountConfigPath, "getCfTemporaryAccountConfigPath");
141
+
142
+ // src/cf/auth-config-file.ts
143
+ function createJsonFileStorage(getPath) {
144
+ return createFileStorage("json", getPath);
145
+ }
146
+ __name(createJsonFileStorage, "createJsonFileStorage");
147
+ var {
148
+ getAuthConfigFilePath,
149
+ getEncryptedAuthConfigFilePath,
150
+ defaultAuthConfigStorage,
151
+ readAuthConfigFile,
152
+ writeAuthConfigFile
153
+ } = createAuthConfigFileHelpers({
154
+ getConfigPath: getCfConfigPath,
155
+ format: "json"
156
+ });
157
+
158
+ // src/cf/index.ts
159
+ var CF_CLI = {
160
+ cliName: CF_CLI_NAME,
161
+ commands: {
162
+ login: "cf auth login",
163
+ whoami: "cf auth whoami"
164
+ },
165
+ keyringServiceName: CF_KEYRING_SERVICE_NAME,
166
+ clientId: getClientIdFromEnv,
167
+ consent: CF_CONSENT_PAGES,
168
+ redirectUri: CF_OAUTH_CALLBACK_URL,
169
+ // cf only supports the scoped `CLOUDFLARE_API_TOKEN` env var, not the global
170
+ // API key + email pair, so the global-key resolution is disabled.
171
+ allowGlobalAuthKey: false,
172
+ getConfigPath: getCfConfigPath,
173
+ getTemporaryAccountConfigPath: getCfTemporaryAccountConfigPath,
174
+ fileFormat: "json",
175
+ accountCachePrefix: "cloudflare-account",
176
+ // cf gets its own cache dir (`.cache/cloudflare` / `.cloudflare/cache`) so
177
+ // `cf auth login`/`logout` never purges wrangler's shared cache.
178
+ cacheNamespace: "cloudflare",
179
+ // cf persists its default account in `config.json` (`defaults.accountId`);
180
+ // this label is surfaced in the "set account_id in your <file>" hint.
181
+ getConfigFileLabel: /* @__PURE__ */ __name(() => "cf config", "getConfigFileLabel"),
182
+ getDefaultScopeKeys: /* @__PURE__ */ __name(() => DefaultScopeKeys, "getDefaultScopeKeys")
183
+ };
184
+ var cfPreferences = createPreferences(getCfConfigPath);
185
+ var { readUserPreferences, updateUserPreferences } = cfPreferences;
186
+ function createCfAuth(ctx) {
187
+ return createCloudflareAuth(CF_CLI, ctx);
188
+ }
189
+ __name(createCfAuth, "createCfAuth");
190
+ function createCfProfileStore(ctx) {
191
+ return createCloudflareProfileStore({
192
+ logger: ctx.logger,
193
+ getConfigPath: getCfConfigPath,
194
+ keyringServiceName: CF_KEYRING_SERVICE_NAME,
195
+ format: "json"
196
+ });
197
+ }
198
+ __name(createCfProfileStore, "createCfProfileStore");
199
+ var cfKeyringPreference = createKeyringPreference({
200
+ loginCommand: CF_CLI.commands.login,
201
+ createProfileCommand: CF_CLI.commands.createProfile,
202
+ keyringServiceName: CF_KEYRING_SERVICE_NAME,
203
+ getConfigPath: getCfConfigPath,
204
+ format: "json",
205
+ preferences: cfPreferences
206
+ });
207
+ function setKeyringPreference(enabled, ctx) {
208
+ return cfKeyringPreference.setKeyringPreference(enabled, ctx);
209
+ }
210
+ __name(setKeyringPreference, "setKeyringPreference");
211
+
212
+ export { CF_CLI, CF_KEYRING_SERVICE_NAME, CF_OAUTH_CALLBACK_URL, DefaultScopeKeys, createCfAuth, createCfProfileStore, createJsonFileStorage, defaultAuthConfigStorage, getAuthConfigFilePath, getCfConfigPath, getClientIdFromEnv, getEncryptedAuthConfigFilePath, readAuthConfigFile, readUserPreferences, setKeyringPreference, setLoginScopeKeys, updateUserPreferences, validateScopeKeys, writeAuthConfigFile };