using CredentialManager.Models; using System.Text.Json; namespace CredentialManager.Utilities; /// /// Metodi di utilità per la validazione delle credenziali /// public static class CredentialValidator { /// /// Valida una credenziale database /// /// La credenziale da validare /// Risultato della validazione public static ValidationResult ValidateDatabaseCredential(DatabaseCredential credential) { var errors = new List(); if (string.IsNullOrWhiteSpace(credential.Name)) errors.Add("Name is required"); if (string.IsNullOrWhiteSpace(credential.Host)) errors.Add("Host is required"); if (credential.Port <= 0) errors.Add("Port must be greater than 0"); if (string.IsNullOrWhiteSpace(credential.DatabaseName)) errors.Add("Database name is required"); if (string.IsNullOrWhiteSpace(credential.Username)) errors.Add("Username is required"); if (string.IsNullOrWhiteSpace(credential.Password)) errors.Add("Password is required"); // Validate port ranges for specific database types switch (credential.DatabaseType) { case DatabaseType.SqlServer: if (credential.Port < 1 || credential.Port > 65535) errors.Add("SQL Server port must be between 1 and 65535"); break; case DatabaseType.MySql: if (credential.Port < 1 || credential.Port > 65535) errors.Add("MySQL port must be between 1 and 65535"); break; case DatabaseType.PostgreSql: if (credential.Port < 1 || credential.Port > 65535) errors.Add("PostgreSQL port must be between 1 and 65535"); break; case DatabaseType.Oracle: if (credential.Port < 1 || credential.Port > 65535) errors.Add("Oracle port must be between 1 and 65535"); break; } return new ValidationResult { IsValid = errors.Count == 0, Errors = errors }; } /// /// Valida una credenziale REST API /// /// La credenziale da validare /// Risultato della validazione public static ValidationResult ValidateRestApiCredential(RestApiCredential credential) { var errors = new List(); if (string.IsNullOrWhiteSpace(credential.Name)) errors.Add("Name is required"); if (string.IsNullOrWhiteSpace(credential.BaseUrl)) errors.Add("Base URL is required"); if (!string.IsNullOrWhiteSpace(credential.BaseUrl) && !Uri.IsWellFormedUriString(credential.BaseUrl, UriKind.Absolute)) errors.Add("Base URL must be a valid absolute URI"); if (credential.TimeoutSeconds <= 0) errors.Add("Timeout must be greater than 0 seconds"); // Check that at least one authentication method is provided bool hasAuth = !string.IsNullOrWhiteSpace(credential.ApiKey) || !string.IsNullOrWhiteSpace(credential.AuthToken) || !string.IsNullOrWhiteSpace(credential.BearerToken) || (!string.IsNullOrWhiteSpace(credential.Username) && !string.IsNullOrWhiteSpace(credential.Password)); if (!hasAuth) errors.Add("At least one authentication method must be provided (ApiKey, AuthToken, BearerToken, or Username/Password)"); return new ValidationResult { IsValid = errors.Count == 0, Errors = errors }; } } /// /// Risultato di una validazione /// public class ValidationResult { public bool IsValid { get; set; } public List Errors { get; set; } = new(); } /// /// Metodi di utilità per l'import/export delle credenziali /// public static class CredentialImportExport { /// /// Esporta le credenziali in formato JSON /// /// Credenziali database /// Credenziali REST API /// JSON string public static string ExportToJson( IEnumerable databaseCredentials, IEnumerable restApiCredentials) { var export = new { ExportDate = DateTime.UtcNow, DatabaseCredentials = databaseCredentials, RestApiCredentials = restApiCredentials }; var options = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; return JsonSerializer.Serialize(export, options); } /// /// Importa le credenziali da formato JSON /// /// JSON string /// Tuple con le credenziali importate public static (List DatabaseCredentials, List RestApiCredentials) ImportFromJson(string json) { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; using var document = JsonDocument.Parse(json); var root = document.RootElement; var databaseCredentials = new List(); var restApiCredentials = new List(); if (root.TryGetProperty("databaseCredentials", out var dbElement)) { databaseCredentials = JsonSerializer.Deserialize>(dbElement.GetRawText(), options) ?? new(); } if (root.TryGetProperty("restApiCredentials", out var restElement)) { restApiCredentials = JsonSerializer.Deserialize>(restElement.GetRawText(), options) ?? new(); } return (databaseCredentials, restApiCredentials); } } /// /// Metodi di utilità per la generazione di credenziali di test /// public static class CredentialTestDataGenerator { /// /// Genera credenziali database di esempio per testing /// /// Lista di credenziali database di test public static List GenerateTestDatabaseCredentials() { return new List { new DatabaseCredential { Name = "Test SQL Server", DatabaseType = DatabaseType.SqlServer, Host = "localhost", Port = 1433, DatabaseName = "TestDB", Username = "testuser", Password = "testpass123", CommandTimeout = 30 }, new DatabaseCredential { Name = "Test MySQL", DatabaseType = DatabaseType.MySql, Host = "localhost", Port = 3306, DatabaseName = "testdb", Username = "testuser", Password = "testpass123", CommandTimeout = 30 }, new DatabaseCredential { Name = "Test PostgreSQL", DatabaseType = DatabaseType.PostgreSql, Host = "localhost", Port = 5432, DatabaseName = "testdb", Username = "testuser", Password = "testpass123", CommandTimeout = 30 }, new DatabaseCredential { Name = "Test SQLite", DatabaseType = DatabaseType.Sqlite, Host = "", Port = 0, DatabaseName = "test.db", Username = "", Password = "", CommandTimeout = 30 } }; } /// /// Genera credenziali REST API di esempio per testing /// /// Lista di credenziali REST API di test public static List GenerateTestRestApiCredentials() { return new List { new RestApiCredential { Name = "Test API with API Key", BaseUrl = "https://api.example.com/v1", ApiKey = "test_api_key_123", TimeoutSeconds = 60, Headers = new Dictionary { { "Accept", "application/json" }, { "Content-Type", "application/json" } } }, new RestApiCredential { Name = "Test API with Bearer Token", BaseUrl = "https://api.another-example.com", BearerToken = "bearer_token_xyz789", TimeoutSeconds = 120 }, new RestApiCredential { Name = "Test API with Basic Auth", BaseUrl = "https://api.basic-auth-example.com", Username = "testuser", Password = "testpassword", TimeoutSeconds = 90, Headers = new Dictionary { { "User-Agent", "CredentialManager-Test/1.0" } } } }; } }