Files
raven-test-integration/CredRepo.cs
2026-02-24 08:51:11 -08:00

37 lines
1.4 KiB
C#

using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace raven_integration
{
public class CredRepo
{
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
private static Dictionary<string, string> authDict = new Dictionary<string, string>();
public CredRepo() { }
public static async Task<string> GetTokenAsync(string login, string password = null)
{
await semaphoreSlim.WaitAsync();
try
{
if (password == null)
password = login;
if (!authDict.ContainsKey(login))
{
dynamic creds = new JObject();
creds.login = login;
creds.password = password;
ApiResponse a = await Util.PostAsync("auth", null, creds.ToString());
if (a.ObjectResponse?["data"] == null)
throw new Exception($"Auth failed for '{login}' (HTTP {a.HttpResponse.StatusCode}): {a.ObjectResponse?.ToString() ?? "(no body)"}");
authDict[login] = a.ObjectResponse["data"]["token"].Value<string>();
}
return authDict[login];
}
finally
{
semaphoreSlim.Release();
}
}
}
}