From e93b0575fff91ad67f6a02f41f9f9908937302fb Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Mon, 24 Mar 2025 16:22:35 +0100 Subject: [PATCH] Refactor import record to credential conversion to BaseImporter.cs (#542) --- .../Components/ImportServiceCard.razor | 37 ++--------- .../Services/CredentialService.cs | 8 ++- .../Importers/BaseImporter.cs | 64 +++++++++++++++++++ .../Importers/KeePassImporter.cs | 4 +- 4 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 src/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs diff --git a/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razor b/src/AliasVault.Client/Main/Pages/Settings/ImportExport/Components/ImportServiceCard.razor index 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
@@ -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 { 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 - { - new() { SecretKey = importedCredential.TwoFactorSecret } - }; - } - - await CredentialService.InsertEntryAsync(credential, false); + await CredentialService.InsertEntryAsync(credential, false, false); } // Save the database diff --git a/src/AliasVault.Client/Services/CredentialService.cs b/src/AliasVault.Client/Services/CredentialService.cs index 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 /// /// Login object to insert. /// Whether to commit changes to database. Defaults to true, but can be set to false if entries are added in bulk by caller. + /// Whether to extract the favicon from the service URL. Defaults to true. /// Guid of inserted entry. - public async Task InsertEntryAsync(Credential loginObject, bool saveToDb = true) + public async Task 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.cs new file mode 100644 index 000000000..90e40400f --- /dev/null +++ b/src/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs @@ -0,0 +1,64 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) lanedirt. All rights reserved. +// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. +// +//----------------------------------------------------------------------- + +namespace AliasVault.ImportExport.Importers; + +using AliasClientDb; +using AliasVault.ImportExport.Models; + +/// +/// Base class for all importers. +/// +public class BaseImporter +{ + /// + /// Converts a list of imported credentials to a list of AliasVault credentials. + /// + /// The list of imported credentials. + /// The list of AliasVault credentials. + public static List ConvertToCredential(List importedCredentials) + { + var credentials = new List(); + + // 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 { 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 + { + new() { SecretKey = importedCredential.TwoFactorSecret } + }; + } + + credentials.Add(credential); + } + + return credentials; + } +} \ No newline at end of file diff --git a/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs index 59fd67551..9d140d398 100644 --- a/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs +++ b/src/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// +// // Copyright (c) lanedirt. All rights reserved. // Licensed under the MIT license. See LICENSE.md file in the project root for full license information. // @@ -16,7 +16,7 @@ using System.Globalization; /// /// Imports credentials from KeePass. /// -public static class KeePassImporter +public class KeePassImporter { /// /// Imports KeePass CSV file and converts contents to list of ImportedCredential model objects.