ox 0.12.4 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/_cjs/core/P256.js +1 -1
  3. package/_cjs/core/P256.js.map +1 -1
  4. package/_cjs/core/WebAuthnP256.js +15 -256
  5. package/_cjs/core/WebAuthnP256.js.map +1 -1
  6. package/_cjs/core/WebCryptoP256.js +3 -1
  7. package/_cjs/core/WebCryptoP256.js.map +1 -1
  8. package/_cjs/core/internal/webauthn.js +5 -13
  9. package/_cjs/core/internal/webauthn.js.map +1 -1
  10. package/_cjs/index.docs.js +1 -0
  11. package/_cjs/index.docs.js.map +1 -1
  12. package/_cjs/tempo/KeyAuthorization.js +18 -3
  13. package/_cjs/tempo/KeyAuthorization.js.map +1 -1
  14. package/_cjs/tempo/SignatureEnvelope.js +26 -0
  15. package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
  16. package/_cjs/tempo/TxEnvelopeTempo.js +5 -10
  17. package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
  18. package/_cjs/version.js +1 -1
  19. package/_cjs/webauthn/Authentication.js +246 -0
  20. package/_cjs/webauthn/Authentication.js.map +1 -0
  21. package/_cjs/webauthn/Authenticator.js +55 -0
  22. package/_cjs/webauthn/Authenticator.js.map +1 -0
  23. package/_cjs/webauthn/Credential.js +53 -0
  24. package/_cjs/webauthn/Credential.js.map +1 -0
  25. package/_cjs/webauthn/Registration.js +349 -0
  26. package/_cjs/webauthn/Registration.js.map +1 -0
  27. package/_cjs/webauthn/Types.js +3 -0
  28. package/_cjs/webauthn/Types.js.map +1 -0
  29. package/_cjs/webauthn/index.js +9 -0
  30. package/_cjs/webauthn/index.js.map +1 -0
  31. package/_cjs/webauthn/internal/utils.js +53 -0
  32. package/_cjs/webauthn/internal/utils.js.map +1 -0
  33. package/_esm/core/P256.js +1 -1
  34. package/_esm/core/P256.js.map +1 -1
  35. package/_esm/core/WebAuthnP256.js +13 -261
  36. package/_esm/core/WebAuthnP256.js.map +1 -1
  37. package/_esm/core/WebCryptoP256.js +4 -1
  38. package/_esm/core/WebCryptoP256.js.map +1 -1
  39. package/_esm/core/internal/webauthn.js +5 -13
  40. package/_esm/core/internal/webauthn.js.map +1 -1
  41. package/_esm/erc8021/index.js +2 -2
  42. package/_esm/index.docs.js +1 -0
  43. package/_esm/index.docs.js.map +1 -1
  44. package/_esm/tempo/KeyAuthorization.js +66 -3
  45. package/_esm/tempo/KeyAuthorization.js.map +1 -1
  46. package/_esm/tempo/SignatureEnvelope.js +74 -0
  47. package/_esm/tempo/SignatureEnvelope.js.map +1 -1
  48. package/_esm/tempo/TransactionReceipt.js +1 -1
  49. package/_esm/tempo/TransactionRequest.js +1 -1
  50. package/_esm/tempo/TxEnvelopeTempo.js +5 -10
  51. package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
  52. package/_esm/version.js +1 -1
  53. package/_esm/webauthn/Authentication.js +453 -0
  54. package/_esm/webauthn/Authentication.js.map +1 -0
  55. package/_esm/webauthn/Authenticator.js +176 -0
  56. package/_esm/webauthn/Authenticator.js.map +1 -0
  57. package/_esm/webauthn/Credential.js +95 -0
  58. package/_esm/webauthn/Credential.js.map +1 -0
  59. package/_esm/webauthn/Registration.js +512 -0
  60. package/_esm/webauthn/Registration.js.map +1 -0
  61. package/_esm/webauthn/Types.js +2 -0
  62. package/_esm/webauthn/Types.js.map +1 -0
  63. package/_esm/webauthn/index.js +31 -0
  64. package/_esm/webauthn/index.js.map +1 -0
  65. package/_esm/webauthn/internal/utils.js +52 -0
  66. package/_esm/webauthn/internal/utils.js.map +1 -0
  67. package/_types/core/WebAuthnP256.d.ts +33 -208
  68. package/_types/core/WebAuthnP256.d.ts.map +1 -1
  69. package/_types/core/WebCryptoP256.d.ts +2 -0
  70. package/_types/core/WebCryptoP256.d.ts.map +1 -1
  71. package/_types/core/internal/webauthn.d.ts +2 -110
  72. package/_types/core/internal/webauthn.d.ts.map +1 -1
  73. package/_types/erc8021/index.d.ts +2 -2
  74. package/_types/index.docs.d.ts +1 -0
  75. package/_types/index.docs.d.ts.map +1 -1
  76. package/_types/tempo/KeyAuthorization.d.ts +57 -0
  77. package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
  78. package/_types/tempo/SignatureEnvelope.d.ts +75 -0
  79. package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
  80. package/_types/tempo/Transaction.d.ts +2 -2
  81. package/_types/tempo/TransactionReceipt.d.ts +2 -2
  82. package/_types/tempo/TransactionRequest.d.ts +2 -2
  83. package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  84. package/_types/version.d.ts +1 -1
  85. package/_types/webauthn/Authentication.d.ts +324 -0
  86. package/_types/webauthn/Authentication.d.ts.map +1 -0
  87. package/_types/webauthn/Authenticator.d.ts +182 -0
  88. package/_types/webauthn/Authenticator.d.ts.map +1 -0
  89. package/_types/webauthn/Credential.d.ts +77 -0
  90. package/_types/webauthn/Credential.d.ts.map +1 -0
  91. package/_types/webauthn/Registration.d.ts +308 -0
  92. package/_types/webauthn/Registration.d.ts.map +1 -0
  93. package/_types/webauthn/Types.d.ts +106 -0
  94. package/_types/webauthn/Types.d.ts.map +1 -0
  95. package/_types/webauthn/index.d.ts +33 -0
  96. package/_types/webauthn/index.d.ts.map +1 -0
  97. package/_types/webauthn/internal/utils.d.ts +17 -0
  98. package/_types/webauthn/internal/utils.d.ts.map +1 -0
  99. package/core/P256.ts +1 -1
  100. package/core/WebAuthnP256.ts +37 -582
  101. package/core/WebCryptoP256.ts +6 -1
  102. package/core/internal/webauthn.ts +6 -165
  103. package/erc8021/index.ts +2 -2
  104. package/index.docs.ts +1 -0
  105. package/package.json +31 -1
  106. package/tempo/KeyAuthorization.test.ts +139 -0
  107. package/tempo/KeyAuthorization.ts +82 -3
  108. package/tempo/SignatureEnvelope.test.ts +147 -0
  109. package/tempo/SignatureEnvelope.ts +113 -0
  110. package/tempo/Transaction.ts +2 -2
  111. package/tempo/TransactionReceipt.ts +2 -2
  112. package/tempo/TransactionRequest.ts +2 -2
  113. package/tempo/TxEnvelopeTempo.ts +5 -12
  114. package/tempo/e2e.test.ts +265 -0
  115. package/version.ts +1 -1
  116. package/webauthn/Authentication/package.json +6 -0
  117. package/webauthn/Authentication.ts +673 -0
  118. package/webauthn/Authenticator/package.json +6 -0
  119. package/webauthn/Authenticator.ts +259 -0
  120. package/webauthn/Credential/package.json +6 -0
  121. package/webauthn/Credential.ts +146 -0
  122. package/webauthn/Registration/package.json +6 -0
  123. package/webauthn/Registration.ts +805 -0
  124. package/webauthn/Types/package.json +6 -0
  125. package/webauthn/Types.ts +158 -0
  126. package/webauthn/index.ts +38 -0
  127. package/webauthn/internal/utils.ts +63 -0
  128. package/webauthn/package.json +6 -0
@@ -1,35 +1,15 @@
1
- import * as Base64 from './Base64.js'
2
- import * as Bytes from './Bytes.js'
3
- import * as Cbor from './Cbor.js'
4
- import * as CoseKey from './CoseKey.js'
5
- import * as Errors from './Errors.js'
6
- import * as Hash from './Hash.js'
7
- import * as Hex from './Hex.js'
8
- import type { Compute, OneOf } from './internal/types.js'
9
- import * as internal from './internal/webauthn.js'
10
- import * as P256 from './P256.js'
11
- import type * as PublicKey from './PublicKey.js'
12
- import type * as Signature from './Signature.js'
1
+ import * as Authentication from '../webauthn/Authentication.js'
2
+ import * as Authenticator from '../webauthn/Authenticator.js'
3
+ import type * as Credential_ from '../webauthn/Credential.js'
4
+ import * as Registration from '../webauthn/Registration.js'
13
5
 
14
6
  /** A WebAuthn-flavored P256 credential. */
15
- export type P256Credential = {
16
- id: string
17
- publicKey: PublicKey.PublicKey
18
- raw: internal.PublicKeyCredential
19
- }
7
+ export type P256Credential = Credential_.Credential
20
8
 
21
9
  /** Metadata for a WebAuthn P256 signature. */
22
- export type SignMetadata = Compute<{
23
- authenticatorData: Hex.Hex
24
- challengeIndex?: number | undefined
25
- clientDataJSON: string
26
- typeIndex?: number | undefined
27
- userVerificationRequired?: boolean | undefined
28
- }>
10
+ export type SignMetadata = Credential_.SignMetadata
29
11
 
30
- export const createChallenge = Uint8Array.from([
31
- 105, 171, 180, 181, 160, 222, 75, 198, 42, 42, 32, 31, 141, 37, 186, 233,
32
- ])
12
+ export const createChallenge = Registration.createChallenge
33
13
 
34
14
  /**
35
15
  * Creates a new WebAuthn P256 Credential, which can be stored and later used for signing.
@@ -57,53 +37,12 @@ export const createChallenge = Uint8Array.from([
57
37
  export async function createCredential(
58
38
  options: createCredential.Options,
59
39
  ): Promise<P256Credential> {
60
- const {
61
- createFn = window.navigator.credentials.create.bind(
62
- window.navigator.credentials,
63
- ),
64
- ...rest
65
- } = options
66
- const creationOptions = getCredentialCreationOptions(rest)
67
- try {
68
- const credential = (await createFn(
69
- creationOptions as never,
70
- )) as internal.PublicKeyCredential
71
- if (!credential) throw new CredentialCreationFailedError()
72
-
73
- const response = credential.response as AuthenticatorAttestationResponse
74
- const publicKey = await internal.parseCredentialPublicKey(response)
75
-
76
- return {
77
- id: credential.id,
78
- publicKey,
79
- raw: credential,
80
- }
81
- } catch (error) {
82
- throw new CredentialCreationFailedError({
83
- cause: error as Error,
84
- })
85
- }
40
+ return Registration.create(options)
86
41
  }
87
42
 
88
43
  export declare namespace createCredential {
89
- type Options = getCredentialCreationOptions.Options & {
90
- /**
91
- * Credential creation function. Useful for environments that do not support
92
- * the WebAuthn API natively (i.e. React Native or testing environments).
93
- *
94
- * @default window.navigator.credentials.create
95
- */
96
- createFn?:
97
- | ((
98
- options?: internal.CredentialCreationOptions | undefined,
99
- ) => Promise<internal.Credential | null>)
100
- | undefined
101
- }
102
-
103
- type ErrorType =
104
- | getCredentialCreationOptions.ErrorType
105
- | internal.parseCredentialPublicKey.ErrorType
106
- | Errors.GlobalErrorType
44
+ type Options = Registration.create.Options
45
+ type ErrorType = Registration.create.ErrorType
107
46
  }
108
47
 
109
48
  /**
@@ -153,55 +92,12 @@ export declare namespace createCredential {
153
92
  * @param options - Options to construct the authenticator data.
154
93
  * @returns The authenticator data.
155
94
  */
156
- export function getAuthenticatorData(
157
- options: getAuthenticatorData.Options = {},
158
- ): Hex.Hex {
159
- const {
160
- credential,
161
- flag = 5,
162
- rpId = window.location.hostname,
163
- signCount = 0,
164
- } = options
165
- const rpIdHash = Hash.sha256(Hex.fromString(rpId))
166
- const flag_bytes = Hex.fromNumber(flag, { size: 1 })
167
- const signCount_bytes = Hex.fromNumber(signCount, { size: 4 })
168
- const base = Hex.concat(rpIdHash, flag_bytes, signCount_bytes)
169
-
170
- if (!credential) return base
171
-
172
- // AAGUID (16 bytes of zeros)
173
- const aaguid = Hex.fromBytes(new Uint8Array(16))
174
-
175
- // Credential ID
176
- const credentialId = Hex.fromBytes(credential.id)
177
- const credIdLen = Hex.fromNumber(credential.id.length, { size: 2 })
178
-
179
- // COSE public key
180
- const coseKey = CoseKey.fromPublicKey(credential.publicKey)
181
-
182
- return Hex.concat(base, aaguid, credIdLen, credentialId, coseKey)
183
- }
95
+ export const getAuthenticatorData = Authenticator.getAuthenticatorData
184
96
 
185
97
  export declare namespace getAuthenticatorData {
186
- type Options = {
187
- /** Attested credential data to include (credential ID + public key). When set, the AT flag (0x40) should also be set. */
188
- credential?:
189
- | {
190
- /** The credential ID as raw bytes. */
191
- id: Uint8Array
192
- /** The P256 public key associated with the credential. */
193
- publicKey: PublicKey.PublicKey
194
- }
195
- | undefined
196
- /** A bitfield that indicates various attributes that were asserted by the authenticator. [Read more](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API/Authenticator_data#flags) */
197
- flag?: number | undefined
198
- /** The [Relying Party ID](https://w3c.github.io/webauthn/#relying-party-identifier) that the credential is scoped to. */
199
- rpId?: internal.PublicKeyCredentialRequestOptions['rpId'] | undefined
200
- /** A signature counter, if supported by the authenticator (set to 0 otherwise). */
201
- signCount?: number | undefined
202
- }
98
+ type Options = Authenticator.getAuthenticatorData.Options
203
99
 
204
- type ErrorType = Errors.GlobalErrorType
100
+ type ErrorType = Authenticator.getAuthenticatorData.ErrorType
205
101
  }
206
102
 
207
103
  /**
@@ -231,39 +127,12 @@ export declare namespace getAuthenticatorData {
231
127
  * @param options - Options to construct the client data.
232
128
  * @returns The client data.
233
129
  */
234
- export function getClientDataJSON(options: getClientDataJSON.Options): string {
235
- const {
236
- challenge,
237
- crossOrigin = false,
238
- extraClientData,
239
- origin = window.location.origin,
240
- type = 'webauthn.get',
241
- } = options
242
-
243
- return JSON.stringify({
244
- type,
245
- challenge: Base64.fromHex(challenge, { url: true, pad: false }),
246
- origin,
247
- crossOrigin,
248
- ...extraClientData,
249
- })
250
- }
130
+ export const getClientDataJSON = Authenticator.getClientDataJSON
251
131
 
252
132
  export declare namespace getClientDataJSON {
253
- type Options = {
254
- /** The challenge to sign. */
255
- challenge: Hex.Hex
256
- /** If set to `true`, it means that the calling context is an `<iframe>` that is not same origin with its ancestor frames. */
257
- crossOrigin?: boolean | undefined
258
- /** Additional client data to include in the client data JSON. */
259
- extraClientData?: Record<string, unknown> | undefined
260
- /** The fully qualified origin of the relying party which has been given by the client/browser to the authenticator. */
261
- origin?: string | undefined
262
- /** The WebAuthn ceremony type. @default 'webauthn.get' */
263
- type?: 'webauthn.create' | 'webauthn.get' | undefined
264
- }
133
+ type Options = Authenticator.getClientDataJSON.Options
265
134
 
266
- type ErrorType = Errors.GlobalErrorType
135
+ type ErrorType = Authenticator.getClientDataJSON.ErrorType
267
136
  }
268
137
 
269
138
  /**
@@ -295,28 +164,12 @@ export declare namespace getClientDataJSON {
295
164
  * @param options - Options to construct the attestation object.
296
165
  * @returns The CBOR-encoded attestation object as a Hex string.
297
166
  */
298
- export function getAttestationObject(
299
- options: getAttestationObject.Options,
300
- ): Hex.Hex {
301
- const { attStmt = {}, authData, fmt = 'none' } = options
302
- return Cbor.encode({
303
- fmt,
304
- attStmt,
305
- authData: Hex.toBytes(authData),
306
- })
307
- }
167
+ export const getAttestationObject = Authenticator.getAttestationObject
308
168
 
309
169
  export declare namespace getAttestationObject {
310
- type Options = {
311
- /** Attestation statement. */
312
- attStmt?: Record<string, unknown> | undefined
313
- /** Authenticator data as a Hex string (from {@link ox#WebAuthnP256.(getAuthenticatorData:function)}). */
314
- authData: Hex.Hex
315
- /** Attestation format. @default 'none' */
316
- fmt?: string | undefined
317
- }
170
+ type Options = Authenticator.getAttestationObject.Options
318
171
 
319
- type ErrorType = Cbor.encode.ErrorType | Errors.GlobalErrorType
172
+ type ErrorType = Authenticator.getAttestationObject.ErrorType
320
173
  }
321
174
 
322
175
  /**
@@ -335,128 +188,12 @@ export declare namespace getAttestationObject {
335
188
  * @param options - Options.
336
189
  * @returns The credential creation options.
337
190
  */
338
- export function getCredentialCreationOptions(
339
- options: getCredentialCreationOptions.Options,
340
- ): internal.CredentialCreationOptions {
341
- const {
342
- attestation = 'none',
343
- authenticatorSelection = {
344
- residentKey: 'preferred',
345
- requireResidentKey: false,
346
- userVerification: 'required',
347
- },
348
- challenge = createChallenge,
349
- excludeCredentialIds,
350
- extensions,
351
- name: name_,
352
- rp = {
353
- id: window.location.hostname,
354
- name: window.document.title,
355
- },
356
- user,
357
- } = options
358
- const name = (user?.name ?? name_)!
359
- return {
360
- publicKey: {
361
- attestation,
362
- authenticatorSelection,
363
- challenge:
364
- typeof challenge === 'string' ? Bytes.fromHex(challenge) : challenge,
365
- ...(excludeCredentialIds
366
- ? {
367
- excludeCredentials: excludeCredentialIds?.map((id) => ({
368
- id: Base64.toBytes(id),
369
- type: 'public-key',
370
- })),
371
- }
372
- : {}),
373
- pubKeyCredParams: [
374
- {
375
- type: 'public-key',
376
- alg: -7, // p256
377
- },
378
- ],
379
- ...(extensions && { extensions }),
380
- rp,
381
- user: {
382
- id: user?.id ?? Hash.keccak256(Bytes.fromString(name), { as: 'Bytes' }),
383
- name,
384
- displayName: user?.displayName ?? name,
385
- },
386
- },
387
- }
388
- }
191
+ export const getCredentialCreationOptions = Registration.getOptions
389
192
 
390
193
  export declare namespace getCredentialCreationOptions {
391
- type Options = {
392
- /**
393
- * A string specifying the relying party's preference for how the attestation statement
394
- * (i.e., provision of verifiable evidence of the authenticity of the authenticator and its data)
395
- * is conveyed during credential creation.
396
- */
397
- attestation?:
398
- | internal.PublicKeyCredentialCreationOptions['attestation']
399
- | undefined
400
- /**
401
- * An object whose properties are criteria used to filter out the potential authenticators
402
- * for the credential creation operation.
403
- */
404
- authenticatorSelection?:
405
- | internal.PublicKeyCredentialCreationOptions['authenticatorSelection']
406
- | undefined
407
- /**
408
- * An `ArrayBuffer`, `TypedArray`, or `DataView` used as a cryptographic challenge.
409
- */
410
- challenge?:
411
- | Hex.Hex
412
- | internal.PublicKeyCredentialCreationOptions['challenge']
413
- | undefined
414
- /**
415
- * List of credential IDs to exclude from the creation. This property can be used
416
- * to prevent creation of a credential if it already exists.
417
- */
418
- excludeCredentialIds?: readonly string[] | undefined
419
- /**
420
- * List of Web Authentication API credentials to use during creation or authentication.
421
- */
422
- extensions?:
423
- | internal.PublicKeyCredentialCreationOptions['extensions']
424
- | undefined
425
- /**
426
- * An object describing the relying party that requested the credential creation
427
- */
428
- rp?:
429
- | {
430
- id: string
431
- name: string
432
- }
433
- | undefined
434
- /**
435
- * A numerical hint, in milliseconds, which indicates the time the calling web app is willing to wait for the creation operation to complete.
436
- */
437
- timeout?: internal.PublicKeyCredentialCreationOptions['timeout'] | undefined
438
- } & OneOf<
439
- | {
440
- /** Name for the credential (user.name). */
441
- name: string
442
- }
443
- | {
444
- /**
445
- * An object describing the user account for which the credential is generated.
446
- */
447
- user: {
448
- displayName?: string
449
- id?: BufferSource
450
- name: string
451
- }
452
- }
453
- >
194
+ type Options = Registration.getOptions.Options
454
195
 
455
- type ErrorType =
456
- | Base64.toBytes.ErrorType
457
- | Hash.keccak256.ErrorType
458
- | Bytes.fromString.ErrorType
459
- | Errors.GlobalErrorType
196
+ type ErrorType = Registration.getOptions.ErrorType
460
197
  }
461
198
 
462
199
  /**
@@ -476,63 +213,11 @@ export declare namespace getCredentialCreationOptions {
476
213
  * @param options - Options.
477
214
  * @returns The credential request options.
478
215
  */
479
- export function getCredentialRequestOptions(
480
- options: getCredentialRequestOptions.Options,
481
- ): internal.CredentialRequestOptions {
482
- const {
483
- credentialId,
484
- challenge,
485
- extensions,
486
- rpId = window.location.hostname,
487
- userVerification = 'required',
488
- } = options
489
- return {
490
- publicKey: {
491
- ...(credentialId
492
- ? {
493
- allowCredentials: Array.isArray(credentialId)
494
- ? credentialId.map((id) => ({
495
- id: Base64.toBytes(id),
496
- type: 'public-key',
497
- }))
498
- : [
499
- {
500
- id: Base64.toBytes(credentialId),
501
- type: 'public-key',
502
- },
503
- ],
504
- }
505
- : {}),
506
- challenge: Bytes.fromHex(challenge),
507
- ...(extensions && { extensions }),
508
- rpId,
509
- userVerification,
510
- },
511
- }
512
- }
216
+ export const getCredentialRequestOptions = Authentication.getOptions
513
217
 
514
218
  export declare namespace getCredentialRequestOptions {
515
- type Options = {
516
- /** The credential ID to use. */
517
- credentialId?: string | string[] | undefined
518
- /** The challenge to sign. */
519
- challenge: Hex.Hex
520
- /** List of Web Authentication API credentials to use during creation or authentication. */
521
- extensions?:
522
- | internal.PublicKeyCredentialRequestOptions['extensions']
523
- | undefined
524
- /** The relying party identifier to use. */
525
- rpId?: internal.PublicKeyCredentialRequestOptions['rpId'] | undefined
526
- /** The user verification requirement. */
527
- userVerification?:
528
- | internal.PublicKeyCredentialRequestOptions['userVerification']
529
- | undefined
530
- }
531
-
532
- type ErrorType =
533
- | Bytes.fromHex.ErrorType
534
- | Base64.toBytes.ErrorType
535
- | Errors.GlobalErrorType
219
+ type Options = Authentication.getOptions.Options
220
+ type ErrorType = Authentication.getOptions.ErrorType
536
221
  }
537
222
 
538
223
  /**
@@ -578,85 +263,14 @@ export declare namespace getCredentialRequestOptions {
578
263
  * @param options - Options to construct the signing payload.
579
264
  * @returns The signing payload.
580
265
  */
581
- export function getSignPayload(
582
- options: getSignPayload.Options,
583
- ): getSignPayload.ReturnType {
584
- const {
585
- challenge,
586
- crossOrigin,
587
- extraClientData,
588
- flag,
589
- origin,
590
- rpId,
591
- signCount,
592
- userVerification = 'required',
593
- } = options
594
-
595
- const authenticatorData = getAuthenticatorData({
596
- flag,
597
- rpId,
598
- signCount,
599
- })
600
- const clientDataJSON = getClientDataJSON({
601
- challenge,
602
- crossOrigin,
603
- extraClientData,
604
- origin,
605
- })
606
- const clientDataJSONHash = Hash.sha256(Hex.fromString(clientDataJSON))
607
-
608
- const challengeIndex = clientDataJSON.indexOf('"challenge"')
609
- const typeIndex = clientDataJSON.indexOf('"type"')
610
-
611
- const metadata = {
612
- authenticatorData,
613
- clientDataJSON,
614
- challengeIndex,
615
- typeIndex,
616
- userVerificationRequired: userVerification === 'required',
617
- }
618
-
619
- const payload = Hex.concat(authenticatorData, clientDataJSONHash)
620
-
621
- return { metadata, payload }
622
- }
266
+ export const getSignPayload = Authentication.getSignPayload
623
267
 
624
268
  export declare namespace getSignPayload {
625
- type Options = {
626
- /** The challenge to sign. */
627
- challenge: Hex.Hex
628
- /** If set to `true`, it means that the calling context is an `<iframe>` that is not same origin with its ancestor frames. */
629
- crossOrigin?: boolean | undefined
630
- /** Additional client data to include in the client data JSON. */
631
- extraClientData?: Record<string, unknown> | undefined
632
- /** If set to `true`, the payload will be hashed before being returned. */
633
- hash?: boolean | undefined
634
- /** A bitfield that indicates various attributes that were asserted by the authenticator. [Read more](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API/Authenticator_data#flags) */
635
- flag?: number | undefined
636
- /** The fully qualified origin of the relying party which has been given by the client/browser to the authenticator. */
637
- origin?: string | undefined
638
- /** The [Relying Party ID](https://w3c.github.io/webauthn/#relying-party-identifier) that the credential is scoped to. */
639
- rpId?: internal.PublicKeyCredentialRequestOptions['rpId'] | undefined
640
- /** A signature counter, if supported by the authenticator (set to 0 otherwise). */
641
- signCount?: number | undefined
642
- /** The user verification requirement that the authenticator will enforce. */
643
- userVerification?:
644
- | internal.PublicKeyCredentialRequestOptions['userVerification']
645
- | undefined
646
- }
269
+ type Options = Authentication.getSignPayload.Options
647
270
 
648
- type ReturnType = {
649
- metadata: SignMetadata
650
- payload: Hex.Hex
651
- }
271
+ type ReturnType = Authentication.getSignPayload.ReturnType
652
272
 
653
- type ErrorType =
654
- | Hash.sha256.ErrorType
655
- | Hex.concat.ErrorType
656
- | Hex.fromString.ErrorType
657
- | getAuthenticatorData.ErrorType
658
- | getClientDataJSON.ErrorType
659
- | Errors.GlobalErrorType
273
+ type ErrorType = Authentication.getSignPayload.ErrorType
660
274
  }
661
275
 
662
276
  /**
@@ -692,74 +306,15 @@ export declare namespace getSignPayload {
692
306
  * @returns The signature.
693
307
  */
694
308
  export async function sign(options: sign.Options): Promise<sign.ReturnType> {
695
- const {
696
- getFn = window.navigator.credentials.get.bind(window.navigator.credentials),
697
- ...rest
698
- } = options
699
- const requestOptions = getCredentialRequestOptions(rest)
700
- try {
701
- const credential = (await getFn(
702
- requestOptions as never,
703
- )) as internal.PublicKeyCredential
704
- if (!credential) throw new CredentialRequestFailedError()
705
- const response = credential.response as AuthenticatorAssertionResponse
706
-
707
- const clientDataJSON = String.fromCharCode(
708
- ...new Uint8Array(response.clientDataJSON),
709
- )
710
- const challengeIndex = clientDataJSON.indexOf('"challenge"')
711
- const typeIndex = clientDataJSON.indexOf('"type"')
712
-
713
- const signature = internal.parseAsn1Signature(
714
- new Uint8Array(response.signature),
715
- )
716
-
717
- return {
718
- metadata: {
719
- authenticatorData: Hex.fromBytes(
720
- new Uint8Array(response.authenticatorData),
721
- ),
722
- clientDataJSON,
723
- challengeIndex,
724
- typeIndex,
725
- userVerificationRequired:
726
- requestOptions.publicKey!.userVerification === 'required',
727
- },
728
- signature,
729
- raw: credential,
730
- }
731
- } catch (error) {
732
- throw new CredentialRequestFailedError({
733
- cause: error as Error,
734
- })
735
- }
309
+ return Authentication.sign(options)
736
310
  }
737
311
 
738
312
  export declare namespace sign {
739
- type Options = getCredentialRequestOptions.Options & {
740
- /**
741
- * Credential request function. Useful for environments that do not support
742
- * the WebAuthn API natively (i.e. React Native or testing environments).
743
- *
744
- * @default window.navigator.credentials.get
745
- */
746
- getFn?:
747
- | ((
748
- options?: internal.CredentialRequestOptions | undefined,
749
- ) => Promise<internal.Credential | null>)
750
- | undefined
751
- }
313
+ type Options = Authentication.sign.Options
752
314
 
753
- type ReturnType = {
754
- metadata: SignMetadata
755
- raw: internal.PublicKeyCredential
756
- signature: Signature.Signature<false>
757
- }
315
+ type ReturnType = Authentication.sign.ReturnType
758
316
 
759
- type ErrorType =
760
- | Hex.fromBytes.ErrorType
761
- | getCredentialRequestOptions.ErrorType
762
- | Errors.GlobalErrorType
317
+ type ErrorType = Authentication.sign.ErrorType
763
318
  }
764
319
 
765
320
  /**
@@ -791,113 +346,13 @@ export declare namespace sign {
791
346
  * @returns Whether the signature is valid.
792
347
  */
793
348
  export function verify(options: verify.Options): boolean {
794
- const { challenge, hash = true, metadata, publicKey, signature } = options
795
- const {
796
- authenticatorData,
797
- challengeIndex,
798
- clientDataJSON,
799
- typeIndex,
800
- userVerificationRequired,
801
- } = metadata
802
-
803
- const authenticatorDataBytes = Bytes.fromHex(authenticatorData)
804
-
805
- // Check length of `authenticatorData`.
806
- if (authenticatorDataBytes.length < 37) return false
807
-
808
- const flag = authenticatorDataBytes[32]!
809
-
810
- // Verify that the UP bit of the flags in authData is set.
811
- if ((flag & 0x01) !== 0x01) return false
812
-
813
- // If user verification was determined to be required, verify that
814
- // the UV bit of the flags in authData is set. Otherwise, ignore the
815
- // value of the UV flag.
816
- if (userVerificationRequired && (flag & 0x04) !== 0x04) return false
817
-
818
- // If the BE bit of the flags in authData is not set, verify that
819
- // the BS bit is not set.
820
- if ((flag & 0x08) !== 0x08 && (flag & 0x10) === 0x10) return false
821
-
822
- // Check that response is for an authentication assertion (if typeIndex is provided)
823
- if (typeIndex !== undefined) {
824
- const type = '"type":"webauthn.get"'
825
- if (
826
- type !==
827
- clientDataJSON.slice(Number(typeIndex), Number(typeIndex) + type.length)
828
- )
829
- return false
830
- }
831
-
832
- // Extract and validate the challenge from clientDataJSON
833
- const challengeMatch =
834
- challengeIndex !== undefined
835
- ? clientDataJSON
836
- .slice(Number(challengeIndex))
837
- .match(/^"challenge":"(.*?)"/)
838
- : clientDataJSON.match(/"challenge":"(.*?)"/)
839
- if (!challengeMatch) return false
840
-
841
- // Validate the challenge in the clientDataJSON.
842
- const [_, challenge_extracted] = challengeMatch
843
- if (Hex.fromBytes(Base64.toBytes(challenge_extracted!)) !== challenge)
844
- return false
845
-
846
- const clientDataJSONHash = Hash.sha256(Bytes.fromString(clientDataJSON), {
847
- as: 'Bytes',
848
- })
849
- const payload = Bytes.concat(authenticatorDataBytes, clientDataJSONHash)
850
-
851
- return P256.verify({
852
- hash,
853
- payload,
854
- publicKey,
855
- signature,
856
- })
349
+ return Authentication.verify(options)
857
350
  }
858
351
 
859
352
  export declare namespace verify {
860
- type Options = {
861
- /** The challenge to verify. */
862
- challenge: Hex.Hex
863
- /** If set to `true`, the payload will be hashed (sha256) before being verified. */
864
- hash?: boolean | undefined
865
- /** The public key to verify the signature with. */
866
- publicKey: PublicKey.PublicKey
867
- /** The signature to verify. */
868
- signature: Signature.Signature<false>
869
- /** The metadata to verify the signature with. */
870
- metadata: SignMetadata
871
- }
872
-
873
- type ErrorType =
874
- | Base64.toBytes.ErrorType
875
- | Bytes.concat.ErrorType
876
- | Bytes.fromHex.ErrorType
877
- | P256.verify.ErrorType
878
- | Errors.GlobalErrorType
879
- }
880
-
881
- /** Thrown when a WebAuthn P256 credential creation fails. */
882
- export class CredentialCreationFailedError extends Errors.BaseError<Error> {
883
- override readonly name = 'WebAuthnP256.CredentialCreationFailedError'
884
-
885
- constructor({ cause }: { cause?: Error | undefined } = {}) {
886
- super('Failed to create credential.', {
887
- cause,
888
- })
889
- }
890
- }
891
-
892
- /** Thrown when a WebAuthn P256 credential request fails. */
893
- export class CredentialRequestFailedError extends Errors.BaseError<Error> {
894
- override readonly name = 'WebAuthnP256.CredentialRequestFailedError'
353
+ type Options = Authentication.verify.Options
895
354
 
896
- constructor({ cause }: { cause?: Error | undefined } = {}) {
897
- super('Failed to request credential.', {
898
- cause,
899
- })
900
- }
355
+ type ErrorType = Authentication.verify.ErrorType
901
356
  }
902
357
 
903
358
  // Export types required for inference.
@@ -926,4 +381,4 @@ export type {
926
381
  PublicKeyCredentialUserEntity,
927
382
  ResidentKeyRequirement,
928
383
  UserVerificationRequirement,
929
- } from './internal/webauthn.js'
384
+ } from '../webauthn/Types.js'