You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
1 year ago
|
using StabilityMatrix.Core.Attributes;
|
||
|
using StabilityMatrix.Core.Models.Api.Lykos;
|
||
|
using StabilityMatrix.Core.Services;
|
||
|
|
||
|
namespace StabilityMatrix.Core.Api;
|
||
|
|
||
|
[Singleton]
|
||
|
public class LykosAuthTokenProvider : ITokenProvider
|
||
|
{
|
||
|
private readonly ISecretsManager secretsManager;
|
||
|
private readonly Lazy<ILykosAuthApi> lazyLykosAuthApi;
|
||
|
|
||
|
public LykosAuthTokenProvider(
|
||
|
Lazy<ILykosAuthApi> lazyLykosAuthApi,
|
||
|
ISecretsManager secretsManager
|
||
|
)
|
||
|
{
|
||
|
// Lazy as instantiating requires the current class to be instantiated.
|
||
|
this.lazyLykosAuthApi = lazyLykosAuthApi;
|
||
|
this.secretsManager = secretsManager;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public async Task<string> GetAccessTokenAsync()
|
||
|
{
|
||
|
var secrets = await secretsManager.SafeLoadAsync().ConfigureAwait(false);
|
||
|
|
||
|
return secrets.LykosAccount?.AccessToken ?? "";
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public async Task<(string AccessToken, string RefreshToken)> RefreshTokensAsync()
|
||
|
{
|
||
|
var secrets = await secretsManager.SafeLoadAsync().ConfigureAwait(false);
|
||
|
|
||
|
if (string.IsNullOrWhiteSpace(secrets.LykosAccount?.RefreshToken))
|
||
|
{
|
||
|
throw new InvalidOperationException("No refresh token found");
|
||
|
}
|
||
|
|
||
|
var lykosAuthApi = lazyLykosAuthApi.Value;
|
||
|
var newTokens = await lykosAuthApi
|
||
|
.PostLoginRefresh(new PostLoginRefreshRequest(secrets.LykosAccount.RefreshToken))
|
||
|
.ConfigureAwait(false);
|
||
|
|
||
|
secrets = secrets with { LykosAccount = newTokens };
|
||
|
|
||
|
await secretsManager.SaveAsync(secrets).ConfigureAwait(false);
|
||
|
|
||
|
return (newTokens.AccessToken, newTokens.RefreshToken);
|
||
|
}
|
||
|
}
|