@simplewebauthn/browser 11.0.0 → 12.0.0

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.
Files changed (110) hide show
  1. package/README.md +34 -24
  2. package/dist/bundle/index.es5.umd.min.js +2 -2
  3. package/dist/bundle/index.umd.min.js +2 -2
  4. package/esm/helpers/base64URLStringToBuffer.d.ts +9 -0
  5. package/esm/helpers/base64URLStringToBuffer.d.ts.map +1 -0
  6. package/esm/helpers/base64URLStringToBuffer.js +29 -0
  7. package/esm/helpers/browserSupportsWebAuthn.d.ts +8 -0
  8. package/esm/helpers/browserSupportsWebAuthn.d.ts.map +1 -0
  9. package/esm/helpers/browserSupportsWebAuthn.js +11 -0
  10. package/esm/helpers/browserSupportsWebAuthnAutofill.d.ts +9 -0
  11. package/esm/helpers/browserSupportsWebAuthnAutofill.d.ts.map +1 -0
  12. package/esm/helpers/browserSupportsWebAuthnAutofill.js +26 -0
  13. package/esm/helpers/bufferToBase64URLString.d.ts +8 -0
  14. package/esm/helpers/bufferToBase64URLString.d.ts.map +1 -0
  15. package/esm/helpers/bufferToBase64URLString.js +15 -0
  16. package/esm/helpers/identifyAuthenticationError.d.ts +9 -0
  17. package/esm/helpers/identifyAuthenticationError.d.ts.map +1 -0
  18. package/esm/helpers/identifyAuthenticationError.js +61 -0
  19. package/esm/helpers/identifyRegistrationError.d.ts +9 -0
  20. package/esm/helpers/identifyRegistrationError.d.ts.map +1 -0
  21. package/esm/helpers/identifyRegistrationError.js +126 -0
  22. package/esm/helpers/isValidDomain.d.ts +10 -0
  23. package/esm/helpers/isValidDomain.d.ts.map +1 -0
  24. package/esm/helpers/isValidDomain.js +14 -0
  25. package/esm/helpers/platformAuthenticatorIsAvailable.d.ts +8 -0
  26. package/esm/helpers/platformAuthenticatorIsAvailable.d.ts.map +1 -0
  27. package/esm/helpers/platformAuthenticatorIsAvailable.js +13 -0
  28. package/{dist/types → esm}/helpers/toAuthenticatorAttachment.d.ts +4 -0
  29. package/esm/helpers/toAuthenticatorAttachment.d.ts.map +1 -0
  30. package/esm/helpers/toAuthenticatorAttachment.js +13 -0
  31. package/esm/helpers/toPublicKeyCredentialDescriptor.d.ts +3 -0
  32. package/esm/helpers/toPublicKeyCredentialDescriptor.d.ts.map +1 -0
  33. package/esm/helpers/toPublicKeyCredentialDescriptor.js +14 -0
  34. package/esm/helpers/webAuthnAbortService.d.ts +14 -0
  35. package/esm/helpers/webAuthnAbortService.d.ts.map +1 -0
  36. package/esm/helpers/webAuthnAbortService.js +45 -0
  37. package/{dist/types → esm}/helpers/webAuthnError.d.ts +18 -0
  38. package/esm/helpers/webAuthnError.d.ts.map +1 -0
  39. package/esm/helpers/webAuthnError.js +31 -0
  40. package/esm/index.d.ts +16 -0
  41. package/esm/index.d.ts.map +1 -0
  42. package/{dist/types/index.d.ts → esm/index.js} +13 -10
  43. package/esm/methods/startAuthentication.d.ts +15 -0
  44. package/esm/methods/startAuthentication.d.ts.map +1 -0
  45. package/esm/methods/startAuthentication.js +89 -0
  46. package/esm/methods/startRegistration.d.ts +13 -0
  47. package/esm/methods/startRegistration.d.ts.map +1 -0
  48. package/esm/methods/startRegistration.js +115 -0
  49. package/esm/package.json +3 -0
  50. package/package.json +25 -29
  51. package/script/helpers/base64URLStringToBuffer.d.ts +9 -0
  52. package/script/helpers/base64URLStringToBuffer.d.ts.map +1 -0
  53. package/script/helpers/base64URLStringToBuffer.js +32 -0
  54. package/script/helpers/browserSupportsWebAuthn.d.ts +8 -0
  55. package/script/helpers/browserSupportsWebAuthn.d.ts.map +1 -0
  56. package/script/helpers/browserSupportsWebAuthn.js +15 -0
  57. package/script/helpers/browserSupportsWebAuthnAutofill.d.ts +9 -0
  58. package/script/helpers/browserSupportsWebAuthnAutofill.d.ts.map +1 -0
  59. package/script/helpers/browserSupportsWebAuthnAutofill.js +30 -0
  60. package/script/helpers/bufferToBase64URLString.d.ts +8 -0
  61. package/script/helpers/bufferToBase64URLString.d.ts.map +1 -0
  62. package/script/helpers/bufferToBase64URLString.js +18 -0
  63. package/script/helpers/identifyAuthenticationError.d.ts +9 -0
  64. package/script/helpers/identifyAuthenticationError.d.ts.map +1 -0
  65. package/script/helpers/identifyAuthenticationError.js +64 -0
  66. package/script/helpers/identifyRegistrationError.d.ts +9 -0
  67. package/script/helpers/identifyRegistrationError.d.ts.map +1 -0
  68. package/script/helpers/identifyRegistrationError.js +129 -0
  69. package/script/helpers/isValidDomain.d.ts +10 -0
  70. package/script/helpers/isValidDomain.d.ts.map +1 -0
  71. package/script/helpers/isValidDomain.js +17 -0
  72. package/script/helpers/platformAuthenticatorIsAvailable.d.ts +8 -0
  73. package/script/helpers/platformAuthenticatorIsAvailable.d.ts.map +1 -0
  74. package/script/helpers/platformAuthenticatorIsAvailable.js +16 -0
  75. package/script/helpers/toAuthenticatorAttachment.d.ts +6 -0
  76. package/script/helpers/toAuthenticatorAttachment.d.ts.map +1 -0
  77. package/script/helpers/toAuthenticatorAttachment.js +16 -0
  78. package/script/helpers/toPublicKeyCredentialDescriptor.d.ts +3 -0
  79. package/script/helpers/toPublicKeyCredentialDescriptor.d.ts.map +1 -0
  80. package/script/helpers/toPublicKeyCredentialDescriptor.js +17 -0
  81. package/script/helpers/webAuthnAbortService.d.ts +14 -0
  82. package/script/helpers/webAuthnAbortService.d.ts.map +1 -0
  83. package/script/helpers/webAuthnAbortService.js +48 -0
  84. package/script/helpers/webAuthnError.d.ts +28 -0
  85. package/script/helpers/webAuthnError.d.ts.map +1 -0
  86. package/script/helpers/webAuthnError.js +35 -0
  87. package/script/index.d.ts +16 -0
  88. package/script/index.d.ts.map +1 -0
  89. package/script/index.js +25 -0
  90. package/script/methods/startAuthentication.d.ts +15 -0
  91. package/script/methods/startAuthentication.d.ts.map +1 -0
  92. package/script/methods/startAuthentication.js +92 -0
  93. package/script/methods/startRegistration.d.ts +13 -0
  94. package/script/methods/startRegistration.d.ts.map +1 -0
  95. package/script/methods/startRegistration.js +118 -0
  96. package/script/package.json +3 -0
  97. package/dist/bundle/index.js +0 -398
  98. package/dist/types/helpers/base64URLStringToBuffer.d.ts +0 -1
  99. package/dist/types/helpers/browserSupportsWebAuthn.d.ts +0 -1
  100. package/dist/types/helpers/browserSupportsWebAuthnAutofill.d.ts +0 -1
  101. package/dist/types/helpers/bufferToBase64URLString.d.ts +0 -1
  102. package/dist/types/helpers/identifyAuthenticationError.d.ts +0 -5
  103. package/dist/types/helpers/identifyRegistrationError.d.ts +0 -5
  104. package/dist/types/helpers/isValidDomain.d.ts +0 -1
  105. package/dist/types/helpers/platformAuthenticatorIsAvailable.d.ts +0 -1
  106. package/dist/types/helpers/toPublicKeyCredentialDescriptor.d.ts +0 -2
  107. package/dist/types/helpers/webAuthnAbortService.d.ts +0 -7
  108. package/dist/types/methods/startAuthentication.d.ts +0 -7
  109. package/dist/types/methods/startRegistration.d.ts +0 -6
  110. package/jest-environment.js +0 -29
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Determine whether the browser can communicate with a built-in authenticator, like
3
+ * Touch ID, Android fingerprint scanner, or Windows Hello.
4
+ *
5
+ * This method will _not_ be able to tell you the name of the platform authenticator.
6
+ */
7
+ export declare function platformAuthenticatorIsAvailable(): Promise<boolean>;
8
+ //# sourceMappingURL=platformAuthenticatorIsAvailable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platformAuthenticatorIsAvailable.d.ts","sourceRoot":"","sources":["../../src/helpers/platformAuthenticatorIsAvailable.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,gCAAgC,IAAI,OAAO,CAAC,OAAO,CAAC,CAMnE"}
@@ -0,0 +1,13 @@
1
+ import { browserSupportsWebAuthn } from './browserSupportsWebAuthn.js';
2
+ /**
3
+ * Determine whether the browser can communicate with a built-in authenticator, like
4
+ * Touch ID, Android fingerprint scanner, or Windows Hello.
5
+ *
6
+ * This method will _not_ be able to tell you the name of the platform authenticator.
7
+ */
8
+ export function platformAuthenticatorIsAvailable() {
9
+ if (!browserSupportsWebAuthn()) {
10
+ return new Promise((resolve) => resolve(false));
11
+ }
12
+ return PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
13
+ }
@@ -1,2 +1,6 @@
1
1
  import { AuthenticatorAttachment } from '@simplewebauthn/types';
2
+ /**
3
+ * If possible coerce a `string` value into a known `AuthenticatorAttachment`
4
+ */
2
5
  export declare function toAuthenticatorAttachment(attachment: string | null): AuthenticatorAttachment | undefined;
6
+ //# sourceMappingURL=toAuthenticatorAttachment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toAuthenticatorAttachment.d.ts","sourceRoot":"","sources":["../../src/helpers/toAuthenticatorAttachment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAIhE;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,MAAM,GAAG,IAAI,GACxB,uBAAuB,GAAG,SAAS,CAUrC"}
@@ -0,0 +1,13 @@
1
+ const attachments = ['cross-platform', 'platform'];
2
+ /**
3
+ * If possible coerce a `string` value into a known `AuthenticatorAttachment`
4
+ */
5
+ export function toAuthenticatorAttachment(attachment) {
6
+ if (!attachment) {
7
+ return;
8
+ }
9
+ if (attachments.indexOf(attachment) < 0) {
10
+ return;
11
+ }
12
+ return attachment;
13
+ }
@@ -0,0 +1,3 @@
1
+ import type { PublicKeyCredentialDescriptor, PublicKeyCredentialDescriptorJSON } from '@simplewebauthn/types';
2
+ export declare function toPublicKeyCredentialDescriptor(descriptor: PublicKeyCredentialDescriptorJSON): PublicKeyCredentialDescriptor;
3
+ //# sourceMappingURL=toPublicKeyCredentialDescriptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toPublicKeyCredentialDescriptor.d.ts","sourceRoot":"","sources":["../../src/helpers/toPublicKeyCredentialDescriptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,6BAA6B,EAC7B,iCAAiC,EAClC,MAAM,uBAAuB,CAAC;AAI/B,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,iCAAiC,GAC5C,6BAA6B,CAa/B"}
@@ -0,0 +1,14 @@
1
+ import { base64URLStringToBuffer } from './base64URLStringToBuffer.js';
2
+ export function toPublicKeyCredentialDescriptor(descriptor) {
3
+ const { id } = descriptor;
4
+ return {
5
+ ...descriptor,
6
+ id: base64URLStringToBuffer(id),
7
+ /**
8
+ * `descriptor.transports` is an array of our `AuthenticatorTransportFuture` that includes newer
9
+ * transports that TypeScript's DOM lib is ignorant of. Convince TS that our list of transports
10
+ * are fine to pass to WebAuthn since browsers will recognize the new value.
11
+ */
12
+ transports: descriptor.transports,
13
+ };
14
+ }
@@ -0,0 +1,14 @@
1
+ interface WebAuthnAbortService {
2
+ createNewAbortSignal(): AbortSignal;
3
+ cancelCeremony(): void;
4
+ }
5
+ /**
6
+ * A service singleton to help ensure that only a single WebAuthn ceremony is active at a time.
7
+ *
8
+ * Users of **@simplewebauthn/browser** shouldn't typically need to use this, but it can help e.g.
9
+ * developers building projects that use client-side routing to better control the behavior of
10
+ * their UX in response to router navigation events.
11
+ */
12
+ export declare const WebAuthnAbortService: WebAuthnAbortService;
13
+ export {};
14
+ //# sourceMappingURL=webAuthnAbortService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webAuthnAbortService.d.ts","sourceRoot":"","sources":["../../src/helpers/webAuthnAbortService.ts"],"names":[],"mappings":"AAAA,UAAU,oBAAoB;IAC5B,oBAAoB,IAAI,WAAW,CAAC;IACpC,cAAc,IAAI,IAAI,CAAC;CACxB;AA0CD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,EAAE,oBAAqD,CAAC"}
@@ -0,0 +1,45 @@
1
+ class BaseWebAuthnAbortService {
2
+ constructor() {
3
+ Object.defineProperty(this, "controller", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: void 0
8
+ });
9
+ }
10
+ /**
11
+ * Prepare an abort signal that will help support multiple auth attempts without needing to
12
+ * reload the page. This is automatically called whenever `startRegistration()` and
13
+ * `startAuthentication()` are called.
14
+ */
15
+ createNewAbortSignal() {
16
+ // Abort any existing calls to navigator.credentials.create() or navigator.credentials.get()
17
+ if (this.controller) {
18
+ const abortError = new Error('Cancelling existing WebAuthn API call for new one');
19
+ abortError.name = 'AbortError';
20
+ this.controller.abort(abortError);
21
+ }
22
+ const newController = new AbortController();
23
+ this.controller = newController;
24
+ return newController.signal;
25
+ }
26
+ /**
27
+ * Manually cancel any active WebAuthn registration or authentication attempt.
28
+ */
29
+ cancelCeremony() {
30
+ if (this.controller) {
31
+ const abortError = new Error('Manually cancelling existing WebAuthn API call');
32
+ abortError.name = 'AbortError';
33
+ this.controller.abort(abortError);
34
+ this.controller = undefined;
35
+ }
36
+ }
37
+ }
38
+ /**
39
+ * A service singleton to help ensure that only a single WebAuthn ceremony is active at a time.
40
+ *
41
+ * Users of **@simplewebauthn/browser** shouldn't typically need to use this, but it can help e.g.
42
+ * developers building projects that use client-side routing to better control the behavior of
43
+ * their UX in response to router navigation events.
44
+ */
45
+ export const WebAuthnAbortService = new BaseWebAuthnAbortService();
@@ -1,3 +1,20 @@
1
+ /**
2
+ * A custom Error used to return a more nuanced error detailing _why_ one of the eight documented
3
+ * errors in the spec was raised after calling `navigator.credentials.create()` or
4
+ * `navigator.credentials.get()`:
5
+ *
6
+ * - `AbortError`
7
+ * - `ConstraintError`
8
+ * - `InvalidStateError`
9
+ * - `NotAllowedError`
10
+ * - `NotSupportedError`
11
+ * - `SecurityError`
12
+ * - `TypeError`
13
+ * - `UnknownError`
14
+ *
15
+ * Error messages were determined through investigation of the spec to determine under which
16
+ * scenarios a given error would be raised.
17
+ */
1
18
  export declare class WebAuthnError extends Error {
2
19
  code: WebAuthnErrorCode;
3
20
  constructor({ message, code, cause, name, }: {
@@ -8,3 +25,4 @@ export declare class WebAuthnError extends Error {
8
25
  });
9
26
  }
10
27
  export type WebAuthnErrorCode = 'ERROR_CEREMONY_ABORTED' | 'ERROR_INVALID_DOMAIN' | 'ERROR_INVALID_RP_ID' | 'ERROR_INVALID_USER_ID_LENGTH' | 'ERROR_MALFORMED_PUBKEYCREDPARAMS' | 'ERROR_AUTHENTICATOR_GENERAL_ERROR' | 'ERROR_AUTHENTICATOR_MISSING_DISCOVERABLE_CREDENTIAL_SUPPORT' | 'ERROR_AUTHENTICATOR_MISSING_USER_VERIFICATION_SUPPORT' | 'ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED' | 'ERROR_AUTHENTICATOR_NO_SUPPORTED_PUBKEYCREDPARAMS_ALG' | 'ERROR_AUTO_REGISTER_USER_VERIFICATION_FAILURE' | 'ERROR_PASSTHROUGH_SEE_CAUSE_PROPERTY';
28
+ //# sourceMappingURL=webAuthnError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webAuthnError.d.ts","sourceRoot":"","sources":["../../src/helpers/webAuthnError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACtC,IAAI,EAAE,iBAAiB,CAAC;gBAEZ,EACV,OAAO,EACP,IAAI,EACJ,KAAK,EACL,IAAI,GACL,EAAE;QACD,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,iBAAiB,CAAC;QACxB,KAAK,EAAE,KAAK,CAAC;QACb,IAAI,CAAC,EAAE,MAAM,CAAC;KACf;CAMF;AAED,MAAM,MAAM,iBAAiB,GACzB,wBAAwB,GACxB,sBAAsB,GACtB,qBAAqB,GACrB,8BAA8B,GAC9B,kCAAkC,GAClC,mCAAmC,GACnC,6DAA6D,GAC7D,uDAAuD,GACvD,2CAA2C,GAC3C,uDAAuD,GACvD,+CAA+C,GAC/C,sCAAsC,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * A custom Error used to return a more nuanced error detailing _why_ one of the eight documented
3
+ * errors in the spec was raised after calling `navigator.credentials.create()` or
4
+ * `navigator.credentials.get()`:
5
+ *
6
+ * - `AbortError`
7
+ * - `ConstraintError`
8
+ * - `InvalidStateError`
9
+ * - `NotAllowedError`
10
+ * - `NotSupportedError`
11
+ * - `SecurityError`
12
+ * - `TypeError`
13
+ * - `UnknownError`
14
+ *
15
+ * Error messages were determined through investigation of the spec to determine under which
16
+ * scenarios a given error would be raised.
17
+ */
18
+ export class WebAuthnError extends Error {
19
+ constructor({ message, code, cause, name, }) {
20
+ // @ts-ignore: help Rollup understand that `cause` is okay to set
21
+ super(message, { cause });
22
+ Object.defineProperty(this, "code", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.name = name ?? cause.name;
29
+ this.code = code;
30
+ }
31
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * @module @simplewebauthn/browser
4
+ */
5
+ import { startRegistration } from './methods/startRegistration.js';
6
+ import { startAuthentication } from './methods/startAuthentication.js';
7
+ import { browserSupportsWebAuthn } from './helpers/browserSupportsWebAuthn.js';
8
+ import { platformAuthenticatorIsAvailable } from './helpers/platformAuthenticatorIsAvailable.js';
9
+ import { browserSupportsWebAuthnAutofill } from './helpers/browserSupportsWebAuthnAutofill.js';
10
+ import { base64URLStringToBuffer } from './helpers/base64URLStringToBuffer.js';
11
+ import { bufferToBase64URLString } from './helpers/bufferToBase64URLString.js';
12
+ import { WebAuthnAbortService } from './helpers/webAuthnAbortService.js';
13
+ import { WebAuthnError } from './helpers/webAuthnError.js';
14
+ export { base64URLStringToBuffer, browserSupportsWebAuthn, browserSupportsWebAuthnAutofill, bufferToBase64URLString, platformAuthenticatorIsAvailable, startAuthentication, startRegistration, WebAuthnAbortService, WebAuthnError, };
15
+ export type { WebAuthnErrorCode } from './helpers/webAuthnError.js';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,gCAAgC,EAAE,MAAM,+CAA+C,CAAC;AACjG,OAAO,EAAE,+BAA+B,EAAE,MAAM,8CAA8C,CAAC;AAC/F,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,+BAA+B,EAC/B,uBAAuB,EACvB,gCAAgC,EAChC,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,GACd,CAAC;AAEF,YAAY,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC"}
@@ -1,11 +1,14 @@
1
- import { startRegistration } from './methods/startRegistration';
2
- import { startAuthentication } from './methods/startAuthentication';
3
- import { browserSupportsWebAuthn } from './helpers/browserSupportsWebAuthn';
4
- import { platformAuthenticatorIsAvailable } from './helpers/platformAuthenticatorIsAvailable';
5
- import { browserSupportsWebAuthnAutofill } from './helpers/browserSupportsWebAuthnAutofill';
6
- import { base64URLStringToBuffer } from './helpers/base64URLStringToBuffer';
7
- import { bufferToBase64URLString } from './helpers/bufferToBase64URLString';
8
- import { WebAuthnAbortService } from './helpers/webAuthnAbortService';
9
- import { WebAuthnError } from './helpers/webAuthnError';
1
+ /**
2
+ * @packageDocumentation
3
+ * @module @simplewebauthn/browser
4
+ */
5
+ import { startRegistration } from './methods/startRegistration.js';
6
+ import { startAuthentication } from './methods/startAuthentication.js';
7
+ import { browserSupportsWebAuthn } from './helpers/browserSupportsWebAuthn.js';
8
+ import { platformAuthenticatorIsAvailable } from './helpers/platformAuthenticatorIsAvailable.js';
9
+ import { browserSupportsWebAuthnAutofill } from './helpers/browserSupportsWebAuthnAutofill.js';
10
+ import { base64URLStringToBuffer } from './helpers/base64URLStringToBuffer.js';
11
+ import { bufferToBase64URLString } from './helpers/bufferToBase64URLString.js';
12
+ import { WebAuthnAbortService } from './helpers/webAuthnAbortService.js';
13
+ import { WebAuthnError } from './helpers/webAuthnError.js';
10
14
  export { base64URLStringToBuffer, browserSupportsWebAuthn, browserSupportsWebAuthnAutofill, bufferToBase64URLString, platformAuthenticatorIsAvailable, startAuthentication, startRegistration, WebAuthnAbortService, WebAuthnError, };
11
- export type { WebAuthnErrorCode } from './helpers/webAuthnError';
@@ -0,0 +1,15 @@
1
+ import { AuthenticationResponseJSON, PublicKeyCredentialRequestOptionsJSON } from '@simplewebauthn/types';
2
+ export type StartAuthenticationOpts = {
3
+ optionsJSON: PublicKeyCredentialRequestOptionsJSON;
4
+ useBrowserAutofill?: boolean;
5
+ verifyBrowserAutofillInput?: boolean;
6
+ };
7
+ /**
8
+ * Begin authenticator "login" via WebAuthn assertion
9
+ *
10
+ * @param optionsJSON Output from **@simplewebauthn/server**'s `generateAuthenticationOptions()`
11
+ * @param useBrowserAutofill (Optional) Initialize conditional UI to enable logging in via browser autofill prompts. Defaults to `false`.
12
+ * @param verifyBrowserAutofillInput (Optional) Ensure a suitable `<input>` element is present when `useBrowserAutofill` is `true`. Defaults to `true`.
13
+ */
14
+ export declare function startAuthentication(options: StartAuthenticationOpts): Promise<AuthenticationResponseJSON>;
15
+ //# sourceMappingURL=startAuthentication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startAuthentication.d.ts","sourceRoot":"","sources":["../../src/methods/startAuthentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,0BAA0B,EAC1B,qCAAqC,EACtC,MAAM,uBAAuB,CAAC;AAW/B,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,EAAE,qCAAqC,CAAC;IACnD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACtC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,0BAA0B,CAAC,CAkGrC"}
@@ -0,0 +1,89 @@
1
+ import { bufferToBase64URLString } from '../helpers/bufferToBase64URLString.js';
2
+ import { base64URLStringToBuffer } from '../helpers/base64URLStringToBuffer.js';
3
+ import { browserSupportsWebAuthn } from '../helpers/browserSupportsWebAuthn.js';
4
+ import { browserSupportsWebAuthnAutofill } from '../helpers/browserSupportsWebAuthnAutofill.js';
5
+ import { toPublicKeyCredentialDescriptor } from '../helpers/toPublicKeyCredentialDescriptor.js';
6
+ import { identifyAuthenticationError } from '../helpers/identifyAuthenticationError.js';
7
+ import { WebAuthnAbortService } from '../helpers/webAuthnAbortService.js';
8
+ import { toAuthenticatorAttachment } from '../helpers/toAuthenticatorAttachment.js';
9
+ /**
10
+ * Begin authenticator "login" via WebAuthn assertion
11
+ *
12
+ * @param optionsJSON Output from **@simplewebauthn/server**'s `generateAuthenticationOptions()`
13
+ * @param useBrowserAutofill (Optional) Initialize conditional UI to enable logging in via browser autofill prompts. Defaults to `false`.
14
+ * @param verifyBrowserAutofillInput (Optional) Ensure a suitable `<input>` element is present when `useBrowserAutofill` is `true`. Defaults to `true`.
15
+ */
16
+ export async function startAuthentication(options) {
17
+ const { optionsJSON, useBrowserAutofill = false, verifyBrowserAutofillInput = true, } = options;
18
+ if (!browserSupportsWebAuthn()) {
19
+ throw new Error('WebAuthn is not supported in this browser');
20
+ }
21
+ // We need to avoid passing empty array to avoid blocking retrieval
22
+ // of public key
23
+ let allowCredentials;
24
+ if (optionsJSON.allowCredentials?.length !== 0) {
25
+ allowCredentials = optionsJSON.allowCredentials?.map(toPublicKeyCredentialDescriptor);
26
+ }
27
+ // We need to convert some values to Uint8Arrays before passing the credentials to the navigator
28
+ const publicKey = {
29
+ ...optionsJSON,
30
+ challenge: base64URLStringToBuffer(optionsJSON.challenge),
31
+ allowCredentials,
32
+ };
33
+ // Prepare options for `.get()`
34
+ const getOptions = {};
35
+ /**
36
+ * Set up the page to prompt the user to select a credential for authentication via the browser's
37
+ * input autofill mechanism.
38
+ */
39
+ if (useBrowserAutofill) {
40
+ if (!(await browserSupportsWebAuthnAutofill())) {
41
+ throw Error('Browser does not support WebAuthn autofill');
42
+ }
43
+ // Check for an <input> with "webauthn" in its `autocomplete` attribute
44
+ const eligibleInputs = document.querySelectorAll("input[autocomplete$='webauthn']");
45
+ // WebAuthn autofill requires at least one valid input
46
+ if (eligibleInputs.length < 1 && verifyBrowserAutofillInput) {
47
+ throw Error('No <input> with "webauthn" as the only or last value in its `autocomplete` attribute was detected');
48
+ }
49
+ // `CredentialMediationRequirement` doesn't know about "conditional" yet as of
50
+ // typescript@4.6.3
51
+ getOptions.mediation = 'conditional';
52
+ // Conditional UI requires an empty allow list
53
+ publicKey.allowCredentials = [];
54
+ }
55
+ // Finalize options
56
+ getOptions.publicKey = publicKey;
57
+ // Set up the ability to cancel this request if the user attempts another
58
+ getOptions.signal = WebAuthnAbortService.createNewAbortSignal();
59
+ // Wait for the user to complete assertion
60
+ let credential;
61
+ try {
62
+ credential = (await navigator.credentials.get(getOptions));
63
+ }
64
+ catch (err) {
65
+ throw identifyAuthenticationError({ error: err, options: getOptions });
66
+ }
67
+ if (!credential) {
68
+ throw new Error('Authentication was not completed');
69
+ }
70
+ const { id, rawId, response, type } = credential;
71
+ let userHandle = undefined;
72
+ if (response.userHandle) {
73
+ userHandle = bufferToBase64URLString(response.userHandle);
74
+ }
75
+ // Convert values to base64 to make it easier to send back to the server
76
+ return {
77
+ id,
78
+ rawId: bufferToBase64URLString(rawId),
79
+ response: {
80
+ authenticatorData: bufferToBase64URLString(response.authenticatorData),
81
+ clientDataJSON: bufferToBase64URLString(response.clientDataJSON),
82
+ signature: bufferToBase64URLString(response.signature),
83
+ userHandle,
84
+ },
85
+ type,
86
+ clientExtensionResults: credential.getClientExtensionResults(),
87
+ authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment),
88
+ };
89
+ }
@@ -0,0 +1,13 @@
1
+ import { PublicKeyCredentialCreationOptionsJSON, RegistrationResponseJSON } from '@simplewebauthn/types';
2
+ export type StartRegistrationOpts = {
3
+ optionsJSON: PublicKeyCredentialCreationOptionsJSON;
4
+ useAutoRegister?: boolean;
5
+ };
6
+ /**
7
+ * Begin authenticator "registration" via WebAuthn attestation
8
+ *
9
+ * @param optionsJSON Output from **@simplewebauthn/server**'s `generateRegistrationOptions()`
10
+ * @param useAutoRegister (Optional) Try to silently create a passkey with the password manager that the user just signed in with. Defaults to `false`.
11
+ */
12
+ export declare function startRegistration(options: StartRegistrationOpts): Promise<RegistrationResponseJSON>;
13
+ //# sourceMappingURL=startRegistration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startRegistration.d.ts","sourceRoot":"","sources":["../../src/methods/startRegistration.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sCAAsC,EAEtC,wBAAwB,EACzB,MAAM,uBAAuB,CAAC;AAU/B,MAAM,MAAM,qBAAqB,GAAG;IAClC,WAAW,EAAE,sCAAsC,CAAC;IACpD,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,wBAAwB,CAAC,CA6GnC"}
@@ -0,0 +1,115 @@
1
+ import { bufferToBase64URLString } from '../helpers/bufferToBase64URLString.js';
2
+ import { base64URLStringToBuffer } from '../helpers/base64URLStringToBuffer.js';
3
+ import { browserSupportsWebAuthn } from '../helpers/browserSupportsWebAuthn.js';
4
+ import { toPublicKeyCredentialDescriptor } from '../helpers/toPublicKeyCredentialDescriptor.js';
5
+ import { identifyRegistrationError } from '../helpers/identifyRegistrationError.js';
6
+ import { WebAuthnAbortService } from '../helpers/webAuthnAbortService.js';
7
+ import { toAuthenticatorAttachment } from '../helpers/toAuthenticatorAttachment.js';
8
+ /**
9
+ * Begin authenticator "registration" via WebAuthn attestation
10
+ *
11
+ * @param optionsJSON Output from **@simplewebauthn/server**'s `generateRegistrationOptions()`
12
+ * @param useAutoRegister (Optional) Try to silently create a passkey with the password manager that the user just signed in with. Defaults to `false`.
13
+ */
14
+ export async function startRegistration(options) {
15
+ const { optionsJSON, useAutoRegister = false } = options;
16
+ if (!browserSupportsWebAuthn()) {
17
+ throw new Error('WebAuthn is not supported in this browser');
18
+ }
19
+ // We need to convert some values to Uint8Arrays before passing the credentials to the navigator
20
+ const publicKey = {
21
+ ...optionsJSON,
22
+ challenge: base64URLStringToBuffer(optionsJSON.challenge),
23
+ user: {
24
+ ...optionsJSON.user,
25
+ id: base64URLStringToBuffer(optionsJSON.user.id),
26
+ },
27
+ excludeCredentials: optionsJSON.excludeCredentials?.map(toPublicKeyCredentialDescriptor),
28
+ };
29
+ // Prepare options for `.create()`
30
+ const createOptions = {};
31
+ /**
32
+ * Try to use conditional create to register a passkey for the user with the password manager
33
+ * the user just used to authenticate with. The user won't be shown any prominent UI by the
34
+ * browser.
35
+ */
36
+ if (useAutoRegister) {
37
+ // @ts-ignore: `mediation` doesn't yet exist on CredentialCreationOptions but it's possible as of Sept 2024
38
+ createOptions.mediation = 'conditional';
39
+ }
40
+ // Finalize options
41
+ createOptions.publicKey = publicKey;
42
+ // Set up the ability to cancel this request if the user attempts another
43
+ createOptions.signal = WebAuthnAbortService.createNewAbortSignal();
44
+ // Wait for the user to complete attestation
45
+ let credential;
46
+ try {
47
+ credential = (await navigator.credentials.create(createOptions));
48
+ }
49
+ catch (err) {
50
+ throw identifyRegistrationError({ error: err, options: createOptions });
51
+ }
52
+ if (!credential) {
53
+ throw new Error('Registration was not completed');
54
+ }
55
+ const { id, rawId, response, type } = credential;
56
+ // Continue to play it safe with `getTransports()` for now, even when L3 types say it's required
57
+ let transports = undefined;
58
+ if (typeof response.getTransports === 'function') {
59
+ transports = response.getTransports();
60
+ }
61
+ // L3 says this is required, but browser and webview support are still not guaranteed.
62
+ let responsePublicKeyAlgorithm = undefined;
63
+ if (typeof response.getPublicKeyAlgorithm === 'function') {
64
+ try {
65
+ responsePublicKeyAlgorithm = response.getPublicKeyAlgorithm();
66
+ }
67
+ catch (error) {
68
+ warnOnBrokenImplementation('getPublicKeyAlgorithm()', error);
69
+ }
70
+ }
71
+ let responsePublicKey = undefined;
72
+ if (typeof response.getPublicKey === 'function') {
73
+ try {
74
+ const _publicKey = response.getPublicKey();
75
+ if (_publicKey !== null) {
76
+ responsePublicKey = bufferToBase64URLString(_publicKey);
77
+ }
78
+ }
79
+ catch (error) {
80
+ warnOnBrokenImplementation('getPublicKey()', error);
81
+ }
82
+ }
83
+ // L3 says this is required, but browser and webview support are still not guaranteed.
84
+ let responseAuthenticatorData;
85
+ if (typeof response.getAuthenticatorData === 'function') {
86
+ try {
87
+ responseAuthenticatorData = bufferToBase64URLString(response.getAuthenticatorData());
88
+ }
89
+ catch (error) {
90
+ warnOnBrokenImplementation('getAuthenticatorData()', error);
91
+ }
92
+ }
93
+ return {
94
+ id,
95
+ rawId: bufferToBase64URLString(rawId),
96
+ response: {
97
+ attestationObject: bufferToBase64URLString(response.attestationObject),
98
+ clientDataJSON: bufferToBase64URLString(response.clientDataJSON),
99
+ transports,
100
+ publicKeyAlgorithm: responsePublicKeyAlgorithm,
101
+ publicKey: responsePublicKey,
102
+ authenticatorData: responseAuthenticatorData,
103
+ },
104
+ type,
105
+ clientExtensionResults: credential.getClientExtensionResults(),
106
+ authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment),
107
+ };
108
+ }
109
+ /**
110
+ * Visibly warn when we detect an issue related to a passkey provider intercepting WebAuthn API
111
+ * calls
112
+ */
113
+ function warnOnBrokenImplementation(methodName, cause) {
114
+ console.warn(`The browser extension that intercepted this WebAuthn API call incorrectly implemented ${methodName}. You should report this error to them.\n`, cause);
115
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json CHANGED
@@ -1,46 +1,42 @@
1
1
  {
2
2
  "name": "@simplewebauthn/browser",
3
- "version": "11.0.0",
3
+ "version": "12.0.0",
4
4
  "description": "SimpleWebAuthn for Browsers",
5
- "main": "dist/bundle/index.js",
6
- "unpkg": "dist/bundle/index.umd.min.js",
7
- "types": "dist/types/index.d.ts",
5
+ "keywords": [
6
+ "typescript",
7
+ "webauthn",
8
+ "passkeys",
9
+ "fido",
10
+ "umd"
11
+ ],
8
12
  "author": "Matthew Miller <matthew@millerti.me>",
9
- "license": "MIT",
13
+ "homepage": "https://github.com/MasterKale/SimpleWebAuthn/tree/master/packages/browser#readme",
10
14
  "repository": {
11
15
  "type": "git",
12
16
  "url": "git+https://github.com/MasterKale/SimpleWebAuthn.git",
13
17
  "directory": "packages/browser"
14
18
  },
15
- "homepage": "https://github.com/MasterKale/SimpleWebAuthn/tree/master/packages/browser#readme",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/MasterKale/SimpleWebAuthn/issues"
22
+ },
23
+ "main": "./script/index.js",
24
+ "module": "./esm/index.js",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./esm/index.js",
28
+ "require": "./script/index.js"
29
+ }
30
+ },
16
31
  "publishConfig": {
17
32
  "access": "public"
18
33
  },
19
- "keywords": [
20
- "typescript",
21
- "webauthn",
22
- "passkeys",
23
- "fido",
24
- "umd"
25
- ],
34
+ "unpkg": "dist/bundle/index.umd.min.js",
26
35
  "dependencies": {
27
- "@simplewebauthn/types": "^11.0.0"
36
+ "@simplewebauthn/types": "^12.0.0"
28
37
  },
29
38
  "devDependencies": {
30
- "@rollup/plugin-node-resolve": "^13.0.0",
31
- "@rollup/plugin-terser": "^0.4.3",
32
- "@rollup/plugin-typescript": "^8.2.1",
33
- "rollup": "^2.52.1",
34
- "rollup-plugin-version-injector": "^1.3.3",
35
- "tslib": "^2.6.2"
39
+ "@types/node": "^20.9.0"
36
40
  },
37
- "type": "module",
38
- "gitHead": "ba039fdd5fdff87f78d3bd246e9bea5f7aa39ccb",
39
- "scripts": {
40
- "build": "rimraf dist && rollup -c",
41
- "test": "jest",
42
- "test:watch": "jest --watch",
43
- "test:coverage": "npm test -- --coverage",
44
- "prepublish": "npm run build"
45
- }
41
+ "_generatedBy": "dnt@dev"
46
42
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Convert from a Base64URL-encoded string to an Array Buffer. Best used when converting a
3
+ * credential ID from a JSON string to an ArrayBuffer, like in allowCredentials or
4
+ * excludeCredentials
5
+ *
6
+ * Helper method to compliment `bufferToBase64URLString`
7
+ */
8
+ export declare function base64URLStringToBuffer(base64URLString: string): ArrayBuffer;
9
+ //# sourceMappingURL=base64URLStringToBuffer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64URLStringToBuffer.d.ts","sourceRoot":"","sources":["../../src/helpers/base64URLStringToBuffer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,eAAe,EAAE,MAAM,GAAG,WAAW,CAyB5E"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.base64URLStringToBuffer = base64URLStringToBuffer;
4
+ /**
5
+ * Convert from a Base64URL-encoded string to an Array Buffer. Best used when converting a
6
+ * credential ID from a JSON string to an ArrayBuffer, like in allowCredentials or
7
+ * excludeCredentials
8
+ *
9
+ * Helper method to compliment `bufferToBase64URLString`
10
+ */
11
+ function base64URLStringToBuffer(base64URLString) {
12
+ // Convert from Base64URL to Base64
13
+ const base64 = base64URLString.replace(/-/g, '+').replace(/_/g, '/');
14
+ /**
15
+ * Pad with '=' until it's a multiple of four
16
+ * (4 - (85 % 4 = 1) = 3) % 4 = 3 padding
17
+ * (4 - (86 % 4 = 2) = 2) % 4 = 2 padding
18
+ * (4 - (87 % 4 = 3) = 1) % 4 = 1 padding
19
+ * (4 - (88 % 4 = 0) = 4) % 4 = 0 padding
20
+ */
21
+ const padLength = (4 - (base64.length % 4)) % 4;
22
+ const padded = base64.padEnd(base64.length + padLength, '=');
23
+ // Convert to a binary string
24
+ const binary = atob(padded);
25
+ // Convert binary string to buffer
26
+ const buffer = new ArrayBuffer(binary.length);
27
+ const bytes = new Uint8Array(buffer);
28
+ for (let i = 0; i < binary.length; i++) {
29
+ bytes[i] = binary.charCodeAt(i);
30
+ }
31
+ return buffer;
32
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Determine if the browser is capable of Webauthn
3
+ */
4
+ export declare function browserSupportsWebAuthn(): boolean;
5
+ export declare const _browserSupportsWebAuthnInternals: {
6
+ stubThis: (value: boolean) => boolean;
7
+ };
8
+ //# sourceMappingURL=browserSupportsWebAuthn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browserSupportsWebAuthn.d.ts","sourceRoot":"","sources":["../../src/helpers/browserSupportsWebAuthn.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAKjD;AAGD,eAAO,MAAM,iCAAiC;sBAC1B,OAAO;CAC1B,CAAC"}