Skip to content

Commit

Permalink
added apitoken generation for development purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
daliclovr committed Oct 16, 2023
1 parent 9464406 commit 4a949d7
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
{
{ "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();
Expand Down Expand Up @@ -426,6 +453,22 @@ private static void SetRoles(RoleManager<IdentityRole>? 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)
Expand Down

0 comments on commit 4a949d7

Please sign in to comment.