Fixed secret retrieval for connection testing while adding an ERI data source (#325)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2025-03-11 13:57:47 +01:00
1 parent 0125f48f61
commit 2429788ccc
10 files changed
+53 -40

No files matched your search

@@ -7,7 +7,7 @@ using AIStudio.Tools.Services;
namespace AIStudio.Tools.ERIClient;
public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIClient
public class ERIClientV1(IERIDataSource dataSource) : ERIClientBase(dataSource), IERIClient
{
#region Implementation of IERIClient
@@ -59,13 +59,13 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
}
}
public async Task<APIResponse<AuthResponse>> AuthenticateAsync(IERIDataSource dataSource, RustService rustService, CancellationToken cancellationToken = default)
public async Task<APIResponse<AuthResponse>> AuthenticateAsync(RustService rustService, string? temporarySecret = null, CancellationToken cancellationToken = default)
{
try
{
var authMethod = dataSource.AuthMethod;
var username = dataSource.Username;
switch (dataSource.AuthMethod)
var authMethod = this.dataSource.AuthMethod;
var username = this.dataSource.Username;
switch (this.dataSource.AuthMethod)
{
case AuthMethod.NONE:
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
@@ -99,17 +99,24 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
}
case AuthMethod.USERNAME_PASSWORD:
var passwordResponse = await rustService.GetSecret(dataSource);
if (!passwordResponse.Success)
string password;
if (string.IsNullOrWhiteSpace(temporarySecret))
{
return new()
var passwordResponse = await rustService.GetSecret(this.dataSource);
if (!passwordResponse.Success)
{
Successful = false,
Message = "Failed to retrieve the password."
};
}
return new()
{
Successful = false,
Message = "Failed to retrieve the password."
};
}
password = await passwordResponse.Secret.Decrypt(Program.ENCRYPTION);
}
else
password = temporarySecret;
var password = await passwordResponse.Secret.Decrypt(Program.ENCRYPTION);
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
{
// We must send both values inside the header. The username field is named 'user'.
@@ -146,17 +153,24 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
}
case AuthMethod.TOKEN:
var tokenResponse = await rustService.GetSecret(dataSource);
if (!tokenResponse.Success)
string token;
if (string.IsNullOrWhiteSpace(temporarySecret))
{
return new()
var tokenResponse = await rustService.GetSecret(this.dataSource);
if (!tokenResponse.Success)
{
Successful = false,
Message = "Failed to retrieve the access token."
};
}
return new()
{
Successful = false,
Message = "Failed to retrieve the access token."
};
}
token = await tokenResponse.Secret.Decrypt(Program.ENCRYPTION);
}
else
token = temporarySecret;
var token = await tokenResponse.Secret.Decrypt(Program.ENCRYPTION);
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
{
request.Headers.Add("Authorization", $"Bearer {token}");