Skip to content

Commit b36fb83

Browse files
committed
Create new browserSupportsPasskeys helper
1 parent eff2c19 commit b36fb83

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/// <reference lib="DOM" />
2+
import { assert, assertFalse } from '@std/assert';
3+
import { returnsNext, stub } from '@std/testing/mock';
4+
import { browserSupportsPasskeys } from './browserSupportsPasskeys.ts';
5+
import { _getBrowserCapabilitiesInternals } from './getBrowserCapabilities.ts';
6+
7+
Deno.test('Returns true when passkeyPlatformAuthenticator capability is supported', async () => {
8+
// @ts-ignore: Setting up global state
9+
globalThis.PublicKeyCredential = () => {};
10+
11+
const mockGetBrowserCapabilities = stub(
12+
_getBrowserCapabilitiesInternals,
13+
'stubThis',
14+
// @ts-ignore: Keeping tests lean
15+
returnsNext([{ passkeyPlatformAuthenticator: 'supported' }]),
16+
);
17+
18+
const supported = await browserSupportsPasskeys();
19+
20+
assert(supported);
21+
22+
mockGetBrowserCapabilities.restore();
23+
});
24+
25+
Deno.test('Returns true when userVerifyingPlatformAuthenticator capability is supported', async () => {
26+
// @ts-ignore: Setting up global state
27+
globalThis.PublicKeyCredential = () => {};
28+
29+
const mockGetBrowserCapabilities = stub(
30+
_getBrowserCapabilitiesInternals,
31+
'stubThis',
32+
// @ts-ignore: Keeping tests lean
33+
returnsNext([{ userVerifyingPlatformAuthenticator: 'supported' }]),
34+
);
35+
36+
const supported = await browserSupportsPasskeys();
37+
38+
assert(supported);
39+
40+
mockGetBrowserCapabilities.restore();
41+
});
42+
43+
Deno.test('Returns true when hybridTransport capability is supported', async () => {
44+
// @ts-ignore: Setting up global state
45+
globalThis.PublicKeyCredential = () => {};
46+
47+
const mockGetBrowserCapabilities = stub(
48+
_getBrowserCapabilitiesInternals,
49+
'stubThis',
50+
// @ts-ignore: Keeping tests lean
51+
returnsNext([{ hybridTransport: 'supported' }]),
52+
);
53+
54+
const supported = await browserSupportsPasskeys();
55+
56+
assert(supported);
57+
58+
mockGetBrowserCapabilities.restore();
59+
});
60+
61+
Deno.test('Returns false when no relevant capabilities are supported', async () => {
62+
// @ts-ignore: Setting up global state
63+
globalThis.PublicKeyCredential = () => {};
64+
65+
const mockGetBrowserCapabilities = stub(
66+
_getBrowserCapabilitiesInternals,
67+
'stubThis',
68+
// @ts-ignore: Keeping tests lean
69+
returnsNext([{}]),
70+
);
71+
72+
const supported = await browserSupportsPasskeys();
73+
74+
assertFalse(supported);
75+
76+
mockGetBrowserCapabilities.restore();
77+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { browserSupportsWebAuthn } from './browserSupportsWebAuthn.ts';
2+
import { getBrowserCapabilities } from './getBrowserCapabilities.ts';
3+
4+
/**
5+
* Determine if the browser is capable of facilitating use of synced passkeys for **authentication**
6+
* via local platform authenticator or via the hybrid transport. This method does NOT know if the
7+
* user actually has a passkey available to use.
8+
*/
9+
export async function browserSupportsPasskeys(): Promise<boolean> {
10+
if (!browserSupportsWebAuthn()) {
11+
return new Promise((resolve) => resolve(false));
12+
}
13+
14+
const capabilities = await getBrowserCapabilities();
15+
const {
16+
passkeyPlatformAuthenticator,
17+
userVerifyingPlatformAuthenticator,
18+
hybridTransport,
19+
} = capabilities;
20+
21+
return passkeyPlatformAuthenticator === 'supported' ||
22+
hybridTransport === 'supported' ||
23+
userVerifyingPlatformAuthenticator === 'supported';
24+
}

‎packages/browser/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './methods/startRegistration.ts';
22
export * from './methods/startAuthentication.ts';
33
export * from './helpers/browserSupportsWebAuthn.ts';
4+
export * from './helpers/browserSupportsPasskeys.ts';
45
export * from './helpers/platformAuthenticatorIsAvailable.ts';
56
export * from './helpers/browserSupportsWebAuthnAutofill.ts';
67
export * from './helpers/base64URLStringToBuffer.ts';

0 commit comments

Comments
 (0)