Skip to content

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

Add favicon extraction to mobile app (#771)

Leendert de Borst1051ee653ec2
diff --git a/mobile-app/app/(tabs)/credentials/add-edit.tsx b/mobile-app/app/(tabs)/credentials/add-edit.tsx
index 68c157bba..bf7007f7c 100644
--- a/mobile-app/app/(tabs)/credentials/add-edit.tsx
+++ b/mobile-app/app/(tabs)/credentials/add-edit.tsx
@@ -14,8 +14,9 @@ import Toast from 'react-native-toast-message';
import { Gender } from "@/utils/generators/Identity/types/Gender";
import emitter from '@/utils/EventEmitter';
import NativeVaultManager from '@/specs/NativeVaultManager';
-import AsyncStorage from '@react-native-async-storage/async-storage';
import { useAuth } from '@/context/AuthContext';
+import { FaviconExtractModel } from '@/utils/types/webapi/FaviconExtractModel';
+import * as FileSystem from 'expo-file-system';
type CredentialMode = 'random' | 'manual';
interface VaultPostResponse {
@@ -37,8 +38,8 @@ export default function AddEditCredentialScreen() {
Id: "",
Username: "",
Password: "",
- ServiceName: "",
- ServiceUrl: "",
+ ServiceName: "test",
+ ServiceUrl: "https://google.com",
Notes: "",
Alias: {
FirstName: "",
@@ -259,6 +260,30 @@ export default function AddEditCredentialScreen() {
// Update existing credential
await dbContext.sqliteClient!.updateCredentialById(credentialToSave);
} else {
+ // For new credentials, try to extract favicon
+ if (credentialToSave.ServiceUrl) {
+ try {
+ // Set a timeout of 5 seconds for favicon extraction
+ const timeoutPromise = new Promise((_, reject) =>
+ setTimeout(() => reject(new Error('Favicon extraction timed out')), 5000)
+ );
+
+ const faviconPromise = webApi.get<FaviconExtractModel>('Favicon/Extract?url=' + credentialToSave.ServiceUrl);
+ const faviconResponse = await Promise.race([faviconPromise, timeoutPromise]) as FaviconExtractModel;
+ if (faviconResponse?.image) {
+ // The WebApi returns a base64 encoded string for the favicon image.
+ // We need to decode it to a Uint8Array before storing it in the service logo.
+ const decodedImage = Uint8Array.from(Buffer.from(faviconResponse.image as string, 'base64'));
+
+ // Store the favicon in the service logo.
+ credentialToSave.Logo = decodedImage;
+ }
+ } catch (error) {
+ console.log('Favicon extraction failed or timed out:', error);
+ // Continue without favicon
+ }
+ }
+
// Create new credential
await dbContext.sqliteClient!.createCredential(credentialToSave);
}
@@ -275,6 +300,8 @@ export default function AddEditCredentialScreen() {
setSyncStatus('Uploading vault to server...');
// Get email addresses from credentials
+ // TODO: this gets all email addresses, seemingly even non-aliasvault ones.
+ // Should we filter out non-aliasvault ones?
const credentials = await dbContext.sqliteClient!.getAllCredentials();
const emailAddresses = credentials
.filter(cred => cred.Alias?.Email != null)
diff --git a/mobile-app/ios/VaultStoreKit/VaultStoreKit.swift b/mobile-app/ios/VaultStoreKit/VaultStoreKit.swift
index 18dac5015..81be47a08 100644
--- a/mobile-app/ios/VaultStoreKit/VaultStoreKit.swift
+++ b/mobile-app/ios/VaultStoreKit/VaultStoreKit.swift
@@ -278,7 +278,7 @@ public class VaultStore {
vaultRevisionNumber: revisionNumber
)
}
-
+
metadata.vaultRevisionNumber = revisionNumber
if let data = try? JSONEncoder().encode(metadata),
let jsonString = String(data: data, encoding: .utf8) {
@@ -651,11 +651,29 @@ public class VaultStore {
// MARK: - Query Execution
+ /**
+ * Execute a SELECT query.
+ */
public func executeQuery(_ query: String, params: [Binding?]) throws -> [[String: Any]] {
guard let db = db else {
throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])
}
+ // Loop through all params and convert any base64 strings to SQLite.Blob.
+ // Do this by checking for a statically determined prefix "av-base64:"
+ // and then decoding the base64 string so we're inserting the original binary data.
+ var params = params // Make params mutable
+ for (index, param) in params.enumerated() {
+ if let base64String = param as? String {
+ if base64String.hasPrefix("av-base64-to-blob:") {
+ let base64 = String(base64String.dropFirst("av-base64-to-blob:".count))
+ if let data = Data(base64Encoded: base64) {
+ params[index] = Blob(bytes: [UInt8](data))
+ }
+ }
+ }
+ }
+
let statement = try db.prepare(query)
var results: [[String: Any]] = []
@@ -689,6 +707,34 @@ public class VaultStore {
return results
}
+ /**
+ * Execute an update (INSERT, DELETE) query.
+ */
+ public func executeUpdate(_ query: String, params: [Binding?]) throws -> Int {
+ guard let db = db else {
+ throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])
+ }
+
+ // Loop through all params and convert any base64 strings to SQLite.Blob.
+ // Do this by checking for a statically determined prefix "av-base64:"
+ // and then decoding the base64 string so we're inserting the original binary data.
+ var params = params // Make params mutable
+ for (index, param) in params.enumerated() {
+ if let base64String = param as? String {
+ if base64String.hasPrefix("av-base64-to-blob:") {
+ let base64 = String(base64String.dropFirst("av-base64-to-blob:".count))
+ if let data = Data(base64Encoded: base64) {
+ params[index] = Blob(bytes: [UInt8](data))
+ }
+ }
+ }
+ }
+
+ let statement = try db.prepare(query)
+ try statement.run(params)
+ return db.changes
+ }
+
public func beginTransaction() throws {
guard let db = db else {
throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])
@@ -750,16 +796,6 @@ public class VaultStore {
try db.execute("ROLLBACK")
}
- public func executeUpdate(_ query: String, params: [Binding?]) throws -> Int {
- guard let db = db else {
- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])
- }
-
- let statement = try db.prepare(query)
- try statement.run(params)
- return db.changes
- }
-
// MARK: - Auto Lock Timeout Management
public func setAutoLockTimeout(_ timeout: Int) {
print("Setting auto-lock timeout to \(timeout) seconds")
diff --git a/mobile-app/utils/SqliteClient.tsx b/mobile-app/utils/SqliteClient.tsx
index f8aa28960..a0af552be 100644
--- a/mobile-app/utils/SqliteClient.tsx
+++ b/mobile-app/utils/SqliteClient.tsx
@@ -66,7 +66,18 @@ class SqliteClient {
*/
public async executeQuery<T>(query: string, params: SQLiteBindValue[] = []): Promise<T[]> {
try {
- const results = await NativeVaultManager.executeQuery(query, params);
+ // Convert any Uint8Array parameters to base64 strings as the Native wrapper
+ // communication requires everything to be a string.
+ const convertedParams = params.map(param => {
+ if (param instanceof Uint8Array) {
+ // We prefix the base64 string with "av-base64:" to indicate that it is a base64 encoded Uint8Array.
+ // So the receiving end knows that it should convert this value back to a Uint8Array before using it in the query.
+ return 'av-base64-to-blob:' + Buffer.from(param).toString('base64');
+ }
+ return param;
+ });
+
+ const results = await NativeVaultManager.executeQuery(query, convertedParams);
return results as T[];
} catch (error) {
console.error('Error executing query:', error);
@@ -79,7 +90,18 @@ class SqliteClient {
*/
public async executeUpdate(query: string, params: SQLiteBindValue[] = []): Promise<number> {
try {
- const result = await NativeVaultManager.executeUpdate(query, params);
+ // Convert any Uint8Array parameters to base64 strings as the Native wrapper
+ // communication requires everything to be a string.
+ const convertedParams = params.map(param => {
+ if (param instanceof Uint8Array) {
+ // We prefix the base64 string with "av-base64-to-blob:" to indicate that it is a base64 encoded Uint8Array.
+ // So the receiving end knows that it should convert this value back to a Uint8Array before using it in the query.
+ return 'av-base64-to-blob:' + Buffer.from(param).toString('base64');
+ }
+ return param;
+ });
+
+ const result = await NativeVaultManager.executeUpdate(query, convertedParams);
return result as number;
} catch (error) {
console.error('Error executing update:', error);
diff --git a/mobile-app/utils/types/webapi/FaviconExtractModel.ts b/mobile-app/utils/types/webapi/FaviconExtractModel.ts
new file mode 100644
index 000000000..acb24911d
--- /dev/null
+++ b/mobile-app/utils/types/webapi/FaviconExtractModel.ts
@@ -0,0 +1,3 @@
+export type FaviconExtractModel = {
+ image: string | null;
+}