jwtbutler 2.0.2 → 2.0.3

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/README.md CHANGED
@@ -62,14 +62,13 @@ const api = new jwtbutler({
62
62
  });
63
63
  ```
64
64
 
65
- if your auth server uses [hCaptcha](https://www.hcaptcha.com), \
66
- add the public sitekey:
65
+ `jwtbutler` can also render captchas (currently `hCaptcha` and `Cloudflare Turnstile`). note that `jwtbutler` only renders the widget and sends the resulting token to `/login` — your auth server must verify that token (e.g. [simpleauth](https://github.com/vielhuber/simpleauth) supports this out of the box):
67
66
 
68
67
  ```js
69
68
  const api = new jwtbutler({
70
69
  /* ... */
71
70
  captcha: {
72
- provider: 'hcaptcha',
71
+ provider: '...', // 'turnstile'|'hcaptcha'
73
72
  sitekey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
74
73
  theme: 'light'
75
74
  }
@@ -411,7 +411,30 @@ var jwtbutler = class {
411
411
  if ("login_form_rendered" in this.config && this.config.login_form_rendered != "" && typeof this.config.login_form_rendered === "function") this.config.login_form_rendered(document.querySelector("." + this.config.login_form_class));
412
412
  }
413
413
  captchaEnabled() {
414
- return this.config.captcha !== false && typeof this.config.captcha === "object" && (!("provider" in this.config.captcha) || this.config.captcha.provider === "hcaptcha") && "sitekey" in this.config.captcha && this.config.captcha.sitekey !== "";
414
+ return this.config.captcha !== false && typeof this.config.captcha === "object" && (!("provider" in this.config.captcha) || this.captchaProvider() in this.captchaVendors()) && "sitekey" in this.config.captcha && this.config.captcha.sitekey !== "";
415
+ }
416
+ captchaProvider() {
417
+ return this.config.captcha && this.config.captcha.provider || "hcaptcha";
418
+ }
419
+ captchaVendors() {
420
+ return {
421
+ hcaptcha: {
422
+ script: "https://js.hcaptcha.com/1/api.js?render=explicit",
423
+ langKey: "hl",
424
+ field: "h-captcha-response"
425
+ },
426
+ turnstile: {
427
+ script: "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",
428
+ langKey: "language",
429
+ field: "cf-turnstile-response"
430
+ }
431
+ };
432
+ }
433
+ captchaVendor() {
434
+ return this.captchaVendors()[this.captchaProvider()] || this.captchaVendors().hcaptcha;
435
+ }
436
+ captchaApi() {
437
+ return window[this.captchaProvider()];
415
438
  }
416
439
  passkeyEnabled() {
417
440
  return this.config.passkeys !== false;
@@ -486,8 +509,8 @@ var jwtbutler = class {
486
509
  sitekey: this.config.captcha.sitekey,
487
510
  theme: this.config.captcha.theme || "light"
488
511
  };
489
- if (this.config.language !== "") options.hl = this.config.language;
490
- captcha.setAttribute("data-widget-id", window.hcaptcha.render(captcha, options));
512
+ if (this.config.language !== "") options[this.captchaVendor().langKey] = this.config.language;
513
+ captcha.setAttribute("data-widget-id", this.captchaApi().render(captcha, options));
491
514
  resolve();
492
515
  }).catch((error) => {
493
516
  reject(error);
@@ -496,20 +519,21 @@ var jwtbutler = class {
496
519
  }
497
520
  captchaLoad() {
498
521
  return new Promise((resolve, reject) => {
499
- if (!this.captchaEnabled() || "hcaptcha" in window) {
522
+ if (!this.captchaEnabled() || this.captchaProvider() in window) {
500
523
  resolve();
501
524
  return;
502
525
  }
503
- if (document.querySelector("script[data-jwtbutler-hcaptcha]") !== null) {
504
- document.querySelector("script[data-jwtbutler-hcaptcha]").addEventListener("load", () => resolve());
505
- document.querySelector("script[data-jwtbutler-hcaptcha]").addEventListener("error", (error) => reject(error));
526
+ let selector = "script[data-jwtbutler-captcha=\"" + this.captchaProvider() + "\"]";
527
+ if (document.querySelector(selector) !== null) {
528
+ document.querySelector(selector).addEventListener("load", () => resolve());
529
+ document.querySelector(selector).addEventListener("error", (error) => reject(error));
506
530
  return;
507
531
  }
508
532
  let script = document.createElement("script");
509
- script.setAttribute("src", "https://js.hcaptcha.com/1/api.js?render=explicit");
533
+ script.setAttribute("src", this.captchaVendor().script);
510
534
  script.setAttribute("async", "async");
511
535
  script.setAttribute("defer", "defer");
512
- script.setAttribute("data-jwtbutler-hcaptcha", "true");
536
+ script.setAttribute("data-jwtbutler-captcha", this.captchaProvider());
513
537
  script.addEventListener("load", () => resolve());
514
538
  script.addEventListener("error", (error) => reject(error));
515
539
  document.head.appendChild(script);
@@ -522,14 +546,14 @@ var jwtbutler = class {
522
546
  return;
523
547
  }
524
548
  let captcha = form.querySelector("." + this.config.login_form_class + "__captcha");
525
- if (captcha === null || !("hcaptcha" in window)) {
549
+ if (captcha === null || !(this.captchaProvider() in window)) {
526
550
  reject({
527
551
  message: "captcha not successful",
528
552
  public_message: "Captcha not successful"
529
553
  });
530
554
  return;
531
555
  }
532
- let token = window.hcaptcha.getResponse(captcha.getAttribute("data-widget-id"));
556
+ let token = this.captchaApi().getResponse(captcha.getAttribute("data-widget-id"));
533
557
  if (token === "") {
534
558
  reject({
535
559
  message: "captcha not successful",
@@ -541,10 +565,10 @@ var jwtbutler = class {
541
565
  });
542
566
  }
543
567
  captchaReset(form) {
544
- if (!this.captchaEnabled() || !("hcaptcha" in window)) return;
568
+ if (!this.captchaEnabled() || !(this.captchaProvider() in window)) return;
545
569
  let captcha = form.querySelector("." + this.config.login_form_class + "__captcha");
546
570
  if (captcha === null || captcha.getAttribute("data-widget-id") === null) return;
547
- window.hcaptcha.reset(captcha.getAttribute("data-widget-id"));
571
+ this.captchaApi().reset(captcha.getAttribute("data-widget-id"));
548
572
  }
549
573
  bindLoginFormSubmit() {
550
574
  return new Promise((resolve, reject) => {
@@ -558,7 +582,7 @@ var jwtbutler = class {
558
582
  password: form.querySelector("input[name=\"password\"]").value
559
583
  };
560
584
  this.captchaToken(form).then((captchaToken) => {
561
- if (captchaToken !== null) body["h-captcha-response"] = captchaToken;
585
+ if (captchaToken !== null) body[this.captchaVendor().field] = captchaToken;
562
586
  return fetch(this.config.auth_server + "/login", {
563
587
  method: "POST",
564
588
  body: JSON.stringify(body),
package/package.json CHANGED
@@ -67,7 +67,7 @@
67
67
  "uuid": "^11.1.1"
68
68
  },
69
69
  "name": "jwtbutler",
70
- "version": "2.0.2",
70
+ "version": "2.0.3",
71
71
  "description": "",
72
72
  "main": "_js/_build/script.js",
73
73
  "files": [