Skip to content

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

Add vault unlock flow and credential print (#541)

Leendert de Borst7ea60a1fa649
diff --git a/browser-extensions/chrome/src/components/App.tsx b/browser-extensions/chrome/src/components/App.tsx
index 2e68345de..2dfb27fa1 100644
--- a/browser-extensions/chrome/src/components/App.tsx
+++ b/browser-extensions/chrome/src/components/App.tsx
@@ -1,30 +1,13 @@
import React from 'react';
import '../styles/tailwind.css';
-import Button from './Button';
-import Login from './Login';
-import { useAuth } from '../context/AuthContext';
+import { DbProvider } from '../context/DbContext';
+import AppContent from './AppContent';
const App: React.FC = () => {
- const { isLoggedIn, username, logout } = useAuth();
-
- const handleLogout = () => {
- logout();
- };
-
return (
- <div className="min-h-screen bg-blue-500 items-center justify-center p-4">
- <h1 className="text-white text-2xl mb-8">AliasVault</h1>
- {isLoggedIn ? (
- <div className="mt-4">
- <p className="text-white text-lg mb-4">Logged in as {username}</p>
- <Button onClick={handleLogout}>
- Logout
- </Button>
- </div>
- ) : (
- <Login />
- )}
- </div>
+ <DbProvider>
+ <AppContent />
+ </DbProvider>
);
};
diff --git a/browser-extensions/chrome/src/components/AppContent.tsx b/browser-extensions/chrome/src/components/AppContent.tsx
new file mode 100644
index 000000000..b38657d6c
--- /dev/null
+++ b/browser-extensions/chrome/src/components/AppContent.tsx
@@ -0,0 +1,99 @@
+import React, { useState, useEffect } from 'react';
+import Button from './Button';
+import { useAuth } from '../context/AuthContext';
+import { useDb } from '../context/DbContext';
+import Unlock from './Unlock';
+import Login from './Login';
+
+interface Credential {
+ Id: string;
+ ServiceName: string;
+ Username: string;
+ Logo?: string; // base64 encoded image data
+}
+
+const AppContent: React.FC = () => {
+ const { isLoggedIn, username, logout } = useAuth();
+ const dbContext = useDb();
+ const [credentials, setCredentials] = useState<Credential[]>([]);
+ const [needsUnlock, setNeedsUnlock] = useState(true);
+
+ useEffect(() => {
+ if (isLoggedIn && dbContext?.isInitialized) {
+ setNeedsUnlock(false);
+ loadCredentials();
+ }
+ }, [isLoggedIn, dbContext?.isInitialized]);
+
+ const loadCredentials = () => {
+ if (!dbContext?.sqliteClient) return;
+
+ try {
+ const results = dbContext.sqliteClient.executeQuery<Credential>(
+ `SELECT
+ c.Id,
+ c.Username as Username,
+ s.Name as ServiceName,
+ s.Logo as Logo
+ FROM Credentials c
+ JOIN Services s ON s.Id = c.ServiceId
+ WHERE c.IsDeleted = 0`
+ );
+ setCredentials(results);
+
+ console.log(results);
+ } catch (err) {
+ console.error('Error loading credentials:', err);
+ }
+ };
+
+ const handleLogout = () => {
+ setCredentials([]);
+ setNeedsUnlock(true);
+ logout();
+ };
+
+ return (
+ <div className="min-h-screen bg-blue-500 items-center justify-center p-4">
+ <h1 className="text-white text-2xl mb-8">AliasVault</h1>
+ {isLoggedIn ? (
+ <div className="mt-4">
+ <p className="text-white text-lg mb-4">Logged in as {username}</p>
+ {needsUnlock ? (
+ <Unlock />
+ ) : (
+ <div>
+ <div className="bg-white rounded-lg p-4 mb-4">
+ <h2 className="text-xl mb-4">Your Credentials</h2>
+ {credentials.length === 0 ? (
+ <p className="text-gray-500">No credentials found</p>
+ ) : (
+ <ul className="space-y-2">
+ {credentials.map(cred => (
+ <li key={cred.Id} className="p-2 border rounded flex items-center">
+ <img
+ src={cred.Logo ? `data:image/png;base64,${cred.Logo}` : '/images/service-placeholder.webp'}
+ alt={cred.ServiceName}
+ className="w-8 h-8 mr-2 flex-shrink-0"
+ />
+ <div>
+ <p className="font-medium">{cred.ServiceName}</p>
+ <p className="text-sm text-gray-600">{cred.Username}</p>
+ </div>
+ </li>
+ ))}
+ </ul>
+ )}
+ </div>
+ <Button onClick={handleLogout}>Logout</Button>
+ </div>
+ )}
+ </div>
+ ) : (
+ <Login />
+ )}
+ </div>
+ );
+};
+
+export default AppContent;
\ No newline at end of file
diff --git a/browser-extensions/chrome/src/components/Login.tsx b/browser-extensions/chrome/src/components/Login.tsx
index 4bdb129ca..12b53313d 100644
--- a/browser-extensions/chrome/src/components/Login.tsx
+++ b/browser-extensions/chrome/src/components/Login.tsx
@@ -5,6 +5,7 @@ import { srpUtility } from '../utilities/SrpUtility';
import EncryptionUtility from '../utilities/EncryptionUtility';
import SqliteClient from '../utilities/SqliteClient';
import { useAuth } from '../context/AuthContext';
+import { useDb } from '../context/DbContext';
const Login: React.FC = () => {
const { login } = useAuth();
@@ -14,6 +15,7 @@ const Login: React.FC = () => {
});
const [rememberMe, setRememberMe] = useState(false);
const [error, setError] = useState<string | null>(null);
+ const dbContext = useDb();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
@@ -47,7 +49,8 @@ const Login: React.FC = () => {
// Store access and refresh token using the context
if (validationResponse.token) {
- login(credentials.username,validationResponse.token.token, validationResponse.token.refreshToken);
+ // Store auth info
+ login(credentials.username, validationResponse.token.token, validationResponse.token.refreshToken);
} else {
throw new Error('Login failed -- no token returned');
}
@@ -73,17 +76,8 @@ const Login: React.FC = () => {
console.log('Decrypted blob:');
console.log(decryptedBlob);
- // Inside your handleSubmit function, after decrypting the blob:
- const sqliteClient = new SqliteClient();
- await sqliteClient.initializeFromBase64(decryptedBlob);
-
- // Example query to test the connection
- const vaultCredentials = sqliteClient.executeQuery('SELECT * FROM Credentials WHERE IsDeleted = 0');
- console.log('Credentials:', vaultCredentials);
-
- // Don't forget to close the connection when done
- sqliteClient.close();
-
+ // Initialize the SQLite context with decrypted data
+ await dbContext.initializeDatabase(decryptedBlob);
// 3. Handle 2FA if required
/*if (validationResponse.requiresTwoFactor) {
diff --git a/browser-extensions/chrome/src/components/Unlock.tsx b/browser-extensions/chrome/src/components/Unlock.tsx
new file mode 100644
index 000000000..7d7116f2c
--- /dev/null
+++ b/browser-extensions/chrome/src/components/Unlock.tsx
@@ -0,0 +1,106 @@
+import React, { useState } from 'react';
+import Button from './Button';
+import { Buffer } from 'buffer';
+import EncryptionUtility from '../utilities/EncryptionUtility';
+import { useDb } from '../context/DbContext';
+import { useAuth } from '../context/AuthContext';
+import { srpUtility } from '../utilities/SrpUtility';
+
+const Unlock: React.FC = () => {
+ const { username, logout } = useAuth();
+
+ const [password, setPassword] = useState('');
+ const [error, setError] = useState<string | null>(null);
+ const dbContext = useDb();
+
+ const handleSubmit = async (e: React.FormEvent) => {
+ e.preventDefault();
+ setError(null);
+
+ try {
+ // 1. Initiate login to get salt and server ephemeral
+ // TODO: adding encryption settings to vault response would save a call to the server.
+ // Check how WASM client does this? Should we store the settings in localstorage instead?
+ const loginResponse = await srpUtility.initiateLogin(username!);
+
+ // Derive key from password using user's encryption settings
+ const passwordHash = await EncryptionUtility.deriveKeyFromPassword(
+ password,
+ loginResponse.salt,
+ loginResponse.encryptionType,
+ loginResponse.encryptionSettings
+ );
+
+ // Make API call to get latest vault
+ const vaultResponse = await fetch('https://localhost:7223/v1/Vault', {
+ headers: {
+ 'Authorization': `Bearer ${localStorage.getItem('accessToken')}`
+ }
+ });
+ const vaultResponseJson = await vaultResponse.json();
+
+ // Attempt to decrypt the blob
+ const passwordHashBase64 = Buffer.from(passwordHash).toString('base64');
+ const decryptedBlob = await EncryptionUtility.symmetricDecrypt(
+ vaultResponseJson.vault.blob,
+ passwordHashBase64
+ );
+
+ // Initialize the SQLite context with decrypted data
+ await dbContext.initializeDatabase(decryptedBlob);
+ } catch (err) {
+ setError('Failed to unlock vault. Please check your password and try again.');
+ console.error('Unlock error:', err);
+ }
+ };
+
+ const handleLogout = async () => {
+ logout();
+ };
+
+ return (
+ <div className="max-w-md">
+ <form onSubmit={handleSubmit} className="bg-white w-full shadow-md rounded px-8 pt-6 pb-8 mb-4">
+ <div className="flex space-x-4 mb-6">
+ <img className="w-8 h-8 rounded-full" src="/images/avatar.webp" alt="User avatar" />
+ <h2 className="text-2xl font-bold text-gray-900">{username}</h2>
+ </div>
+
+ <p className="text-base text-gray-500 mb-6">
+ Enter your master password to unlock your database.
+ </p>
+
+ {error && (
+ <div className="mb-4 text-red-500 text-sm">
+ {error}
+ </div>
+ )}
+
+ <div className="mb-6">
+ <label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="password">
+ Password
+ </label>
+ <input
+ className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline"
+ id="password"
+ type="password"
+ value={password}
+ onChange={(e) => setPassword(e.target.value)}
+ placeholder="Enter your password"
+ required
+ />
+ </div>
+
+ <Button type="submit">
+ Unlock
+ </Button>
+
+ <div className="text-sm font-medium text-gray-500 mt-6">
+ Switch accounts? <a href="#" onClick={handleLogout} className="text-primary-700 hover:underline">Log out</a>
+ </div>
+ </form>
+ </div>
+ );
+};
+
+export default Unlock;
\ No newline at end of file
diff --git a/browser-extensions/chrome/src/context/DbContext.tsx b/browser-extensions/chrome/src/context/DbContext.tsx
new file mode 100644
index 000000000..911ad3412
--- /dev/null
+++ b/browser-extensions/chrome/src/context/DbContext.tsx
@@ -0,0 +1,56 @@
+import React, { createContext, useContext, useState } from 'react';
+import SqliteClient from '../utilities/SqliteClient';
+
+interface DbContextType {
+ sqliteClient: SqliteClient | null;
+ initializeDatabase: (base64Data: string) => Promise<void>;
+ clearDatabase: () => void;
+ isInitialized: boolean;
+}
+
+const DbContext = createContext<DbContextType | undefined>(undefined);
+
+export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
+ const [sqliteClient, setSqliteClient] = useState<SqliteClient | null>(null);
+ const [isInitialized, setIsInitialized] = useState(false);
+
+ const initializeDatabase = async (base64Data: string) => {
+ try {
+ const client = new SqliteClient();
+ await client.initializeFromBase64(base64Data);
+ setSqliteClient(client);
+ setIsInitialized(true);
+ } catch (error) {
+ console.error('Failed to initialize SQLite database:', error);
+ clearDatabase();
+ throw error;
+ }
+ };
+
+ const clearDatabase = () => {
+ if (sqliteClient) {
+ sqliteClient.close();
+ }
+ setSqliteClient(null);
+ setIsInitialized(false);
+ };
+
+ return (
+ <DbContext.Provider value={{
+ sqliteClient,
+ initializeDatabase,
+ clearDatabase,
+ isInitialized
+ }}>
+ {children}
+ </DbContext.Provider>
+ );
+};
+
+export const useDb = () => {
+ const context = useContext(DbContext);
+ if (context === undefined) {
+ throw new Error('useDb must be used within a DbProvider');
+ }
+ return context;
+};
\ No newline at end of file
diff --git a/browser-extensions/chrome/src/images/avatar.webp b/browser-extensions/chrome/src/images/avatar.webp
new file mode 100644
index 000000000..d52d725f1
Binary files /dev/null and b/browser-extensions/chrome/src/images/avatar.webp differ
diff --git a/browser-extensions/chrome/src/images/service-placeholder.webp b/browser-extensions/chrome/src/images/service-placeholder.webp
new file mode 100644
index 000000000..85460a537
Binary files /dev/null and b/browser-extensions/chrome/src/images/service-placeholder.webp differ