@depup/supabase__auth-js 2.99.2-depup.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 (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/changes.json +5 -0
  4. package/dist/main/AuthAdminApi.d.ts +4 -0
  5. package/dist/main/AuthAdminApi.d.ts.map +1 -0
  6. package/dist/main/AuthAdminApi.js +7 -0
  7. package/dist/main/AuthAdminApi.js.map +1 -0
  8. package/dist/main/AuthClient.d.ts +4 -0
  9. package/dist/main/AuthClient.d.ts.map +1 -0
  10. package/dist/main/AuthClient.js +7 -0
  11. package/dist/main/AuthClient.js.map +1 -0
  12. package/dist/main/GoTrueAdminApi.d.ts +227 -0
  13. package/dist/main/GoTrueAdminApi.d.ts.map +1 -0
  14. package/dist/main/GoTrueAdminApi.js +596 -0
  15. package/dist/main/GoTrueAdminApi.js.map +1 -0
  16. package/dist/main/GoTrueClient.d.ts +783 -0
  17. package/dist/main/GoTrueClient.d.ts.map +1 -0
  18. package/dist/main/GoTrueClient.js +3029 -0
  19. package/dist/main/GoTrueClient.js.map +1 -0
  20. package/dist/main/index.d.ts +9 -0
  21. package/dist/main/index.d.ts.map +1 -0
  22. package/dist/main/index.js +20 -0
  23. package/dist/main/index.js.map +1 -0
  24. package/dist/main/lib/base64url.d.ts +76 -0
  25. package/dist/main/lib/base64url.d.ts.map +1 -0
  26. package/dist/main/lib/base64url.js +269 -0
  27. package/dist/main/lib/base64url.js.map +1 -0
  28. package/dist/main/lib/constants.d.ts +26 -0
  29. package/dist/main/lib/constants.d.ts.map +1 -0
  30. package/dist/main/lib/constants.js +31 -0
  31. package/dist/main/lib/constants.js.map +1 -0
  32. package/dist/main/lib/error-codes.d.ts +7 -0
  33. package/dist/main/lib/error-codes.d.ts.map +1 -0
  34. package/dist/main/lib/error-codes.js +3 -0
  35. package/dist/main/lib/error-codes.js.map +1 -0
  36. package/dist/main/lib/errors.d.ts +243 -0
  37. package/dist/main/lib/errors.d.ts.map +1 -0
  38. package/dist/main/lib/errors.js +289 -0
  39. package/dist/main/lib/errors.js.map +1 -0
  40. package/dist/main/lib/fetch.d.ts +34 -0
  41. package/dist/main/lib/fetch.d.ts.map +1 -0
  42. package/dist/main/lib/fetch.js +184 -0
  43. package/dist/main/lib/fetch.js.map +1 -0
  44. package/dist/main/lib/helpers.d.ts +91 -0
  45. package/dist/main/lib/helpers.d.ts.map +1 -0
  46. package/dist/main/lib/helpers.js +395 -0
  47. package/dist/main/lib/helpers.js.map +1 -0
  48. package/dist/main/lib/local-storage.d.ts +9 -0
  49. package/dist/main/lib/local-storage.d.ts.map +1 -0
  50. package/dist/main/lib/local-storage.js +21 -0
  51. package/dist/main/lib/local-storage.js.map +1 -0
  52. package/dist/main/lib/locks.d.ts +107 -0
  53. package/dist/main/lib/locks.d.ts.map +1 -0
  54. package/dist/main/lib/locks.js +314 -0
  55. package/dist/main/lib/locks.js.map +1 -0
  56. package/dist/main/lib/polyfills.d.ts +5 -0
  57. package/dist/main/lib/polyfills.d.ts.map +1 -0
  58. package/dist/main/lib/polyfills.js +29 -0
  59. package/dist/main/lib/polyfills.js.map +1 -0
  60. package/dist/main/lib/types.d.ts +1861 -0
  61. package/dist/main/lib/types.d.ts.map +1 -0
  62. package/dist/main/lib/types.js +23 -0
  63. package/dist/main/lib/types.js.map +1 -0
  64. package/dist/main/lib/version.d.ts +2 -0
  65. package/dist/main/lib/version.d.ts.map +1 -0
  66. package/dist/main/lib/version.js +11 -0
  67. package/dist/main/lib/version.js.map +1 -0
  68. package/dist/main/lib/web3/ethereum.d.ts +96 -0
  69. package/dist/main/lib/web3/ethereum.d.ts.map +1 -0
  70. package/dist/main/lib/web3/ethereum.js +66 -0
  71. package/dist/main/lib/web3/ethereum.js.map +1 -0
  72. package/dist/main/lib/web3/solana.d.ts +160 -0
  73. package/dist/main/lib/web3/solana.d.ts.map +1 -0
  74. package/dist/main/lib/web3/solana.js +4 -0
  75. package/dist/main/lib/web3/solana.js.map +1 -0
  76. package/dist/main/lib/webauthn.d.ts +276 -0
  77. package/dist/main/lib/webauthn.d.ts.map +1 -0
  78. package/dist/main/lib/webauthn.dom.d.ts +583 -0
  79. package/dist/main/lib/webauthn.dom.d.ts.map +1 -0
  80. package/dist/main/lib/webauthn.dom.js +4 -0
  81. package/dist/main/lib/webauthn.dom.js.map +1 -0
  82. package/dist/main/lib/webauthn.errors.d.ts +80 -0
  83. package/dist/main/lib/webauthn.errors.d.ts.map +1 -0
  84. package/dist/main/lib/webauthn.errors.js +265 -0
  85. package/dist/main/lib/webauthn.errors.js.map +1 -0
  86. package/dist/main/lib/webauthn.js +706 -0
  87. package/dist/main/lib/webauthn.js.map +1 -0
  88. package/dist/module/AuthAdminApi.d.ts +4 -0
  89. package/dist/module/AuthAdminApi.d.ts.map +1 -0
  90. package/dist/module/AuthAdminApi.js +4 -0
  91. package/dist/module/AuthAdminApi.js.map +1 -0
  92. package/dist/module/AuthClient.d.ts +4 -0
  93. package/dist/module/AuthClient.d.ts.map +1 -0
  94. package/dist/module/AuthClient.js +4 -0
  95. package/dist/module/AuthClient.js.map +1 -0
  96. package/dist/module/GoTrueAdminApi.d.ts +227 -0
  97. package/dist/module/GoTrueAdminApi.d.ts.map +1 -0
  98. package/dist/module/GoTrueAdminApi.js +593 -0
  99. package/dist/module/GoTrueAdminApi.js.map +1 -0
  100. package/dist/module/GoTrueClient.d.ts +783 -0
  101. package/dist/module/GoTrueClient.d.ts.map +1 -0
  102. package/dist/module/GoTrueClient.js +3026 -0
  103. package/dist/module/GoTrueClient.js.map +1 -0
  104. package/dist/module/index.d.ts +9 -0
  105. package/dist/module/index.d.ts.map +1 -0
  106. package/dist/module/index.js +9 -0
  107. package/dist/module/index.js.map +1 -0
  108. package/dist/module/lib/base64url.d.ts +76 -0
  109. package/dist/module/lib/base64url.d.ts.map +1 -0
  110. package/dist/module/lib/base64url.js +257 -0
  111. package/dist/module/lib/base64url.js.map +1 -0
  112. package/dist/module/lib/constants.d.ts +26 -0
  113. package/dist/module/lib/constants.d.ts.map +1 -0
  114. package/dist/module/lib/constants.js +28 -0
  115. package/dist/module/lib/constants.js.map +1 -0
  116. package/dist/module/lib/error-codes.d.ts +7 -0
  117. package/dist/module/lib/error-codes.d.ts.map +1 -0
  118. package/dist/module/lib/error-codes.js +2 -0
  119. package/dist/module/lib/error-codes.js.map +1 -0
  120. package/dist/module/lib/errors.d.ts +243 -0
  121. package/dist/module/lib/errors.d.ts.map +1 -0
  122. package/dist/module/lib/errors.js +266 -0
  123. package/dist/module/lib/errors.js.map +1 -0
  124. package/dist/module/lib/fetch.d.ts +34 -0
  125. package/dist/module/lib/fetch.d.ts.map +1 -0
  126. package/dist/module/lib/fetch.js +174 -0
  127. package/dist/module/lib/fetch.js.map +1 -0
  128. package/dist/module/lib/helpers.d.ts +91 -0
  129. package/dist/module/lib/helpers.d.ts.map +1 -0
  130. package/dist/module/lib/helpers.js +368 -0
  131. package/dist/module/lib/helpers.js.map +1 -0
  132. package/dist/module/lib/local-storage.d.ts +9 -0
  133. package/dist/module/lib/local-storage.d.ts.map +1 -0
  134. package/dist/module/lib/local-storage.js +18 -0
  135. package/dist/module/lib/local-storage.js.map +1 -0
  136. package/dist/module/lib/locks.d.ts +107 -0
  137. package/dist/module/lib/locks.d.ts.map +1 -0
  138. package/dist/module/lib/locks.js +306 -0
  139. package/dist/module/lib/locks.js.map +1 -0
  140. package/dist/module/lib/polyfills.d.ts +5 -0
  141. package/dist/module/lib/polyfills.d.ts.map +1 -0
  142. package/dist/module/lib/polyfills.js +26 -0
  143. package/dist/module/lib/polyfills.js.map +1 -0
  144. package/dist/module/lib/types.d.ts +1861 -0
  145. package/dist/module/lib/types.d.ts.map +1 -0
  146. package/dist/module/lib/types.js +20 -0
  147. package/dist/module/lib/types.js.map +1 -0
  148. package/dist/module/lib/version.d.ts +2 -0
  149. package/dist/module/lib/version.d.ts.map +1 -0
  150. package/dist/module/lib/version.js +8 -0
  151. package/dist/module/lib/version.js.map +1 -0
  152. package/dist/module/lib/web3/ethereum.d.ts +96 -0
  153. package/dist/module/lib/web3/ethereum.d.ts.map +1 -0
  154. package/dist/module/lib/web3/ethereum.js +60 -0
  155. package/dist/module/lib/web3/ethereum.js.map +1 -0
  156. package/dist/module/lib/web3/solana.d.ts +160 -0
  157. package/dist/module/lib/web3/solana.d.ts.map +1 -0
  158. package/dist/module/lib/web3/solana.js +3 -0
  159. package/dist/module/lib/web3/solana.js.map +1 -0
  160. package/dist/module/lib/webauthn.d.ts +276 -0
  161. package/dist/module/lib/webauthn.d.ts.map +1 -0
  162. package/dist/module/lib/webauthn.dom.d.ts +583 -0
  163. package/dist/module/lib/webauthn.dom.d.ts.map +1 -0
  164. package/dist/module/lib/webauthn.dom.js +3 -0
  165. package/dist/module/lib/webauthn.dom.js.map +1 -0
  166. package/dist/module/lib/webauthn.errors.d.ts +80 -0
  167. package/dist/module/lib/webauthn.errors.d.ts.map +1 -0
  168. package/dist/module/lib/webauthn.errors.js +257 -0
  169. package/dist/module/lib/webauthn.errors.js.map +1 -0
  170. package/dist/module/lib/webauthn.js +689 -0
  171. package/dist/module/lib/webauthn.js.map +1 -0
  172. package/dist/tsconfig.module.tsbuildinfo +1 -0
  173. package/dist/tsconfig.tsbuildinfo +1 -0
  174. package/package.json +56 -0
  175. package/src/AuthAdminApi.ts +5 -0
  176. package/src/AuthClient.ts +5 -0
  177. package/src/GoTrueAdminApi.ts +723 -0
  178. package/src/GoTrueClient.ts +4078 -0
  179. package/src/index.ts +13 -0
  180. package/src/lib/base64url.ts +308 -0
  181. package/src/lib/constants.ts +34 -0
  182. package/src/lib/error-codes.ts +90 -0
  183. package/src/lib/errors.ts +324 -0
  184. package/src/lib/fetch.ts +283 -0
  185. package/src/lib/helpers.ts +463 -0
  186. package/src/lib/local-storage.ts +21 -0
  187. package/src/lib/locks.ts +375 -0
  188. package/src/lib/polyfills.ts +23 -0
  189. package/src/lib/types.ts +2229 -0
  190. package/src/lib/version.ts +7 -0
  191. package/src/lib/web3/ethereum.ts +184 -0
  192. package/src/lib/web3/solana.ts +186 -0
  193. package/src/lib/webauthn.dom.ts +636 -0
  194. package/src/lib/webauthn.errors.ts +317 -0
  195. package/src/lib/webauthn.ts +946 -0
@@ -0,0 +1,706 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebAuthnApi = exports.DEFAULT_REQUEST_OPTIONS = exports.DEFAULT_CREATION_OPTIONS = exports.webAuthnAbortService = exports.WebAuthnAbortService = exports.identifyAuthenticationError = exports.identifyRegistrationError = exports.isWebAuthnError = exports.WebAuthnError = void 0;
4
+ exports.deserializeCredentialCreationOptions = deserializeCredentialCreationOptions;
5
+ exports.deserializeCredentialRequestOptions = deserializeCredentialRequestOptions;
6
+ exports.serializeCredentialCreationResponse = serializeCredentialCreationResponse;
7
+ exports.serializeCredentialRequestResponse = serializeCredentialRequestResponse;
8
+ exports.isValidDomain = isValidDomain;
9
+ exports.createCredential = createCredential;
10
+ exports.getCredential = getCredential;
11
+ exports.mergeCredentialCreationOptions = mergeCredentialCreationOptions;
12
+ exports.mergeCredentialRequestOptions = mergeCredentialRequestOptions;
13
+ const tslib_1 = require("tslib");
14
+ const base64url_1 = require("./base64url");
15
+ const errors_1 = require("./errors");
16
+ const helpers_1 = require("./helpers");
17
+ const webauthn_errors_1 = require("./webauthn.errors");
18
+ Object.defineProperty(exports, "identifyAuthenticationError", { enumerable: true, get: function () { return webauthn_errors_1.identifyAuthenticationError; } });
19
+ Object.defineProperty(exports, "identifyRegistrationError", { enumerable: true, get: function () { return webauthn_errors_1.identifyRegistrationError; } });
20
+ Object.defineProperty(exports, "isWebAuthnError", { enumerable: true, get: function () { return webauthn_errors_1.isWebAuthnError; } });
21
+ Object.defineProperty(exports, "WebAuthnError", { enumerable: true, get: function () { return webauthn_errors_1.WebAuthnError; } });
22
+ /**
23
+ * WebAuthn abort service to manage ceremony cancellation.
24
+ * Ensures only one WebAuthn ceremony is active at a time to prevent "operation already in progress" errors.
25
+ *
26
+ * @experimental This class is experimental and may change in future releases
27
+ * @see {@link https://w3c.github.io/webauthn/#sctn-automation-webdriver-capability W3C WebAuthn Spec - Aborting Ceremonies}
28
+ */
29
+ class WebAuthnAbortService {
30
+ /**
31
+ * Create an abort signal for a new WebAuthn operation.
32
+ * Automatically cancels any existing operation.
33
+ *
34
+ * @returns {AbortSignal} Signal to pass to navigator.credentials.create() or .get()
35
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal MDN - AbortSignal}
36
+ */
37
+ createNewAbortSignal() {
38
+ // Abort any existing calls to navigator.credentials.create() or navigator.credentials.get()
39
+ if (this.controller) {
40
+ const abortError = new Error('Cancelling existing WebAuthn API call for new one');
41
+ abortError.name = 'AbortError';
42
+ this.controller.abort(abortError);
43
+ }
44
+ const newController = new AbortController();
45
+ this.controller = newController;
46
+ return newController.signal;
47
+ }
48
+ /**
49
+ * Manually cancel the current WebAuthn operation.
50
+ * Useful for cleaning up when user cancels or navigates away.
51
+ *
52
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort MDN - AbortController.abort}
53
+ */
54
+ cancelCeremony() {
55
+ if (this.controller) {
56
+ const abortError = new Error('Manually cancelling existing WebAuthn API call');
57
+ abortError.name = 'AbortError';
58
+ this.controller.abort(abortError);
59
+ this.controller = undefined;
60
+ }
61
+ }
62
+ }
63
+ exports.WebAuthnAbortService = WebAuthnAbortService;
64
+ /**
65
+ * Singleton instance to ensure only one WebAuthn ceremony is active at a time.
66
+ * This prevents "operation already in progress" errors when retrying WebAuthn operations.
67
+ *
68
+ * @experimental This instance is experimental and may change in future releases
69
+ */
70
+ exports.webAuthnAbortService = new WebAuthnAbortService();
71
+ /**
72
+ * Convert base64url encoded strings in WebAuthn credential creation options to ArrayBuffers
73
+ * as required by the WebAuthn browser API.
74
+ * Supports both native WebAuthn Level 3 parseCreationOptionsFromJSON and manual fallback.
75
+ *
76
+ * @param {ServerCredentialCreationOptions} options - JSON options from server with base64url encoded fields
77
+ * @returns {PublicKeyCredentialCreationOptionsFuture} Options ready for navigator.credentials.create()
78
+ * @see {@link https://w3c.github.io/webauthn/#sctn-parseCreationOptionsFromJSON W3C WebAuthn Spec - parseCreationOptionsFromJSON}
79
+ */
80
+ function deserializeCredentialCreationOptions(options) {
81
+ if (!options) {
82
+ throw new Error('Credential creation options are required');
83
+ }
84
+ // Check if the native parseCreationOptionsFromJSON method is available
85
+ if (typeof PublicKeyCredential !== 'undefined' &&
86
+ 'parseCreationOptionsFromJSON' in PublicKeyCredential &&
87
+ typeof PublicKeyCredential
88
+ .parseCreationOptionsFromJSON === 'function') {
89
+ // Use the native WebAuthn Level 3 method
90
+ return PublicKeyCredential.parseCreationOptionsFromJSON(
91
+ /** we assert the options here as typescript still doesn't know about future webauthn types */
92
+ options);
93
+ }
94
+ // Fallback to manual parsing for browsers that don't support the native method
95
+ // Destructure to separate fields that need transformation
96
+ const { challenge: challengeStr, user: userOpts, excludeCredentials } = options, restOptions = tslib_1.__rest(options
97
+ // Convert challenge from base64url to ArrayBuffer
98
+ , ["challenge", "user", "excludeCredentials"]);
99
+ // Convert challenge from base64url to ArrayBuffer
100
+ const challenge = (0, base64url_1.base64UrlToUint8Array)(challengeStr).buffer;
101
+ // Convert user.id from base64url to ArrayBuffer
102
+ const user = Object.assign(Object.assign({}, userOpts), { id: (0, base64url_1.base64UrlToUint8Array)(userOpts.id).buffer });
103
+ // Build the result object
104
+ const result = Object.assign(Object.assign({}, restOptions), { challenge,
105
+ user });
106
+ // Only add excludeCredentials if it exists
107
+ if (excludeCredentials && excludeCredentials.length > 0) {
108
+ result.excludeCredentials = new Array(excludeCredentials.length);
109
+ for (let i = 0; i < excludeCredentials.length; i++) {
110
+ const cred = excludeCredentials[i];
111
+ result.excludeCredentials[i] = Object.assign(Object.assign({}, cred), { id: (0, base64url_1.base64UrlToUint8Array)(cred.id).buffer, type: cred.type || 'public-key',
112
+ // Cast transports to handle future transport types like "cable"
113
+ transports: cred.transports });
114
+ }
115
+ }
116
+ return result;
117
+ }
118
+ /**
119
+ * Convert base64url encoded strings in WebAuthn credential request options to ArrayBuffers
120
+ * as required by the WebAuthn browser API.
121
+ * Supports both native WebAuthn Level 3 parseRequestOptionsFromJSON and manual fallback.
122
+ *
123
+ * @param {ServerCredentialRequestOptions} options - JSON options from server with base64url encoded fields
124
+ * @returns {PublicKeyCredentialRequestOptionsFuture} Options ready for navigator.credentials.get()
125
+ * @see {@link https://w3c.github.io/webauthn/#sctn-parseRequestOptionsFromJSON W3C WebAuthn Spec - parseRequestOptionsFromJSON}
126
+ */
127
+ function deserializeCredentialRequestOptions(options) {
128
+ if (!options) {
129
+ throw new Error('Credential request options are required');
130
+ }
131
+ // Check if the native parseRequestOptionsFromJSON method is available
132
+ if (typeof PublicKeyCredential !== 'undefined' &&
133
+ 'parseRequestOptionsFromJSON' in PublicKeyCredential &&
134
+ typeof PublicKeyCredential
135
+ .parseRequestOptionsFromJSON === 'function') {
136
+ // Use the native WebAuthn Level 3 method
137
+ return PublicKeyCredential.parseRequestOptionsFromJSON(options);
138
+ }
139
+ // Fallback to manual parsing for browsers that don't support the native method
140
+ // Destructure to separate fields that need transformation
141
+ const { challenge: challengeStr, allowCredentials } = options, restOptions = tslib_1.__rest(options
142
+ // Convert challenge from base64url to ArrayBuffer
143
+ , ["challenge", "allowCredentials"]);
144
+ // Convert challenge from base64url to ArrayBuffer
145
+ const challenge = (0, base64url_1.base64UrlToUint8Array)(challengeStr).buffer;
146
+ // Build the result object
147
+ const result = Object.assign(Object.assign({}, restOptions), { challenge });
148
+ // Only add allowCredentials if it exists
149
+ if (allowCredentials && allowCredentials.length > 0) {
150
+ result.allowCredentials = new Array(allowCredentials.length);
151
+ for (let i = 0; i < allowCredentials.length; i++) {
152
+ const cred = allowCredentials[i];
153
+ result.allowCredentials[i] = Object.assign(Object.assign({}, cred), { id: (0, base64url_1.base64UrlToUint8Array)(cred.id).buffer, type: cred.type || 'public-key',
154
+ // Cast transports to handle future transport types like "cable"
155
+ transports: cred.transports });
156
+ }
157
+ }
158
+ return result;
159
+ }
160
+ /**
161
+ * Convert a registration/enrollment credential response to server format.
162
+ * Serializes binary fields to base64url for JSON transmission.
163
+ * Supports both native WebAuthn Level 3 toJSON and manual fallback.
164
+ *
165
+ * @param {RegistrationCredential} credential - Credential from navigator.credentials.create()
166
+ * @returns {RegistrationResponseJSON} JSON-serializable credential for server
167
+ * @see {@link https://w3c.github.io/webauthn/#dom-publickeycredential-tojson W3C WebAuthn Spec - toJSON}
168
+ */
169
+ function serializeCredentialCreationResponse(credential) {
170
+ var _a;
171
+ // Check if the credential instance has the toJSON method
172
+ if ('toJSON' in credential && typeof credential.toJSON === 'function') {
173
+ // Use the native WebAuthn Level 3 method
174
+ return credential.toJSON();
175
+ }
176
+ const credentialWithAttachment = credential;
177
+ return {
178
+ id: credential.id,
179
+ rawId: credential.id,
180
+ response: {
181
+ attestationObject: (0, base64url_1.bytesToBase64URL)(new Uint8Array(credential.response.attestationObject)),
182
+ clientDataJSON: (0, base64url_1.bytesToBase64URL)(new Uint8Array(credential.response.clientDataJSON)),
183
+ },
184
+ type: 'public-key',
185
+ clientExtensionResults: credential.getClientExtensionResults(),
186
+ // Convert null to undefined and cast to AuthenticatorAttachment type
187
+ authenticatorAttachment: ((_a = credentialWithAttachment.authenticatorAttachment) !== null && _a !== void 0 ? _a : undefined),
188
+ };
189
+ }
190
+ /**
191
+ * Convert an authentication/verification credential response to server format.
192
+ * Serializes binary fields to base64url for JSON transmission.
193
+ * Supports both native WebAuthn Level 3 toJSON and manual fallback.
194
+ *
195
+ * @param {AuthenticationCredential} credential - Credential from navigator.credentials.get()
196
+ * @returns {AuthenticationResponseJSON} JSON-serializable credential for server
197
+ * @see {@link https://w3c.github.io/webauthn/#dom-publickeycredential-tojson W3C WebAuthn Spec - toJSON}
198
+ */
199
+ function serializeCredentialRequestResponse(credential) {
200
+ var _a;
201
+ // Check if the credential instance has the toJSON method
202
+ if ('toJSON' in credential && typeof credential.toJSON === 'function') {
203
+ // Use the native WebAuthn Level 3 method
204
+ return credential.toJSON();
205
+ }
206
+ // Fallback to manual conversion for browsers that don't support toJSON
207
+ // Access authenticatorAttachment via type assertion to handle TypeScript version differences
208
+ // @simplewebauthn/types includes this property but base TypeScript 4.7.4 doesn't
209
+ const credentialWithAttachment = credential;
210
+ const clientExtensionResults = credential.getClientExtensionResults();
211
+ const assertionResponse = credential.response;
212
+ return {
213
+ id: credential.id,
214
+ rawId: credential.id, // W3C spec expects rawId to match id for JSON format
215
+ response: {
216
+ authenticatorData: (0, base64url_1.bytesToBase64URL)(new Uint8Array(assertionResponse.authenticatorData)),
217
+ clientDataJSON: (0, base64url_1.bytesToBase64URL)(new Uint8Array(assertionResponse.clientDataJSON)),
218
+ signature: (0, base64url_1.bytesToBase64URL)(new Uint8Array(assertionResponse.signature)),
219
+ userHandle: assertionResponse.userHandle
220
+ ? (0, base64url_1.bytesToBase64URL)(new Uint8Array(assertionResponse.userHandle))
221
+ : undefined,
222
+ },
223
+ type: 'public-key',
224
+ clientExtensionResults,
225
+ // Convert null to undefined and cast to AuthenticatorAttachment type
226
+ authenticatorAttachment: ((_a = credentialWithAttachment.authenticatorAttachment) !== null && _a !== void 0 ? _a : undefined),
227
+ };
228
+ }
229
+ /**
230
+ * A simple test to determine if a hostname is a properly-formatted domain name.
231
+ * Considers localhost valid for development environments.
232
+ *
233
+ * A "valid domain" is defined here: https://url.spec.whatwg.org/#valid-domain
234
+ *
235
+ * Regex sourced from here:
236
+ * https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html
237
+ *
238
+ * @param {string} hostname - The hostname to validate
239
+ * @returns {boolean} True if valid domain or localhost
240
+ * @see {@link https://url.spec.whatwg.org/#valid-domain WHATWG URL Spec - Valid Domain}
241
+ */
242
+ function isValidDomain(hostname) {
243
+ return (
244
+ // Consider localhost valid as well since it's okay wrt Secure Contexts
245
+ hostname === 'localhost' || /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i.test(hostname));
246
+ }
247
+ /**
248
+ * Determine if the browser is capable of WebAuthn.
249
+ * Checks for necessary Web APIs: PublicKeyCredential and Credential Management.
250
+ *
251
+ * @returns {boolean} True if browser supports WebAuthn
252
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential#browser_compatibility MDN - PublicKeyCredential Browser Compatibility}
253
+ */
254
+ function browserSupportsWebAuthn() {
255
+ var _a, _b;
256
+ return !!((0, helpers_1.isBrowser)() &&
257
+ 'PublicKeyCredential' in window &&
258
+ window.PublicKeyCredential &&
259
+ 'credentials' in navigator &&
260
+ typeof ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.credentials) === null || _a === void 0 ? void 0 : _a.create) === 'function' &&
261
+ typeof ((_b = navigator === null || navigator === void 0 ? void 0 : navigator.credentials) === null || _b === void 0 ? void 0 : _b.get) === 'function');
262
+ }
263
+ /**
264
+ * Create a WebAuthn credential using the browser's credentials API.
265
+ * Wraps navigator.credentials.create() with error handling.
266
+ *
267
+ * @param {CredentialCreationOptions} options - Options including publicKey parameters
268
+ * @returns {Promise<RequestResult<RegistrationCredential, WebAuthnError>>} Created credential or error
269
+ * @see {@link https://w3c.github.io/webauthn/#sctn-createCredential W3C WebAuthn Spec - Create Credential}
270
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/create MDN - credentials.create}
271
+ */
272
+ async function createCredential(options) {
273
+ try {
274
+ const response = await navigator.credentials.create(
275
+ /** we assert the type here until typescript types are updated */
276
+ options);
277
+ if (!response) {
278
+ return {
279
+ data: null,
280
+ error: new webauthn_errors_1.WebAuthnUnknownError('Empty credential response', response),
281
+ };
282
+ }
283
+ if (!(response instanceof PublicKeyCredential)) {
284
+ return {
285
+ data: null,
286
+ error: new webauthn_errors_1.WebAuthnUnknownError('Browser returned unexpected credential type', response),
287
+ };
288
+ }
289
+ return { data: response, error: null };
290
+ }
291
+ catch (err) {
292
+ return {
293
+ data: null,
294
+ error: (0, webauthn_errors_1.identifyRegistrationError)({
295
+ error: err,
296
+ options,
297
+ }),
298
+ };
299
+ }
300
+ }
301
+ /**
302
+ * Get a WebAuthn credential using the browser's credentials API.
303
+ * Wraps navigator.credentials.get() with error handling.
304
+ *
305
+ * @param {CredentialRequestOptions} options - Options including publicKey parameters
306
+ * @returns {Promise<RequestResult<AuthenticationCredential, WebAuthnError>>} Retrieved credential or error
307
+ * @see {@link https://w3c.github.io/webauthn/#sctn-getAssertion W3C WebAuthn Spec - Get Assertion}
308
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/get MDN - credentials.get}
309
+ */
310
+ async function getCredential(options) {
311
+ try {
312
+ const response = await navigator.credentials.get(
313
+ /** we assert the type here until typescript types are updated */
314
+ options);
315
+ if (!response) {
316
+ return {
317
+ data: null,
318
+ error: new webauthn_errors_1.WebAuthnUnknownError('Empty credential response', response),
319
+ };
320
+ }
321
+ if (!(response instanceof PublicKeyCredential)) {
322
+ return {
323
+ data: null,
324
+ error: new webauthn_errors_1.WebAuthnUnknownError('Browser returned unexpected credential type', response),
325
+ };
326
+ }
327
+ return { data: response, error: null };
328
+ }
329
+ catch (err) {
330
+ return {
331
+ data: null,
332
+ error: (0, webauthn_errors_1.identifyAuthenticationError)({
333
+ error: err,
334
+ options,
335
+ }),
336
+ };
337
+ }
338
+ }
339
+ exports.DEFAULT_CREATION_OPTIONS = {
340
+ hints: ['security-key'],
341
+ authenticatorSelection: {
342
+ authenticatorAttachment: 'cross-platform',
343
+ requireResidentKey: false,
344
+ /** set to preferred because older yubikeys don't have PIN/Biometric */
345
+ userVerification: 'preferred',
346
+ residentKey: 'discouraged',
347
+ },
348
+ attestation: 'direct',
349
+ };
350
+ exports.DEFAULT_REQUEST_OPTIONS = {
351
+ /** set to preferred because older yubikeys don't have PIN/Biometric */
352
+ userVerification: 'preferred',
353
+ hints: ['security-key'],
354
+ attestation: 'direct',
355
+ };
356
+ function deepMerge(...sources) {
357
+ const isObject = (val) => val !== null && typeof val === 'object' && !Array.isArray(val);
358
+ const isArrayBufferLike = (val) => val instanceof ArrayBuffer || ArrayBuffer.isView(val);
359
+ const result = {};
360
+ for (const source of sources) {
361
+ if (!source)
362
+ continue;
363
+ for (const key in source) {
364
+ const value = source[key];
365
+ if (value === undefined)
366
+ continue;
367
+ if (Array.isArray(value)) {
368
+ // preserve array reference, including unions like AuthenticatorTransport[]
369
+ result[key] = value;
370
+ }
371
+ else if (isArrayBufferLike(value)) {
372
+ result[key] = value;
373
+ }
374
+ else if (isObject(value)) {
375
+ const existing = result[key];
376
+ if (isObject(existing)) {
377
+ result[key] = deepMerge(existing, value);
378
+ }
379
+ else {
380
+ result[key] = deepMerge(value);
381
+ }
382
+ }
383
+ else {
384
+ result[key] = value;
385
+ }
386
+ }
387
+ }
388
+ return result;
389
+ }
390
+ /**
391
+ * Merges WebAuthn credential creation options with overrides.
392
+ * Sets sensible defaults for authenticator selection and extensions.
393
+ *
394
+ * @param {PublicKeyCredentialCreationOptionsFuture} baseOptions - The base options from the server
395
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides - Optional overrides to apply
396
+ * @param {string} friendlyName - Optional friendly name for the credential
397
+ * @returns {PublicKeyCredentialCreationOptionsFuture} Merged credential creation options
398
+ * @see {@link https://w3c.github.io/webauthn/#dictdef-authenticatorselectioncriteria W3C WebAuthn Spec - AuthenticatorSelectionCriteria}
399
+ */
400
+ function mergeCredentialCreationOptions(baseOptions, overrides) {
401
+ return deepMerge(exports.DEFAULT_CREATION_OPTIONS, baseOptions, overrides || {});
402
+ }
403
+ /**
404
+ * Merges WebAuthn credential request options with overrides.
405
+ * Sets sensible defaults for user verification and hints.
406
+ *
407
+ * @param {PublicKeyCredentialRequestOptionsFuture} baseOptions - The base options from the server
408
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides - Optional overrides to apply
409
+ * @returns {PublicKeyCredentialRequestOptionsFuture} Merged credential request options
410
+ * @see {@link https://w3c.github.io/webauthn/#dictdef-publickeycredentialrequestoptions W3C WebAuthn Spec - PublicKeyCredentialRequestOptions}
411
+ */
412
+ function mergeCredentialRequestOptions(baseOptions, overrides) {
413
+ return deepMerge(exports.DEFAULT_REQUEST_OPTIONS, baseOptions, overrides || {});
414
+ }
415
+ /**
416
+ * WebAuthn API wrapper for Supabase Auth.
417
+ * Provides methods for enrolling, challenging, verifying, authenticating, and registering WebAuthn credentials.
418
+ *
419
+ * @experimental This API is experimental and may change in future releases
420
+ * @see {@link https://w3c.github.io/webauthn/ W3C WebAuthn Specification}
421
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API MDN - Web Authentication API}
422
+ */
423
+ class WebAuthnApi {
424
+ constructor(client) {
425
+ this.client = client;
426
+ // Bind all methods so they can be destructured
427
+ this.enroll = this._enroll.bind(this);
428
+ this.challenge = this._challenge.bind(this);
429
+ this.verify = this._verify.bind(this);
430
+ this.authenticate = this._authenticate.bind(this);
431
+ this.register = this._register.bind(this);
432
+ }
433
+ /**
434
+ * Enroll a new WebAuthn factor.
435
+ * Creates an unverified WebAuthn factor that must be verified with a credential.
436
+ *
437
+ * @experimental This method is experimental and may change in future releases
438
+ * @param {Omit<MFAEnrollWebauthnParams, 'factorType'>} params - Enrollment parameters (friendlyName required)
439
+ * @returns {Promise<AuthMFAEnrollWebauthnResponse>} Enrolled factor details or error
440
+ * @see {@link https://w3c.github.io/webauthn/#sctn-registering-a-new-credential W3C WebAuthn Spec - Registering a New Credential}
441
+ */
442
+ async _enroll(params) {
443
+ return this.client.mfa.enroll(Object.assign(Object.assign({}, params), { factorType: 'webauthn' }));
444
+ }
445
+ /**
446
+ * Challenge for WebAuthn credential creation or authentication.
447
+ * Combines server challenge with browser credential operations.
448
+ * Handles both registration (create) and authentication (request) flows.
449
+ *
450
+ * @experimental This method is experimental and may change in future releases
451
+ * @param {MFAChallengeWebauthnParams & { friendlyName?: string; signal?: AbortSignal }} params - Challenge parameters including factorId
452
+ * @param {Object} overrides - Allows you to override the parameters passed to navigator.credentials
453
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides.create - Override options for credential creation
454
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides.request - Override options for credential request
455
+ * @returns {Promise<RequestResult>} Challenge response with credential or error
456
+ * @see {@link https://w3c.github.io/webauthn/#sctn-credential-creation W3C WebAuthn Spec - Credential Creation}
457
+ * @see {@link https://w3c.github.io/webauthn/#sctn-verifying-assertion W3C WebAuthn Spec - Verifying Assertion}
458
+ */
459
+ async _challenge({ factorId, webauthn, friendlyName, signal, }, overrides) {
460
+ var _a;
461
+ try {
462
+ // Get challenge from server using the client's MFA methods
463
+ const { data: challengeResponse, error: challengeError } = await this.client.mfa.challenge({
464
+ factorId,
465
+ webauthn,
466
+ });
467
+ if (!challengeResponse) {
468
+ return { data: null, error: challengeError };
469
+ }
470
+ const abortSignal = signal !== null && signal !== void 0 ? signal : exports.webAuthnAbortService.createNewAbortSignal();
471
+ /** webauthn will fail if either of the name/displayname are blank */
472
+ if (challengeResponse.webauthn.type === 'create') {
473
+ const { user } = challengeResponse.webauthn.credential_options.publicKey;
474
+ if (!user.name) {
475
+ // Preserve original format: use friendlyName if provided, otherwise fetch fallback
476
+ // This maintains backward compatibility with the ${user.id}:${name} format
477
+ const nameToUse = friendlyName;
478
+ if (!nameToUse) {
479
+ // Only fetch user data if friendlyName is not provided (bug fix for null friendlyName)
480
+ const currentUser = await this.client.getUser();
481
+ const userData = currentUser.data.user;
482
+ const fallbackName = ((_a = userData === null || userData === void 0 ? void 0 : userData.user_metadata) === null || _a === void 0 ? void 0 : _a.name) || (userData === null || userData === void 0 ? void 0 : userData.email) || (userData === null || userData === void 0 ? void 0 : userData.id) || 'User';
483
+ user.name = `${user.id}:${fallbackName}`;
484
+ }
485
+ else {
486
+ user.name = `${user.id}:${nameToUse}`;
487
+ }
488
+ }
489
+ if (!user.displayName) {
490
+ user.displayName = user.name;
491
+ }
492
+ }
493
+ switch (challengeResponse.webauthn.type) {
494
+ case 'create': {
495
+ const options = mergeCredentialCreationOptions(challengeResponse.webauthn.credential_options.publicKey, overrides === null || overrides === void 0 ? void 0 : overrides.create);
496
+ const { data, error } = await createCredential({
497
+ publicKey: options,
498
+ signal: abortSignal,
499
+ });
500
+ if (data) {
501
+ return {
502
+ data: {
503
+ factorId,
504
+ challengeId: challengeResponse.id,
505
+ webauthn: {
506
+ type: challengeResponse.webauthn.type,
507
+ credential_response: data,
508
+ },
509
+ },
510
+ error: null,
511
+ };
512
+ }
513
+ return { data: null, error };
514
+ }
515
+ case 'request': {
516
+ const options = mergeCredentialRequestOptions(challengeResponse.webauthn.credential_options.publicKey, overrides === null || overrides === void 0 ? void 0 : overrides.request);
517
+ const { data, error } = await getCredential(Object.assign(Object.assign({}, challengeResponse.webauthn.credential_options), { publicKey: options, signal: abortSignal }));
518
+ if (data) {
519
+ return {
520
+ data: {
521
+ factorId,
522
+ challengeId: challengeResponse.id,
523
+ webauthn: {
524
+ type: challengeResponse.webauthn.type,
525
+ credential_response: data,
526
+ },
527
+ },
528
+ error: null,
529
+ };
530
+ }
531
+ return { data: null, error };
532
+ }
533
+ }
534
+ }
535
+ catch (error) {
536
+ if ((0, errors_1.isAuthError)(error)) {
537
+ return { data: null, error };
538
+ }
539
+ return {
540
+ data: null,
541
+ error: new errors_1.AuthUnknownError('Unexpected error in challenge', error),
542
+ };
543
+ }
544
+ }
545
+ /**
546
+ * Verify a WebAuthn credential with the server.
547
+ * Completes the WebAuthn ceremony by sending the credential to the server for verification.
548
+ *
549
+ * @experimental This method is experimental and may change in future releases
550
+ * @param {Object} params - Verification parameters
551
+ * @param {string} params.challengeId - ID of the challenge being verified
552
+ * @param {string} params.factorId - ID of the WebAuthn factor
553
+ * @param {MFAVerifyWebauthnParams<T>['webauthn']} params.webauthn - WebAuthn credential response
554
+ * @returns {Promise<AuthMFAVerifyResponse>} Verification result with session or error
555
+ * @see {@link https://w3c.github.io/webauthn/#sctn-verifying-assertion W3C WebAuthn Spec - Verifying an Authentication Assertion}
556
+ * */
557
+ async _verify({ challengeId, factorId, webauthn, }) {
558
+ return this.client.mfa.verify({
559
+ factorId,
560
+ challengeId,
561
+ webauthn: webauthn,
562
+ });
563
+ }
564
+ /**
565
+ * Complete WebAuthn authentication flow.
566
+ * Performs challenge and verification in a single operation for existing credentials.
567
+ *
568
+ * @experimental This method is experimental and may change in future releases
569
+ * @param {Object} params - Authentication parameters
570
+ * @param {string} params.factorId - ID of the WebAuthn factor to authenticate with
571
+ * @param {Object} params.webauthn - WebAuthn configuration
572
+ * @param {string} params.webauthn.rpId - Relying Party ID (defaults to current hostname)
573
+ * @param {string[]} params.webauthn.rpOrigins - Allowed origins (defaults to current origin)
574
+ * @param {AbortSignal} params.webauthn.signal - Optional abort signal
575
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides - Override options for navigator.credentials.get
576
+ * @returns {Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>>} Authentication result
577
+ * @see {@link https://w3c.github.io/webauthn/#sctn-authentication W3C WebAuthn Spec - Authentication Ceremony}
578
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredentialRequestOptions MDN - PublicKeyCredentialRequestOptions}
579
+ */
580
+ async _authenticate({ factorId, webauthn: { rpId = typeof window !== 'undefined' ? window.location.hostname : undefined, rpOrigins = typeof window !== 'undefined' ? [window.location.origin] : undefined, signal, } = {}, }, overrides) {
581
+ if (!rpId) {
582
+ return {
583
+ data: null,
584
+ error: new errors_1.AuthError('rpId is required for WebAuthn authentication'),
585
+ };
586
+ }
587
+ try {
588
+ if (!browserSupportsWebAuthn()) {
589
+ return {
590
+ data: null,
591
+ error: new errors_1.AuthUnknownError('Browser does not support WebAuthn', null),
592
+ };
593
+ }
594
+ // Get challenge and credential
595
+ const { data: challengeResponse, error: challengeError } = await this.challenge({
596
+ factorId,
597
+ webauthn: { rpId, rpOrigins },
598
+ signal,
599
+ }, { request: overrides });
600
+ if (!challengeResponse) {
601
+ return { data: null, error: challengeError };
602
+ }
603
+ const { webauthn } = challengeResponse;
604
+ // Verify credential
605
+ return this._verify({
606
+ factorId,
607
+ challengeId: challengeResponse.challengeId,
608
+ webauthn: {
609
+ type: webauthn.type,
610
+ rpId,
611
+ rpOrigins,
612
+ credential_response: webauthn.credential_response,
613
+ },
614
+ });
615
+ }
616
+ catch (error) {
617
+ if ((0, errors_1.isAuthError)(error)) {
618
+ return { data: null, error };
619
+ }
620
+ return {
621
+ data: null,
622
+ error: new errors_1.AuthUnknownError('Unexpected error in authenticate', error),
623
+ };
624
+ }
625
+ }
626
+ /**
627
+ * Complete WebAuthn registration flow.
628
+ * Performs enrollment, challenge, and verification in a single operation for new credentials.
629
+ *
630
+ * @experimental This method is experimental and may change in future releases
631
+ * @param {Object} params - Registration parameters
632
+ * @param {string} params.friendlyName - User-friendly name for the credential
633
+ * @param {string} params.rpId - Relying Party ID (defaults to current hostname)
634
+ * @param {string[]} params.rpOrigins - Allowed origins (defaults to current origin)
635
+ * @param {AbortSignal} params.signal - Optional abort signal
636
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides - Override options for navigator.credentials.create
637
+ * @returns {Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>>} Registration result
638
+ * @see {@link https://w3c.github.io/webauthn/#sctn-registering-a-new-credential W3C WebAuthn Spec - Registration Ceremony}
639
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredentialCreationOptions MDN - PublicKeyCredentialCreationOptions}
640
+ */
641
+ async _register({ friendlyName, webauthn: { rpId = typeof window !== 'undefined' ? window.location.hostname : undefined, rpOrigins = typeof window !== 'undefined' ? [window.location.origin] : undefined, signal, } = {}, }, overrides) {
642
+ if (!rpId) {
643
+ return {
644
+ data: null,
645
+ error: new errors_1.AuthError('rpId is required for WebAuthn registration'),
646
+ };
647
+ }
648
+ try {
649
+ if (!browserSupportsWebAuthn()) {
650
+ return {
651
+ data: null,
652
+ error: new errors_1.AuthUnknownError('Browser does not support WebAuthn', null),
653
+ };
654
+ }
655
+ // Enroll factor
656
+ const { data: factor, error: enrollError } = await this._enroll({
657
+ friendlyName,
658
+ });
659
+ if (!factor) {
660
+ await this.client.mfa
661
+ .listFactors()
662
+ .then((factors) => {
663
+ var _a;
664
+ return (_a = factors.data) === null || _a === void 0 ? void 0 : _a.all.find((v) => v.factor_type === 'webauthn' &&
665
+ v.friendly_name === friendlyName &&
666
+ v.status !== 'unverified');
667
+ })
668
+ .then((factor) => (factor ? this.client.mfa.unenroll({ factorId: factor === null || factor === void 0 ? void 0 : factor.id }) : void 0));
669
+ return { data: null, error: enrollError };
670
+ }
671
+ // Get challenge and create credential
672
+ const { data: challengeResponse, error: challengeError } = await this._challenge({
673
+ factorId: factor.id,
674
+ friendlyName: factor.friendly_name,
675
+ webauthn: { rpId, rpOrigins },
676
+ signal,
677
+ }, {
678
+ create: overrides,
679
+ });
680
+ if (!challengeResponse) {
681
+ return { data: null, error: challengeError };
682
+ }
683
+ return this._verify({
684
+ factorId: factor.id,
685
+ challengeId: challengeResponse.challengeId,
686
+ webauthn: {
687
+ rpId,
688
+ rpOrigins,
689
+ type: challengeResponse.webauthn.type,
690
+ credential_response: challengeResponse.webauthn.credential_response,
691
+ },
692
+ });
693
+ }
694
+ catch (error) {
695
+ if ((0, errors_1.isAuthError)(error)) {
696
+ return { data: null, error };
697
+ }
698
+ return {
699
+ data: null,
700
+ error: new errors_1.AuthUnknownError('Unexpected error in register', error),
701
+ };
702
+ }
703
+ }
704
+ }
705
+ exports.WebAuthnApi = WebAuthnApi;
706
+ //# sourceMappingURL=webauthn.js.map