Skip to content

Commit 1632e62

Browse files
committed
fix: move types to extend simplewebauthn
1 parent a225511 commit 1632e62

4 files changed

Lines changed: 64 additions & 94 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"devDependencies": {
3636
"@biomejs/biome": "1.2.2",
3737
"@changesets/cli": "^2.26.2",
38+
"@simplewebauthn/typescript-types": "^8.0.0",
3839
"@types/react": "^18.0.25",
3940
"expo-module-scripts": "^3.0.11",
4041
"expo-modules-core": "^1.5.7",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ReactNativePasskeys.types.ts

Lines changed: 44 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1-
// ! adapted from https://github.com/github/webauthn-json/blob/63958abfcf04d1e56e3d054d9a156d1cfb3d3ee0/src/webauthn-json/basic/json.ts
2-
3-
/**
4-
* Using a branded type to communicate that this isn't just any string, but a Base64URL-encoded string
5-
*/
6-
export type Base64URLString = string
1+
import type {
2+
// - for override
3+
AuthenticationExtensionsClientInputs as TypeScriptAuthenticationExtensionsClientInputs,
4+
// - for use & reexport
5+
Base64URLString,
6+
AuthenticatorTransportFuture,
7+
PublicKeyCredentialJSON,
8+
PublicKeyCredentialDescriptorJSON,
9+
PublicKeyCredentialUserEntityJSON,
10+
AuthenticatorAttestationResponseJSON,
11+
} from '@simplewebauthn/typescript-types'
12+
13+
export type {
14+
AttestationConveyancePreference,
15+
AuthenticationCredential,
16+
AuthenticatorAssertionResponse,
17+
AuthenticatorAttachment,
18+
AuthenticatorAttestationResponse,
19+
AuthenticatorSelectionCriteria,
20+
AuthenticatorTransport,
21+
COSEAlgorithmIdentifier,
22+
Crypto,
23+
PublicKeyCredentialCreationOptions,
24+
PublicKeyCredentialDescriptor,
25+
PublicKeyCredentialParameters,
26+
PublicKeyCredentialRequestOptions,
27+
PublicKeyCredentialRpEntity,
28+
PublicKeyCredentialType,
29+
PublicKeyCredentialUserEntity,
30+
RegistrationCredential,
31+
UserVerificationRequirement,
32+
} from '@simplewebauthn/typescript-types'
33+
34+
export type {
35+
Base64URLString,
36+
PublicKeyCredentialJSON,
37+
AuthenticatorTransportFuture,
38+
PublicKeyCredentialDescriptorJSON,
39+
PublicKeyCredentialUserEntityJSON,
40+
AuthenticatorAttestationResponseJSON,
41+
}
742

843
/**
944
* A variant of PublicKeyCredentialCreationOptions suitable for JSON transmission
@@ -37,24 +72,6 @@ export interface PublicKeyCredentialRequestOptionsJSON {
3772
extensions?: AuthenticationExtensionsClientInputs
3873
}
3974

40-
/**
41-
* - Specification reference: https://w3c.github.io/webauthn/#dictdef-publickeycredentialdescriptorjson
42-
*/
43-
export interface PublicKeyCredentialDescriptorJSON {
44-
id: Base64URLString
45-
type: PublicKeyCredentialType
46-
transports?: AuthenticatorTransportFuture[]
47-
}
48-
49-
/**
50-
* - Specification reference: https://w3c.github.io/webauthn/#dictdef-publickeycredentialuserentityjson
51-
*/
52-
export interface PublicKeyCredentialUserEntityJSON {
53-
id: string
54-
name: string
55-
displayName: string
56-
}
57-
5875
/**
5976
* A slightly-modified RegistrationCredential to simplify working with ArrayBuffers that
6077
* are Base64URL-encoded so that they can be sent as JSON.
@@ -85,24 +102,6 @@ export interface AuthenticationResponseJSON {
85102
type: PublicKeyCredentialType
86103
}
87104

88-
/**
89-
* A slightly-modified AuthenticatorAttestationResponse to simplify working with ArrayBuffers that
90-
* are Base64URL-encoded so that they can be sent as JSON.
91-
*
92-
* - Specification reference: https://w3c.github.io/webauthn/#dictdef-authenticatorattestationresponsejson
93-
*/
94-
export interface AuthenticatorAttestationResponseJSON {
95-
clientDataJSON: Base64URLString
96-
attestationObject: Base64URLString
97-
// Optional in L2, but becomes required in L3. Play it safe until L3 becomes Recommendation
98-
authenticatorData?: Base64URLString
99-
// Optional in L2, but becomes required in L3. Play it safe until L3 becomes Recommendation
100-
transports?: AuthenticatorTransportFuture[]
101-
// Optional in L2, but becomes required in L3. Play it safe until L3 becomes Recommendation
102-
publicKeyAlgorithm?: COSEAlgorithmIdentifier
103-
publicKey?: Base64URLString
104-
}
105-
106105
/**
107106
* A slightly-modified AuthenticatorAssertionResponse to simplify working with ArrayBuffers that
108107
* are Base64URL-encoded so that they can be sent as JSON.
@@ -116,57 +115,15 @@ export interface AuthenticatorAssertionResponseJSON {
116115
userHandle?: string
117116
}
118117

119-
/**
120-
* A WebAuthn-compatible device and the information needed to verify assertions by it
121-
*/
122-
export type AuthenticatorDevice = {
123-
credentialPublicKey: Uint8Array
124-
credentialID: Uint8Array
125-
// Number of times this authenticator is expected to have been used
126-
counter: number
127-
// From browser's `startRegistration()` -> RegistrationCredentialJSON.transports (API L2 and up)
128-
transports?: AuthenticatorTransportFuture[]
129-
}
130-
131-
/**
132-
* A super class of TypeScript's `AuthenticatorTransport` that includes support for the latest
133-
* transports. Should eventually be replaced by TypeScript's when TypeScript gets updated to
134-
* know about it (sometime after 5.3)
135-
*/
136-
export type AuthenticatorTransportFuture =
137-
| 'ble'
138-
| 'cable'
139-
| 'hybrid'
140-
| 'internal'
141-
| 'nfc'
142-
| 'smart-card'
143-
| 'usb'
144-
145-
/**
146-
* A super class of TypeScript's `PublicKeyCredentialDescriptor` that knows about the latest
147-
* transports. Should eventually be replaced by TypeScript's when TypeScript gets updated to
148-
* know about it (sometime after 5.3)
149-
*/
150-
export interface PublicKeyCredentialDescriptorFuture
151-
extends Omit<PublicKeyCredentialDescriptor, 'transports'> {
152-
transports?: AuthenticatorTransportFuture[]
153-
}
154-
155-
/**
156-
* - Specification reference: https://w3c.github.io/webauthn/#typedefdef-publickeycredentialjson
157-
*/
158-
export type PublicKeyCredentialJSON = RegistrationResponseJSON | AuthenticationResponseJSON
159-
160118
/**
161119
* TypeScript's types are behind the latest extensions spec, so we define them here.
162120
* Should eventually be replaced by TypeScript's when TypeScript gets updated to
163121
* know about it (sometime after 5.3)
164-
*/
165-
166-
/**
122+
*
167123
* - Specification reference: https://w3c.github.io/webauthn/#dictdef-authenticationextensionsclientinputs
168124
*/
169-
export interface AuthenticationExtensionsClientInputs {
125+
export interface AuthenticationExtensionsClientInputs
126+
extends TypeScriptAuthenticationExtensionsClientInputs {
170127
largeBlob?: AuthenticationExtensionsLargeBlobInputs
171128
}
172129

src/ReactNativePasskeysModule.web.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type {
22
AuthenticationCredential,
3+
AuthenticationExtensionsClientInputs,
4+
AuthenticationExtensionsClientOutputs,
35
AuthenticationResponseJSON,
46
PublicKeyCredentialCreationOptionsJSON,
57
PublicKeyCredentialRequestOptionsJSON,
6-
AuthenticationExtensionsClientInputs,
78
RegistrationCredential,
89
RegistrationResponseJSON,
910
} from './ReactNativePasskeys.types'
@@ -39,19 +40,22 @@ export default {
3940

4041
const credential = (await navigator.credentials.create({
4142
signal,
42-
// @ts-expect-error: largeBlob is not included in the TS navigator credential types yet
4343
publicKey: {
4444
...request,
4545
challenge: base64URLStringToBuffer(request.challenge),
4646
user: { ...request.user, id: base64URLStringToBuffer(request.user.id) },
4747
excludeCredentials: request.excludeCredentials?.map((credential) => ({
4848
...credential,
4949
id: base64URLStringToBuffer(credential.id),
50+
// TODO: remove the override when typescript has updated webauthn types
51+
transports: (credential.transports ?? undefined) as AuthenticatorTransport[] | undefined,
5052
})),
5153
},
5254
})) as RegistrationCredential
5355

54-
const clientExtensionResults = credential?.getClientExtensionResults()
56+
// TODO: remove the override when typescript has updated webauthn types
57+
const clientExtensionResults =
58+
credential?.getClientExtensionResults() as AuthenticationExtensionsClientOutputs
5559

5660
warnUserOfMissingWebauthnExtensions(request.extensions, clientExtensionResults)
5761

@@ -66,7 +70,6 @@ export default {
6670
},
6771
authenticatorAttachment: undefined,
6872
type: 'public-key',
69-
// @ts-expect-error: TS navigator credential clientExtensionResults types are behind
7073
clientExtensionResults,
7174
}
7275
},
@@ -83,18 +86,21 @@ export default {
8386
const credential = (await navigator.credentials.get({
8487
mediation,
8588
signal,
86-
// @ts-expect-error: largeBlob is not included in the TS navigator credential types yet
8789
publicKey: {
8890
...request,
8991
challenge: base64URLStringToBuffer(request.challenge),
9092
allowCredentials: request.allowCredentials?.map((credential) => ({
9193
...credential,
9294
id: base64URLStringToBuffer(credential.id),
95+
// TODO: remove the override when typescript has updated webauthn types
96+
transports: (credential.transports ?? undefined) as AuthenticatorTransport[] | undefined,
9397
})),
9498
},
9599
})) as AuthenticationCredential
96100

97-
const clientExtensionResults = credential?.getClientExtensionResults()
101+
// TODO: remove the override when typescript has updated webauthn types
102+
const clientExtensionResults =
103+
credential?.getClientExtensionResults() as AuthenticationExtensionsClientOutputs
98104

99105
warnUserOfMissingWebauthnExtensions(request.extensions, clientExtensionResults)
100106

@@ -112,7 +118,6 @@ export default {
112118
: undefined,
113119
},
114120
authenticatorAttachment: undefined,
115-
// @ts-expect-error: TS navigator credential clientExtensionResults types are behind
116121
clientExtensionResults,
117122
type: 'public-key',
118123
}

0 commit comments

Comments
 (0)