public static JsonWebKeyManagementBuilder AddJsonWebKeyManagement <T>(this IServiceCollection services, Action <IServiceProvider, JsonWebKeyPairManagerOptions> optionsAction = null) where T : class, IJsonWebKeyPairFactory
        {
            services.AddSingleton(provider =>
            {
                var options = new JsonWebKeyPairManagerOptions();

                optionsAction?.Invoke(provider, options);

                return(options);
            });

            services.AddControllers().AddApplicationPart(typeof(KeyController).Assembly).AddControllersAsServices();
            services.AddSingleton <IJsonWebKeyPairFactory, T>();
            services.AddSingleton <JsonWebKeyPairManagerService>();
            services.AddHostedService <JsonWebKeyPairRotationService>();
            services.AddSingleton <ISigningCredentialStore, SigningCredentialStore>();
            services.AddSingleton <IValidationKeysStore, ValidationKeysStore>();

            return(new JsonWebKeyManagementBuilder(services));
        }
Esempio n. 2
0
        public JsonWebKeyPairManagerService(IJsonWebKeyPairFactory jsonWebKeyPairFactory, IJsonWebKeyPairStore jsonWebKeyPairStore, IServiceProvider serviceProvider)
        {
            _jsonWebKeyPairFactory = jsonWebKeyPairFactory;
            _jsonWebKeyPairStore   = jsonWebKeyPairStore;
            _options = serviceProvider.GetService <JsonWebKeyPairManagerOptions>();
            _logger  = serviceProvider.GetService <ILogger <JsonWebKeyPairManagerService> >();

            ReloadFromStore();

            if (!_keyPairs.ContainsKey("current"))
            {
                _logger?.LogDebug("Generating non-existent \"current\" key.");
                _keyPairs.Add("current", jsonWebKeyPairFactory.Create());
            }

            if (!_keyPairs.ContainsKey("next"))
            {
                _logger?.LogDebug("Generating non-existent \"next\" key.");
                _keyPairs.Add("next", jsonWebKeyPairFactory.Create());
            }

            SaveToStore();
        }