Why does AuthenticationFactoryOAuth2.ClientCredentials require credentials saved in file? #220
-
What is the point of having authentication method loading credential from file? It seems as such anti-pattern. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi! This is not something it was invented here, but just ported from the official java library. Once java implementation changes it will open a possibility to change the behavior of this library as well. |
Beta Was this translation helpful? Give feedback.
-
@Lanayx Would it be possible to get example how to create such logic in C#? I have tried to adapt token retrieval with following classes: class PulsarClientAuthentication : Authentication
{
private static readonly ConcurrentDictionary<string, AuthenticationResult> TokenCache = new();
private PulsarClientConfiguration configuration;
public PulsarClientAuthentication(PulsarClientConfiguration configuration)
{
this.configuration = configuration;
}
public override string GetAuthMethodName() => "OAuth2";
public override AuthenticationDataProvider GetAuthData()
{
if (TokenCache.TryGetValue(configuration.ClientId, out var authResult) &&
authResult.ExpiresOn > DateTimeOffset.Now) return new TokenProvider(authResult.AccessToken);
TokenCache.TryRemove(configuration.ClientId, out _);
var pca = ConfidentialClientApplicationBuilder.Create(configuration.ClientId)
.WithClientSecret(configuration.ClientSecret)
.WithTenantId(configuration.Tenant)
.Build();
var scopes = new List<string> {configuration.pulsarScope };
var result = pca.AcquireTokenForClient(scopes).ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult();
TokenCache.TryAdd(configuration.ClientId, result);
authResult = result;
return new TokenProvider(authResult.AccessToken);
}
}
class TokenProvider : AuthenticationDataProvider
{
private string token;
public TokenProvider(string token)
{
this.token = token;
}
public override bool HasDataFromCommand() => true;
public override string GetCommandData() => token;
} And when I try manually I am able to get valid token from both of them (see below), but when I try to use custom Authenticator, creating provider fails with Exception var authNative = AuthenticationFactoryOAuth2.ClientCredentials(issuerUrl, audience, privateKeyFileUri, scope);
var authNative = new PulsarClientAuthentication(configuration);
var a = authMine.GetAuthData()/*.GetCommandData()*/;
var b = authMine.GetAuthData()/*.GetCommandData()*/; |
Beta Was this translation helpful? Give feedback.
Hi! This is not something it was invented here, but just ported from the official java library. Once java implementation changes it will open a possibility to change the behavior of this library as well.
That being said - you can always plugin any custom authentication logic by implementing abstract Authentication class