@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,946 @@
1
+ import GoTrueClient from '../GoTrueClient'
2
+ import { base64UrlToUint8Array, bytesToBase64URL } from './base64url'
3
+ import { AuthError, AuthUnknownError, isAuthError } from './errors'
4
+ import {
5
+ AuthMFAEnrollWebauthnResponse,
6
+ AuthMFAVerifyResponse,
7
+ AuthMFAVerifyResponseData,
8
+ MFAChallengeWebauthnParams,
9
+ MFAEnrollWebauthnParams,
10
+ MFAVerifyWebauthnParamFields,
11
+ MFAVerifyWebauthnParams,
12
+ RequestResult,
13
+ StrictOmit,
14
+ } from './types'
15
+ import { isBrowser } from './helpers'
16
+ import type {
17
+ AuthenticationCredential,
18
+ AuthenticationResponseJSON,
19
+ AuthenticatorAttachment,
20
+ PublicKeyCredentialCreationOptionsFuture,
21
+ PublicKeyCredentialCreationOptionsJSON,
22
+ PublicKeyCredentialFuture,
23
+ PublicKeyCredentialRequestOptionsFuture,
24
+ PublicKeyCredentialRequestOptionsJSON,
25
+ RegistrationCredential,
26
+ RegistrationResponseJSON,
27
+ } from './webauthn.dom'
28
+
29
+ import {
30
+ identifyAuthenticationError,
31
+ identifyRegistrationError,
32
+ isWebAuthnError,
33
+ WebAuthnError,
34
+ WebAuthnUnknownError,
35
+ } from './webauthn.errors'
36
+
37
+ export { WebAuthnError, isWebAuthnError, identifyRegistrationError, identifyAuthenticationError }
38
+ // Re-export the JSON types for use in other files
39
+ export type { RegistrationResponseJSON, AuthenticationResponseJSON }
40
+
41
+ /**
42
+ * WebAuthn abort service to manage ceremony cancellation.
43
+ * Ensures only one WebAuthn ceremony is active at a time to prevent "operation already in progress" errors.
44
+ *
45
+ * @experimental This class is experimental and may change in future releases
46
+ * @see {@link https://w3c.github.io/webauthn/#sctn-automation-webdriver-capability W3C WebAuthn Spec - Aborting Ceremonies}
47
+ */
48
+ export class WebAuthnAbortService {
49
+ private controller: AbortController | undefined
50
+
51
+ /**
52
+ * Create an abort signal for a new WebAuthn operation.
53
+ * Automatically cancels any existing operation.
54
+ *
55
+ * @returns {AbortSignal} Signal to pass to navigator.credentials.create() or .get()
56
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal MDN - AbortSignal}
57
+ */
58
+ createNewAbortSignal(): AbortSignal {
59
+ // Abort any existing calls to navigator.credentials.create() or navigator.credentials.get()
60
+ if (this.controller) {
61
+ const abortError = new Error('Cancelling existing WebAuthn API call for new one')
62
+ abortError.name = 'AbortError'
63
+ this.controller.abort(abortError)
64
+ }
65
+
66
+ const newController = new AbortController()
67
+ this.controller = newController
68
+ return newController.signal
69
+ }
70
+
71
+ /**
72
+ * Manually cancel the current WebAuthn operation.
73
+ * Useful for cleaning up when user cancels or navigates away.
74
+ *
75
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort MDN - AbortController.abort}
76
+ */
77
+ cancelCeremony(): void {
78
+ if (this.controller) {
79
+ const abortError = new Error('Manually cancelling existing WebAuthn API call')
80
+ abortError.name = 'AbortError'
81
+ this.controller.abort(abortError)
82
+ this.controller = undefined
83
+ }
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Singleton instance to ensure only one WebAuthn ceremony is active at a time.
89
+ * This prevents "operation already in progress" errors when retrying WebAuthn operations.
90
+ *
91
+ * @experimental This instance is experimental and may change in future releases
92
+ */
93
+ export const webAuthnAbortService = new WebAuthnAbortService()
94
+
95
+ /**
96
+ * Server response format for WebAuthn credential creation options.
97
+ * Uses W3C standard JSON format with base64url-encoded binary fields.
98
+ */
99
+ export type ServerCredentialCreationOptions = PublicKeyCredentialCreationOptionsJSON
100
+
101
+ /**
102
+ * Server response format for WebAuthn credential request options.
103
+ * Uses W3C standard JSON format with base64url-encoded binary fields.
104
+ */
105
+ export type ServerCredentialRequestOptions = PublicKeyCredentialRequestOptionsJSON
106
+
107
+ /**
108
+ * Convert base64url encoded strings in WebAuthn credential creation options to ArrayBuffers
109
+ * as required by the WebAuthn browser API.
110
+ * Supports both native WebAuthn Level 3 parseCreationOptionsFromJSON and manual fallback.
111
+ *
112
+ * @param {ServerCredentialCreationOptions} options - JSON options from server with base64url encoded fields
113
+ * @returns {PublicKeyCredentialCreationOptionsFuture} Options ready for navigator.credentials.create()
114
+ * @see {@link https://w3c.github.io/webauthn/#sctn-parseCreationOptionsFromJSON W3C WebAuthn Spec - parseCreationOptionsFromJSON}
115
+ */
116
+ export function deserializeCredentialCreationOptions(
117
+ options: ServerCredentialCreationOptions
118
+ ): PublicKeyCredentialCreationOptionsFuture {
119
+ if (!options) {
120
+ throw new Error('Credential creation options are required')
121
+ }
122
+
123
+ // Check if the native parseCreationOptionsFromJSON method is available
124
+ if (
125
+ typeof PublicKeyCredential !== 'undefined' &&
126
+ 'parseCreationOptionsFromJSON' in PublicKeyCredential &&
127
+ typeof (PublicKeyCredential as unknown as PublicKeyCredentialFuture)
128
+ .parseCreationOptionsFromJSON === 'function'
129
+ ) {
130
+ // Use the native WebAuthn Level 3 method
131
+ return (
132
+ PublicKeyCredential as unknown as PublicKeyCredentialFuture
133
+ ).parseCreationOptionsFromJSON(
134
+ /** we assert the options here as typescript still doesn't know about future webauthn types */
135
+ options as any
136
+ ) as PublicKeyCredentialCreationOptionsFuture
137
+ }
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, user: userOpts, excludeCredentials, ...restOptions } = options
142
+
143
+ // Convert challenge from base64url to ArrayBuffer
144
+ const challenge = base64UrlToUint8Array(challengeStr).buffer as ArrayBuffer
145
+
146
+ // Convert user.id from base64url to ArrayBuffer
147
+ const user: PublicKeyCredentialUserEntity = {
148
+ ...userOpts,
149
+ id: base64UrlToUint8Array(userOpts.id).buffer as ArrayBuffer,
150
+ }
151
+
152
+ // Build the result object
153
+ const result: PublicKeyCredentialCreationOptionsFuture = {
154
+ ...restOptions,
155
+ challenge,
156
+ user,
157
+ }
158
+
159
+ // Only add excludeCredentials if it exists
160
+ if (excludeCredentials && excludeCredentials.length > 0) {
161
+ result.excludeCredentials = new Array(excludeCredentials.length)
162
+
163
+ for (let i = 0; i < excludeCredentials.length; i++) {
164
+ const cred = excludeCredentials[i]
165
+ result.excludeCredentials[i] = {
166
+ ...cred,
167
+ id: base64UrlToUint8Array(cred.id).buffer,
168
+ type: cred.type || 'public-key',
169
+ // Cast transports to handle future transport types like "cable"
170
+ transports: cred.transports,
171
+ }
172
+ }
173
+ }
174
+
175
+ return result
176
+ }
177
+
178
+ /**
179
+ * Convert base64url encoded strings in WebAuthn credential request options to ArrayBuffers
180
+ * as required by the WebAuthn browser API.
181
+ * Supports both native WebAuthn Level 3 parseRequestOptionsFromJSON and manual fallback.
182
+ *
183
+ * @param {ServerCredentialRequestOptions} options - JSON options from server with base64url encoded fields
184
+ * @returns {PublicKeyCredentialRequestOptionsFuture} Options ready for navigator.credentials.get()
185
+ * @see {@link https://w3c.github.io/webauthn/#sctn-parseRequestOptionsFromJSON W3C WebAuthn Spec - parseRequestOptionsFromJSON}
186
+ */
187
+ export function deserializeCredentialRequestOptions(
188
+ options: ServerCredentialRequestOptions
189
+ ): PublicKeyCredentialRequestOptionsFuture {
190
+ if (!options) {
191
+ throw new Error('Credential request options are required')
192
+ }
193
+
194
+ // Check if the native parseRequestOptionsFromJSON method is available
195
+ if (
196
+ typeof PublicKeyCredential !== 'undefined' &&
197
+ 'parseRequestOptionsFromJSON' in PublicKeyCredential &&
198
+ typeof (PublicKeyCredential as unknown as PublicKeyCredentialFuture)
199
+ .parseRequestOptionsFromJSON === 'function'
200
+ ) {
201
+ // Use the native WebAuthn Level 3 method
202
+ return (
203
+ PublicKeyCredential as unknown as PublicKeyCredentialFuture
204
+ ).parseRequestOptionsFromJSON(options) as PublicKeyCredentialRequestOptionsFuture
205
+ }
206
+
207
+ // Fallback to manual parsing for browsers that don't support the native method
208
+ // Destructure to separate fields that need transformation
209
+ const { challenge: challengeStr, allowCredentials, ...restOptions } = options
210
+
211
+ // Convert challenge from base64url to ArrayBuffer
212
+ const challenge = base64UrlToUint8Array(challengeStr).buffer as ArrayBuffer
213
+
214
+ // Build the result object
215
+ const result: PublicKeyCredentialRequestOptionsFuture = {
216
+ ...restOptions,
217
+ challenge,
218
+ }
219
+
220
+ // Only add allowCredentials if it exists
221
+ if (allowCredentials && allowCredentials.length > 0) {
222
+ result.allowCredentials = new Array(allowCredentials.length)
223
+
224
+ for (let i = 0; i < allowCredentials.length; i++) {
225
+ const cred = allowCredentials[i]
226
+ result.allowCredentials[i] = {
227
+ ...cred,
228
+ id: base64UrlToUint8Array(cred.id).buffer,
229
+ type: cred.type || 'public-key',
230
+ // Cast transports to handle future transport types like "cable"
231
+ transports: cred.transports,
232
+ }
233
+ }
234
+ }
235
+
236
+ return result
237
+ }
238
+
239
+ /**
240
+ * Server format for credential response with base64url-encoded binary fields
241
+ * Can be either a registration or authentication response
242
+ */
243
+ export type ServerCredentialResponse = RegistrationResponseJSON | AuthenticationResponseJSON
244
+
245
+ /**
246
+ * Convert a registration/enrollment credential response to server format.
247
+ * Serializes binary fields to base64url for JSON transmission.
248
+ * Supports both native WebAuthn Level 3 toJSON and manual fallback.
249
+ *
250
+ * @param {RegistrationCredential} credential - Credential from navigator.credentials.create()
251
+ * @returns {RegistrationResponseJSON} JSON-serializable credential for server
252
+ * @see {@link https://w3c.github.io/webauthn/#dom-publickeycredential-tojson W3C WebAuthn Spec - toJSON}
253
+ */
254
+ export function serializeCredentialCreationResponse(
255
+ credential: RegistrationCredential
256
+ ): RegistrationResponseJSON {
257
+ // Check if the credential instance has the toJSON method
258
+ if ('toJSON' in credential && typeof credential.toJSON === 'function') {
259
+ // Use the native WebAuthn Level 3 method
260
+ return (credential as RegistrationCredential).toJSON()
261
+ }
262
+ const credentialWithAttachment = credential as PublicKeyCredential & {
263
+ response: AuthenticatorAttestationResponse
264
+ authenticatorAttachment?: string | null
265
+ }
266
+
267
+ return {
268
+ id: credential.id,
269
+ rawId: credential.id,
270
+ response: {
271
+ attestationObject: bytesToBase64URL(new Uint8Array(credential.response.attestationObject)),
272
+ clientDataJSON: bytesToBase64URL(new Uint8Array(credential.response.clientDataJSON)),
273
+ },
274
+ type: 'public-key',
275
+ clientExtensionResults: credential.getClientExtensionResults(),
276
+ // Convert null to undefined and cast to AuthenticatorAttachment type
277
+ authenticatorAttachment: (credentialWithAttachment.authenticatorAttachment ?? undefined) as
278
+ | AuthenticatorAttachment
279
+ | undefined,
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Convert an authentication/verification credential response to server format.
285
+ * Serializes binary fields to base64url for JSON transmission.
286
+ * Supports both native WebAuthn Level 3 toJSON and manual fallback.
287
+ *
288
+ * @param {AuthenticationCredential} credential - Credential from navigator.credentials.get()
289
+ * @returns {AuthenticationResponseJSON} JSON-serializable credential for server
290
+ * @see {@link https://w3c.github.io/webauthn/#dom-publickeycredential-tojson W3C WebAuthn Spec - toJSON}
291
+ */
292
+ export function serializeCredentialRequestResponse(
293
+ credential: AuthenticationCredential
294
+ ): AuthenticationResponseJSON {
295
+ // Check if the credential instance has the toJSON method
296
+ if ('toJSON' in credential && typeof credential.toJSON === 'function') {
297
+ // Use the native WebAuthn Level 3 method
298
+ return (credential as AuthenticationCredential).toJSON()
299
+ }
300
+
301
+ // Fallback to manual conversion for browsers that don't support toJSON
302
+ // Access authenticatorAttachment via type assertion to handle TypeScript version differences
303
+ // @simplewebauthn/types includes this property but base TypeScript 4.7.4 doesn't
304
+ const credentialWithAttachment = credential as PublicKeyCredential & {
305
+ response: AuthenticatorAssertionResponse
306
+ authenticatorAttachment?: string | null
307
+ }
308
+
309
+ const clientExtensionResults = credential.getClientExtensionResults()
310
+ const assertionResponse = credential.response
311
+
312
+ return {
313
+ id: credential.id,
314
+ rawId: credential.id, // W3C spec expects rawId to match id for JSON format
315
+ response: {
316
+ authenticatorData: bytesToBase64URL(new Uint8Array(assertionResponse.authenticatorData)),
317
+ clientDataJSON: bytesToBase64URL(new Uint8Array(assertionResponse.clientDataJSON)),
318
+ signature: bytesToBase64URL(new Uint8Array(assertionResponse.signature)),
319
+ userHandle: assertionResponse.userHandle
320
+ ? bytesToBase64URL(new Uint8Array(assertionResponse.userHandle))
321
+ : undefined,
322
+ },
323
+ type: 'public-key',
324
+ clientExtensionResults,
325
+ // Convert null to undefined and cast to AuthenticatorAttachment type
326
+ authenticatorAttachment: (credentialWithAttachment.authenticatorAttachment ?? undefined) as
327
+ | AuthenticatorAttachment
328
+ | undefined,
329
+ }
330
+ }
331
+
332
+ /**
333
+ * A simple test to determine if a hostname is a properly-formatted domain name.
334
+ * Considers localhost valid for development environments.
335
+ *
336
+ * A "valid domain" is defined here: https://url.spec.whatwg.org/#valid-domain
337
+ *
338
+ * Regex sourced from here:
339
+ * https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html
340
+ *
341
+ * @param {string} hostname - The hostname to validate
342
+ * @returns {boolean} True if valid domain or localhost
343
+ * @see {@link https://url.spec.whatwg.org/#valid-domain WHATWG URL Spec - Valid Domain}
344
+ */
345
+ export function isValidDomain(hostname: string): boolean {
346
+ return (
347
+ // Consider localhost valid as well since it's okay wrt Secure Contexts
348
+ hostname === 'localhost' || /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i.test(hostname)
349
+ )
350
+ }
351
+
352
+ /**
353
+ * Determine if the browser is capable of WebAuthn.
354
+ * Checks for necessary Web APIs: PublicKeyCredential and Credential Management.
355
+ *
356
+ * @returns {boolean} True if browser supports WebAuthn
357
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential#browser_compatibility MDN - PublicKeyCredential Browser Compatibility}
358
+ */
359
+ function browserSupportsWebAuthn(): boolean {
360
+ return !!(
361
+ isBrowser() &&
362
+ 'PublicKeyCredential' in window &&
363
+ window.PublicKeyCredential &&
364
+ 'credentials' in navigator &&
365
+ typeof navigator?.credentials?.create === 'function' &&
366
+ typeof navigator?.credentials?.get === 'function'
367
+ )
368
+ }
369
+
370
+ /**
371
+ * Create a WebAuthn credential using the browser's credentials API.
372
+ * Wraps navigator.credentials.create() with error handling.
373
+ *
374
+ * @param {CredentialCreationOptions} options - Options including publicKey parameters
375
+ * @returns {Promise<RequestResult<RegistrationCredential, WebAuthnError>>} Created credential or error
376
+ * @see {@link https://w3c.github.io/webauthn/#sctn-createCredential W3C WebAuthn Spec - Create Credential}
377
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/create MDN - credentials.create}
378
+ */
379
+ export async function createCredential(
380
+ options: StrictOmit<CredentialCreationOptions, 'publicKey'> & {
381
+ publicKey: PublicKeyCredentialCreationOptionsFuture
382
+ }
383
+ ): Promise<RequestResult<RegistrationCredential, WebAuthnError>> {
384
+ try {
385
+ const response = await navigator.credentials.create(
386
+ /** we assert the type here until typescript types are updated */
387
+ options as Parameters<typeof navigator.credentials.create>[0]
388
+ )
389
+ if (!response) {
390
+ return {
391
+ data: null,
392
+ error: new WebAuthnUnknownError('Empty credential response', response),
393
+ }
394
+ }
395
+ if (!(response instanceof PublicKeyCredential)) {
396
+ return {
397
+ data: null,
398
+ error: new WebAuthnUnknownError('Browser returned unexpected credential type', response),
399
+ }
400
+ }
401
+ return { data: response as RegistrationCredential, error: null }
402
+ } catch (err) {
403
+ return {
404
+ data: null,
405
+ error: identifyRegistrationError({
406
+ error: err as Error,
407
+ options,
408
+ }),
409
+ }
410
+ }
411
+ }
412
+
413
+ /**
414
+ * Get a WebAuthn credential using the browser's credentials API.
415
+ * Wraps navigator.credentials.get() with error handling.
416
+ *
417
+ * @param {CredentialRequestOptions} options - Options including publicKey parameters
418
+ * @returns {Promise<RequestResult<AuthenticationCredential, WebAuthnError>>} Retrieved credential or error
419
+ * @see {@link https://w3c.github.io/webauthn/#sctn-getAssertion W3C WebAuthn Spec - Get Assertion}
420
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/get MDN - credentials.get}
421
+ */
422
+ export async function getCredential(
423
+ options: StrictOmit<CredentialRequestOptions, 'publicKey'> & {
424
+ publicKey: PublicKeyCredentialRequestOptionsFuture
425
+ }
426
+ ): Promise<RequestResult<AuthenticationCredential, WebAuthnError>> {
427
+ try {
428
+ const response = await navigator.credentials.get(
429
+ /** we assert the type here until typescript types are updated */
430
+ options as Parameters<typeof navigator.credentials.get>[0]
431
+ )
432
+ if (!response) {
433
+ return {
434
+ data: null,
435
+ error: new WebAuthnUnknownError('Empty credential response', response),
436
+ }
437
+ }
438
+ if (!(response instanceof PublicKeyCredential)) {
439
+ return {
440
+ data: null,
441
+ error: new WebAuthnUnknownError('Browser returned unexpected credential type', response),
442
+ }
443
+ }
444
+ return { data: response as AuthenticationCredential, error: null }
445
+ } catch (err) {
446
+ return {
447
+ data: null,
448
+ error: identifyAuthenticationError({
449
+ error: err as Error,
450
+ options,
451
+ }),
452
+ }
453
+ }
454
+ }
455
+
456
+ export const DEFAULT_CREATION_OPTIONS: Partial<PublicKeyCredentialCreationOptionsFuture> = {
457
+ hints: ['security-key'],
458
+ authenticatorSelection: {
459
+ authenticatorAttachment: 'cross-platform',
460
+ requireResidentKey: false,
461
+ /** set to preferred because older yubikeys don't have PIN/Biometric */
462
+ userVerification: 'preferred',
463
+ residentKey: 'discouraged',
464
+ },
465
+ attestation: 'direct',
466
+ }
467
+
468
+ export const DEFAULT_REQUEST_OPTIONS: Partial<PublicKeyCredentialRequestOptionsFuture> = {
469
+ /** set to preferred because older yubikeys don't have PIN/Biometric */
470
+ userVerification: 'preferred',
471
+ hints: ['security-key'],
472
+ attestation: 'direct',
473
+ }
474
+
475
+ function deepMerge<T>(...sources: Partial<T>[]): T {
476
+ const isObject = (val: unknown): val is Record<string, unknown> =>
477
+ val !== null && typeof val === 'object' && !Array.isArray(val)
478
+
479
+ const isArrayBufferLike = (val: unknown): val is ArrayBuffer | ArrayBufferView =>
480
+ val instanceof ArrayBuffer || ArrayBuffer.isView(val)
481
+
482
+ const result: Partial<T> = {}
483
+
484
+ for (const source of sources) {
485
+ if (!source) continue
486
+
487
+ for (const key in source) {
488
+ const value = source[key]
489
+ if (value === undefined) continue
490
+
491
+ if (Array.isArray(value)) {
492
+ // preserve array reference, including unions like AuthenticatorTransport[]
493
+ result[key] = value as T[typeof key]
494
+ } else if (isArrayBufferLike(value)) {
495
+ result[key] = value as T[typeof key]
496
+ } else if (isObject(value)) {
497
+ const existing = result[key]
498
+ if (isObject(existing)) {
499
+ result[key] = deepMerge(existing, value) as unknown as T[typeof key]
500
+ } else {
501
+ result[key] = deepMerge(value) as unknown as T[typeof key]
502
+ }
503
+ } else {
504
+ result[key] = value as T[typeof key]
505
+ }
506
+ }
507
+ }
508
+
509
+ return result as T
510
+ }
511
+
512
+ /**
513
+ * Merges WebAuthn credential creation options with overrides.
514
+ * Sets sensible defaults for authenticator selection and extensions.
515
+ *
516
+ * @param {PublicKeyCredentialCreationOptionsFuture} baseOptions - The base options from the server
517
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides - Optional overrides to apply
518
+ * @param {string} friendlyName - Optional friendly name for the credential
519
+ * @returns {PublicKeyCredentialCreationOptionsFuture} Merged credential creation options
520
+ * @see {@link https://w3c.github.io/webauthn/#dictdef-authenticatorselectioncriteria W3C WebAuthn Spec - AuthenticatorSelectionCriteria}
521
+ */
522
+ export function mergeCredentialCreationOptions(
523
+ baseOptions: PublicKeyCredentialCreationOptionsFuture,
524
+ overrides?: Partial<PublicKeyCredentialCreationOptionsFuture>
525
+ ): PublicKeyCredentialCreationOptionsFuture {
526
+ return deepMerge(DEFAULT_CREATION_OPTIONS, baseOptions, overrides || {})
527
+ }
528
+
529
+ /**
530
+ * Merges WebAuthn credential request options with overrides.
531
+ * Sets sensible defaults for user verification and hints.
532
+ *
533
+ * @param {PublicKeyCredentialRequestOptionsFuture} baseOptions - The base options from the server
534
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides - Optional overrides to apply
535
+ * @returns {PublicKeyCredentialRequestOptionsFuture} Merged credential request options
536
+ * @see {@link https://w3c.github.io/webauthn/#dictdef-publickeycredentialrequestoptions W3C WebAuthn Spec - PublicKeyCredentialRequestOptions}
537
+ */
538
+ export function mergeCredentialRequestOptions(
539
+ baseOptions: PublicKeyCredentialRequestOptionsFuture,
540
+ overrides?: Partial<PublicKeyCredentialRequestOptionsFuture>
541
+ ): PublicKeyCredentialRequestOptionsFuture {
542
+ return deepMerge(DEFAULT_REQUEST_OPTIONS, baseOptions, overrides || {})
543
+ }
544
+
545
+ /**
546
+ * WebAuthn API wrapper for Supabase Auth.
547
+ * Provides methods for enrolling, challenging, verifying, authenticating, and registering WebAuthn credentials.
548
+ *
549
+ * @experimental This API is experimental and may change in future releases
550
+ * @see {@link https://w3c.github.io/webauthn/ W3C WebAuthn Specification}
551
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API MDN - Web Authentication API}
552
+ */
553
+ export class WebAuthnApi {
554
+ public enroll: typeof WebAuthnApi.prototype._enroll
555
+ public challenge: typeof WebAuthnApi.prototype._challenge
556
+ public verify: typeof WebAuthnApi.prototype._verify
557
+ public authenticate: typeof WebAuthnApi.prototype._authenticate
558
+ public register: typeof WebAuthnApi.prototype._register
559
+
560
+ constructor(private client: GoTrueClient) {
561
+ // Bind all methods so they can be destructured
562
+ this.enroll = this._enroll.bind(this)
563
+ this.challenge = this._challenge.bind(this)
564
+ this.verify = this._verify.bind(this)
565
+ this.authenticate = this._authenticate.bind(this)
566
+ this.register = this._register.bind(this)
567
+ }
568
+
569
+ /**
570
+ * Enroll a new WebAuthn factor.
571
+ * Creates an unverified WebAuthn factor that must be verified with a credential.
572
+ *
573
+ * @experimental This method is experimental and may change in future releases
574
+ * @param {Omit<MFAEnrollWebauthnParams, 'factorType'>} params - Enrollment parameters (friendlyName required)
575
+ * @returns {Promise<AuthMFAEnrollWebauthnResponse>} Enrolled factor details or error
576
+ * @see {@link https://w3c.github.io/webauthn/#sctn-registering-a-new-credential W3C WebAuthn Spec - Registering a New Credential}
577
+ */
578
+ public async _enroll(
579
+ params: Omit<MFAEnrollWebauthnParams, 'factorType'>
580
+ ): Promise<AuthMFAEnrollWebauthnResponse> {
581
+ return this.client.mfa.enroll({ ...params, factorType: 'webauthn' })
582
+ }
583
+
584
+ /**
585
+ * Challenge for WebAuthn credential creation or authentication.
586
+ * Combines server challenge with browser credential operations.
587
+ * Handles both registration (create) and authentication (request) flows.
588
+ *
589
+ * @experimental This method is experimental and may change in future releases
590
+ * @param {MFAChallengeWebauthnParams & { friendlyName?: string; signal?: AbortSignal }} params - Challenge parameters including factorId
591
+ * @param {Object} overrides - Allows you to override the parameters passed to navigator.credentials
592
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides.create - Override options for credential creation
593
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides.request - Override options for credential request
594
+ * @returns {Promise<RequestResult>} Challenge response with credential or error
595
+ * @see {@link https://w3c.github.io/webauthn/#sctn-credential-creation W3C WebAuthn Spec - Credential Creation}
596
+ * @see {@link https://w3c.github.io/webauthn/#sctn-verifying-assertion W3C WebAuthn Spec - Verifying Assertion}
597
+ */
598
+ public async _challenge(
599
+ {
600
+ factorId,
601
+ webauthn,
602
+ friendlyName,
603
+ signal,
604
+ }: MFAChallengeWebauthnParams & { friendlyName?: string; signal?: AbortSignal },
605
+ overrides?:
606
+ | {
607
+ create?: Partial<PublicKeyCredentialCreationOptionsFuture>
608
+ request?: never
609
+ }
610
+ | {
611
+ create?: never
612
+ request?: Partial<PublicKeyCredentialRequestOptionsFuture>
613
+ }
614
+ ): Promise<
615
+ RequestResult<
616
+ { factorId: string; challengeId: string } & {
617
+ webauthn: StrictOmit<
618
+ MFAVerifyWebauthnParamFields<'create' | 'request'>['webauthn'],
619
+ 'rpId' | 'rpOrigins'
620
+ >
621
+ },
622
+ WebAuthnError | AuthError
623
+ >
624
+ > {
625
+ try {
626
+ // Get challenge from server using the client's MFA methods
627
+ const { data: challengeResponse, error: challengeError } = await this.client.mfa.challenge({
628
+ factorId,
629
+ webauthn,
630
+ })
631
+
632
+ if (!challengeResponse) {
633
+ return { data: null, error: challengeError }
634
+ }
635
+
636
+ const abortSignal = signal ?? webAuthnAbortService.createNewAbortSignal()
637
+
638
+ /** webauthn will fail if either of the name/displayname are blank */
639
+ if (challengeResponse.webauthn.type === 'create') {
640
+ const { user } = challengeResponse.webauthn.credential_options.publicKey
641
+ if (!user.name) {
642
+ // Preserve original format: use friendlyName if provided, otherwise fetch fallback
643
+ // This maintains backward compatibility with the ${user.id}:${name} format
644
+ const nameToUse = friendlyName
645
+ if (!nameToUse) {
646
+ // Only fetch user data if friendlyName is not provided (bug fix for null friendlyName)
647
+ const currentUser = await this.client.getUser()
648
+ const userData = currentUser.data.user
649
+ const fallbackName =
650
+ userData?.user_metadata?.name || userData?.email || userData?.id || 'User'
651
+ user.name = `${user.id}:${fallbackName}`
652
+ } else {
653
+ user.name = `${user.id}:${nameToUse}`
654
+ }
655
+ }
656
+ if (!user.displayName) {
657
+ user.displayName = user.name
658
+ }
659
+ }
660
+
661
+ switch (challengeResponse.webauthn.type) {
662
+ case 'create': {
663
+ const options = mergeCredentialCreationOptions(
664
+ challengeResponse.webauthn.credential_options.publicKey,
665
+ overrides?.create
666
+ )
667
+
668
+ const { data, error } = await createCredential({
669
+ publicKey: options,
670
+ signal: abortSignal,
671
+ })
672
+
673
+ if (data) {
674
+ return {
675
+ data: {
676
+ factorId,
677
+ challengeId: challengeResponse.id,
678
+ webauthn: {
679
+ type: challengeResponse.webauthn.type,
680
+ credential_response: data,
681
+ },
682
+ },
683
+ error: null,
684
+ }
685
+ }
686
+ return { data: null, error }
687
+ }
688
+
689
+ case 'request': {
690
+ const options = mergeCredentialRequestOptions(
691
+ challengeResponse.webauthn.credential_options.publicKey,
692
+ overrides?.request
693
+ )
694
+
695
+ const { data, error } = await getCredential({
696
+ ...challengeResponse.webauthn.credential_options,
697
+ publicKey: options,
698
+ signal: abortSignal,
699
+ })
700
+
701
+ if (data) {
702
+ return {
703
+ data: {
704
+ factorId,
705
+ challengeId: challengeResponse.id,
706
+ webauthn: {
707
+ type: challengeResponse.webauthn.type,
708
+ credential_response: data,
709
+ },
710
+ },
711
+ error: null,
712
+ }
713
+ }
714
+ return { data: null, error }
715
+ }
716
+ }
717
+ } catch (error) {
718
+ if (isAuthError(error)) {
719
+ return { data: null, error }
720
+ }
721
+ return {
722
+ data: null,
723
+ error: new AuthUnknownError('Unexpected error in challenge', error),
724
+ }
725
+ }
726
+ }
727
+
728
+ /**
729
+ * Verify a WebAuthn credential with the server.
730
+ * Completes the WebAuthn ceremony by sending the credential to the server for verification.
731
+ *
732
+ * @experimental This method is experimental and may change in future releases
733
+ * @param {Object} params - Verification parameters
734
+ * @param {string} params.challengeId - ID of the challenge being verified
735
+ * @param {string} params.factorId - ID of the WebAuthn factor
736
+ * @param {MFAVerifyWebauthnParams<T>['webauthn']} params.webauthn - WebAuthn credential response
737
+ * @returns {Promise<AuthMFAVerifyResponse>} Verification result with session or error
738
+ * @see {@link https://w3c.github.io/webauthn/#sctn-verifying-assertion W3C WebAuthn Spec - Verifying an Authentication Assertion}
739
+ * */
740
+ public async _verify<T extends 'create' | 'request'>({
741
+ challengeId,
742
+ factorId,
743
+ webauthn,
744
+ }: {
745
+ challengeId: string
746
+ factorId: string
747
+ webauthn: MFAVerifyWebauthnParams<T>['webauthn']
748
+ }): Promise<AuthMFAVerifyResponse> {
749
+ return this.client.mfa.verify({
750
+ factorId,
751
+ challengeId,
752
+ webauthn: webauthn,
753
+ })
754
+ }
755
+
756
+ /**
757
+ * Complete WebAuthn authentication flow.
758
+ * Performs challenge and verification in a single operation for existing credentials.
759
+ *
760
+ * @experimental This method is experimental and may change in future releases
761
+ * @param {Object} params - Authentication parameters
762
+ * @param {string} params.factorId - ID of the WebAuthn factor to authenticate with
763
+ * @param {Object} params.webauthn - WebAuthn configuration
764
+ * @param {string} params.webauthn.rpId - Relying Party ID (defaults to current hostname)
765
+ * @param {string[]} params.webauthn.rpOrigins - Allowed origins (defaults to current origin)
766
+ * @param {AbortSignal} params.webauthn.signal - Optional abort signal
767
+ * @param {PublicKeyCredentialRequestOptionsFuture} overrides - Override options for navigator.credentials.get
768
+ * @returns {Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>>} Authentication result
769
+ * @see {@link https://w3c.github.io/webauthn/#sctn-authentication W3C WebAuthn Spec - Authentication Ceremony}
770
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredentialRequestOptions MDN - PublicKeyCredentialRequestOptions}
771
+ */
772
+ public async _authenticate(
773
+ {
774
+ factorId,
775
+ webauthn: {
776
+ rpId = typeof window !== 'undefined' ? window.location.hostname : undefined,
777
+ rpOrigins = typeof window !== 'undefined' ? [window.location.origin] : undefined,
778
+ signal,
779
+ } = {},
780
+ }: {
781
+ factorId: string
782
+ webauthn?: {
783
+ rpId?: string
784
+ rpOrigins?: string[]
785
+ signal?: AbortSignal
786
+ }
787
+ },
788
+ overrides?: PublicKeyCredentialRequestOptionsFuture
789
+ ): Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>> {
790
+ if (!rpId) {
791
+ return {
792
+ data: null,
793
+ error: new AuthError('rpId is required for WebAuthn authentication'),
794
+ }
795
+ }
796
+ try {
797
+ if (!browserSupportsWebAuthn()) {
798
+ return {
799
+ data: null,
800
+ error: new AuthUnknownError('Browser does not support WebAuthn', null),
801
+ }
802
+ }
803
+
804
+ // Get challenge and credential
805
+ const { data: challengeResponse, error: challengeError } = await this.challenge(
806
+ {
807
+ factorId,
808
+ webauthn: { rpId, rpOrigins },
809
+ signal,
810
+ },
811
+ { request: overrides }
812
+ )
813
+
814
+ if (!challengeResponse) {
815
+ return { data: null, error: challengeError }
816
+ }
817
+
818
+ const { webauthn } = challengeResponse
819
+
820
+ // Verify credential
821
+ return this._verify({
822
+ factorId,
823
+ challengeId: challengeResponse.challengeId,
824
+ webauthn: {
825
+ type: webauthn.type,
826
+ rpId,
827
+ rpOrigins,
828
+ credential_response: webauthn.credential_response,
829
+ },
830
+ })
831
+ } catch (error) {
832
+ if (isAuthError(error)) {
833
+ return { data: null, error }
834
+ }
835
+ return {
836
+ data: null,
837
+ error: new AuthUnknownError('Unexpected error in authenticate', error),
838
+ }
839
+ }
840
+ }
841
+
842
+ /**
843
+ * Complete WebAuthn registration flow.
844
+ * Performs enrollment, challenge, and verification in a single operation for new credentials.
845
+ *
846
+ * @experimental This method is experimental and may change in future releases
847
+ * @param {Object} params - Registration parameters
848
+ * @param {string} params.friendlyName - User-friendly name for the credential
849
+ * @param {string} params.rpId - Relying Party ID (defaults to current hostname)
850
+ * @param {string[]} params.rpOrigins - Allowed origins (defaults to current origin)
851
+ * @param {AbortSignal} params.signal - Optional abort signal
852
+ * @param {PublicKeyCredentialCreationOptionsFuture} overrides - Override options for navigator.credentials.create
853
+ * @returns {Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>>} Registration result
854
+ * @see {@link https://w3c.github.io/webauthn/#sctn-registering-a-new-credential W3C WebAuthn Spec - Registration Ceremony}
855
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredentialCreationOptions MDN - PublicKeyCredentialCreationOptions}
856
+ */
857
+ public async _register(
858
+ {
859
+ friendlyName,
860
+ webauthn: {
861
+ rpId = typeof window !== 'undefined' ? window.location.hostname : undefined,
862
+ rpOrigins = typeof window !== 'undefined' ? [window.location.origin] : undefined,
863
+ signal,
864
+ } = {},
865
+ }: {
866
+ friendlyName: string
867
+ webauthn?: {
868
+ rpId?: string
869
+ rpOrigins?: string[]
870
+ signal?: AbortSignal
871
+ }
872
+ },
873
+ overrides?: Partial<PublicKeyCredentialCreationOptionsFuture>
874
+ ): Promise<RequestResult<AuthMFAVerifyResponseData, WebAuthnError | AuthError>> {
875
+ if (!rpId) {
876
+ return {
877
+ data: null,
878
+ error: new AuthError('rpId is required for WebAuthn registration'),
879
+ }
880
+ }
881
+ try {
882
+ if (!browserSupportsWebAuthn()) {
883
+ return {
884
+ data: null,
885
+ error: new AuthUnknownError('Browser does not support WebAuthn', null),
886
+ }
887
+ }
888
+
889
+ // Enroll factor
890
+ const { data: factor, error: enrollError } = await this._enroll({
891
+ friendlyName,
892
+ })
893
+
894
+ if (!factor) {
895
+ await this.client.mfa
896
+ .listFactors()
897
+ .then((factors) =>
898
+ factors.data?.all.find(
899
+ (v) =>
900
+ v.factor_type === 'webauthn' &&
901
+ v.friendly_name === friendlyName &&
902
+ v.status !== 'unverified'
903
+ )
904
+ )
905
+ .then((factor) => (factor ? this.client.mfa.unenroll({ factorId: factor?.id }) : void 0))
906
+ return { data: null, error: enrollError }
907
+ }
908
+
909
+ // Get challenge and create credential
910
+ const { data: challengeResponse, error: challengeError } = await this._challenge(
911
+ {
912
+ factorId: factor.id,
913
+ friendlyName: factor.friendly_name,
914
+ webauthn: { rpId, rpOrigins },
915
+ signal,
916
+ },
917
+ {
918
+ create: overrides,
919
+ }
920
+ )
921
+
922
+ if (!challengeResponse) {
923
+ return { data: null, error: challengeError }
924
+ }
925
+
926
+ return this._verify({
927
+ factorId: factor.id,
928
+ challengeId: challengeResponse.challengeId,
929
+ webauthn: {
930
+ rpId,
931
+ rpOrigins,
932
+ type: challengeResponse.webauthn.type,
933
+ credential_response: challengeResponse.webauthn.credential_response,
934
+ },
935
+ })
936
+ } catch (error) {
937
+ if (isAuthError(error)) {
938
+ return { data: null, error }
939
+ }
940
+ return {
941
+ data: null,
942
+ error: new AuthUnknownError('Unexpected error in register', error),
943
+ }
944
+ }
945
+ }
946
+ }