diff --git a/src/Data/DbInitializer.cs b/src/Data/DbInitializer.cs index a34507d1..48eb79a9 100644 --- a/src/Data/DbInitializer.cs +++ b/src/Data/DbInitializer.cs @@ -377,6 +377,33 @@ public static void Initialize(IServiceProvider serviceProvider) applicationDbContext.Add(testingSinglesigWallet); applicationDbContext.Add(testingSingleSigBIP39Wallet); } + + // API Tokens generation for services + var authenticatedServices = new Dictionary + { + { "BTCPay", "9Hoz0PMYCsnPUzPO/JbJu8UdaKaAHJsh946xH20UzA0=" }, + { "Morpheus", "C+ktTkMGQupY9LY3IkpyqQQ2pDa7idaeSUKUnm+RawI=" }, + { "Liquidator", "8rvSsUGeyXXdDQrHctcTey/xtHdZQEn945KHwccKp9Q=" } + }; + + var existingTokens = applicationDbContext.ApiTokens.Where(token => authenticatedServices.Keys.Contains(token.Name)).ToList(); + + + if (existingTokens.Count != authenticatedServices.Count && adminUser != null) + { + foreach (var service in authenticatedServices) + { + // Check if the service exists in existingTokens + if (!existingTokens.Any(token => token.Name == service.Key)) + { + // The service does not exist in existingTokens, so create a new ApiToken + var newToken = CreateApiToken(service.Key, service.Value, adminUser.Id); + + // Add the new token to the database + applicationDbContext.ApiTokens.Add(newToken); + } + } + } } applicationDbContext.SaveChanges(); @@ -426,6 +453,22 @@ private static void SetRoles(RoleManager? roleManager) } } + private static APIToken CreateApiToken(string name, string token, string userId) + { + var apiToken = new APIToken + { + Name = name, + TokenHash = token, + IsBlocked = false, + CreatorId = userId + }; + + apiToken.SetCreationDatetime(); + apiToken.SetUpdateDatetime(); + + return apiToken; + } + private static NewTransactionEvent WaitNbxplorerNotification(LongPollingNotificationSession evts, DerivationStrategyBase derivationStrategy) { while (true)