You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In actual implementation, if a publicKey config is specified (and one type of credential config must be specified for both methods), both methods will return a PublicKeyCredential (or null). Currently we're forced to downcast to the correct type, e.g.:
constcredential=awaitCredentialsContainer.create({publicKey: { ... }});if(credential){constpkc=credentialasPublicKeyCredential;// do things with pkc}
Additionally, the PublicKeyCredential type definition is incomplete.
Instances are missing the pkc.toJSON() helper method.
Browser support still varies, so leaving it out might be deliberate. Firefox and Chrome support it; Safari does not, Opera is developing it, and all device WebViews do not support it.
The pkc.response field currently always has type AuthenticatorResponse. This is incomplete. Per MDN:
if the PublicKeyCredential was obtained via create(), the response field will be of type AuthenticatorAttestationResponse (attestation)
if it was obtained via get(), the response field will be of type AuthenticatorAssertionResponse (assertion).
Both AuthenticatorAttestationResponse and AuthenticatorAssertionResponse are already defined in lib.dom.d.ts.
Recommendation
PublicKeyCredential should instead be defined with a generic property that accepts the response type, e.g.:
consttoUint8Array=(str: string)=>Uint8Array.from(str,c=>c.charCodeAt(0));constcredential=awaitnavigator.credentials.create({publicKey: {challenge: toUint8Array("random challenge from server"),rp: {name: "WebAuthN",id: "webauth.io",// Run this code on the webauthn.io domain, or change this to another domain.},user: {id: toUint8Array("username"),name: "Name",displayName: "Display name",},pubKeyCredParams: [{alg: -7,type: "public-key"},],attestation: "direct",},});if(credential){console.debug(credential.toJSON());// ~~~~~~// Property 'toJSON' does not exist on type 'Credential'.ts(2339)console.debug(credential.response);// ~~~~~~~~// Property 'response' does not exist on type 'Credential'.ts(2339)console.debug((credentialasPublicKeyCredential).response.getAuthenticatorData());// ~~~~~~~~~~~~~~~~~~~~// Property 'getAuthenticatorData' does not exist on type 'AuthenticatorResponse'.ts(2339)// Currently forced to downcast twice like this essentially, which TypeScript permits:constpkc=credentialas(PublicKeyCredential&{response: AuthenticatorAttestationResponse});console.debug(pkc.response.getAuthenticatorData());}
The text was updated successfully, but these errors were encountered:
scarletcs
changed the title
CredentialsContainer.create() and CredentialsContainer.get() should be able to return PublicKeyCredential, and the PKC interface should be more well-definedCredentialsContainer.create() & .get() should be able to return PublicKeyCredential, and the PKC interface should be more well-defined
Nov 29, 2024
⚙ Compilation target
ESNext or ES2024
(I am building for current up-to-date browsers.)
⚙ Library
DOM
Missing / Incorrect Definition
CredentialsContainer.create()
andCredentialsContainer.get()
currently always return aPromise<Credential | null>
regardless of config. This is incomplete and less helpful than it could be.In actual implementation, if a
publicKey
config is specified (and one type of credential config must be specified for both methods), both methods will return aPublicKeyCredential
(or null). Currently we're forced to downcast to the correct type, e.g.:Additionally, the
PublicKeyCredential
type definition is incomplete.pkc.toJSON()
helper method.pkc.response
field currently always has typeAuthenticatorResponse
. This is incomplete. Per MDN:create()
, the response field will be of typeAuthenticatorAttestationResponse
(attestation)get()
, the response field will be of typeAuthenticatorAssertionResponse
(assertion).Both
AuthenticatorAttestationResponse
andAuthenticatorAssertionResponse
are already defined inlib.dom.d.ts
.Recommendation
PublicKeyCredential
should instead be defined with a generic property that accepts the response type, e.g.:Then
create()
andget()
should be overloaded methods approximately like the following. All the types referenced below already exist inlib.dom.d.ts
.Sample Code
Documentation Link
CredentialsContainer.create()
: https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/createCredentialsContainer.get()
: https://developer.mozilla.org/en-US/docs/Web/API/CredentialsContainer/getPublicKeyCredential.toJSON()
: https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential/toJSONPublicKeyCredential.response
: https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential/responseAuthenticatorAttestationResponse.getAuthenticatorData()
: https://developer.mozilla.org/en-US/docs/Web/API/AuthenticatorAttestationResponse/getAuthenticatorDataThe text was updated successfully, but these errors were encountered: