Fix UI & Rotate Salt

This commit is contained in:
2025-09-19 22:33:40 +02:00
parent 1755b5cb54
commit 612e7c88a2
7 changed files with 92 additions and 35 deletions

View File

@@ -29,24 +29,25 @@ public class DiscoveryApiClient
{
var token = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(token)) return [];
var distinctHashes = hashes.Distinct(StringComparer.Ordinal).ToArray();
using var req = new HttpRequestMessage(HttpMethod.Post, endpoint);
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var body = JsonSerializer.Serialize(new
{
hashes = hashes.Distinct(StringComparer.Ordinal).ToArray(),
hashes = distinctHashes,
salt = _configProvider.SaltB64
});
req.Content = new StringContent(body, Encoding.UTF8, "application/json");
var resp = await _httpClient.SendAsync(req, ct).ConfigureAwait(false);
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var token2 = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
var token2 = await _tokenProvider.ForceRefreshToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(token2)) return [];
using var req2 = new HttpRequestMessage(HttpMethod.Post, endpoint);
req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token2);
var body2 = JsonSerializer.Serialize(new
{
hashes = hashes.Distinct(StringComparer.Ordinal).ToArray(),
hashes = distinctHashes,
salt = _configProvider.SaltB64
});
req2.Content = new StringContent(body2, Encoding.UTF8, "application/json");
@@ -77,7 +78,7 @@ public class DiscoveryApiClient
var resp = await _httpClient.SendAsync(req, ct).ConfigureAwait(false);
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var jwt2 = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
var jwt2 = await _tokenProvider.ForceRefreshToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(jwt2)) return false;
using var req2 = new HttpRequestMessage(HttpMethod.Post, endpoint);
req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt2);
@@ -121,7 +122,7 @@ public class DiscoveryApiClient
var resp = await _httpClient.SendAsync(req, ct).ConfigureAwait(false);
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var jwt2 = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
var jwt2 = await _tokenProvider.ForceRefreshToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(jwt2)) return false;
using var req2 = new HttpRequestMessage(HttpMethod.Post, endpoint);
req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt2);
@@ -152,7 +153,7 @@ public class DiscoveryApiClient
var resp = await _httpClient.SendAsync(req, ct).ConfigureAwait(false);
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var jwt2 = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
var jwt2 = await _tokenProvider.ForceRefreshToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(jwt2)) return false;
using var req2 = new HttpRequestMessage(HttpMethod.Post, endpoint);
req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt2);
@@ -179,7 +180,7 @@ public class DiscoveryApiClient
var resp = await _httpClient.SendAsync(req, ct).ConfigureAwait(false);
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var jwt2 = await _tokenProvider.GetOrUpdateToken(ct).ConfigureAwait(false);
var jwt2 = await _tokenProvider.ForceRefreshToken(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(jwt2)) return;
using var req2 = new HttpRequestMessage(HttpMethod.Post, endpoint);
req2.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt2);

View File

@@ -49,10 +49,10 @@ public partial class ApiController
await _mareHub!.SendAsync(nameof(GroupClear), group).ConfigureAwait(false);
}
public async Task<GroupPasswordDto> GroupCreate()
public async Task<GroupPasswordDto> GroupCreate(string? alias = null)
{
CheckConnection();
return await _mareHub!.InvokeAsync<GroupPasswordDto>(nameof(GroupCreate)).ConfigureAwait(false);
return await _mareHub!.InvokeAsync<GroupPasswordDto>(nameof(GroupCreate), string.IsNullOrWhiteSpace(alias) ? null : alias.Trim()).ConfigureAwait(false);
}
public async Task<List<string>> GroupCreateTempInvite(GroupDto group, int amount)
@@ -125,4 +125,4 @@ public partial class ApiController
{
if (ServerState is not (ServerState.Connected or ServerState.Connecting or ServerState.Reconnecting)) throw new InvalidDataException("Not connected");
}
}
}

View File

@@ -172,6 +172,16 @@ public sealed class TokenProvider : IDisposable, IMediatorSubscriber
return await GetNewToken(jwtIdentifier, ct).ConfigureAwait(false);
}
public async Task<string?> ForceRefreshToken(CancellationToken ct)
{
JwtIdentifier? jwtIdentifier = await GetIdentifier().ConfigureAwait(false);
if (jwtIdentifier == null) return null;
_tokenCache.TryRemove(jwtIdentifier, out _);
_logger.LogTrace("ForceRefresh: Getting new token");
return await GetNewToken(jwtIdentifier, ct).ConfigureAwait(false);
}
public string? GetStapledWellKnown(string apiUrl)
{
_wellKnownCache.TryGetValue(apiUrl, out var wellKnown);
@@ -180,4 +190,4 @@ public sealed class TokenProvider : IDisposable, IMediatorSubscriber
return null;
return wellKnown;
}
}
}