dh-auth-library 2.0.1 → 2.1.1

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/dist/index.es.js CHANGED
@@ -1,120 +1,9 @@
1
1
  import { BasePage, LocalLoginPage, SessionManager, Url, getUrl, isRunningOnLocalhost, urls } from "nl-portal-playwright-libraries";
2
- import { expect, test } from "@playwright/test";
2
+ import { test } from "@playwright/test";
3
+ import { TOTP } from "totp-generator";
3
4
  import * as process$2 from "node:process";
4
5
  import process$1 from "node:process";
5
- import { TOTP } from "totp-generator";
6
6
  import * as path from "node:path";
7
- //#region src/auth/loginhandlers/localhostLoginHandler.ts
8
- var LocalhostLoginHandler = class {
9
- /**
10
- * @param page the Playwright page to perform the login on
11
- * @param account login details, supplied by the consumer of this library
12
- * @param file path of the storage state file to write the session to
13
- */
14
- async loginAndCreateSessionStorage(page, account, file) {
15
- await new LocalLoginPage(page).loginAndCreateNewSession(account, file);
16
- }
17
- };
18
- //#endregion
19
- //#region src/auth/pages/loginOverzichtPage.ts
20
- var LoginOverzichtPage = class extends BasePage {
21
- digidLoginButton;
22
- digidMachtigenLoginButton;
23
- eHerkenningLoginButton;
24
- eHerkenningMachtigingLoginButton;
25
- klantportaalCookieButton;
26
- constructor(page) {
27
- super(page);
28
- this.digidLoginButton = page.locator("#oidc-digid");
29
- this.digidMachtigenLoginButton = page.locator("#digid-machtigen");
30
- this.eHerkenningLoginButton = page.locator("#oidc-eHerkenning");
31
- this.eHerkenningMachtigingLoginButton = page.locator("#eherkenning-bewindvoering");
32
- this.klantportaalCookieButton = page.locator("#ppms_cm_agree-to-all");
33
- }
34
- /**
35
- * Clicks the button that starts the login flow. Not every consumer has the login overzicht
36
- * page (e.g. Open Forms), so the button to click can be supplied by the consumer.
37
- *
38
- * @param loginButton the button that starts the login flow
39
- */
40
- async clickLoginButton(loginButton) {
41
- await loginButton.waitFor();
42
- await this.page.waitForTimeout(2e3);
43
- await loginButton.click();
44
- }
45
- async clickDigidLoginOption() {
46
- await this.clickLoginButton(this.digidLoginButton);
47
- }
48
- async clickDigidMachtigenLoginOption() {
49
- await this.clickLoginButton(this.digidMachtigenLoginButton);
50
- }
51
- async clickEHerkenningLoginOption() {
52
- await this.eHerkenningLoginButton.waitFor();
53
- await this.page.waitForTimeout(2e3);
54
- try {
55
- await this.eHerkenningLoginButton.click();
56
- } catch (_error) {
57
- await this.reloadPage();
58
- await this.eHerkenningLoginButton.waitFor();
59
- await this.page.waitForTimeout(2e3);
60
- await this.eHerkenningLoginButton.click();
61
- }
62
- }
63
- async clickProfessionalBewindvoerderLoginOption() {
64
- await this.eHerkenningMachtigingLoginButton.waitFor();
65
- await this.page.waitForTimeout(2e3);
66
- try {
67
- await this.eHerkenningMachtigingLoginButton.click();
68
- } catch (_error) {
69
- await this.reloadPage();
70
- await this.eHerkenningMachtigingLoginButton.waitFor();
71
- await this.page.waitForTimeout(2e3);
72
- await this.eHerkenningMachtigingLoginButton.click();
73
- }
74
- }
75
- async verifyLoginOverviewIsVisible() {
76
- await expect(this.page.getByRole("heading", { name: "Inloggen voor mijzelf" })).toBeVisible();
77
- }
78
- /**
79
- * Accepts the cookie banner and waits until the consent is stored in a cookie; skipped when the
80
- * banner doesn't show within 10 seconds.
81
- *
82
- * @param cookieButton the button that accepts the cookie banner, defaults to the klantportaal banner
83
- */
84
- async acceptCookies(cookieButton = this.klantportaalCookieButton) {
85
- try {
86
- const cookiesBeforeAccepting = await this.cookiesAsText();
87
- await cookieButton.click({ timeout: 1e4 });
88
- await this.waitForCookiesToSettle(cookiesBeforeAccepting);
89
- } catch (_ignore) {}
90
- }
91
- /**
92
- * Waits until the cookies differ from `cookiesBefore` and have not changed for a second, at most 10 seconds.
93
- */
94
- async waitForCookiesToSettle(cookiesBefore) {
95
- const settledAfterMs = 1e3;
96
- const deadline = Date.now() + 1e4;
97
- let current = cookiesBefore;
98
- let changedAt = Date.now();
99
- while (Date.now() < deadline) {
100
- await this.page.waitForTimeout(250);
101
- const cookies = await this.cookiesAsText();
102
- if (cookies !== current) {
103
- current = cookies;
104
- changedAt = Date.now();
105
- } else if (current !== cookiesBefore && Date.now() - changedAt >= settledAfterMs) return;
106
- }
107
- }
108
- async cookiesAsText() {
109
- return (await this.page.context().cookies()).map((c) => `${c.domain}|${c.name}=${c.value}`).sort().join(";");
110
- }
111
- async reloadPage() {
112
- try {
113
- await this.page.reload();
114
- } catch (_error) {}
115
- }
116
- };
117
- //#endregion
118
7
  //#region src/auth/utils/maskedFill.ts
119
8
  function isEnvValue(value) {
120
9
  return value !== "" && Object.values(process.env).includes(value);
@@ -139,8 +28,219 @@ async function maskedFill(locator, value, propertyName, forceMask = false) {
139
28
  });
140
29
  }
141
30
  //#endregion
31
+ //#region src/auth/pages/adLoginPage.ts
32
+ var gzacUrlProd = "https://alg-zgw.denhaag.nl";
33
+ var gzacUrlAcp = "https://alg-zgw.acc.denhaag.nl";
34
+ var gzacUrlTest = "https://alg-zgw.test.denhaag.nl";
35
+ function escapeRegExp(value) {
36
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
37
+ }
38
+ var ADLoginPage = class ADLoginPage extends BasePage {
39
+ usernameInput;
40
+ passwordInput;
41
+ nextInput;
42
+ signInInput;
43
+ cannotUseAuthAppLink;
44
+ useVerificationCode;
45
+ codeInput;
46
+ verifyInput;
47
+ constructor(page) {
48
+ super(page);
49
+ this.usernameInput = page.locator("//input[@type='email']");
50
+ this.passwordInput = page.locator("//input[@name='passwd']");
51
+ this.nextInput = page.locator("//input[@type='submit']");
52
+ this.signInInput = page.locator("//input[@type='submit']");
53
+ this.cannotUseAuthAppLink = page.locator("//*[@id='signInAnotherWay']");
54
+ this.useVerificationCode = page.getByText(/Gebruik een verificatiecode|Use a verification code/);
55
+ this.codeInput = page.locator("//input[@name='otc']");
56
+ this.verifyInput = page.locator("//input[@type='submit']");
57
+ }
58
+ async openPage(path) {
59
+ await this.page.goto(path);
60
+ }
61
+ /**
62
+ * Login with the AD account so we can get behind the firewall
63
+ *
64
+ * @param account login details, supplied by the consumer of this library
65
+ * @param targetUrl URL to navigate to before logging in (the app behind the firewall),
66
+ * supplied by the consumer of this library — not every consumer has a GZAC account, so
67
+ * this library no longer assumes GZAC as the destination. The consumer is responsible
68
+ * for switching between environments (e.g. test/acc/prod) when building this URL; this
69
+ * library does not do that for you.
70
+ * @param fileName path of the storage state file to write the session to
71
+ *
72
+ * @example
73
+ * // consumer-side lookup, e.g. based on an `ENV` environment variable
74
+ * export const TARGET_URL_BY_ENVIRONMENT = {
75
+ * test: 'https://alg-zgw.test.denhaag.nl/',
76
+ * acc: 'https://alg-zgw.acc.denhaag.nl/',
77
+ * prod: 'https://alg-zgw.denhaag.nl/',
78
+ * } as const;
79
+ *
80
+ * const env = (process.env.ENV ?? 'test') as keyof typeof TARGET_URL_BY_ENVIRONMENT;
81
+ * await adLoginPage.loginWithADAccountAndCreateSession(account, TARGET_URL_BY_ENVIRONMENT[env], fileName);
82
+ */
83
+ async loginWithADAccountAndCreateSession(account, targetUrl, fileName) {
84
+ const adLoginPage = new ADLoginPage(this.page);
85
+ let targetOrigin;
86
+ try {
87
+ targetOrigin = new URL(targetUrl).origin;
88
+ } catch {
89
+ throw new Error(`AD login failed: targetUrl must be an absolute URL (e.g. "https://app.denhaag.nl/..."), got "${targetUrl}"`);
90
+ }
91
+ const isOnTargetApp = async () => this.page.waitForLoadState("networkidle").then(() => this.page.url().startsWith(targetOrigin), () => false);
92
+ let cachedAccountUsed = false;
93
+ let alreadyFullyAuthenticated = false;
94
+ await test.step("Fill AD username and submit", async () => {
95
+ await this.page.goto(targetUrl);
96
+ alreadyFullyAuthenticated = await isOnTargetApp();
97
+ if (alreadyFullyAuthenticated) return;
98
+ const cachedAccountTile = this.page.getByRole("button", { name: new RegExp(escapeRegExp(account.username), "i") });
99
+ await cachedAccountTile.or(adLoginPage.usernameInput).waitFor({
100
+ state: "visible",
101
+ timeout: 1e4
102
+ }).catch(() => {});
103
+ cachedAccountUsed = await cachedAccountTile.isVisible().catch(() => false);
104
+ if (cachedAccountUsed) {
105
+ await cachedAccountTile.click();
106
+ alreadyFullyAuthenticated = await isOnTargetApp();
107
+ } else {
108
+ await adLoginPage.usernameInput.waitFor();
109
+ await adLoginPage.usernameInput.fill(account.username);
110
+ await adLoginPage.nextInput.click();
111
+ await this.page.getByText("Uw wachtwoord gebruiken").click();
112
+ }
113
+ });
114
+ let sessionWasStale = false;
115
+ if (!alreadyFullyAuthenticated) {
116
+ await test.step("Fill AD password and submit", async () => {
117
+ if (!cachedAccountUsed) {
118
+ await maskedFill(adLoginPage.passwordInput, account.password, "password");
119
+ await adLoginPage.signInInput.click();
120
+ await this.page.waitForTimeout(1e3);
121
+ }
122
+ sessionWasStale = await this.page.getByText("Uw account is niet geverifieerd. Probeer het opnieuw").isVisible().catch(() => false);
123
+ });
124
+ await test.step("Complete MFA via TOTP", async () => {
125
+ if (!sessionWasStale) await adLoginPage.cannotUseAuthAppLink.click();
126
+ await adLoginPage.useVerificationCode.click();
127
+ const token = await TOTP.generate(`${process.env.AD_SECRET}`);
128
+ await maskedFill(adLoginPage.codeInput, token.otp, "otp", true);
129
+ await adLoginPage.verifyInput.click();
130
+ await adLoginPage.page.waitForTimeout(5e3);
131
+ });
132
+ }
133
+ await test.step("Save session state", async () => {
134
+ await adLoginPage.page.context().storageState({ path: fileName });
135
+ });
136
+ }
137
+ /**
138
+ * Determines the GZAC URL to log in against, based on the current environment.
139
+ *
140
+ * @deprecated GZAC-specific and no longer called internally by this library — pass your own
141
+ * target URL to {@link loginWithADAccountAndCreateSession} instead. Kept only so existing
142
+ * GZAC consumers can still derive this URL themselves during migration; will be removed in
143
+ * a future version.
144
+ *
145
+ * Derives the environment from `this.getBaseUrl()` (inherited from `BasePage`), which reads
146
+ * the `PORTAL_BASE_URL` environment variable.
147
+ */
148
+ getGzacUrl() {
149
+ const baseUrl = this.getBaseUrl();
150
+ if (/klantportaal-zgw.denhaag.nl./.exec(baseUrl)) return gzacUrlProd;
151
+ if (/.acc./.exec(baseUrl)) return gzacUrlAcp;
152
+ if (/.test./.exec(baseUrl)) return gzacUrlTest;
153
+ else throw new Error("Cannot find current environment for baseUrl: " + baseUrl);
154
+ }
155
+ };
156
+ //#endregion
157
+ //#region src/auth/loginhandlers/adLoginHandler.ts
158
+ var ADLoginHandler = class {
159
+ targetUrl;
160
+ /**
161
+ * @param targetUrl URL to navigate to before logging in (the app behind the firewall),
162
+ * supplied by the consumer of this library
163
+ */
164
+ constructor(targetUrl) {
165
+ this.targetUrl = targetUrl;
166
+ }
167
+ /**
168
+ * @param page the Playwright page to perform the login on
169
+ * @param account login details, supplied by the consumer of this library
170
+ * @param file path of the storage state file to write the session to
171
+ */
172
+ async loginAndCreateSessionStorage(page, account, file) {
173
+ await new ADLoginPage(page).loginWithADAccountAndCreateSession(account, this.targetUrl, file);
174
+ }
175
+ };
176
+ //#endregion
177
+ //#region src/auth/utils/sessionFileHelper.ts
178
+ var sessionsDir = process$2.env.SESSIONS_DIR ?? path.resolve(process$2.cwd(), "sessions");
179
+ var environment = process$2.env.ENV ?? "test";
180
+ /**
181
+ * Easy way to create the filenames for the sessions
182
+ */
183
+ var SessionFileHelper = class {
184
+ /**
185
+ * @param browser the browser instance the session file name is based on
186
+ * @param account login details, supplied by the consumer of this library; only `username` is used
187
+ */
188
+ static getLocalhostFileName(browser, account) {
189
+ return path.join(sessionsDir, `${browser.browserType().name()}-${environment}-${account.username}.json`);
190
+ }
191
+ /**
192
+ * @param browser the browser instance the session file name is based on
193
+ * @param account login details, supplied by the consumer of this library; only `username` is used
194
+ */
195
+ static getLocalhostFileNameBedrijf(browser, account) {
196
+ return path.join(sessionsDir, `${browser.browserType().name()}-${environment}-${account.username}.json`);
197
+ }
198
+ /**
199
+ * @param browser the browser instance the session file name is based on
200
+ * @param account login details, supplied by the consumer of this library; only `username` is used
201
+ */
202
+ static getDigidFileName(browser, account) {
203
+ return path.join(sessionsDir, `${browser.browserType().name()}-digid-${environment}-${account.username}.json`);
204
+ }
205
+ /**
206
+ * @param browser the browser instance the session file name is based on
207
+ * @param account login details, supplied by the consumer of this library; only `username` is used
208
+ */
209
+ static getDigidGemachtigdeFileName(browser, account) {
210
+ return path.join(sessionsDir, `${browser.browserType().name()}-digid-gemachtigde-${environment}-${account.username}.json`);
211
+ }
212
+ /**
213
+ * @param browser the browser instance the session file name is based on
214
+ * @param account login details, supplied by the consumer of this library; only `username` is used
215
+ */
216
+ static getEHerkenningFileName(browser, account) {
217
+ return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-${environment}-${account.username}.json`);
218
+ }
219
+ /**
220
+ * @param browser the browser instance the session file name is based on
221
+ * @param account login details, supplied by the consumer of this library; only `username` is used
222
+ */
223
+ static getEHerkenningVestigingFileName(browser, account) {
224
+ return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-vestiging-${environment}-${account.username}.json`);
225
+ }
226
+ /**
227
+ * @param browser the browser instance the session file name is based on
228
+ * @param account login details, supplied by the consumer of this library; only `username` is used
229
+ */
230
+ static getEHerkenningBewindvoerderFileName(browser, account) {
231
+ return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-bewindvoerder-${environment}-${account.username}.json`);
232
+ }
233
+ /**
234
+ * @param browser the browser instance the session file name is based on
235
+ * @param account login details, supplied by the consumer of this library; only `username` is used
236
+ */
237
+ static getADFileName(browser, account) {
238
+ return path.join(sessionsDir, `${browser.browserType().name()}-ad-${environment}-${account.username}.json`);
239
+ }
240
+ };
241
+ //#endregion
142
242
  //#region src/auth/pages/digidLoginPage.ts
143
- var DigidLoginPage = class extends LoginOverzichtPage {
243
+ var DigidLoginPage = class extends BasePage {
144
244
  constructor(page) {
145
245
  super(page);
146
246
  }
@@ -157,11 +257,11 @@ var DigidLoginPage = class extends LoginOverzichtPage {
157
257
  await this.openPage(startUrl);
158
258
  await this.handleAnySpecialADLoginScreen();
159
259
  try {
160
- await this.clickLoginButton(loginButton);
260
+ await loginButton.click();
161
261
  } catch (_error) {
162
262
  await this.openPage(startUrl);
163
263
  await this.handleAnySpecialADLoginScreen();
164
- await this.clickLoginButton(loginButton);
264
+ await loginButton.click();
165
265
  }
166
266
  if (!this.page.url().match(/authn/)) {
167
267
  await this.loginWithDigid(account);
@@ -183,10 +283,10 @@ var DigidLoginPage = class extends LoginOverzichtPage {
183
283
  await this.openPage(startUrl);
184
284
  await this.handleAnySpecialADLoginScreen();
185
285
  try {
186
- await this.clickLoginButton(loginButton);
286
+ await loginButton.click();
187
287
  } catch (_error) {
188
288
  await this.openPage(startUrl);
189
- await this.clickLoginButton(loginButton);
289
+ await loginButton.click();
190
290
  }
191
291
  await this.loginWithDigid(account, true);
192
292
  await this.acceptCookiesForApp(options);
@@ -197,7 +297,7 @@ var DigidLoginPage = class extends LoginOverzichtPage {
197
297
  * Accepts the app's cookie banner when the consumer supplied one, so the consent is in the saved session.
198
298
  */
199
299
  async acceptCookiesForApp(options) {
200
- if (options.cookieButton) await this.acceptCookies(options.cookieButton(this.page));
300
+ if (options.cookieButton) await options.cookieButton(this.page).click();
201
301
  }
202
302
  /**
203
303
  * When there is already an AD session, but for another environment, a special screen asks to
@@ -234,19 +334,19 @@ var DigidLoginPage = class extends LoginOverzichtPage {
234
334
  username: `${process$1.env.DIGID_GEMACHTIGDE_USER}`,
235
335
  password: `${process$1.env.DIGID_GEMACHTIGDE_PASSWORD}`
236
336
  };
237
- await this.clickLoginButton(loginButton);
337
+ await loginButton.click();
238
338
  await this.loginWithDigid(gemachtigde, true);
239
339
  });
240
340
  }
241
341
  };
242
342
  //#endregion
243
- //#region src/auth/loginhandlers/digidLoginHandler.ts
244
- var DigidLoginHandler = class {
343
+ //#region src/auth/loginhandlers/digidGemachtigdeLoginHandler.ts
344
+ var DigidGemachtigdeLoginHandler = class {
245
345
  options;
246
346
  /**
247
347
  * Create one per app, so every test user that logs in to that app shares its options.
248
348
  *
249
- * @param options the app's DigiD login button (required), and optionally where to start and the
349
+ * @param options the app's DigiD machtigen login button (required), and optionally where to start and the
250
350
  * app's cookie banner, supplied by the consumer of this library
251
351
  */
252
352
  constructor(options) {
@@ -258,12 +358,25 @@ var DigidLoginHandler = class {
258
358
  * @param file path of the storage state file to write the session to
259
359
  */
260
360
  async loginAndCreateSessionStorage(page, account, file) {
261
- await new DigidLoginPage(page).loginAndCreateNewSession(account, file, this.options);
361
+ await new DigidLoginPage(page).loginAndCreateNewGemachtigdeSession(account, file, this.options);
362
+ }
363
+ };
364
+ //#endregion
365
+ //#region src/auth/pages/baseLoginPage.ts
366
+ var BaseLoginPage = class extends BasePage {
367
+ constructor(page) {
368
+ super(page);
369
+ }
370
+ /**
371
+ * Accepts the app's cookie banner when the consumer supplied one, so the consent is in the saved session.
372
+ */
373
+ async acceptCookiesForApp(options) {
374
+ if (options.cookieButton) await options.cookieButton(this.page).click();
262
375
  }
263
376
  };
264
377
  //#endregion
265
378
  //#region src/auth/pages/eHerkenningLoginPage.ts
266
- var EHerkenningLoginPage = class extends LoginOverzichtPage {
379
+ var EHerkenningLoginPage = class extends BaseLoginPage {
267
380
  dropdownSelectIdentityProvider;
268
381
  buttonConfirmIdentityProviderSelection;
269
382
  textfieldUsername;
@@ -302,67 +415,78 @@ var EHerkenningLoginPage = class extends LoginOverzichtPage {
302
415
  /**
303
416
  * @param fileName path of the storage state file to write the session to
304
417
  * @param account login details, supplied by the consumer of this library
418
+ * @param options
305
419
  */
306
- async loginWithEHerkenningAndCreateSession(fileName, account) {
420
+ async loginWithEHerkenningAndCreateSession(fileName, account, options) {
307
421
  await test.step("Login with eHerkenning and create session", async () => {
308
- await this.openPage();
422
+ const loginButton = options.loginButton(this.page);
423
+ const { startUrl } = options;
424
+ await this.openPage(startUrl);
309
425
  try {
310
- await this.clickEHerkenningLoginOption();
426
+ await loginButton.click();
311
427
  } catch (_error) {
312
428
  await this.openPage();
313
- await this.clickEHerkenningLoginOption();
429
+ await this.loginButton.click();
314
430
  }
315
- await this.loginWithEHerkenning(account);
431
+ await this.loginWithEHerkenning(account, options);
316
432
  await this.page.context().storageState({ path: fileName });
317
433
  });
318
434
  }
319
435
  /**
320
436
  * @param account login details, supplied by the consumer of this library
437
+ * @param options
321
438
  */
322
- async loginWithEHerkenning(account) {
439
+ async loginWithEHerkenning(account, options) {
323
440
  await this.tryLoginWithEHerkenning(account);
324
441
  await test.step("Confirm eHerkenning login and accept cookies", async () => {
325
442
  await this.page.waitForTimeout(2e3);
326
443
  if (await this.continueButton.isVisible()) await this.continueButton.click();
327
- await this.acceptCookies();
444
+ await this.acceptCookiesForApp(options);
328
445
  });
329
446
  }
330
447
  /**
331
448
  * @param fileName path of the storage state file to write the session to
332
449
  * @param account login details, supplied by the consumer of this library
450
+ * @param options
333
451
  */
334
- async loginWithEHerkenningVestigingAndCreateSession(fileName, account) {
452
+ async loginWithEHerkenningVestigingAndCreateSession(fileName, account, options) {
335
453
  await test.step("Login with eHerkenning vestiging and create session", async () => {
336
- await this.openPage();
454
+ const loginButton = options.loginButton(this.page);
455
+ const { startUrl } = options;
456
+ await this.openPage(startUrl);
337
457
  try {
338
- await this.clickEHerkenningLoginOption();
458
+ await loginButton.click();
339
459
  } catch (_error) {
340
460
  await this.openPage();
341
- await this.clickEHerkenningLoginOption();
461
+ await loginButton.click();
342
462
  }
343
- await this.loginWithEHerkenningVestiging(account);
463
+ await this.loginWithEHerkenningVestiging(account, options);
344
464
  await this.page.context().storageState({ path: fileName });
345
465
  });
346
466
  }
347
467
  /**
348
468
  * @param account login details, supplied by the consumer of this library
469
+ * @param options
349
470
  */
350
- async loginWithEHerkenningVestiging(account) {
471
+ async loginWithEHerkenningVestiging(account, options) {
351
472
  await this.tryLoginWithEHerkenning(account);
352
473
  await test.step("Confirm eHerkenning vestiging login and accept cookies", async () => {
353
474
  await this.page.waitForTimeout(2e3);
354
475
  if (await this.continueButton.isVisible()) await this.continueButton.click();
355
- await this.acceptCookies();
476
+ await this.acceptCookiesForApp(options);
356
477
  });
357
478
  }
358
479
  /**
359
480
  * @param gemachtigde login details of the authorized representative (bewindvoerder), supplied by the consumer of this library
481
+ * @param options
360
482
  * @param file optional path of the storage state file to write the session to
361
483
  */
362
- async loginAsProfessionalBewindvoerder(gemachtigde, file) {
484
+ async loginAsProfessionalBewindvoerder(gemachtigde, options, file) {
363
485
  await test.step("Open bewindvoerder login option", async () => {
364
- await this.openPage();
365
- await this.clickProfessionalBewindvoerderLoginOption();
486
+ const loginButton = options.loginButton(this.page);
487
+ const { startUrl } = options;
488
+ await this.openPage(startUrl);
489
+ await loginButton.click();
366
490
  });
367
491
  await test.step("Login with eHerkenning credentials as bewindvoerder", async () => {
368
492
  try {
@@ -379,7 +503,7 @@ var EHerkenningLoginPage = class extends LoginOverzichtPage {
379
503
  }
380
504
  await this.searchButton.click();
381
505
  await this.continueLink.click();
382
- await this.acceptCookies();
506
+ await this.acceptCookiesForApp(options);
383
507
  });
384
508
  if (file) await test.step("Save session state", async () => {
385
509
  await this.page.context().storageState({ path: file });
@@ -387,25 +511,13 @@ var EHerkenningLoginPage = class extends LoginOverzichtPage {
387
511
  }
388
512
  };
389
513
  //#endregion
390
- //#region src/auth/loginhandlers/eHerkenningLoginHandler.ts
391
- var EHerkenningLoginHandler = class {
392
- /**
393
- * @param page the Playwright page to perform the login on
394
- * @param account login details, supplied by the consumer of this library
395
- * @param file path of the storage state file to write the session to
396
- */
397
- async loginAndCreateSessionStorage(page, account, file) {
398
- await new EHerkenningLoginPage(page).loginWithEHerkenningVestigingAndCreateSession(file, account);
399
- }
400
- };
401
- //#endregion
402
- //#region src/auth/loginhandlers/digidGemachtigdeLoginHandler.ts
403
- var DigidGemachtigdeLoginHandler = class {
514
+ //#region src/auth/loginhandlers/digidLoginHandler.ts
515
+ var DigidLoginHandler = class {
404
516
  options;
405
517
  /**
406
518
  * Create one per app, so every test user that logs in to that app shares its options.
407
519
  *
408
- * @param options the app's DigiD machtigen login button (required), and optionally where to start and the
520
+ * @param options the app's DigiD login button (required), and optionally where to start and the
409
521
  * app's cookie banner, supplied by the consumer of this library
410
522
  */
411
523
  constructor(options) {
@@ -417,165 +529,63 @@ var DigidGemachtigdeLoginHandler = class {
417
529
  * @param file path of the storage state file to write the session to
418
530
  */
419
531
  async loginAndCreateSessionStorage(page, account, file) {
420
- await new DigidLoginPage(page).loginAndCreateNewGemachtigdeSession(account, file, this.options);
532
+ await new DigidLoginPage(page).loginAndCreateNewSession(account, file, this.options);
421
533
  }
422
534
  };
423
535
  //#endregion
424
536
  //#region src/auth/loginhandlers/eHerkenningBewindvoerderLoginHandler.ts
425
537
  var EHerkenningBewindvoerderLoginHandler = class {
538
+ options;
539
+ /**
540
+ * Create one per app, so every test user that logs in to that app shares its options.
541
+ *
542
+ * @param options the app's DigiD login button (required), and optionally where to start and the
543
+ * app's cookie banner, supplied by the consumer of this library
544
+ */
545
+ constructor(options) {
546
+ this.options = options;
547
+ }
426
548
  /**
427
549
  * @param page the Playwright page to perform the login on
428
550
  * @param account login details, supplied by the consumer of this library
429
551
  * @param file path of the storage state file to write the session to
430
552
  */
431
553
  async loginAndCreateSessionStorage(page, account, file) {
432
- await new EHerkenningLoginPage(page).loginAsProfessionalBewindvoerder(account, file);
554
+ await new EHerkenningLoginPage(page).loginAsProfessionalBewindvoerder(account, this.options, file);
433
555
  }
434
556
  };
435
557
  //#endregion
436
- //#region src/auth/pages/adLoginPage.ts
437
- var gzacUrlProd = "https://alg-zgw.denhaag.nl";
438
- var gzacUrlAcp = "https://alg-zgw.acc.denhaag.nl";
439
- var gzacUrlTest = "https://alg-zgw.test.denhaag.nl";
440
- function escapeRegExp(value) {
441
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
442
- }
443
- var ADLoginPage = class ADLoginPage extends BasePage {
444
- usernameInput;
445
- passwordInput;
446
- nextInput;
447
- signInInput;
448
- cannotUseAuthAppLink;
449
- useVerificationCode;
450
- codeInput;
451
- verifyInput;
452
- constructor(page) {
453
- super(page);
454
- this.usernameInput = page.locator("//input[@type='email']");
455
- this.passwordInput = page.locator("//input[@name='passwd']");
456
- this.nextInput = page.locator("//input[@type='submit']");
457
- this.signInInput = page.locator("//input[@type='submit']");
458
- this.cannotUseAuthAppLink = page.locator("//*[@id='signInAnotherWay']");
459
- this.useVerificationCode = page.getByText(/Gebruik een verificatiecode|Use a verification code/);
460
- this.codeInput = page.locator("//input[@name='otc']");
461
- this.verifyInput = page.locator("//input[@type='submit']");
462
- }
463
- async openPage(path) {
464
- await this.page.goto(path);
465
- }
558
+ //#region src/auth/loginhandlers/eHerkenningLoginHandler.ts
559
+ var EHerkenningLoginHandler = class {
560
+ options;
466
561
  /**
467
- * Login with the AD account so we can get behind the firewall
468
- *
469
- * @param account login details, supplied by the consumer of this library
470
- * @param targetUrl URL to navigate to before logging in (the app behind the firewall),
471
- * supplied by the consumer of this library — not every consumer has a GZAC account, so
472
- * this library no longer assumes GZAC as the destination. The consumer is responsible
473
- * for switching between environments (e.g. test/acc/prod) when building this URL; this
474
- * library does not do that for you.
475
- * @param fileName path of the storage state file to write the session to
476
- *
477
- * @example
478
- * // consumer-side lookup, e.g. based on an `ENV` environment variable
479
- * export const TARGET_URL_BY_ENVIRONMENT = {
480
- * test: 'https://alg-zgw.test.denhaag.nl/',
481
- * acc: 'https://alg-zgw.acc.denhaag.nl/',
482
- * prod: 'https://alg-zgw.denhaag.nl/',
483
- * } as const;
562
+ * Create one per app, so every test user that logs in to that app shares its options.
484
563
  *
485
- * const env = (process.env.ENV ?? 'test') as keyof typeof TARGET_URL_BY_ENVIRONMENT;
486
- * await adLoginPage.loginWithADAccountAndCreateSession(account, TARGET_URL_BY_ENVIRONMENT[env], fileName);
564
+ * @param options the app's DigiD login button (required), and optionally where to start and the
565
+ * app's cookie banner, supplied by the consumer of this library
487
566
  */
488
- async loginWithADAccountAndCreateSession(account, targetUrl, fileName) {
489
- const adLoginPage = new ADLoginPage(this.page);
490
- let targetOrigin;
491
- try {
492
- targetOrigin = new URL(targetUrl).origin;
493
- } catch {
494
- throw new Error(`AD login failed: targetUrl must be an absolute URL (e.g. "https://app.denhaag.nl/..."), got "${targetUrl}"`);
495
- }
496
- const isOnTargetApp = async () => this.page.waitForLoadState("networkidle").then(() => this.page.url().startsWith(targetOrigin), () => false);
497
- let cachedAccountUsed = false;
498
- let alreadyFullyAuthenticated = false;
499
- await test.step("Fill AD username and submit", async () => {
500
- await this.page.goto(targetUrl);
501
- alreadyFullyAuthenticated = await isOnTargetApp();
502
- if (alreadyFullyAuthenticated) return;
503
- const cachedAccountTile = this.page.getByRole("button", { name: new RegExp(escapeRegExp(account.username), "i") });
504
- await cachedAccountTile.or(adLoginPage.usernameInput).waitFor({
505
- state: "visible",
506
- timeout: 1e4
507
- }).catch(() => {});
508
- cachedAccountUsed = await cachedAccountTile.isVisible().catch(() => false);
509
- if (cachedAccountUsed) {
510
- await cachedAccountTile.click();
511
- alreadyFullyAuthenticated = await isOnTargetApp();
512
- } else {
513
- await adLoginPage.usernameInput.waitFor();
514
- await adLoginPage.usernameInput.fill(account.username);
515
- await adLoginPage.nextInput.click();
516
- await this.page.getByText("Uw wachtwoord gebruiken").click();
517
- }
518
- });
519
- let sessionWasStale = false;
520
- if (!alreadyFullyAuthenticated) {
521
- await test.step("Fill AD password and submit", async () => {
522
- if (!cachedAccountUsed) {
523
- await maskedFill(adLoginPage.passwordInput, account.password, "password");
524
- await adLoginPage.signInInput.click();
525
- await this.page.waitForTimeout(1e3);
526
- }
527
- sessionWasStale = await this.page.getByText("Uw account is niet geverifieerd. Probeer het opnieuw").isVisible().catch(() => false);
528
- });
529
- await test.step("Complete MFA via TOTP", async () => {
530
- if (!sessionWasStale) await adLoginPage.cannotUseAuthAppLink.click();
531
- await adLoginPage.useVerificationCode.click();
532
- const token = await TOTP.generate(`${process.env.AD_SECRET}`);
533
- await maskedFill(adLoginPage.codeInput, token.otp, "otp", true);
534
- await adLoginPage.verifyInput.click();
535
- await adLoginPage.page.waitForTimeout(5e3);
536
- });
537
- }
538
- await test.step("Save session state", async () => {
539
- await adLoginPage.page.context().storageState({ path: fileName });
540
- });
567
+ constructor(options) {
568
+ this.options = options;
541
569
  }
542
570
  /**
543
- * Determines the GZAC URL to log in against, based on the current environment.
544
- *
545
- * @deprecated GZAC-specific and no longer called internally by this library — pass your own
546
- * target URL to {@link loginWithADAccountAndCreateSession} instead. Kept only so existing
547
- * GZAC consumers can still derive this URL themselves during migration; will be removed in
548
- * a future version.
549
- *
550
- * Derives the environment from `this.getBaseUrl()` (inherited from `BasePage`), which reads
551
- * the `PORTAL_BASE_URL` environment variable.
571
+ * @param page the Playwright page to perform the login on
572
+ * @param account login details, supplied by the consumer of this library
573
+ * @param file path of the storage state file to write the session to
552
574
  */
553
- getGzacUrl() {
554
- const baseUrl = this.getBaseUrl();
555
- if (/klantportaal-zgw.denhaag.nl./.exec(baseUrl)) return gzacUrlProd;
556
- if (/.acc./.exec(baseUrl)) return gzacUrlAcp;
557
- if (/.test./.exec(baseUrl)) return gzacUrlTest;
558
- else throw new Error("Cannot find current environment for baseUrl: " + baseUrl);
575
+ async loginAndCreateSessionStorage(page, account, file) {
576
+ await new EHerkenningLoginPage(page).loginWithEHerkenningVestigingAndCreateSession(file, account, this.options);
559
577
  }
560
578
  };
561
579
  //#endregion
562
- //#region src/auth/loginhandlers/adLoginHandler.ts
563
- var ADLoginHandler = class {
564
- targetUrl;
565
- /**
566
- * @param targetUrl URL to navigate to before logging in (the app behind the firewall),
567
- * supplied by the consumer of this library
568
- */
569
- constructor(targetUrl) {
570
- this.targetUrl = targetUrl;
571
- }
580
+ //#region src/auth/loginhandlers/localhostLoginHandler.ts
581
+ var LocalhostLoginHandler = class {
572
582
  /**
573
583
  * @param page the Playwright page to perform the login on
574
584
  * @param account login details, supplied by the consumer of this library
575
585
  * @param file path of the storage state file to write the session to
576
586
  */
577
587
  async loginAndCreateSessionStorage(page, account, file) {
578
- await new ADLoginPage(page).loginWithADAccountAndCreateSession(account, this.targetUrl, file);
588
+ await new LocalLoginPage(page).loginAndCreateNewSession(account, file);
579
589
  }
580
590
  };
581
591
  //#endregion
@@ -604,18 +614,18 @@ var ADLoginHandler = class {
604
614
  */
605
615
  function getLoginHandler(authType, options = {}) {
606
616
  switch (authType) {
607
- case "digid": return isRunningOnLocalhost() ? new LocalhostLoginHandler() : new DigidLoginHandler(digidOptions(authType, options));
608
- case "digid_gemachtigde": return new DigidGemachtigdeLoginHandler(digidOptions(authType, options));
609
- case "eherkenning": return isRunningOnLocalhost() ? new LocalhostLoginHandler() : new EHerkenningLoginHandler();
610
- case "eherkenning_vestiging": return new EHerkenningLoginHandler();
611
- case "eherkenning_bewindvoerder": return new EHerkenningBewindvoerderLoginHandler();
617
+ case "digid": return isRunningOnLocalhost() ? new LocalhostLoginHandler() : new DigidLoginHandler(loginOptions(authType, options));
618
+ case "digid_gemachtigde": return new DigidGemachtigdeLoginHandler(loginOptions(authType, options));
619
+ case "eherkenning": return isRunningOnLocalhost() ? new LocalhostLoginHandler() : new EHerkenningLoginHandler(loginOptions(authType, options));
620
+ case "eherkenning_vestiging": return new EHerkenningLoginHandler(loginOptions(authType, options));
621
+ case "eherkenning_bewindvoerder": return new EHerkenningBewindvoerderLoginHandler(loginOptions(authType, options));
612
622
  case "ad-session":
613
623
  if (!options.startUrl) throw new Error("startUrl is required when authType is 'ad-session'");
614
624
  return new ADLoginHandler(options.startUrl);
615
625
  default: throw new Error(`Unsupported auth type: ${authType}. If needed please create a new LoginHandler and register it here in getLoginHandler().`);
616
626
  }
617
627
  }
618
- function digidOptions(authType, options) {
628
+ function loginOptions(authType, options) {
619
629
  if (!options.loginButton) throw new Error(`loginButton is required when authType is '${authType}'`);
620
630
  return {
621
631
  ...options,
@@ -623,71 +633,6 @@ function digidOptions(authType, options) {
623
633
  };
624
634
  }
625
635
  //#endregion
626
- //#region src/auth/utils/sessionFileHelper.ts
627
- var sessionsDir = process$2.env.SESSIONS_DIR ?? path.resolve(process$2.cwd(), "sessions");
628
- var environment = process$2.env.ENV ?? "test";
629
- /**
630
- * Easy way to create the filenames for the sessions
631
- */
632
- var SessionFileHelper = class {
633
- /**
634
- * @param browser the browser instance the session file name is based on
635
- * @param account login details, supplied by the consumer of this library; only `username` is used
636
- */
637
- static getLocalhostFileName(browser, account) {
638
- return path.join(sessionsDir, `${browser.browserType().name()}-${environment}-${account.username}.json`);
639
- }
640
- /**
641
- * @param browser the browser instance the session file name is based on
642
- * @param account login details, supplied by the consumer of this library; only `username` is used
643
- */
644
- static getLocalhostFileNameBedrijf(browser, account) {
645
- return path.join(sessionsDir, `${browser.browserType().name()}-${environment}-${account.username}.json`);
646
- }
647
- /**
648
- * @param browser the browser instance the session file name is based on
649
- * @param account login details, supplied by the consumer of this library; only `username` is used
650
- */
651
- static getDigidFileName(browser, account) {
652
- return path.join(sessionsDir, `${browser.browserType().name()}-digid-${environment}-${account.username}.json`);
653
- }
654
- /**
655
- * @param browser the browser instance the session file name is based on
656
- * @param account login details, supplied by the consumer of this library; only `username` is used
657
- */
658
- static getDigidGemachtigdeFileName(browser, account) {
659
- return path.join(sessionsDir, `${browser.browserType().name()}-digid-gemachtigde-${environment}-${account.username}.json`);
660
- }
661
- /**
662
- * @param browser the browser instance the session file name is based on
663
- * @param account login details, supplied by the consumer of this library; only `username` is used
664
- */
665
- static getEHerkenningFileName(browser, account) {
666
- return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-${environment}-${account.username}.json`);
667
- }
668
- /**
669
- * @param browser the browser instance the session file name is based on
670
- * @param account login details, supplied by the consumer of this library; only `username` is used
671
- */
672
- static getEHerkenningVestigingFileName(browser, account) {
673
- return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-vestiging-${environment}-${account.username}.json`);
674
- }
675
- /**
676
- * @param browser the browser instance the session file name is based on
677
- * @param account login details, supplied by the consumer of this library; only `username` is used
678
- */
679
- static getEHerkenningBewindvoerderFileName(browser, account) {
680
- return path.join(sessionsDir, `${browser.browserType().name()}-eherkenning-bewindvoerder-${environment}-${account.username}.json`);
681
- }
682
- /**
683
- * @param browser the browser instance the session file name is based on
684
- * @param account login details, supplied by the consumer of this library; only `username` is used
685
- */
686
- static getADFileName(browser, account) {
687
- return path.join(sessionsDir, `${browser.browserType().name()}-ad-${environment}-${account.username}.json`);
688
- }
689
- };
690
- //#endregion
691
636
  //#region src/auth/sessionHandler.ts
692
637
  var MAX_SESSION_TIME_IN_MINUTES = 10;
693
638
  /**
@@ -823,6 +768,7 @@ function getApiTokenFromSession(baseTest, handler) {
823
768
  //#region src/auth/getSessionHandler.ts
824
769
  /**
825
770
  * @param testProfile the test user to log in
771
+ * @param loginOptionsWithButtons with the login button and the accept cookiebutton if necessary
826
772
  * @param loginHandler the login handler for the app the user logs in to, with that app's options, e.g.
827
773
  * `new DigidLoginHandler({ loginButton, ... })` or `getLoginHandler(authType, options)`. Required for
828
774
  * auth types that need options (`ad-session`, `digid`, `digid_gemachtigde`); left out, the handler
@@ -838,8 +784,8 @@ function getApiTokenFromSession(baseTest, handler) {
838
784
  *
839
785
  * getSessionHandler(digidUser1, openFormsDigid);
840
786
  */
841
- function getSessionHandler(testProfile, loginHandler) {
842
- return createSessionHandler(testProfile.authType, testProfile.account, loginHandler ?? getLoginHandler(testProfile.authType));
787
+ function getSessionHandler(testProfile, loginOptionsWithButtons, loginHandler) {
788
+ return createSessionHandler(testProfile.authType, testProfile.account, loginHandler ?? getLoginHandler(testProfile.authType, loginOptionsWithButtons));
843
789
  }
844
790
  //#endregion
845
- export { ADLoginHandler, ADLoginPage, DigidGemachtigdeLoginHandler, DigidLoginHandler, DigidLoginPage, EHerkenningBewindvoerderLoginHandler, EHerkenningLoginHandler, EHerkenningLoginPage, LocalhostLoginHandler, LoginOverzichtPage, MAX_SESSION_TIME_IN_MINUTES, SessionFileHelper, createSessionHandler, getApiTokenFromSession, getLoginHandler, getSessionHandler, maskedFill };
791
+ export { ADLoginHandler, ADLoginPage, BaseLoginPage, DigidGemachtigdeLoginHandler, DigidLoginHandler, DigidLoginPage, EHerkenningBewindvoerderLoginHandler, EHerkenningLoginHandler, EHerkenningLoginPage, LocalhostLoginHandler, MAX_SESSION_TIME_IN_MINUTES, SessionFileHelper, createSessionHandler, getApiTokenFromSession, getLoginHandler, getSessionHandler, maskedFill };