diff --git a/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs b/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs index 5ebf230d..6a0f0957 100644 --- a/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs +++ b/app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs @@ -199,7 +199,7 @@ public sealed class AgentDataSourceSelection (ILogger // Call the ERI server to get the server description: // using var eriClient = ERIClientFactory.Get(eriDataSource.Version, eriDataSource)!; - var authResponse = await eriClient.AuthenticateAsync(rustService, token); + var authResponse = await eriClient.AuthenticateAsync(rustService, cancellationToken: token); if (authResponse.Successful) { var serverDescriptionResponse = await eriClient.GetDataSourceInfoAsync(token); diff --git a/app/MindWork AI Studio/Dialogs/DataSourceERI_V1Dialog.razor.cs b/app/MindWork AI Studio/Dialogs/DataSourceERI_V1Dialog.razor.cs index da80dc06..81612a8e 100644 --- a/app/MindWork AI Studio/Dialogs/DataSourceERI_V1Dialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/DataSourceERI_V1Dialog.razor.cs @@ -211,7 +211,7 @@ public partial class DataSourceERI_V1Dialog : ComponentBase, ISecretId this.availableAuthMethods = authSchemes.Data!.Select(n => n.AuthMethod).ToList(); - var loginResult = await client.AuthenticateAsync(this.RustService, cts.Token); + var loginResult = await client.AuthenticateAsync(this.RustService, this.dataSecret, cts.Token); if (!loginResult.Successful) { await this.form.Validate(); diff --git a/app/MindWork AI Studio/Settings/DataModel/DataSourceERI_V1.cs b/app/MindWork AI Studio/Settings/DataModel/DataSourceERI_V1.cs index c3a959f8..cc43a3eb 100644 --- a/app/MindWork AI Studio/Settings/DataModel/DataSourceERI_V1.cs +++ b/app/MindWork AI Studio/Settings/DataModel/DataSourceERI_V1.cs @@ -62,7 +62,7 @@ public readonly record struct DataSourceERI_V1 : IERIDataSource var logger = Program.SERVICE_PROVIDER.GetRequiredService>(); using var eriClient = ERIClientFactory.Get(this.Version, this)!; - var authResponse = await eriClient.AuthenticateAsync(rustService, token); + var authResponse = await eriClient.AuthenticateAsync(rustService, cancellationToken: token); if (authResponse.Successful) { var retrievalRequest = new RetrievalRequest diff --git a/app/MindWork AI Studio/Tools/ERIClient/ERIClientV1.cs b/app/MindWork AI Studio/Tools/ERIClient/ERIClientV1.cs index 0f79fe6f..178849dd 100644 --- a/app/MindWork AI Studio/Tools/ERIClient/ERIClientV1.cs +++ b/app/MindWork AI Studio/Tools/ERIClient/ERIClientV1.cs @@ -59,7 +59,7 @@ public class ERIClientV1(IERIDataSource dataSource) : ERIClientBase(dataSource), } } - public async Task> AuthenticateAsync(RustService rustService, CancellationToken cancellationToken = default) + public async Task> AuthenticateAsync(RustService rustService, string? temporarySecret = null, CancellationToken cancellationToken = default) { try { @@ -99,17 +99,24 @@ public class ERIClientV1(IERIDataSource dataSource) : ERIClientBase(dataSource), } case AuthMethod.USERNAME_PASSWORD: - var passwordResponse = await rustService.GetSecret(this.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(IERIDataSource dataSource) : ERIClientBase(dataSource), } case AuthMethod.TOKEN: - var tokenResponse = await rustService.GetSecret(this.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}"); diff --git a/app/MindWork AI Studio/Tools/ERIClient/IERIClient.cs b/app/MindWork AI Studio/Tools/ERIClient/IERIClient.cs index b7bbe391..80d8c4f7 100644 --- a/app/MindWork AI Studio/Tools/ERIClient/IERIClient.cs +++ b/app/MindWork AI Studio/Tools/ERIClient/IERIClient.cs @@ -19,9 +19,10 @@ public interface IERIClient : IDisposable /// Authenticate the user to the ERI server. /// /// The Rust service. + /// The temporary secret when adding a new data source, and the secret is not yet stored in the OS. /// The cancellation token. /// The authentication response. - public Task> AuthenticateAsync(RustService rustService, CancellationToken cancellationToken = default); + public Task> AuthenticateAsync(RustService rustService, string? temporarySecret = null, CancellationToken cancellationToken = default); /// /// Retrieves the data source information from the ERI server. diff --git a/app/MindWork AI Studio/Tools/Services/DataSourceService.cs b/app/MindWork AI Studio/Tools/Services/DataSourceService.cs index e8c6b496..f545aca5 100644 --- a/app/MindWork AI Studio/Tools/Services/DataSourceService.cs +++ b/app/MindWork AI Studio/Tools/Services/DataSourceService.cs @@ -113,7 +113,7 @@ public sealed class DataSourceService } this.logger.LogInformation($"Authenticating with ERI source '{source.Name}' (id={source.Id})..."); - var loginResult = await client.AuthenticateAsync(this.rustService, cancellationTokenSource.Token); + var loginResult = await client.AuthenticateAsync(this.rustService, cancellationToken: cancellationTokenSource.Token); if (!loginResult.Successful) { this.logger.LogWarning($"Authentication with ERI source '{source.Name}' (id={source.Id}) failed. We skip this source. Reason: {loginResult.Message}");