Skip to content

Commit

Permalink
WebAuthn: Improve error msg and add version check
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Oct 16, 2024
1 parent 9a956b3 commit 43d517d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
38 changes: 34 additions & 4 deletions Guard.Core/Security/WebAuthn/WebAuthnHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ public static class WebAuthnHelper
{
public static bool IsSupported()
{
return WebAuthnInterop.CheckApiAvailable();
bool apiAvailable = WebAuthnInterop.CheckApiAvailable();
if (!apiAvailable)
{
Log.Logger.Warning("WebAuthn API is not available on this platform.");
return false;
}

int version = GetApiVersion();
if (version < 4)
{
Log.Logger.Warning("WebAuthn API version {ApiVersion} is not supported.", version);
return false;
}

return true;
}

public static int GetApiVersion()
Expand All @@ -20,9 +34,17 @@ public static int GetApiVersion()
string keyName
)
{
Log.Logger.Information(
"Registering WebAuthn device with Win32 WebAuthn api version {ApiVersion}",
GetApiVersion()
);

if (!IsSupported())
{
return (false, "WebAuthn API is not available on this platform.");
return (
false,
"Either the WebAuthn API is not available on this platform or the version is not supported."
);
}

var challenge = EncryptionHelper.GetRandomBytes(32);
Expand Down Expand Up @@ -121,7 +143,11 @@ out var credential
{
if (!IsSupported())
{
return (false, "WebAuthn API is not available on this platform.", null);
return (
false,
"Either the WebAuthn API is not available on this platform or the version is not supported.",
null
);
}

webauthnDevices ??= Auth.GetWebAuthnDevices();
Expand Down Expand Up @@ -187,7 +213,11 @@ out var assertion
|| assertion.HmacSecret.Second == null
)
{
return (false, "HmacSecret is null", null);
return (
false,
"HmacSecret is null. This normally means that your device does not support the HMAC secret extension.",
null
);
}

if (
Expand Down
1 change: 0 additions & 1 deletion Guard.Core/Security/WebAuthn/WebAuthnInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static int GetApiVersion()
);
}
_apiVersion = WebAuthNGetApiVersionNumber();
Log.Logger.Information("WebAuthn API version: {ApiVersion}", _apiVersion);
}
return _apiVersion
?? throw new PlatformNotSupportedException("Can not get WebAuthn API version.");
Expand Down

0 comments on commit 43d517d

Please sign in to comment.