Skip to content

Commit

Permalink
Use IEnumerable<ICertificatePasswordProvider> to prepare for multiple…
Browse files Browse the repository at this point in the history
… ICertificatePasswordProvider implementations
  • Loading branch information
arenekosreal committed Jul 2, 2024
1 parent 19cbb08 commit e90fc30
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions E5Renewer/Models/GraphAPIs/RandomGraphAPICaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ public class RandomGraphAPICaller : IGraphAPICaller
private readonly ILogger<RandomGraphAPICaller> logger;
private readonly IEnumerable<IAPIFunctionsContainer> apiFunctions;
private readonly IStatusManager statusManager;
private readonly ICertificatePasswordProvider certificatePasswordProvider;
private readonly IEnumerable<ICertificatePasswordProvider> certificatePasswordProviders;
private readonly Dictionary<GraphUser, GraphServiceClient> clients = new();

/// <summary>Initialize <c>RandomGraphAPICaller</c> with parameters given.</summary>
/// <param name="logger">The logger to generate log.</param>
/// <param name="apiFunctions">All known api functions with their id.</param>
/// <param name="statusManager"><see cref="IStatusManager">IStatusManager</see> implementation.</param>
/// <param name="certificatePasswordProvider"><see cref="ICertificatePasswordProvider">ICertificatePasswordProvider</see> implementation.</param>
/// <param name="certificatePasswordProviders"><see cref="ICertificatePasswordProvider">ICertificatePasswordProvider</see> implementations.</param>
/// <remarks>All parameters should be injected by Asp.Net Core.</remarks>
public RandomGraphAPICaller(
ILogger<RandomGraphAPICaller> logger,
IEnumerable<IAPIFunctionsContainer> apiFunctions,
IStatusManager statusManager,
ICertificatePasswordProvider certificatePasswordProvider
IEnumerable<ICertificatePasswordProvider> certificatePasswordProviders
)
{
this.logger = logger;
this.apiFunctions = apiFunctions;
this.statusManager = statusManager;
this.certificatePasswordProvider = certificatePasswordProvider;
this.certificatePasswordProviders = certificatePasswordProviders;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -68,7 +68,15 @@ public async Task CallNextAPIAsync(GraphUser user)
if (File.Exists(user.certificate))
{
this.logger.LogDebug("Using certificate to get user token.");
string? password = await this.certificatePasswordProvider.GetPasswordForCertificateAsync(user.certificate);
string? password = null;
foreach (ICertificatePasswordProvider provider in this.certificatePasswordProviders)
{
password = await provider.GetPasswordForCertificateAsync(user.certificate);
if (password is not null)
{
break;
}
}
if (password is not null)
{
this.logger.LogDebug("Found password for certificate given.");
Expand Down

0 comments on commit e90fc30

Please sign in to comment.