using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace SimpleOTP.DependencyInjection;
///
/// Extension methods for the One-Time Password service.
///
public static class OtpServiceExtensions
{
///
/// Adds the One-Time Password service to the service collection.
///
/// The service collection.
/// The issuer/application/service name.
/// A reference to this instance after the operation has completed.
public static IServiceCollection AddAuthenticator(this IServiceCollection services, string issuerName) =>
AddAuthenticator(services, issuerName);
///
/// Adds the One-Time Password service to the service collection.
///
/// The service collection.
/// The issuer/application/service name.
/// The configuration for the One-Time Password service.
/// A reference to this instance after the operation has completed.
public static IServiceCollection AddAuthenticator(this IServiceCollection services, string issuerName, Action? configure = null)
{
OtpOptions options = new()
{
Issuer = issuerName
};
configure?.Invoke(options);
services.AddTransient(_ => new OtpService(options));
return services;
}
///
/// Adds the One-Time Password service to the service collection.
///
/// The service collection.
/// The configuration for the One-Time Password service.
/// A reference to this instance after the operation has completed.
public static IServiceCollection AddAuthenticator(this IServiceCollection services, IConfiguration configuration) =>
AddAuthenticator(services, configuration);
///
/// Adds the One-Time Password service to the service collection.
///
/// The service collection.
/// The configuration for the One-Time Password service.
/// The configuration for the One-Time Password service.
/// A reference to this instance after the operation has completed.
public static IServiceCollection AddAuthenticator(this IServiceCollection services, IConfiguration configuration, Action? configure = null)
{
OtpOptions? options = GetOptionsFromConfiguration(configuration);
if (options is null)
return services;
configure?.Invoke(options);
services.AddTransient(_ => new OtpService(options));
return services;
}
private static OtpOptions? GetOptionsFromConfiguration(IConfiguration configuration)
{
OtpServiceConfig config = new();
IConfigurationSection configSection = configuration.GetSection("Authenticator");
if (!configSection.Exists() || string.IsNullOrWhiteSpace(configuration["Authenticator:Issuer"]))
return null;
configSection.Bind(config);
OtpOptions options = new()
{
Issuer = config.Issuer,
Algorithm = config.Algorithm,
Type = config.Type,
Digits = config.Digits,
Period = config.Period,
IssuerDomain = config.IssuerDomain,
ToleranceSpan = (config.ToleranceSpan.Behind, config.ToleranceSpan.Ahead),
UriFormat = config.UriFormat
};
options.UriFormat |= config.MinimalUri ? OtpUriFormat.Minimal : OtpUriFormat.Full;
foreach (KeyValuePair pair in config.CustomProperties)
options.CustomProperties.Add(pair.Key, pair.Value);
return options;
}
}