-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathauth-config.ts
More file actions
209 lines (188 loc) · 5.43 KB
/
auth-config.ts
File metadata and controls
209 lines (188 loc) · 5.43 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
198
199
200
201
202
203
204
205
206
207
208
209
import { apiClient } from "../../lib/apiClient.js";
import logger from "../../logger.js";
import { getA11yBaseURL } from "../../lib/a11y-base-url.js";
import { BrowserStackConfig } from "../../lib/types.js";
export interface AuthConfigResponse {
success: boolean;
data?: {
id: number;
name: string;
type: string;
username?: string;
password?: string;
url?: string;
usernameSelector?: string;
passwordSelector?: string;
submitSelector?: string;
};
errors?: string[];
}
export interface FormAuthData {
username: string;
usernameSelector: string;
password: string;
passwordSelector: string;
submitSelector: string;
url: string;
}
export interface BasicAuthData {
url: string;
username: string;
password: string;
}
export class AccessibilityAuthConfig {
private auth: { username: string; password: string } | undefined;
private config: BrowserStackConfig;
constructor(config: BrowserStackConfig) {
this.config = config;
}
public setAuth(auth: { username: string; password: string }): void {
this.auth = auth;
}
private transformLocalUrl(url: string): string {
try {
const parsed = new URL(url);
const localHosts = new Set(["127.0.0.1", "localhost", "0.0.0.0"]);
const BS_LOCAL_DOMAIN = "bs-local.com";
if (localHosts.has(parsed.hostname)) {
parsed.hostname = BS_LOCAL_DOMAIN;
return parsed.toString();
}
return url;
} catch {
return url;
}
}
async createFormAuthConfig(
name: string,
authData: FormAuthData,
): Promise<AuthConfigResponse> {
if (!this.auth?.username || !this.auth?.password) {
throw new Error(
"BrowserStack credentials are not set for AccessibilityAuthConfig.",
);
}
const transformedAuthData = {
...authData,
url: this.transformLocalUrl(authData.url),
};
const requestBody = {
name,
type: "form",
authData: transformedAuthData,
};
try {
const response = await apiClient.post<AuthConfigResponse>({
url: "https://api-accessibility.browserstack.com/api/website-scanner/v1/auth_configs",
headers: {
Authorization:
"Basic " +
Buffer.from(`${this.auth.username}:${this.auth.password}`).toString(
"base64",
),
"Content-Type": "application/json",
},
body: requestBody,
});
const data = response.data;
logger.info(`The data returned from the API is: ${JSON.stringify(data)}`);
if (!data.success) {
throw new Error(
`Unable to create auth config: ${data.errors?.join(", ")}`,
);
}
return data;
} catch (err: any) {
logger.error(
`Error creating form auth config: ${JSON.stringify(err?.response?.data)}`,
);
const msg =
err?.response?.data?.error ||
err?.response?.data?.message ||
err?.message ||
String(err);
throw new Error(`Failed to create form auth config: ${msg}`);
}
}
async createBasicAuthConfig(
name: string,
authData: BasicAuthData,
): Promise<AuthConfigResponse> {
if (!this.auth?.username || !this.auth?.password) {
throw new Error(
"BrowserStack credentials are not set for AccessibilityAuthConfig.",
);
}
const transformedAuthData = {
...authData,
url: this.transformLocalUrl(authData.url),
};
const requestBody = {
name,
type: "basic",
authData: transformedAuthData,
};
try {
const response = await apiClient.post<AuthConfigResponse>({
url: "https://api-accessibility.browserstack.com/api/website-scanner/v1/auth_configs",
headers: {
Authorization:
"Basic " +
Buffer.from(`${this.auth.username}:${this.auth.password}`).toString(
"base64",
),
"Content-Type": "application/json",
},
body: requestBody,
});
const data = response.data;
if (!data.success) {
throw new Error(
`Unable to create auth config: ${data.errors?.join(", ")}`,
);
}
return data;
} catch (err: any) {
const msg =
err?.response?.data?.error ||
err?.response?.data?.message ||
err?.message ||
String(err);
throw new Error(`Failed to create basic auth config: ${msg}`);
}
}
async getAuthConfig(configId: number): Promise<AuthConfigResponse> {
if (!this.auth?.username || !this.auth?.password) {
throw new Error(
"BrowserStack credentials are not set for AccessibilityAuthConfig.",
);
}
try {
const baseUrl = await getA11yBaseURL(this.config);
const response = await apiClient.get<AuthConfigResponse>({
url: `${baseUrl}/api/website-scanner/v1/auth_configs/${configId}`,
headers: {
Authorization:
"Basic " +
Buffer.from(`${this.auth.username}:${this.auth.password}`).toString(
"base64",
),
},
});
const data = response.data;
if (!data.success) {
throw new Error(
`Unable to get auth config: ${data.errors?.join(", ")}`,
);
}
return data;
} catch (err: any) {
const msg =
err?.response?.data?.error ||
err?.response?.data?.message ||
err?.message ||
String(err);
throw new Error(`Failed to get auth config: ${msg}`);
}
}
}