60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using System;
|
|
using NuGet.Protocol.Plugins;
|
|
|
|
namespace NugetSecretCredential;
|
|
|
|
internal class GetAuthenticationCredentialsRequestHandler : RequestHandlerBase<GetAuthenticationCredentialsRequest, GetAuthenticationCredentialsResponse>
|
|
{
|
|
private readonly IReadOnlyCollection<ICredentialProvider> _credentialProviders;
|
|
private readonly TimeSpan progressReporterTimeSpan = TimeSpan.FromSeconds(2);
|
|
|
|
public GetAuthenticationCredentialsRequestHandler(IReadOnlyCollection<ICredentialProvider> credentialProviders)
|
|
{
|
|
this._credentialProviders = credentialProviders ?? throw new ArgumentNullException();
|
|
}
|
|
|
|
public override async Task<GetAuthenticationCredentialsResponse> HandleRequestAsync(GetAuthenticationCredentialsRequest request, CancellationToken cancel)
|
|
{
|
|
if (request?.Uri == null)
|
|
return new GetAuthenticationCredentialsResponse(
|
|
username: null,
|
|
password: null,
|
|
message: "Uri is null",
|
|
authenticationTypes: null,
|
|
responseCode: MessageResponseCode.Error);
|
|
|
|
foreach (ICredentialProvider credentialProvider in _credentialProviders)
|
|
{
|
|
if (await credentialProvider.CanProvideCredentialsAsync(request.Uri, cancel) == false)
|
|
continue;
|
|
|
|
try
|
|
{
|
|
GetAuthenticationCredentialsResponse response = await credentialProvider.HandleRequestAsync(request, cancel);
|
|
if (response != null && response.ResponseCode == MessageResponseCode.Success)
|
|
return response;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return new GetAuthenticationCredentialsResponse(
|
|
username: null,
|
|
password: null,
|
|
message: e.Message,
|
|
authenticationTypes: null,
|
|
responseCode: MessageResponseCode.Error);
|
|
}
|
|
}
|
|
|
|
return new GetAuthenticationCredentialsResponse(
|
|
username: null,
|
|
password: null,
|
|
message: null,
|
|
authenticationTypes: null,
|
|
responseCode: MessageResponseCode.NotFound);
|
|
}
|
|
|
|
protected override AutomaticProgressReporter GetProgressReporter(IConnection connection, Message message, CancellationToken cancellationToken)
|
|
{
|
|
return AutomaticProgressReporter.Create(connection, message, progressReporterTimeSpan, cancellationToken);
|
|
}
|
|
} |