// Copyright (c) Microsoft. All rights reserved.
//
// Licensed under the MIT license.
using System;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Protocol.Plugins;
using NuGetCredentialProvider.Logging;
using NuGetCredentialProvider.Util;
namespace NuGetCredentialProvider.CredentialProviders
{
///
/// Represents a base class for credential providers.
///
public abstract class CredentialProviderBase : ICredentialProvider
{
///
/// A for when credentials could not be retrieved.
///
protected static readonly GetAuthenticationCredentialsResponse NotFoundGetAuthenticationCredentialsResponse = new GetAuthenticationCredentialsResponse(
username: null,
password: null,
message: null,
authenticationTypes: null,
responseCode: MessageResponseCode.NotFound);
///
/// Initializes a new instance of the class.
///
/// A to use for logging.
protected CredentialProviderBase(ILogger logger)
{
Logger = logger;
}
public virtual bool IsCachable { get { return true; } }
protected abstract string LoggingName { get; }
///
/// Gets a to use for logging.
///
protected ILogger Logger { get; }
///
public abstract Task CanProvideCredentialsAsync(Uri uri);
///
public virtual void Dispose()
{
}
///
public abstract Task HandleRequestAsync(GetAuthenticationCredentialsRequest request, CancellationToken cancellationToken);
protected void Error(string message)
{
Logger.Error($"{LoggingName} - {message}");
}
protected void Warning(string message)
{
Logger.Warning($"{LoggingName} - {message}");
}
protected void Info(string message)
{
Logger.Info($"{LoggingName} - {message}");
}
protected void Verbose(string message)
{
Logger.Verbose($"{LoggingName} - {message}");
}
}
}