dh-auth-library 3.0.2 → 3.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.
@@ -22,6 +22,8 @@ export interface LoginOptions {
22
22
  */
23
23
  excludeCookies?: Array<string>;
24
24
  }
25
+ /** Whether `url` is authn (Keycloak) answering with a server error, e.g. its IdP broker endpoint with `error=server_error`. */
26
+ export declare function isAuthnServerError(url: string, status?: number): boolean;
25
27
  export declare class BaseLoginPage extends BasePage {
26
28
  constructor(page: Page);
27
29
  /**
@@ -29,6 +31,14 @@ export declare class BaseLoginPage extends BasePage {
29
31
  * throws instead of saving the page it ended on.
30
32
  */
31
33
  waitForApp(options: LoginOptionsWithButton): Promise<void>;
34
+ /**
35
+ * Runs `action`, but throws as soon as authn answers with a server error, instead of letting its next wait time out.
36
+ */
37
+ failOnAuthnServerError<T>(action: () => Promise<T>): Promise<T>;
38
+ /** Waits for the redirects after the login button, which can land on an authn server error instead of the login. */
39
+ waitForLoginStart(): Promise<void>;
40
+ /** Throws when the page is on an authn server error, e.g. after a login button redirected there. */
41
+ throwOnAuthnServerError(): Promise<void>;
32
42
  /**
33
43
  * Accepts the app's cookie banner when the consumer supplied one, so the consent is in the saved session.
34
44
  */
package/dist/index.cjs.js CHANGED
@@ -169,8 +169,13 @@ var ADLoginPage = class ADLoginPage extends nl_portal_playwright_libraries.BaseP
169
169
  sessionWasStale = await this.page.getByText("Uw account is niet geverifieerd. Probeer het opnieuw").isVisible().catch(() => false);
170
170
  });
171
171
  await _playwright_test.test.step("Complete MFA via TOTP", async () => {
172
- if (!sessionWasStale) await adLoginPage.cannotUseAuthAppLink.click();
173
- await adLoginPage.useVerificationCode.click();
172
+ const mfaScreen = adLoginPage.cannotUseAuthAppLink.or(adLoginPage.useVerificationCode).or(adLoginPage.codeInput).filter({ visible: true }).first();
173
+ if (await Promise.race([this.page.waitForURL((url) => url.origin === targetOrigin, {
174
+ waitUntil: "commit",
175
+ timeout: 3e4
176
+ }).then(() => true), mfaScreen.waitFor({ timeout: 3e4 }).then(() => false)])) return;
177
+ if (!sessionWasStale && await adLoginPage.cannotUseAuthAppLink.isVisible()) await adLoginPage.cannotUseAuthAppLink.click();
178
+ if (!await adLoginPage.codeInput.isVisible()) await adLoginPage.useVerificationCode.click();
174
179
  const token = await totp_generator.TOTP.generate(`${process.env.AD_SECRET}`);
175
180
  await maskedFill(adLoginPage.codeInput, token.otp, "otp", true);
176
181
  await adLoginPage.verifyInput.click();
@@ -310,6 +315,15 @@ var SessionFileHelper = class {
310
315
  }
311
316
  };
312
317
  // src/auth/pages/baseLoginPage.ts
318
+ /** Whether `url` is authn (Keycloak) answering with a server error, e.g. its IdP broker endpoint with `error=server_error`. */
319
+ function isAuthnServerError(url, status = 200) {
320
+ const { hostname, searchParams } = new URL(url);
321
+ return hostname.startsWith("authn.") && (status >= 500 || searchParams.get("error") === "server_error");
322
+ }
323
+ function authnServerError(url, status) {
324
+ const { origin, pathname } = new URL(url);
325
+ return status !== void 0 && status >= 500 && status < 600 ? /* @__PURE__ */ new Error(`authn returned status ${status} on ${origin}${pathname}: this is not a test failure, contact Team ADM`) : /* @__PURE__ */ new Error(`authn returned an error on ${origin}${pathname} (status ${status ?? "unknown"}): verify the error yourself`);
326
+ }
313
327
  var BaseLoginPage = class extends nl_portal_playwright_libraries.BasePage {
314
328
  constructor(page) {
315
329
  super(page);
@@ -323,11 +337,44 @@ var BaseLoginPage = class extends nl_portal_playwright_libraries.BasePage {
323
337
  await this.page.waitForURL((url) => url.origin === appOrigin, {
324
338
  waitUntil: "commit",
325
339
  timeout: 3e4
326
- }).catch(() => {
340
+ }).catch(async () => {
341
+ await this.throwOnAuthnServerError();
327
342
  throw new Error(`Login didn't return to ${appOrigin}, stuck on ${this.page.url()}`);
328
343
  });
329
344
  }
330
345
  /**
346
+ * Runs `action`, but throws as soon as authn answers with a server error, instead of letting its next wait time out.
347
+ */
348
+ async failOnAuthnServerError(action) {
349
+ await this.throwOnAuthnServerError();
350
+ let onResponse = () => {};
351
+ const serverError = new Promise((_, reject) => {
352
+ onResponse = (response) => {
353
+ if (response.request().isNavigationRequest() && isAuthnServerError(response.url(), response.status())) reject(authnServerError(response.url(), response.status()));
354
+ };
355
+ this.page.on("response", onResponse);
356
+ });
357
+ try {
358
+ return await Promise.race([action(), serverError]);
359
+ } catch (error) {
360
+ await this.throwOnAuthnServerError();
361
+ throw error;
362
+ } finally {
363
+ this.page.off("response", onResponse);
364
+ }
365
+ }
366
+ /** Waits for the redirects after the login button, which can land on an authn server error instead of the login. */
367
+ async waitForLoginStart() {
368
+ await this.page.waitForLoadState("networkidle", { timeout: 1e4 }).catch(() => {});
369
+ await this.throwOnAuthnServerError();
370
+ }
371
+ /** Throws when the page is on an authn server error, e.g. after a login button redirected there. */
372
+ async throwOnAuthnServerError() {
373
+ const url = this.page.url();
374
+ if (!isAuthnServerError(url)) return;
375
+ throw authnServerError(url, await this.page.evaluate(() => performance.getEntriesByType("navigation")[0]?.responseStatus).catch(() => void 0));
376
+ }
377
+ /**
331
378
  * Accepts the app's cookie banner when the consumer supplied one, so the consent is in the saved session.
332
379
  */
333
380
  async acceptCookiesForApp(options) {
@@ -375,6 +422,7 @@ var DigidLoginPage = class extends BaseLoginPage {
375
422
  await this.handleAnySpecialADLoginScreen();
376
423
  await loginButton.click();
377
424
  }
425
+ await this.waitForLoginStart();
378
426
  if (!this.page.url().match(/authn/)) {
379
427
  await this.loginWithDigid(account);
380
428
  await this.waitForApp(options);
@@ -403,6 +451,7 @@ var DigidLoginPage = class extends BaseLoginPage {
403
451
  await this.handleAnySpecialADLoginScreen();
404
452
  await loginButton.click();
405
453
  }
454
+ await this.waitForLoginStart();
406
455
  await this.loginWithDigid(account, true);
407
456
  await this.waitForApp(options);
408
457
  await this.acceptCookiesForApp(options);
@@ -414,7 +463,7 @@ var DigidLoginPage = class extends BaseLoginPage {
414
463
  * @param gemachtigde whether to log in as an authorized representative (machtiging) instead of a regular user
415
464
  */
416
465
  async loginWithDigid(account, gemachtigde) {
417
- return await _playwright_test.test.step("Fill DigiD credentials and submit", async () => {
466
+ return await _playwright_test.test.step("Fill DigiD credentials and submit", async () => this.failOnAuthnServerError(async () => {
418
467
  await this.page.getByLabel("Inlogoptie 5 van 5: Inloggen").click();
419
468
  await this.page.getByLabel("Testen betrouwbaarheidsniveau").selectOption("25");
420
469
  await this.page.getByLabel("DigiD gebruikersnaam").fill(account.username);
@@ -423,7 +472,7 @@ var DigidLoginPage = class extends BaseLoginPage {
423
472
  await this.page.getByRole("button", { name: "Inloggen" }).click();
424
473
  if (gemachtigde) await this.page.getByRole("button", { name: "Kies" }).click();
425
474
  return await responsePromise;
426
- });
475
+ }));
427
476
  }
428
477
  /**
429
478
  * @param loginButton button that starts the DigiD machtigen login, supplied by the consumer of this library
@@ -515,7 +564,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
515
564
  * @param account login details, supplied by the consumer of this library
516
565
  */
517
566
  async tryLoginWithEHerkenning(account) {
518
- await _playwright_test.test.step("Fill eHerkenning credentials and submit", async () => {
567
+ await _playwright_test.test.step("Fill eHerkenning credentials and submit", async () => this.failOnAuthnServerError(async () => {
519
568
  await this.dropdownSelectIdentityProvider.or(this.textfieldUsername).first().waitFor();
520
569
  if (await this.dropdownSelectIdentityProvider.isVisible()) {
521
570
  await this.dropdownSelectIdentityProvider.selectOption("Digidentity AD 1.13 (preproduction)");
@@ -526,7 +575,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
526
575
  await this.continueButton.click();
527
576
  await maskedFill(this.textfieldPassword, account.password, "password");
528
577
  await this.loginButton.click();
529
- });
578
+ }));
530
579
  }
531
580
  /**
532
581
  * @param fileName path of the storage state file to write the session to
@@ -547,6 +596,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
547
596
  await this.handleAnySpecialADLoginScreen();
548
597
  await loginButton.click();
549
598
  }
599
+ await this.waitForLoginStart();
550
600
  await this.loginWithEHerkenning(account, options);
551
601
  await saveSession(this.page.context(), fileName, options.excludeCookies);
552
602
  });
@@ -582,6 +632,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
582
632
  await this.handleAnySpecialADLoginScreen();
583
633
  await loginButton.click();
584
634
  }
635
+ await this.waitForLoginStart();
585
636
  await this.loginWithEHerkenningVestiging(account, options);
586
637
  await saveSession(this.page.context(), fileName, options.excludeCookies);
587
638
  });
@@ -610,6 +661,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
610
661
  await this.openPage(startUrl);
611
662
  await this.handleAnySpecialADLoginScreen();
612
663
  await loginButton.click();
664
+ await this.waitForLoginStart();
613
665
  };
614
666
  await _playwright_test.test.step("Open bewindvoerder login option", async () => {
615
667
  try {
package/dist/index.es.js CHANGED
@@ -144,8 +144,13 @@ var ADLoginPage = class ADLoginPage extends BasePage {
144
144
  sessionWasStale = await this.page.getByText("Uw account is niet geverifieerd. Probeer het opnieuw").isVisible().catch(() => false);
145
145
  });
146
146
  await test.step("Complete MFA via TOTP", async () => {
147
- if (!sessionWasStale) await adLoginPage.cannotUseAuthAppLink.click();
148
- await adLoginPage.useVerificationCode.click();
147
+ const mfaScreen = adLoginPage.cannotUseAuthAppLink.or(adLoginPage.useVerificationCode).or(adLoginPage.codeInput).filter({ visible: true }).first();
148
+ if (await Promise.race([this.page.waitForURL((url) => url.origin === targetOrigin, {
149
+ waitUntil: "commit",
150
+ timeout: 3e4
151
+ }).then(() => true), mfaScreen.waitFor({ timeout: 3e4 }).then(() => false)])) return;
152
+ if (!sessionWasStale && await adLoginPage.cannotUseAuthAppLink.isVisible()) await adLoginPage.cannotUseAuthAppLink.click();
153
+ if (!await adLoginPage.codeInput.isVisible()) await adLoginPage.useVerificationCode.click();
149
154
  const token = await TOTP.generate(`${process.env.AD_SECRET}`);
150
155
  await maskedFill(adLoginPage.codeInput, token.otp, "otp", true);
151
156
  await adLoginPage.verifyInput.click();
@@ -285,6 +290,15 @@ var SessionFileHelper = class {
285
290
  }
286
291
  };
287
292
  // src/auth/pages/baseLoginPage.ts
293
+ /** Whether `url` is authn (Keycloak) answering with a server error, e.g. its IdP broker endpoint with `error=server_error`. */
294
+ function isAuthnServerError(url, status = 200) {
295
+ const { hostname, searchParams } = new URL(url);
296
+ return hostname.startsWith("authn.") && (status >= 500 || searchParams.get("error") === "server_error");
297
+ }
298
+ function authnServerError(url, status) {
299
+ const { origin, pathname } = new URL(url);
300
+ return status !== void 0 && status >= 500 && status < 600 ? /* @__PURE__ */ new Error(`authn returned status ${status} on ${origin}${pathname}: this is not a test failure, contact Team ADM`) : /* @__PURE__ */ new Error(`authn returned an error on ${origin}${pathname} (status ${status ?? "unknown"}): verify the error yourself`);
301
+ }
288
302
  var BaseLoginPage = class extends BasePage {
289
303
  constructor(page) {
290
304
  super(page);
@@ -298,11 +312,44 @@ var BaseLoginPage = class extends BasePage {
298
312
  await this.page.waitForURL((url) => url.origin === appOrigin, {
299
313
  waitUntil: "commit",
300
314
  timeout: 3e4
301
- }).catch(() => {
315
+ }).catch(async () => {
316
+ await this.throwOnAuthnServerError();
302
317
  throw new Error(`Login didn't return to ${appOrigin}, stuck on ${this.page.url()}`);
303
318
  });
304
319
  }
305
320
  /**
321
+ * Runs `action`, but throws as soon as authn answers with a server error, instead of letting its next wait time out.
322
+ */
323
+ async failOnAuthnServerError(action) {
324
+ await this.throwOnAuthnServerError();
325
+ let onResponse = () => {};
326
+ const serverError = new Promise((_, reject) => {
327
+ onResponse = (response) => {
328
+ if (response.request().isNavigationRequest() && isAuthnServerError(response.url(), response.status())) reject(authnServerError(response.url(), response.status()));
329
+ };
330
+ this.page.on("response", onResponse);
331
+ });
332
+ try {
333
+ return await Promise.race([action(), serverError]);
334
+ } catch (error) {
335
+ await this.throwOnAuthnServerError();
336
+ throw error;
337
+ } finally {
338
+ this.page.off("response", onResponse);
339
+ }
340
+ }
341
+ /** Waits for the redirects after the login button, which can land on an authn server error instead of the login. */
342
+ async waitForLoginStart() {
343
+ await this.page.waitForLoadState("networkidle", { timeout: 1e4 }).catch(() => {});
344
+ await this.throwOnAuthnServerError();
345
+ }
346
+ /** Throws when the page is on an authn server error, e.g. after a login button redirected there. */
347
+ async throwOnAuthnServerError() {
348
+ const url = this.page.url();
349
+ if (!isAuthnServerError(url)) return;
350
+ throw authnServerError(url, await this.page.evaluate(() => performance.getEntriesByType("navigation")[0]?.responseStatus).catch(() => void 0));
351
+ }
352
+ /**
306
353
  * Accepts the app's cookie banner when the consumer supplied one, so the consent is in the saved session.
307
354
  */
308
355
  async acceptCookiesForApp(options) {
@@ -350,6 +397,7 @@ var DigidLoginPage = class extends BaseLoginPage {
350
397
  await this.handleAnySpecialADLoginScreen();
351
398
  await loginButton.click();
352
399
  }
400
+ await this.waitForLoginStart();
353
401
  if (!this.page.url().match(/authn/)) {
354
402
  await this.loginWithDigid(account);
355
403
  await this.waitForApp(options);
@@ -378,6 +426,7 @@ var DigidLoginPage = class extends BaseLoginPage {
378
426
  await this.handleAnySpecialADLoginScreen();
379
427
  await loginButton.click();
380
428
  }
429
+ await this.waitForLoginStart();
381
430
  await this.loginWithDigid(account, true);
382
431
  await this.waitForApp(options);
383
432
  await this.acceptCookiesForApp(options);
@@ -389,7 +438,7 @@ var DigidLoginPage = class extends BaseLoginPage {
389
438
  * @param gemachtigde whether to log in as an authorized representative (machtiging) instead of a regular user
390
439
  */
391
440
  async loginWithDigid(account, gemachtigde) {
392
- return await test.step("Fill DigiD credentials and submit", async () => {
441
+ return await test.step("Fill DigiD credentials and submit", async () => this.failOnAuthnServerError(async () => {
393
442
  await this.page.getByLabel("Inlogoptie 5 van 5: Inloggen").click();
394
443
  await this.page.getByLabel("Testen betrouwbaarheidsniveau").selectOption("25");
395
444
  await this.page.getByLabel("DigiD gebruikersnaam").fill(account.username);
@@ -398,7 +447,7 @@ var DigidLoginPage = class extends BaseLoginPage {
398
447
  await this.page.getByRole("button", { name: "Inloggen" }).click();
399
448
  if (gemachtigde) await this.page.getByRole("button", { name: "Kies" }).click();
400
449
  return await responsePromise;
401
- });
450
+ }));
402
451
  }
403
452
  /**
404
453
  * @param loginButton button that starts the DigiD machtigen login, supplied by the consumer of this library
@@ -490,7 +539,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
490
539
  * @param account login details, supplied by the consumer of this library
491
540
  */
492
541
  async tryLoginWithEHerkenning(account) {
493
- await test.step("Fill eHerkenning credentials and submit", async () => {
542
+ await test.step("Fill eHerkenning credentials and submit", async () => this.failOnAuthnServerError(async () => {
494
543
  await this.dropdownSelectIdentityProvider.or(this.textfieldUsername).first().waitFor();
495
544
  if (await this.dropdownSelectIdentityProvider.isVisible()) {
496
545
  await this.dropdownSelectIdentityProvider.selectOption("Digidentity AD 1.13 (preproduction)");
@@ -501,7 +550,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
501
550
  await this.continueButton.click();
502
551
  await maskedFill(this.textfieldPassword, account.password, "password");
503
552
  await this.loginButton.click();
504
- });
553
+ }));
505
554
  }
506
555
  /**
507
556
  * @param fileName path of the storage state file to write the session to
@@ -522,6 +571,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
522
571
  await this.handleAnySpecialADLoginScreen();
523
572
  await loginButton.click();
524
573
  }
574
+ await this.waitForLoginStart();
525
575
  await this.loginWithEHerkenning(account, options);
526
576
  await saveSession(this.page.context(), fileName, options.excludeCookies);
527
577
  });
@@ -557,6 +607,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
557
607
  await this.handleAnySpecialADLoginScreen();
558
608
  await loginButton.click();
559
609
  }
610
+ await this.waitForLoginStart();
560
611
  await this.loginWithEHerkenningVestiging(account, options);
561
612
  await saveSession(this.page.context(), fileName, options.excludeCookies);
562
613
  });
@@ -585,6 +636,7 @@ var EHerkenningLoginPage = class extends BaseLoginPage {
585
636
  await this.openPage(startUrl);
586
637
  await this.handleAnySpecialADLoginScreen();
587
638
  await loginButton.click();
639
+ await this.waitForLoginStart();
588
640
  };
589
641
  await test.step("Open bewindvoerder login option", async () => {
590
642
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dh-auth-library",
3
- "version": "3.0.2",
3
+ "version": "3.1.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/Gemeente-DenHaag/dh-auth-library.git"