Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add firefox importer and unit test (#542)
Leendert de Borsta3a3d39664d9
diff --git a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csprojindex 7c0b446b5..8ed0aee60 100644--- a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj+++ b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj@@ -63,6 +63,7 @@<ItemGroup><EmbeddedResource Include="TestData\Exports\bitwarden.csv" /><EmbeddedResource Include="TestData\Exports\chrome.csv" />+ <EmbeddedResource Include="TestData\Exports\firefox.csv" /><EmbeddedResource Include="TestData\Exports\keepass.kdbx.csv" /><EmbeddedResource Include="TestData\Exports\1password_8.csv" /></ItemGroup>diff --git a/src/Tests/AliasVault.UnitTests/TestData/Exports/firefox.csv b/src/Tests/AliasVault.UnitTests/TestData/Exports/firefox.csvnew file mode 100644index 000000000..2b0d3ad0e--- /dev/null+++ b/src/Tests/AliasVault.UnitTests/TestData/Exports/firefox.csv@@ -0,0 +1,4 @@+"url","username","password","httpRealm","formActionOrigin","guid","timeCreated","timeLastUsed","timePasswordChanged"+"https://example.com","username-example","examplepassword",,"","{2a5df2a4-c75b-4b32-bf7e-8cca012cf82c}","1743265493999","1743265493999","1743265493999"+"https://youtube.com","youtubeusername","youtubepassword",,"","{40d0b777-551c-4875-98a2-20e486fc4289}","1743265510074","1743265510074","1743265510074"+"https://www.aliasvault.net","username","password",,"","{73bed185-dd2a-44e1-b0f6-d397cb880300}","1743265527711","1743265527711","1743265527711"\ No newline at end of filediff --git a/src/Tests/AliasVault.UnitTests/Utilities/ImportExportTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/ImportExportTests.csindex c903f17cd..69d6ba9f4 100644--- a/src/Tests/AliasVault.UnitTests/Utilities/ImportExportTests.cs+++ b/src/Tests/AliasVault.UnitTests/Utilities/ImportExportTests.cs@@ -241,4 +241,40 @@ public class ImportExportTestsAssert.That(facebookCredential.Notes, Is.EqualTo("Facebook comment"));});}++ /// <summary>+ /// Test case for importing credentials from Firefox CSV and ensuring all values are present.+ /// </summary>+ /// <returns>Async task.</returns>+ [Test]+ public async Task ImportCredentialsFromFirefoxCsv()+ {+ // Arrange+ var fileContent = await ResourceReaderUtility.ReadEmbeddedResourceStringAsync("AliasVault.UnitTests.TestData.Exports.firefox.csv");++ // Act+ var importedCredentials = await FirefoxImporter.ImportFromCsvAsync(fileContent);++ // Assert+ Assert.That(importedCredentials, Has.Count.EqualTo(3));++ // Test specific entries+ var exampleCredential = importedCredentials.First(c => c.ServiceName == "example.com");+ Assert.Multiple(() =>+ {+ Assert.That(exampleCredential.ServiceName, Is.EqualTo("example.com"));+ Assert.That(exampleCredential.ServiceUrl, Is.EqualTo("https://example.com"));+ Assert.That(exampleCredential.Username, Is.EqualTo("username-example"));+ Assert.That(exampleCredential.Password, Is.EqualTo("examplepassword"));+ });++ var youtubeCredential = importedCredentials.First(c => c.ServiceName == "youtube.com");+ Assert.Multiple(() =>+ {+ Assert.That(youtubeCredential.ServiceName, Is.EqualTo("youtube.com"));+ Assert.That(youtubeCredential.ServiceUrl, Is.EqualTo("https://youtube.com"));+ Assert.That(youtubeCredential.Username, Is.EqualTo("youtubeusername"));+ Assert.That(youtubeCredential.Password, Is.EqualTo("youtubepassword"));+ });+ }}diff --git a/src/Utilities/AliasVault.ImportExport/Importers/ChromeImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/ChromeImporter.csindex f6164fc17..edf36d77a 100644--- a/src/Utilities/AliasVault.ImportExport/Importers/ChromeImporter.cs+++ b/src/Utilities/AliasVault.ImportExport/Importers/ChromeImporter.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="BitwardenImporter.cs" company="lanedirt">+// <copyright file="ChromeImporter.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>diff --git a/src/Utilities/AliasVault.ImportExport/Importers/FirefoxImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/FirefoxImporter.csnew file mode 100644index 000000000..31fbce335--- /dev/null+++ b/src/Utilities/AliasVault.ImportExport/Importers/FirefoxImporter.cs@@ -0,0 +1,56 @@+//-----------------------------------------------------------------------+// <copyright file="FirefoxImporter.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 AliasVault.ImportExport.Models;+using AliasVault.ImportExport.Models.Imports;+using CsvHelper;+using CsvHelper.Configuration;+using System.Globalization;++/// <summary>+/// Imports credentials from Firefox Password Manager.+/// </summary>+public class FirefoxImporter+{+ /// <summary>+ /// Imports Firefox CSV file and converts contents to list of ImportedCredential model objects.+ /// </summary>+ /// <param name="fileContent">The content of the CSV file.</param>+ /// <returns>The imported list of ImportedCredential objects.</returns>+ public static async Task<List<ImportedCredential>> ImportFromCsvAsync(string fileContent)+ {+ using var reader = new StringReader(fileContent);+ using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture));++ var credentials = new List<ImportedCredential>();+ await foreach (var record in csv.GetRecordsAsync<FirefoxCsvRecord>())+ {+ // Extract service name from URL, e.g. https://example.com/path -> example.com.+ var uri = new Uri(record.Url);+ var serviceName = uri.Host.StartsWith("www.") ? uri.Host[4..] : uri.Host;++ var credential = new ImportedCredential+ {+ ServiceName = serviceName,+ ServiceUrl = record.Url,+ Username = record.Username,+ Password = record.Password+ };++ credentials.Add(credential);+ }++ if (credentials.Count == 0)+ {+ throw new InvalidOperationException("No records found in the CSV file.");+ }++ return credentials;+ }+}\ No newline at end of filediff --git a/src/Utilities/AliasVault.ImportExport/Importers/OnePasswordImporter.cs b/src/Utilities/AliasVault.ImportExport/Importers/OnePasswordImporter.csindex c36f1324e..b17d561da 100644--- a/src/Utilities/AliasVault.ImportExport/Importers/OnePasswordImporter.cs+++ b/src/Utilities/AliasVault.ImportExport/Importers/OnePasswordImporter.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="BitwardenImporter.cs" company="lanedirt">+// <copyright file="OnePasswordImporter.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>diff --git a/src/Utilities/AliasVault.ImportExport/Models/Imports/FirefoxCsvRecord.cs b/src/Utilities/AliasVault.ImportExport/Models/Imports/FirefoxCsvRecord.csnew file mode 100644index 000000000..5678a1d42--- /dev/null+++ b/src/Utilities/AliasVault.ImportExport/Models/Imports/FirefoxCsvRecord.cs@@ -0,0 +1,70 @@+//-----------------------------------------------------------------------+// <copyright file="FirefoxCsvRecord.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.Models.Imports;++using CsvHelper.Configuration.Attributes;++/// <summary>+/// Represents a Firefox CSV record that is being imported from a Firefox CSV export file.+/// </summary>+public class FirefoxCsvRecord+{+ /// <summary>+ /// Gets or sets the URL of the item.+ /// </summary>+ [Name("url")]+ public string Url { get; set; } = string.Empty;++ /// <summary>+ /// Gets or sets the username of the item.+ /// </summary>+ [Name("username")]+ public string Username { get; set; } = string.Empty;++ /// <summary>+ /// Gets or sets the password of the item.+ /// </summary>+ [Name("password")]+ public string Password { get; set; } = string.Empty;++ /// <summary>+ /// Gets or sets the HTTP realm for authentication.+ /// </summary>+ [Name("httpRealm")]+ public string? HttpRealm { get; set; }++ /// <summary>+ /// Gets or sets the form action origin URL.+ /// </summary>+ [Name("formActionOrigin")]+ public string? FormActionOrigin { get; set; }++ /// <summary>+ /// Gets or sets the GUID of the password entry.+ /// </summary>+ [Name("guid")]+ public string? Guid { get; set; }++ /// <summary>+ /// Gets or sets the timestamp when the password was created.+ /// </summary>+ [Name("timeCreated")]+ public string? TimeCreated { get; set; }++ /// <summary>+ /// Gets or sets the timestamp when the password was last used.+ /// </summary>+ [Name("timeLastUsed")]+ public string? TimeLastUsed { get; set; }++ /// <summary>+ /// Gets or sets the timestamp when the password was last changed.+ /// </summary>+ [Name("timePasswordChanged")]+ public string? TimePasswordChanged { get; set; }+}