Refactor import record to credential conversion to BaseImporter.cs (#542)

This commit is contained in:
Leendert de Borst
2025-03-24 16:22:35 +01:00
committed by Leendert de Borst
parent 3f6575dfe5
commit e93b0575ff
4 changed files with 77 additions and 36 deletions

View File

@@ -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 database

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -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.