Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Code style refactor (#43)

Leendert de Borst05e9285752a3
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index cffdcdb68..242d8d61f 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -35,6 +35,11 @@ using Microsoft.IdentityModel.Tokens;
[ApiVersion("1")]
public class AuthController(AliasDbContext context, UserManager<AliasVaultUser> userManager, SignInManager<AliasVaultUser> signInManager, IConfiguration configuration, IMemoryCache cache) : ControllerBase
{
+ /// <summary>
+ /// Error message for invalid email or password.
+ /// </summary>
+ public static readonly string[] InvalidEmailOrPasswordError = { "Invalid email or password. Please try again." };
+
/// <summary>
/// Login endpoint used to process login attempt using credentials.
/// </summary>
@@ -46,7 +51,7 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
var user = await userManager.FindByEmailAsync(model.Email);
if (user == null)
{
- return BadRequest(ServerValidationErrorResponse.Create(new[] { "Invalid email or password. Please try again." }, 400));
+ return BadRequest(ServerValidationErrorResponse.Create(InvalidEmailOrPasswordError, 400));
}
// Server creates ephemeral and sends to client
@@ -69,12 +74,12 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
var user = await userManager.FindByEmailAsync(model.Email);
if (user == null)
{
- return BadRequest(ServerValidationErrorResponse.Create(new[] { "Invalid email or password. Please try again." }, 400));
+ return BadRequest(ServerValidationErrorResponse.Create(InvalidEmailOrPasswordError, 400));
}
if (!cache.TryGetValue(model.Email, out var serverSecretEphemeral) || !(serverSecretEphemeral is string))
{
- return BadRequest(ServerValidationErrorResponse.Create(new[] { "Invalid email or password. Please try again." }, 400));
+ return BadRequest(ServerValidationErrorResponse.Create(InvalidEmailOrPasswordError, 400));
}
try
@@ -95,7 +100,7 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
}
catch
{
- return BadRequest(ServerValidationErrorResponse.Create(new[] { "Invalid email or password. Please try again." }, 400));
+ return BadRequest(ServerValidationErrorResponse.Create(InvalidEmailOrPasswordError, 400));
}
}
@@ -228,6 +233,22 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
return rawIdentifier;
}
+ /// <summary>
+ /// Get the JWT key from the environment variables.
+ /// </summary>
+ /// <returns>JWT key as string.</returns>
+ /// <exception cref="KeyNotFoundException">Thrown if environment variable does not exist.</exception>
+ private static string GetJwtKey()
+ {
+ var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
+ if (jwtKey is null)
+ {
+ throw new KeyNotFoundException("JWT_KEY environment variable is not set.");
+ }
+
+ return jwtKey;
+ }
+
/// <summary>
/// Generate a Jwt access token for a user. This token is used to authenticate the user for a limited time
/// and is short-lived by design. With the separate refresh token, the user can request a new access token
@@ -239,7 +260,7 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
{
var claims = new List<Claim>
{
- new(ClaimTypes.NameIdentifier, user.Id ?? string.Empty),
+ new(ClaimTypes.NameIdentifier, user.Id),
new(ClaimTypes.Name, user.UserName ?? string.Empty),
new(ClaimTypes.Email, user.Email ?? string.Empty),
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
@@ -325,20 +346,4 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
return new TokenModel() { Token = token, RefreshToken = refreshToken };
}
-
- /// <summary>
- /// Get the JWT key from the environment variables.
- /// </summary>
- /// <returns>JWT key as string.</returns>
- /// <exception cref="KeyNotFoundException">Thrown if environment variable does not exist.</exception>
- private string GetJwtKey()
- {
- var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
- if (jwtKey is null)
- {
- throw new KeyNotFoundException("JWT_KEY environment variable is not set.");
- }
-
- return jwtKey;
- }
}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs b/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs
index 889f2702f..bbb1e2780 100644
--- a/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/RegisterModel.cs
@@ -29,7 +29,7 @@ public class RegisterModel
/// <summary>
/// Gets or sets the email.
/// </summary>
- public string Email { get; set; } = null!;
+ public string Email { get; set; }
/// <summary>
/// Gets or sets the salt.
diff --git a/src/Utilities/Cryptography/Srp.cs b/src/Utilities/Cryptography/Srp.cs
index e6d8b3962..109727049 100644
--- a/src/Utilities/Cryptography/Srp.cs
+++ b/src/Utilities/Cryptography/Srp.cs
@@ -7,7 +7,6 @@
namespace Cryptography;
-using System.Security.Cryptography;
using Cryptography.Models;
using SecureRemotePassword;
@@ -15,7 +14,7 @@ using SecureRemotePassword;
/// SRP is a secure remote password protocol that allows a user to authenticate themselves
/// to a server without sending the password over the network.
/// </summary>
-public class Srp
+public static class Srp
{
/// <summary>
/// Prepare signup step.