Skip to content

Commit

Permalink
Refactor (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Oct 4, 2024
1 parent daf25fc commit 229ec05
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{
var webauthCredentialDerivedKey = await JsInteropService.CreateWebAuthnCredentialDerivedKey(await GetUsernameAsync());
await AuthService.SetWebAuthnEnabledAsync(true, webauthCredentialDerivedKey.CredentialId, webauthCredentialDerivedKey.Salt, webauthCredentialDerivedKey.DerivedKey);
GlobalNotificationService.AddSuccessMessage("WebAuthn is successfully enabled. The next time your vault is locked you can unlock it with your created passkey.", true);
GlobalNotificationService.AddSuccessMessage("Quick Vault Unlock is successfully enabled. The next time your vault is locked you can unlock it with your created passkey.", true);
}
catch (NotSupportedException)
{
Expand All @@ -72,7 +72,7 @@
}
catch (Exception ex)
{
GlobalNotificationService.AddErrorMessage("An error occurred while trying to enable WebAuthn. Please try again (later).", true);
GlobalNotificationService.AddErrorMessage("An error occurred while trying to enable Quick Vault Unlock. Please try again (later).", true);
Logger.LogInformation(ex, "An error occurred while trying to enable WebAuthn.");
return;
}
Expand All @@ -86,6 +86,7 @@
public async Task DisableWebAuthn()
{
await AuthService.SetWebAuthnEnabledAsync(false, string.Empty, string.Empty, string.Empty);
GlobalNotificationService.AddSuccessMessage("Quick Vault Unlock is successfully disabled.", true);
await LoadData();
}
}
2 changes: 1 addition & 1 deletion src/AliasVault.Client/Services/Auth/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task<byte[]> GetDecryptedWebAuthnEncryptionKeyAsync(string username
var webauthnSalt = await localStorage.GetItemAsStringAsync("webAuthnSalt");
if (string.IsNullOrEmpty(encryptedEncryptionKey) || string.IsNullOrEmpty(webauthnCredentialId) || string.IsNullOrEmpty(webauthnSalt))
{
throw new NullReferenceException("WebAuthn encrypted encryption key is not set or WebAuthn credential ID is not set.");
throw new InvalidOperationException("WebAuthn encrypted encryption key is not set or WebAuthn credential ID is not set.");
}

var webauthnCredentialDerivedKey = await jsInteropService.GetWebAuthnCredentialDerivedKey(webauthnCredentialId, webauthnSalt);
Expand Down
36 changes: 18 additions & 18 deletions src/AliasVault.Client/Services/JsInteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,52 +236,52 @@ public async Task<string> GetWebAuthnCredentialDerivedKey(string credentialId, s
/// <summary>
/// Represents the result of a WebAuthn credential operation.
/// </summary>
private class WebAuthnCreateCredentialResult
private sealed class WebAuthnCreateCredentialResult
{
/// <summary>
/// Gets or sets the credential ID as a base64 string.
/// Gets the credential ID as a base64 string.
/// </summary>
public string? CredentialId { get; set; }
public string? CredentialId { get; }

/// <summary>
/// Gets or sets the salt as a base64 string.
/// Gets the salt as a base64 string.
/// </summary>
public string? Salt { get; set; }
public string? Salt { get; }

/// <summary>
/// Gets or sets the derived key as a base64 string.
/// Gets the derived key as a base64 string.
/// </summary>
public string? DerivedKey { get; set; }
public string? DerivedKey { get; }

/// <summary>
/// Gets or sets the error message.
/// Gets the optional error message.
/// </summary>
public string? Error { get; set; }
public string? Error { get; }

/// <summary>
/// Gets or sets additional error details.
/// Gets the optional additional error details.
/// </summary>
public string? Message { get; set; }
public string? Message { get; }
}

/// <summary>
/// Represents the result of a WebAuthn get credential operation.
/// </summary>
private class WebAuthnGetCredentialResult
private sealed class WebAuthnGetCredentialResult
{
/// <summary>
/// Gets or sets the derived key.
/// Gets the derived key.
/// </summary>
public string? DerivedKey { get; set; }
public string? DerivedKey { get; }

/// <summary>
/// Gets or sets the error message.
/// Gets the optional error message.
/// </summary>
public string? Error { get; set; }
public string? Error { get; }

/// <summary>
/// Gets or sets additional error details.
/// Gets the optional additional error details.
/// </summary>
public string? Message { get; set; }
public string? Message { get; }
}
}
8 changes: 4 additions & 4 deletions src/AliasVault.Client/wwwroot/js/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ async function getWebAuthnCredentialAndDeriveKey(credentialIdToUse, salt) {
});

const extensionsResult = existingCredential.getClientExtensionResults();
if (!extensionsResult.prf) {
if (!extensionsResult?.prf) {
return { error: "PRF_NOT_SUPPORTED" };
}

if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {
if (!extensionsResult.prf?.results?.first) {
return { error: "PRF_DERIVATION_FAILED" };
}

Expand Down Expand Up @@ -260,7 +260,7 @@ async function createWebAuthnCredentialAndDeriveKey(username) {
return { error: "PRF_NOT_SUPPORTED" };
}

if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {
if (!extensionsResult.prf?.results?.first) {
alert("Your authenticator has been successfully registered. Please use your authenticator again to complete the process.")

// Note: Some authenticators do not return the derived key in the create response. In this case,
Expand Down Expand Up @@ -288,7 +288,7 @@ async function createWebAuthnCredentialAndDeriveKey(username) {
extensionsResult = existingCredential.getClientExtensionResults();
}

if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {
if (!extensionsResult.prf?.results?.first) {
return { error: "PRF_DERIVATION_FAILED" };
}

Expand Down

0 comments on commit 229ec05

Please sign in to comment.