fix: do up No audit of document access in vigilcare-records-gap-analysis.md
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
public static class AuthHelper
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, string> TokenCache = new();
|
||||
|
||||
/// <summary>
|
||||
/// Logs in as the given user and returns an HttpClient with the JWT
|
||||
/// Authorization header pre-configured. Login also returns a refresh token;
|
||||
@@ -11,19 +14,25 @@ public static class AuthHelper
|
||||
public static async Task<HttpClient> LoginAsync(
|
||||
ApiFixture fixture, string username = "entry1", string password = "password")
|
||||
{
|
||||
var cacheKey = $"{username}:{password}";
|
||||
|
||||
if (!TokenCache.TryGetValue(cacheKey, out var token))
|
||||
{
|
||||
var loginClient = fixture.CreateClient();
|
||||
var loginResp = await loginClient.PostAsJsonAsync("/api/v1/auth/login",
|
||||
new { username, password });
|
||||
|
||||
loginResp.EnsureSuccessStatusCode();
|
||||
|
||||
var body = await loginResp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
token = body!.RootElement.GetProperty("data").GetProperty("token").GetString()!;
|
||||
TokenCache[cacheKey] = token;
|
||||
}
|
||||
|
||||
var client = fixture.CreateClient();
|
||||
|
||||
var loginResp = await client.PostAsJsonAsync("/api/v1/auth/login",
|
||||
new { username, password });
|
||||
|
||||
loginResp.EnsureSuccessStatusCode();
|
||||
|
||||
var body = await loginResp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
var token = body!.RootElement.GetProperty("data").GetProperty("token").GetString()!;
|
||||
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user