internal static IServiceCollection AddDefaultTotpService(this IServiceCollection services, Action <TotpOptions> configure = null) { var serviceProvider = services.BuildServiceProvider(); var totpSection = serviceProvider.GetRequiredService <IConfiguration>().GetSection(TotpOptions.Name); var totpOptions = new TotpOptions { Services = services, CodeDuration = totpSection.GetValue <int?>(nameof(TotpOptions.CodeDuration)) ?? TotpOptions.DefaultCodeDuration, CodeLength = totpSection.GetValue <int?>(nameof(TotpOptions.CodeLength)) ?? TotpOptions.DefaultCodeLength, EnableDeveloperTotp = totpSection.GetValue <bool>(nameof(TotpOptions.EnableDeveloperTotp)) }; configure?.Invoke(totpOptions); totpOptions.Services = null; services.TryAddSingleton(totpOptions); services.TryAddTransient <IPushNotificationService, PushNotificationServiceNoop>(); services.TryAddTransient <ITotpService, TotpService>(); services.TryAddSingleton(new Rfc6238AuthenticationService(totpOptions.Timestep, totpOptions.CodeLength)); if (totpOptions.EnableDeveloperTotp) { var implementation = services.LastOrDefault(x => x.ServiceType == typeof(ITotpService))?.ImplementationType; if (implementation != null) { var decoratorType = typeof(DeveloperTotpService); services.TryAddTransient(implementation); services.AddTransient(typeof(ITotpService), decoratorType); } } return(services); }
internal static IServiceCollection AddDefaultTotpService(this IServiceCollection services, Action <TotpOptions> configure = null) { services.TryAddTransient <ITotpService, TotpService>(); services.TryAddSingleton(serviceProvider => { var options = new TotpOptions { TokenDuration = serviceProvider.GetRequiredService <IConfiguration>().GetSection(TotpOptions.Name).GetValue <int?>(nameof(TotpOptions.TokenDuration)) }; configure?.Invoke(options); return(new Rfc6238AuthenticationService(options)); }); return(services); }
/// <summary> /// Creates a new instance of <see cref="OtpAuthenticateExtensionGrantValidator"/>. /// </summary> /// <param name="validator">Validates an access token.</param> /// <param name="userManager">Provides the APIs for managing user in a persistence store.</param> /// <param name="totpOptions">Configuration used in <see cref="System.Security.Rfc6238AuthenticationService"/> service.</param> /// <param name="identityMessageDescriber">Provides an extensibility point for altering localizing used inside the package.</param> /// <param name="totpService">Used to generate, send and verify time based one time passwords.</param> public OtpAuthenticateExtensionGrantValidator( ITokenValidator validator, UserManager <User> userManager, TotpOptions totpOptions, IdentityMessageDescriber identityMessageDescriber, ITotpService totpService ) { _tokenValidator = validator ?? throw new ArgumentNullException(nameof(validator)); _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager)); _identityMessageDescriber = identityMessageDescriber ?? throw new ArgumentNullException(nameof(identityMessageDescriber)); _totpService = totpService ?? throw new ArgumentNullException(nameof(totpService)); _rfc6238AuthenticationService = new Rfc6238AuthenticationService(totpOptions.Timestep, totpOptions.CodeLength); }
/// <summary> /// Creates a new instance of <see cref="Rfc6238AuthenticationService"/>. /// </summary> public Rfc6238AuthenticationService(TotpOptions options) { _options = options ?? throw new ArgumentNullException(nameof(options)); if (_options.Timestep.HasValue) { var timestep = _options.Timestep.Value; if (timestep < _MIN_TIMESTEP) { timestep = _MIN_TIMESTEP; } else if (timestep > _MAX_TIMESTEP) { timestep = _MAX_TIMESTEP; } _timestep = TimeSpan.FromMinutes(timestep); } }
public IdentityController(IConfiguration config) { _config = config; _settings = TotpOptions.Construct(_config); }