-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadAuthConfig.ts
More file actions
197 lines (173 loc) · 6.41 KB
/
Copy pathloadAuthConfig.ts
File metadata and controls
197 lines (173 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import path from "node:path";
import { pathToFileURL } from "node:url";
import { findAuthConfigFile } from "./findAuthConfigFile";
import type { AuthConfig, AuthConfigLoadResult, AuthProfileConfig } from "./types";
import { createUserError } from "../internal/userError";
function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function assertSafeProfileName(profile: string): void {
const trimmed = profile.trim();
if (trimmed.length === 0 || trimmed !== profile) {
throw createUserError(
`Invalid profile name "${profile}" (must not be empty or contain leading/trailing whitespace).`,
);
}
if (profile.includes("/") || profile.includes("\\") || profile.includes("..")) {
throw createUserError(
`Invalid profile name "${profile}" (must not contain path separators or "..").`,
);
}
}
function isAbsoluteHttpUrl(value: string): boolean {
return value.startsWith("http://") || value.startsWith("https://");
}
function assertProfileConfig(
profileName: string,
profile: unknown,
global: AuthConfig,
): asserts profile is AuthProfileConfig {
if (!isObject(profile)) {
throw createUserError(`Auth profile "${profileName}" must be an object.`);
}
if (typeof profile.login !== "function") {
throw createUserError(
`Auth profile "${profileName}" must define "login(page, ctx)".`,
);
}
if (typeof profile.validate !== "function") {
throw createUserError(
`Auth profile "${profileName}" must define "validate(page, ctx)" (required).`,
);
}
const hasValidateUrl =
typeof profile.validateUrl === "string" && profile.validateUrl.length > 0;
const hasProfileBaseUrl =
typeof profile.baseURL === "string" && profile.baseURL.length > 0;
const hasGlobalValidateUrl =
typeof global.validateUrl === "string" && global.validateUrl.length > 0;
const hasGlobalBaseUrl =
typeof global.baseURL === "string" && global.baseURL.length > 0;
if (!(hasValidateUrl || hasProfileBaseUrl || hasGlobalValidateUrl || hasGlobalBaseUrl)) {
throw createUserError(
`Auth profile "${profileName}" must set "validateUrl" (or a baseURL) either on the profile or in the root config.`,
);
}
// Playwright only allows relative navigation when baseURL is set.
// If validateUrl is relative (common), require baseURL at the profile or root level.
const effectiveValidateUrl =
(typeof profile.validateUrl === "string" && profile.validateUrl.length > 0
? profile.validateUrl
: undefined) ??
(typeof global.validateUrl === "string" && global.validateUrl.length > 0
? global.validateUrl
: undefined);
if (
effectiveValidateUrl &&
!isAbsoluteHttpUrl(effectiveValidateUrl) &&
!(hasProfileBaseUrl || hasGlobalBaseUrl)
) {
throw createUserError(
`Auth profile "${profileName}" uses a relative validateUrl "${effectiveValidateUrl}", but no baseURL is set (set profile.baseURL or root baseURL).`,
);
}
}
function assertAuthConfig(config: unknown): asserts config is AuthConfig {
if (!isObject(config)) {
throw createUserError(`Auth config must be an object (default export).`);
}
if (!isObject(config.profiles)) {
throw createUserError(`Auth config must define "profiles" as an object.`);
}
for (const profileName of Object.keys(config.profiles)) {
assertSafeProfileName(profileName);
}
if (config.webServer !== undefined) {
if (!isObject(config.webServer)) {
throw createUserError(`Auth config "webServer" must be an object.`);
}
if (typeof config.webServer.command !== "string" || config.webServer.command.length === 0) {
throw createUserError(`Auth config "webServer.command" must be a non-empty string.`);
}
if (
config.webServer.url !== undefined &&
(typeof config.webServer.url !== "string" || config.webServer.url.length === 0)
) {
throw createUserError(`Auth config "webServer.url" must be a non-empty string.`);
}
if (
config.webServer.url === undefined &&
!(typeof config.baseURL === "string" && config.baseURL.length > 0)
) {
throw createUserError(
`Auth config "webServer.url" is optional, but when omitted you must set root "baseURL".`,
);
}
if (
config.webServer.args !== undefined &&
!Array.isArray(config.webServer.args)
) {
throw createUserError(`Auth config "webServer.args" must be an array of strings.`);
}
if (config.webServer.env !== undefined) {
if (!isObject(config.webServer.env)) {
throw createUserError(
`Auth config "webServer.env" must be an object (key/value pairs) with string values.`,
);
}
for (const [key, value] of Object.entries(config.webServer.env)) {
if (typeof value !== "string") {
throw createUserError(
`Auth config "webServer.env.${key}" must be a string (got ${typeof value}).`,
);
}
}
}
}
if (
config.browser !== undefined &&
config.browser !== "chromium" &&
config.browser !== "firefox" &&
config.browser !== "webkit"
) {
throw createUserError(
`Auth config "browser" must be "chromium", "firefox", or "webkit".`,
);
}
const profileNames = Object.keys(config.profiles);
if (profileNames.length === 0) {
throw createUserError(
`Auth config must contain at least one profile in "profiles".`,
);
}
for (const name of profileNames) {
assertProfileConfig(name, config.profiles[name], config as unknown as AuthConfig);
}
}
export async function loadAuthConfig(options: {
cwd: string;
configPath?: string;
}): Promise<AuthConfigLoadResult> {
const configFilePath = options.configPath
? path.resolve(options.cwd, options.configPath)
: findAuthConfigFile(options.cwd);
if (!configFilePath) {
throw createUserError(
`Could not find a Playwright auth config. Create "playwright.auth.config.ts" in your project root or pass "--config <path>".`,
);
}
const configUrl = pathToFileURL(configFilePath).href;
const moduleExports: unknown = await import(configUrl);
const config = (moduleExports as { default?: unknown }).default;
if (!config) {
throw createUserError(
`Auth config at "${configFilePath}" must have a default export (e.g. "export default defineAuthConfig({...})").`,
);
}
assertAuthConfig(config);
return {
config,
configFilePath,
projectRoot: path.dirname(configFilePath),
};
}