Skip to content

Commit

Permalink
Ensure the browser supports passkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
itaihanski committed Nov 13, 2024
1 parent 80c4544 commit 4b9d20d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/src/internal/others/web_passkeys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const _webauthnScript = """
// webauthn create
async function descopeWebAuthnCreate(options) {
if (!descopeIsWebAuthnSupported()) throw Error('Passkeys are not supported');
const createOptions = descopeDecodeCreateOptions(options);
const createResponse = await window.navigator.credentials.create(createOptions);
return descopeEncodeCreateResponse(createResponse);
Expand Down Expand Up @@ -75,6 +76,7 @@ function descopeEncodeCreateResponse(credential) {
// webauthn get
async function descopeWebAuthnGet(options) {
if (!descopeIsWebAuthnSupported()) throw Error('Passkeys are not supported');
const getOptions = descopeDecodeGetOptions(options);
const getResponse = await navigator.credentials.get(getOptions);
return descopeEncodeGetResponse(getResponse);
Expand Down Expand Up @@ -116,4 +118,16 @@ function descopeEncodeBase64Url(value) {
const base64 = btoa(String.fromCharCode.apply(null, new Uint8Array(value)));
return base64.replace(/\\//g, '_').replace(/\\+/g, '-').replace(/=/g, '');
}
// Is supported
function descopeIsWebAuthnSupported() {
const supported = !!(
window.PublicKeyCredential &&
navigator.credentials &&
navigator.credentials.create &&
navigator.credentials.get
);
return supported;
}
""";

0 comments on commit 4b9d20d

Please sign in to comment.