|
| 1 | +import type { PublicKeyCredentialClientCapabilities } from '../types/index.ts'; |
| 2 | + |
| 3 | +/** |
| 4 | + * A helper method that wraps WebAuthn's |
| 5 | + * [`PublicKeyCredential.getClientCapabilities()`](https://w3c.github.io/webauthn/#sctn-getClientCapabilities) |
| 6 | + * feature detection method. This method includes efforts to determine a feature's availability if a |
| 7 | + * browser reports an "unknown" level of support for it but there exists an alternative WebAuthn API |
| 8 | + * that reports the feature's availability. |
| 9 | + * |
| 10 | + * Capabilities are mapped to one of the following values: |
| 11 | + * |
| 12 | + * - **supported**: The browser supports the capability |
| 13 | + * - **unsupported**: The browser does not support the capability |
| 14 | + * - **unknown**: The browser may or may not support the capability, try it and see |
| 15 | + * |
| 16 | + * **Note:** If a WebAuthn client other than the browser is handling WebAuthn API calls (e.g. a |
| 17 | + * password manager's browser extension) then this method will report that client's capabilities |
| 18 | + * instead (assuming that client has implemented `PublicKeyCredential.getClientCapabilities()`.) |
| 19 | + */ |
| 20 | +export async function getBrowserCapabilities(): Promise<BrowserCapabilities> { |
| 21 | + if (typeof PublicKeyCredential.getClientCapabilities === 'function') { |
| 22 | + const capabilities = await PublicKeyCredential |
| 23 | + .getClientCapabilities() as PublicKeyCredentialClientCapabilities; |
| 24 | + |
| 25 | + /** |
| 26 | + * The `userVerifyingPlatformAuthenticator` capability and `isUVPAA()` return value are |
| 27 | + * equivalent, so we can fall back to the value of the latter when the capability is unknown. |
| 28 | + * |
| 29 | + * https://w3c.github.io/webauthn/#dom-clientcapability-userverifyingplatformauthenticator |
| 30 | + */ |
| 31 | + let _userVerifyingPlatformAuthenticator = mapCapabilityToEnum( |
| 32 | + capabilities.userVerifyingPlatformAuthenticator, |
| 33 | + ); |
| 34 | + |
| 35 | + if ( |
| 36 | + _userVerifyingPlatformAuthenticator === 'unknown' && |
| 37 | + typeof PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable === 'function' |
| 38 | + ) { |
| 39 | + const isUVPAA = await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(); |
| 40 | + if (isUVPAA) { |
| 41 | + _userVerifyingPlatformAuthenticator = 'supported'; |
| 42 | + } else { |
| 43 | + _userVerifyingPlatformAuthenticator = 'unsupported'; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The `conditionalGet` capability and `isConditionalMediationAvailable()` return value are |
| 49 | + * equivalent, so we can fall back to the value of the latter when the capability is unknown. |
| 50 | + * |
| 51 | + * https://w3c.github.io/webauthn/#dom-clientcapability-conditionalget |
| 52 | + */ |
| 53 | + let _conditionalGet = mapCapabilityToEnum(capabilities.conditionalGet); |
| 54 | + if ( |
| 55 | + _conditionalGet === 'unknown' && |
| 56 | + typeof PublicKeyCredential.isConditionalMediationAvailable === 'function' |
| 57 | + ) { |
| 58 | + const isCMA = await PublicKeyCredential.isConditionalMediationAvailable(); |
| 59 | + if (isCMA) { |
| 60 | + _conditionalGet = 'supported'; |
| 61 | + } else { |
| 62 | + _conditionalGet = 'unsupported'; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return _getBrowserCapabilitiesInternals.stubThis({ |
| 67 | + conditionalCreate: mapCapabilityToEnum(capabilities.conditionalCreate), |
| 68 | + conditionalGet: _conditionalGet, |
| 69 | + hybridTransport: mapCapabilityToEnum(capabilities.hybridTransport), |
| 70 | + passkeyPlatformAuthenticator: mapCapabilityToEnum(capabilities.passkeyPlatformAuthenticator), |
| 71 | + userVerifyingPlatformAuthenticator: _userVerifyingPlatformAuthenticator, |
| 72 | + relatedOrigins: mapCapabilityToEnum(capabilities.relatedOrigins), |
| 73 | + signalAllAcceptedCredentials: mapCapabilityToEnum(capabilities.signalAllAcceptedCredentials), |
| 74 | + signalCurrentUserDetails: mapCapabilityToEnum(capabilities.signalCurrentUserDetails), |
| 75 | + signalUnknownCredential: mapCapabilityToEnum(capabilities.signalUnknownCredential), |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return _getBrowserCapabilitiesInternals.stubThis({ |
| 80 | + conditionalCreate: 'unknown', |
| 81 | + conditionalGet: 'unknown', |
| 82 | + hybridTransport: 'unknown', |
| 83 | + passkeyPlatformAuthenticator: 'unknown', |
| 84 | + userVerifyingPlatformAuthenticator: 'unknown', |
| 85 | + relatedOrigins: 'unknown', |
| 86 | + signalAllAcceptedCredentials: 'unknown', |
| 87 | + signalCurrentUserDetails: 'unknown', |
| 88 | + signalUnknownCredential: 'unknown', |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * One of the following values: |
| 94 | + * |
| 95 | + * - **supported**: The browser supports the corresponding capability |
| 96 | + * - **unsupported**: The browser does not support the corresponding capability |
| 97 | + * - **unknown**: The browser may or may not support the capability, try it and see |
| 98 | + */ |
| 99 | +export type BrowserCapabilitySupport = 'supported' | 'unsupported' | 'unknown'; |
| 100 | + |
| 101 | +/** |
| 102 | + * Various WebAuthn features the browser may support. See each property's description for more info. |
| 103 | + */ |
| 104 | +export type BrowserCapabilities = { |
| 105 | + /** The browser can facilitate silent passkey registration after a successful auth */ |
| 106 | + conditionalCreate: BrowserCapabilitySupport; |
| 107 | + /** The browser supports autofill UI to present available passkeys */ |
| 108 | + conditionalGet: BrowserCapabilitySupport; |
| 109 | + /** The browser can communicate with another device via the hybrid transport to use a passkey */ |
| 110 | + hybridTransport: BrowserCapabilitySupport; |
| 111 | + /** The browser can use a local platform authenticator, or a platform authenticator available on another device via the hybrid transport */ |
| 112 | + passkeyPlatformAuthenticator: BrowserCapabilitySupport; |
| 113 | + /** The browser can use a locally available user-verifying platform authenticator */ |
| 114 | + userVerifyingPlatformAuthenticator: BrowserCapabilitySupport; |
| 115 | + /** The browser can facilitate use of a passkey, bound to one RP ID, across different origins */ |
| 116 | + relatedOrigins: BrowserCapabilitySupport; |
| 117 | + /** The browser supports the signal API that communicates the user's current allowed passkeys for this site */ |
| 118 | + signalAllAcceptedCredentials: BrowserCapabilitySupport; |
| 119 | + /** The browser supports the signal API that communicates the user's current metadata */ |
| 120 | + signalCurrentUserDetails: BrowserCapabilitySupport; |
| 121 | + /** The browser supports the signal API that communicates an invalid credential ID */ |
| 122 | + signalUnknownCredential: BrowserCapabilitySupport; |
| 123 | +}; |
| 124 | + |
| 125 | +/** |
| 126 | + * Map the tri-state booleans in `PublicKeyCredential.getClientCapablities()` to more descriptive |
| 127 | + * values instead. |
| 128 | + */ |
| 129 | +function mapCapabilityToEnum(value?: boolean): BrowserCapabilitySupport { |
| 130 | + if (value === true) { |
| 131 | + return 'supported'; |
| 132 | + } else if (value === false) { |
| 133 | + return 'unsupported'; |
| 134 | + } else if (typeof value === 'undefined') { |
| 135 | + return 'unknown'; |
| 136 | + } else { |
| 137 | + throw new Error('Unexpected capability value:', value); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// Make it possible to stub the return value during testing |
| 142 | +export const _getBrowserCapabilitiesInternals = { |
| 143 | + stubThis: (value: BrowserCapabilities) => value, |
| 144 | +}; |
0 commit comments