diff --git a/src/Zeebe.Client.Accelerator/Extensions/ServiceCollectionExtensions.cs b/src/Zeebe.Client.Accelerator/Extensions/ServiceCollectionExtensions.cs index 045b066..481071e 100644 --- a/src/Zeebe.Client.Accelerator/Extensions/ServiceCollectionExtensions.cs +++ b/src/Zeebe.Client.Accelerator/Extensions/ServiceCollectionExtensions.cs @@ -110,19 +110,20 @@ private static IServiceCollection AddZeebeJobHandlers(this IServiceCollection se private static IServiceCollection AddZeebeClient(this IServiceCollection services) { - return services + return services .AddZeebeBuilders() .AddScoped(sp => { var options = sp.GetRequiredService>(); var builder = sp.GetRequiredService(); + var tokenSupplier = sp.GetService(); var loggerFactory = sp.GetService(); StateBuilderExtensions.Configure(sp.GetRequiredService()); if(loggerFactory != null) builder = builder.UseLoggerFactory(loggerFactory); - + return builder - .Build(options.Value.Client); + .Build(options.Value.Client, tokenSupplier); }); } diff --git a/src/Zeebe.Client.Accelerator/Extensions/ZeebeClientBuilderExtensions.cs b/src/Zeebe.Client.Accelerator/Extensions/ZeebeClientBuilderExtensions.cs index 75af12a..00d497b 100644 --- a/src/Zeebe.Client.Accelerator/Extensions/ZeebeClientBuilderExtensions.cs +++ b/src/Zeebe.Client.Accelerator/Extensions/ZeebeClientBuilderExtensions.cs @@ -7,11 +7,11 @@ namespace Zeebe.Client.Accelerator.Extensions { public static class ZeebeClientBuilderExtensions { - public static IZeebeClient Build(this IZeebeClientBuilder builder, ClientOptions options) + public static IZeebeClient Build(this IZeebeClientBuilder builder, ClientOptions options, IAccessTokenSupplier tokenSupplier = null) { return builder .BuildTransportBuilder(options) - .BuildFinalStep(options) + .BuildFinalStep(options, tokenSupplier) .BuildClient(options); } @@ -23,7 +23,7 @@ private static IZeebeClientTransportBuilder BuildTransportBuilder(this IZeebeCli return builder.UseGatewayAddress(options.GatewayAddress); } - private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransportBuilder builder, ClientOptions options) + private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransportBuilder builder, ClientOptions options, IAccessTokenSupplier tokenSupplier = null) { if (options.Cloud == null && (Environment.GetEnvironmentVariable("ZEEBE_CLIENT_ID") != null || Environment.GetEnvironmentVariable("ZEEBE_CLIENT_SECRET") != null)) @@ -41,20 +41,26 @@ private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransp return builder.UsePlainText(); IZeebeSecureClientBuilder clientBuilder = null; - + if (options.Cloud != null) { clientBuilder = builder.UseTransportEncryption(); - var tokenSupplier = CamundaCloudTokenProvider.Builder().UseAuthServer(options.Cloud.AuthorizationServerUrl) - .UseClientId(options.Cloud.ClientId).UseClientSecret(options.Cloud.ClientSecret).UseAudience(options.Cloud.TokenAudience) - .Build(); + + if (tokenSupplier == null) + { + // The CamundaCloudTokenProvider maintains a local token cache, using a local file as a means to persist the token... + tokenSupplier = CamundaCloudTokenProvider.Builder().UseAuthServer(options.Cloud.AuthorizationServerUrl) + .UseClientId(options.Cloud.ClientId).UseClientSecret(options.Cloud.ClientSecret).UseAudience(options.Cloud.TokenAudience) + .Build(); + } + clientBuilder.UseAccessTokenSupplier(tokenSupplier); - // The CamundaCloudTokenProvider maintains a local token cache, using a local file as a means to persist the token... // -> try to get token early in order to prevent errors when writing credential file in parallel upon startup try { tokenSupplier.GetAccessTokenForRequestAsync().Wait(); } catch (Exception) { /* NOOP */ } + return clientBuilder; } - + if (!String.IsNullOrEmpty(options.TransportEncryption.RootCertificatePath)) clientBuilder = builder.UseTransportEncryption(options.TransportEncryption.RootCertificatePath); else