Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor import record to credential conversion to BaseImporter.cs (#542)
Leendert de Borste93b0575fff9
diff --git a/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razor b/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razorindex 208724c2a..e93cf3c65 100644--- a/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razor+++ b/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razor@@ -2,6 +2,7 @@@inject IJSRuntime JSRuntime@inject CredentialService CredentialService@inject DbService DbService+@using AliasVault.ImportExport.Importers@using AliasVault.ImportExport.Models<div class="flex flex-col p-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700">@@ -145,39 +146,11 @@try{- // Convert imported credentials to AliasVault format and save them- foreach (var importedCredential in ImportedCredentials)+ // Convert imported credentials to AliasVault format and insert them to the database.+ var credentials = BaseImporter.ConvertToCredential(ImportedCredentials);+ foreach (var credential in credentials){- var credential = new Credential- {- Service = new Service { Name = importedCredential.ServiceName, Url = importedCredential.ServiceUrl },- Username = importedCredential.Username,- Passwords = new List<Password> { new() { Value = importedCredential.Password } },- Notes = importedCredential.Notes,- CreatedAt = importedCredential.CreatedAt ?? DateTime.UtcNow,- UpdatedAt = importedCredential.ModifiedAt ?? DateTime.UtcNow- };-- credential.Alias = new Alias- {- CreatedAt = importedCredential.CreatedAt ?? DateTime.UtcNow,- UpdatedAt = importedCredential.ModifiedAt ?? DateTime.UtcNow,- };-- if (!string.IsNullOrEmpty(importedCredential.Email))- {- credential.Alias = new Alias { Email = importedCredential.Email };- }-- if (!string.IsNullOrEmpty(importedCredential.TwoFactorSecret))- {- credential.TotpCodes = new List<TotpCode>- {- new() { SecretKey = importedCredential.TwoFactorSecret }- };- }-- await CredentialService.InsertEntryAsync(credential, false);+ await CredentialService.InsertEntryAsync(credential, false, false);}// Save the databasediff --git a/src/AliasVault.Client/Services/CredentialService.cs b/src/AliasVault.Client/Services/CredentialService.csindex 1a073aa67..0d505a672 100644--- a/src/AliasVault.Client/Services/CredentialService.cs+++ b/src/AliasVault.Client/Services/CredentialService.cs@@ -143,13 +143,17 @@ public sealed class CredentialService(HttpClient httpClient, DbService dbService/// </summary>/// <param name="loginObject">Login object to insert.</param>/// <param name="saveToDb">Whether to commit changes to database. Defaults to true, but can be set to false if entries are added in bulk by caller.</param>+ /// <param name="extractFavicon">Whether to extract the favicon from the service URL. Defaults to true.</param>/// <returns>Guid of inserted entry.</returns>- public async Task<Guid> InsertEntryAsync(Credential loginObject, bool saveToDb = true)+ public async Task<Guid> InsertEntryAsync(Credential loginObject, bool saveToDb = true, bool extractFavicon = true){var context = await dbService.GetDbContextAsync();// Try to extract favicon from service URL- await ExtractFaviconAsync(loginObject);+ if (extractFavicon)+ {+ await ExtractFaviconAsync(loginObject);+ }// If the email starts with an @ it is most likely still the placeholder which hasn't been filled.// So we remove it.diff --git a/src/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/BaseImporter.csnew file mode 100644index 000000000..90e40400f--- /dev/null+++ b/src/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs@@ -0,0 +1,64 @@+//-----------------------------------------------------------------------+// <copyright file="BaseImporter.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.ImportExport.Importers;++using AliasClientDb;+using AliasVault.ImportExport.Models;++/// <summary>+/// Base class for all importers.+/// </summary>+public class BaseImporter+{+ /// <summary>+ /// Converts a list of imported credentials to a list of AliasVault credentials.+ /// </summary>+ /// <param name="importedCredentials">The list of imported credentials.</param>+ /// <returns>The list of AliasVault credentials.</returns>+ public static List<Credential> ConvertToCredential(List<ImportedCredential> importedCredentials)+ {+ var credentials = new List<Credential>();++ // Convert imported credentials to AliasVault relational DB format.+ foreach (var importedCredential in importedCredentials)+ {+ var credential = new Credential+ {+ Service = new Service { Name = importedCredential.ServiceName, Url = importedCredential.ServiceUrl },+ Username = importedCredential.Username,+ Passwords = new List<Password> { new() { Value = importedCredential.Password } },+ Notes = importedCredential.Notes,+ CreatedAt = importedCredential.CreatedAt ?? DateTime.UtcNow,+ UpdatedAt = importedCredential.ModifiedAt ?? DateTime.UtcNow+ };++ credential.Alias = new Alias+ {+ CreatedAt = importedCredential.CreatedAt ?? DateTime.UtcNow,+ UpdatedAt = importedCredential.ModifiedAt ?? DateTime.UtcNow,+ };++ if (!string.IsNullOrEmpty(importedCredential.Email))+ {+ credential.Alias = new Alias { Email = importedCredential.Email };+ }++ if (!string.IsNullOrEmpty(importedCredential.TwoFactorSecret))+ {+ credential.TotpCodes = new List<TotpCode>+ {+ new() { SecretKey = importedCredential.TwoFactorSecret }+ };+ }++ credentials.Add(credential);+ }++ return credentials;+ }+}\ No newline at end of filediff --git a/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.csindex 59fd67551..9d140d398 100644--- a/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs+++ b/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="CredentialCsvService.cs" company="lanedirt">+// <copyright file="KeePassImporter.cs" company="lanedirt">// Copyright (c) lanedirt. All rights reserved.// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.// </copyright>@@ -16,7 +16,7 @@ using System.Globalization;/// <summary>/// Imports credentials from KeePass./// </summary>-public static class KeePassImporter+public class KeePassImporter{/// <summary>/// Imports KeePass CSV file and converts contents to list of ImportedCredential model objects.