Skip to content

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

Refactor and tweak UI (#672)

Leendert de Borsta7d1536140a2
diff --git a/browser-extension/src/entrypoints/popup/components/EmailPreview.tsx b/browser-extension/src/entrypoints/popup/components/EmailPreview.tsx
index 23d2bc907..c06112c9e 100644
--- a/browser-extension/src/entrypoints/popup/components/EmailPreview.tsx
+++ b/browser-extension/src/entrypoints/popup/components/EmailPreview.tsx
@@ -135,7 +135,7 @@ export const EmailPreview: React.FC<EmailPreviewProps> = ({ email }) => {
href={`https://spamok.com/${email.split('@')[0]}/${mail.id}`}
target="_blank"
rel="noopener noreferrer"
- className={`flex justify-between items-center p-2 rounded cursor-pointer bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 ${
+ className={`flex justify-between items-center p-2 ps-3 pe-3 rounded cursor-pointer bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 ${
mail.id > lastEmailId ? 'bg-yellow-50 dark:bg-yellow-900/30' : ''
}`}
>
@@ -152,7 +152,7 @@ export const EmailPreview: React.FC<EmailPreviewProps> = ({ email }) => {
<Link
key={mail.id}
to={`/emails/${mail.id}`}
- className={`flex justify-between items-center p-2 rounded cursor-pointer bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 ${
+ className={`flex justify-between items-center p-2 ps-3 pe-3 rounded cursor-pointer bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 ${
mail.id > lastEmailId ? 'bg-yellow-50 dark:bg-yellow-900/30' : ''
}`}
>
diff --git a/browser-extension/src/entrypoints/popup/components/TotpViewer.tsx b/browser-extension/src/entrypoints/popup/components/TotpViewer.tsx
index 983563d30..868c0e032 100644
--- a/browser-extension/src/entrypoints/popup/components/TotpViewer.tsx
+++ b/browser-extension/src/entrypoints/popup/components/TotpViewer.tsx
@@ -1,4 +1,4 @@
-import React, { useState, useEffect, useRef } from 'react';
+import React, { useState, useEffect } from 'react';
import { useDb } from '../context/DbContext';
import { TotpCode } from '../../../utils/types/TotpCode';
import * as OTPAuth from 'otpauth';
@@ -16,7 +16,6 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
const [currentCodes, setCurrentCodes] = useState<Record<string, string>>({});
const [copiedId, setCopiedId] = useState<string | null>(null);
const dbContext = useDb();
- const intervalRef = useRef<NodeJS.Timeout | null>(null);
/**
* Gets the remaining seconds for the TOTP code.
@@ -58,26 +57,6 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
}
};
- /**
- * Refreshes the TOTP codes.
- */
- const refreshCodes = (): void => {
- const newCodes: Record<string, string> = {};
-
- totpCodes.forEach(code => {
- const generatedCode = generateTotpCode(code.SecretKey);
- // Only update if we have a valid code
- if (generatedCode !== 'Error') {
- newCodes[code.Id] = generatedCode;
- } else {
- // Keep the previous code if there's an error
- newCodes[code.Id] = currentCodes[code.Id] || 'Error';
- }
- });
-
- setCurrentCodes(newCodes);
- };
-
/**
* Copies a TOTP code to the clipboard.
*/
@@ -96,6 +75,9 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
};
useEffect(() => {
+ /**
+ * Loads the TOTP codes for the credential.
+ */
const loadTotpCodes = async (): Promise<void> => {
if (!dbContext?.sqliteClient) {
return;
@@ -115,6 +97,24 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
}, [credentialId, dbContext?.sqliteClient]);
useEffect(() => {
+ /**
+ * Updates the current TOTP codes.
+ */
+ const updateTotpCodes = (prevCodes: Record<string, string>): Record<string, string> => {
+ const newCodes: Record<string, string> = {};
+ totpCodes.forEach(code => {
+ const generatedCode = generateTotpCode(code.SecretKey);
+ // Only update if we have a valid code
+ if (generatedCode !== 'Error') {
+ newCodes[code.Id] = generatedCode;
+ } else {
+ // Keep the previous code if there's an error
+ newCodes[code.Id] = prevCodes[code.Id] ?? 'Error';
+ }
+ });
+ return newCodes;
+ };
+
// Generate initial codes
const initialCodes: Record<string, string> = {};
totpCodes.forEach(code => {
@@ -123,48 +123,50 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
setCurrentCodes(initialCodes);
// Set up interval to refresh codes
- const interval = setInterval(refreshCodes, 1000);
- intervalRef.current = interval;
+ const intervalId = setInterval(() => {
+ setCurrentCodes(updateTotpCodes);
+ }, 1000);
// Clean up interval on unmount or when totpCodes change
- return () => {
- clearInterval(interval);
- intervalRef.current = null;
+ return () : void => {
+ clearInterval(intervalId);
};
}, [totpCodes]);
if (loading) {
return (
<div className="text-gray-500 dark:text-gray-400 mb-4">
- <h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-2">Two-factor authentication</h2>
+ <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">Two-factor authentication</h2>
Loading TOTP codes...
</div>
);
}
if (totpCodes.length === 0) {
- return null; // Don't show anything if there are no TOTP codes
+ return null;
}
return (
<div className="mb-4">
<div className="space-y-2">
- <h2 className="text-lg font-semibold text-gray-900 dark:text-white">Two-factor authentication</h2>
+ <h2 className="text-base font-semibold text-gray-900 dark:text-white">Two-factor authentication</h2>
<div className="grid grid-cols-1 gap-2">
{totpCodes.map(totpCode => (
- <div key={totpCode.Id} className="p-2 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
+ <button
+ key={totpCode.Id}
+ className={`w-full text-left p-2 ps-3 pe-3 rounded bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700`}
+ onClick={() => copyToClipboard(currentCodes[totpCode.Id], totpCode.Id)}
+ aria-label={`Copy ${totpCode.Name} code`}
+ >
<div className="flex justify-between items-center gap-2">
<div className="flex items-center flex-1">
- <h4 className="text-base font-medium text-gray-900 dark:text-white">{totpCode.Name}</h4>
+ <h4 className="text-sm font-medium text-gray-900 dark:text-white">{totpCode.Name}</h4>
</div>
<div className="flex items-center gap-2">
<div className="flex flex-col items-end">
- <div
- className="text-xl font-bold cursor-pointer text-gray-900 dark:text-white hover:text-primary-600 dark:hover:text-primary-400 transition-colors"
- onClick={() => copyToClipboard(currentCodes[totpCode.Id], totpCode.Id)}
- >
+ <span className="text-lg font-bold text-gray-900 dark:text-white">
{currentCodes[totpCode.Id]}
- </div>
+ </span>
<div className="text-xs">
{copiedId === totpCode.Id ? (
<span className="text-green-600 dark:text-green-400">Copied!</span>
@@ -181,7 +183,7 @@ export const TotpViewer: React.FC<TotpViewerProps> = ({ credentialId }) => {
</div>
</div>
</div>
- </div>
+ </button>
))}
</div>
</div>
diff --git a/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx b/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx
index 2ca507bb3..73f2a3c8a 100644
--- a/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx
+++ b/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx
@@ -101,7 +101,7 @@ const CredentialDetails: React.FC = () => {
return (
<div className="">
- <div className="space-y-6">
+ <div className="space-y-4 mb-4">
<div className="flex items-center justify-between mb-6">
<div className="flex items-center">
<img
@@ -148,17 +148,15 @@ const CredentialDetails: React.FC = () => {
{credential.Email && (
<>
{isEmailDomainSupported(credential.Email) && (
- <div className="mt-6">
- <EmailPreview
- email={credential.Email}
- />
- </div>
+ <EmailPreview
+ email={credential.Email}
+ />
)}
</>
)}
- </div>
- <TotpViewer credentialId={credential.Id} />
+ <TotpViewer credentialId={credential.Id} />
+ </div>
<div className="grid gap-6">
<div className="space-y-4 lg:col-span-2 xl:col-span-1">
diff --git a/browser-extension/src/utils/SqliteClient.tsx b/browser-extension/src/utils/SqliteClient.tsx
index b4aa4add9..49bf56a59 100644
--- a/browser-extension/src/utils/SqliteClient.tsx
+++ b/browser-extension/src/utils/SqliteClient.tsx
@@ -434,9 +434,11 @@ class SqliteClient {
}
try {
- // Check if TotpCodes table exists (for backward compatibility).
- // TODO: whenever the browser extension has a minimum client DB version of 1.5.0+,
- // we can remove this check as the TotpCodes table then is guaranteed to exist.
+ /*
+ * Check if TotpCodes table exists (for backward compatibility).
+ * TODO: whenever the browser extension has a minimum client DB version of 1.5.0+,
+ * we can remove this check as the TotpCodes table then is guaranteed to exist.
+ */
if (!this.tableExists('TotpCodes')) {
return [];
}
@@ -463,7 +465,7 @@ class SqliteClient {
* @param tableName - The name of the table to check
* @returns True if the table exists, false otherwise
*/
- private tableExists(tableName: string): boolean {
+ private tableExists(tableName: string): boolean {
if (!this.db) {
throw new Error('Database not initialized');
}
diff --git a/browser-extension/src/utils/generators/Totp/TotpGenerator.ts b/browser-extension/src/utils/generators/Totp/TotpGenerator.ts
deleted file mode 100644
index 684771331..000000000
--- a/browser-extension/src/utils/generators/Totp/TotpGenerator.ts
+++ /dev/null
@@ -1,196 +0,0 @@
-/**
- * TOTP Generator utility for generating Time-based One-Time Passwords.
- * Based on RFC 6238 (https://tools.ietf.org/html/rfc6238).
- */
-export class TotpGenerator {
- /**
- * Generates a Time-based One-Time Password (TOTP) for the given secret key.
- * @param secretKey The secret key in Base32 encoding.
- * @param digits The number of digits in the generated code. Default is 6.
- * @param step The time step in seconds. Default is 30.
- * @returns The generated TOTP code.
- */
- public static generateTotpCode(secretKey: string, digits: number = 6, step: number = 30): string {
- // Remove any whitespace and hyphens from the secret key
- secretKey = secretKey.replace(/\s|-/g, '');
-
- // Convert the secret key from Base32 to byte array
- const keyBytes = this.base32ToBytes(secretKey);
-
- // Get the current Unix timestamp and calculate the counter value
- const counter = Math.floor(Date.now() / 1000 / step);
-
- // Generate the TOTP code
- return this.generateHOTP(keyBytes, counter, digits);
- }
-
- /**
- * Calculates the remaining seconds until the next TOTP code generation.
- * @param step The time step in seconds. Default is 30.
- * @returns The remaining seconds.
- */
- public static getRemainingSeconds(step: number = 30): number {
- const unixTimestamp = Math.floor(Date.now() / 1000);
- return step - (unixTimestamp % step);
- }
-
- /**
- * Calculates the remaining percentage until the next TOTP code generation.
- * @param step The time step in seconds. Default is 30.
- * @returns The remaining percentage (0-100).
- */
- public static getRemainingPercentage(step: number = 30): number {
- const remaining = this.getRemainingSeconds(step);
- // Invert the percentage so it counts down instead of up
- return Math.floor(((step - remaining) / step) * 100);
- }
-
- /**
- * Generates an HMAC-based One-Time Password (HOTP) for the given key and counter.
- * @param key The secret key as a byte array.
- * @param counter The counter value.
- * @param digits The number of digits in the generated code.
- * @returns The generated HOTP code.
- */
- private static generateHOTP(key: Uint8Array, counter: number, digits: number): string {
- // Convert counter to a byte array (8 bytes, big-endian)
- const counterBytes = new Uint8Array(8);
- for (let i = 7; i >= 0; i--) {
- counterBytes[i] = counter & 0xff;
- counter = counter >> 8;
- }
-
- // Calculate HMAC-SHA1
- const hmac = this.hmacSha1(key, counterBytes);
-
- // Dynamic truncation
- const offset = hmac[hmac.length - 1] & 0xf;
- const binary =
- ((hmac[offset] & 0x7f) << 24) |
- ((hmac[offset + 1] & 0xff) << 16) |
- ((hmac[offset + 2] & 0xff) << 8) |
- (hmac[offset + 3] & 0xff);
-
- // Generate the code
- const code = binary % Math.pow(10, digits);
- return code.toString().padStart(digits, '0');
- }
-
- /**
- * Converts a Base32 encoded string to a byte array.
- * @param base32 The Base32 encoded string.
- * @returns The byte array.
- */
- private static base32ToBytes(base32: string): Uint8Array {
- // Base32 character set (RFC 4648)
- const base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
-
- // Convert to uppercase and remove padding
- base32 = base32.toUpperCase().replace(/=+$/, '');
-
- // Initialize variables
- const length = base32.length;
- const output = new Uint8Array(Math.floor(length * 5 / 8));
- let bits = 0;
- let value = 0;
- let index = 0;
-
- // Process each character
- for (let i = 0; i < length; i++) {
- const charValue = base32Chars.indexOf(base32[i]);
- if (charValue === -1) {
- throw new Error('Invalid Base32 character: ' + base32[i]);
- }
-
- // Add 5 bits to the buffer
- value = (value << 5) | charValue;
- bits += 5;
-
- // If we have at least 8 bits, extract a byte
- if (bits >= 8) {
- bits -= 8;
- output[index++] = (value >> bits) & 0xff;
- }
- }
-
- return output;
- }
-
- /**
- * Calculates the HMAC-SHA1 of a message using a key.
- * @param key The key as a byte array.
- * @param message The message as a byte array.
- * @returns The HMAC-SHA1 digest as a byte array.
- */
- private static hmacSha1(key: Uint8Array, message: Uint8Array): Uint8Array {
- // Simple implementation of HMAC-SHA1
- // In a real implementation, you would use a crypto library
- // This is a placeholder that should be replaced with a proper implementation
-
- // For now, we'll use the Web Crypto API if available
- if (typeof crypto !== 'undefined' && crypto.subtle) {
- // This is async, but we need a sync implementation for this example
- // In a real implementation, you would make this method async
- console.warn('Web Crypto API is available but not used in this synchronous implementation');
- }
-
- // Fallback implementation (not secure, for demonstration only)
- // In a real implementation, use a proper crypto library
- const blockSize = 64;
-
- // Prepare the key
- let keyToUse = key;
- if (key.length > blockSize) {
- // If key is too long, hash it
- keyToUse = this.sha1(key);
- }
- if (key.length < blockSize) {
- // If key is too short, pad it with zeros
- const paddedKey = new Uint8Array(blockSize);
- paddedKey.set(keyToUse);
- keyToUse = paddedKey;
- }
-
- // Prepare the inner and outer padded keys
- const innerPadded = new Uint8Array(blockSize);
- const outerPadded = new Uint8Array(blockSize);
-
- for (let i = 0; i < blockSize; i++) {
- innerPadded[i] = keyToUse[i] ^ 0x36;
- outerPadded[i] = keyToUse[i] ^ 0x5c;
- }
-
- // Concatenate inner padded key with message
- const innerData = new Uint8Array(innerPadded.length + message.length);
- innerData.set(innerPadded);
- innerData.set(message, innerPadded.length);
-
- // Hash the inner data
- const innerHash = this.sha1(innerData);
-
- // Concatenate outer padded key with inner hash
- const outerData = new Uint8Array(outerPadded.length + innerHash.length);
- outerData.set(outerPadded);
- outerData.set(innerHash, outerPadded.length);
-
- // Hash the outer data
- return this.sha1(outerData);
- }
-
- /**
- * Calculates the SHA-1 hash of a message.
- * @param message The message as a byte array.
- * @returns The SHA-1 hash as a byte array.
- */
- private static sha1(message: Uint8Array): Uint8Array {
- // This is a placeholder for a proper SHA-1 implementation
- // In a real implementation, use a proper crypto library
-
- // For demonstration purposes, we'll return a fixed-length array
- // This is NOT a real SHA-1 implementation
- console.warn('Using placeholder SHA-1 implementation - NOT SECURE');
-
- // Return a 20-byte array (SHA-1 digest length)
- return new Uint8Array(20).fill(1);
- }
-}
\ No newline at end of file
diff --git a/src/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor b/src/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
index 469a5da82..f92c4d0e9 100644
--- a/src/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
+++ b/src/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
@@ -64,12 +64,12 @@
<div class="grid grid-cols-1 gap-4 mt-4">
@foreach (var totpCode in TotpCodeList.Where(t => !t.IsDeleted))
{
- <div class="p-4 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
- <div class="flex justify-between items-center gap-4">
+ <div class="p-2 ps-3 pe-3 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
+ <div class="flex justify-between items-center gap-2">
<div class="flex items-center flex-1">
- <h4 class="text-lg font-medium text-gray-900 dark:text-white">@totpCode.Name</h4>
+ <h4 class="text-sm font-medium text-gray-900 dark:text-white">@totpCode.Name</h4>
</div>
- <div class="flex items-center gap-4">
+ <div class="flex items-center gap-2">
<div class="flex flex-col items-end">
<div class="text-sm text-gray-500 dark:text-gray-400">Save to view code</div>
</div>
diff --git a/src/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor b/src/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
index eff7fd6dc..343396ade 100644
--- a/src/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
+++ b/src/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
@@ -26,14 +26,14 @@
<div class="grid grid-cols-1 gap-4 mt-4">
@foreach (var totpCode in TotpCodeList)
{
- <div class="p-4 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
- <div class="flex justify-between items-center gap-4">
+ <div class="p-2 ps-3 pe-3 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
+ <div class="flex justify-between items-center gap-2">
<div class="flex items-center flex-1">
- <h4 class="text-lg font-medium text-gray-900 dark:text-white">@totpCode.Name</h4>
+ <h4 class="text-sm font-medium text-gray-900 dark:text-white">@totpCode.Name</h4>
</div>
- <div class="flex items-center gap-4">
+ <div class="flex items-center gap-2">
<div class="flex flex-col items-end">
- <div class="text-2xl font-bold cursor-pointer text-gray-900 dark:text-white hover:text-primary-600 dark:hover:text-primary-400 transition-colors" @onclick="() => CopyToClipboard(totpCode)">
+ <div class="totp-code text-lg font-bold cursor-pointer text-gray-900 dark:text-white hover:text-primary-600 dark:hover:text-primary-400 transition-colors" @onclick="() => CopyToClipboard(totpCode)">
@GetTotpCode(totpCode.SecretKey)
</div>
<div class="text-xs">
diff --git a/src/AliasVault.Client/wwwroot/css/tailwind.css b/src/AliasVault.Client/wwwroot/css/tailwind.css
index 667b35006..26105b659 100644
--- a/src/AliasVault.Client/wwwroot/css/tailwind.css
+++ b/src/AliasVault.Client/wwwroot/css/tailwind.css
@@ -1172,6 +1172,10 @@ video {
justify-content: space-between;
}
+.gap-2 {
+ gap: 0.5rem;
+}
+
.gap-3 {
gap: 0.75rem;
}
@@ -1751,6 +1755,22 @@ video {
padding-top: 2rem;
}
+.pe-4 {
+ padding-inline-end: 1rem;
+}
+
+.ps-4 {
+ padding-inline-start: 1rem;
+}
+
+.pe-3 {
+ padding-inline-end: 0.75rem;
+}
+
+.ps-3 {
+ padding-inline-start: 0.75rem;
+}
+
.text-left {
text-align: left;
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
index 0aa088b87..4d32beee6 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
@@ -61,7 +61,7 @@ public class TotpTests : ClientPlaywrightTest
Assert.That(pageContent, Does.Contain("Test TOTP"), "TOTP code name does not appear on the page");
// Verify that a 6-digit code is generated and displayed
- var codeElement = Page.Locator(".text-2xl.font-bold");
+ var codeElement = Page.Locator(".totp-code");
var code = await codeElement.TextContentAsync();
Assert.That(code, Does.Match(@"^\d{6}$"), "Generated TOTP code is not a 6-digit number");
}