public KeyRingProvider(IKeyManager keyManager, KeyManagementOptions keyManagementOptions, IServiceProvider services)
 {
     _keyManagementOptions = new KeyManagementOptions(keyManagementOptions); // clone so new instance is immutable
     _keyManager = keyManager;
     _cacheableKeyRingProvider = services?.GetService<ICacheableKeyRingProvider>() ?? this;
     _logger = services?.GetLogger<KeyRingProvider>();
     _defaultKeyResolver = services?.GetService<IDefaultKeyResolver>()
         ?? new DefaultKeyResolver(_keyManagementOptions.KeyPropagationWindow, _keyManagementOptions.MaxServerClockSkew, services);
 }
Example #2
0
 public KeyRingProvider(IKeyManager keyManager, KeyManagementOptions keyManagementOptions, IServiceProvider services)
 {
     _keyManagementOptions     = new KeyManagementOptions(keyManagementOptions); // clone so new instance is immutable
     _keyManager               = keyManager;
     _cacheableKeyRingProvider = services?.GetService <ICacheableKeyRingProvider>() ?? this;
     _logger             = services?.GetLogger <KeyRingProvider>();
     _defaultKeyResolver = services?.GetService <IDefaultKeyResolver>()
                           ?? new DefaultKeyResolver(_keyManagementOptions.KeyPropagationWindow, _keyManagementOptions.MaxServerClockSkew, services);
 }
Example #3
0
 public KeyRingProvider(
     IKeyManager keyManager,
     IOptions <KeyManagementOptions> keyManagementOptions,
     IDefaultKeyResolver defaultKeyResolver)
     : this(
         keyManager,
         keyManagementOptions,
         defaultKeyResolver,
         NullLoggerFactory.Instance)
 {
 }
 public KeyRingProvider(
     IKeyManager keyManager,
     IOptions <KeyRotationOptions> keyManagementOptions, // options change from original KeyManagementOptions
     IDefaultKeyResolver defaultKeyResolver)
     : this(
         keyManager,
         keyManagementOptions,
         defaultKeyResolver,
         NullLoggerFactory.Instance)
 {
 }
Example #5
0
 public KeyRingProvider(
     IKeyManager keyManager,
     IOptions <KeyManagementOptions> keyManagementOptions,
     IDefaultKeyResolver defaultKeyResolver,
     ILoggerFactory loggerFactory)
 {
     _keyManagementOptions    = new KeyManagementOptions(keyManagementOptions.Value); // clone so new instance is immutable
     _keyManager              = keyManager;
     CacheableKeyRingProvider = this;
     _defaultKeyResolver      = defaultKeyResolver;
     _logger = loggerFactory.CreateLogger <KeyRingProvider>();
 }
Example #6
0
        public KeyRingProvider(
            IKeyManager keyManager,
            IOptions <KeyManagementOptions> keyManagementOptions,
            IDefaultKeyResolver defaultKeyResolver,
            ILoggerFactory loggerFactory)
        {
            _keyManagementOptions    = new KeyManagementOptions(keyManagementOptions.Value); // clone so new instance is immutable
            _keyManager              = keyManager;
            CacheableKeyRingProvider = this;
            _defaultKeyResolver      = defaultKeyResolver;
            _logger = loggerFactory.CreateLogger <KeyRingProvider>();

            // We will automatically refresh any unknown keys for 2 minutes see https://github.com/dotnet/aspnetcore/issues/3975
            AutoRefreshWindowEnd = DateTime.UtcNow.AddMinutes(2);
        }
Example #7
0
 public static DefaultKeyResolution ResolveDefaultKeyPolicy(this IDefaultKeyResolver resolver, string now, params IKey[] allKeys)
 {
     return(resolver.ResolveDefaultKeyPolicy(DateTimeOffset.ParseExact(now, "u", CultureInfo.InvariantCulture), (IEnumerable <IKey>)allKeys));
 }
Example #8
0
        /// <summary>
        /// Converts to keycollection.
        /// </summary>
        /// <param name="keys">The keys.</param>
        /// <param name="defaultKeyResolver">The default key resolver.</param>
        /// <returns></returns>
        public static PageResponse <Key> ToPageResponse(this IEnumerable <IKey> keys, IDefaultKeyResolver defaultKeyResolver)
        {
            var defaultKeyId = defaultKeyResolver.ResolveDefaultKeyPolicy(DateTimeOffset.UtcNow, keys).DefaultKey?.KeyId;

            return(new PageResponse <Key>
            {
                Count = keys.Count(),
                Items = keys.OrderBy(k => k.CreationDate)
                        .Select(k => new Key
                {
                    ActivationDate = k.ActivationDate,
                    CreationDate = k.CreationDate,
                    ExpirationDate = k.ExpirationDate,
                    Id = k.KeyId.ToString(),
                    IsRevoked = k.IsRevoked,
                    IsDefault = k.KeyId == defaultKeyId
                })
            });
        }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="KeyManagerWrapper{T}" /> class.
 /// </summary>
 /// <param name="keyManager">The key manager.</param>
 /// <param name="defaultKeyResolver">The default key resolver.</param>
 /// <param name="providerClient">The provider client.</param>
 /// <exception cref="ArgumentNullException">
 /// keyManager
 /// or
 /// defaultKeyResolver
 /// or
 /// providerClient
 /// </exception>
 /// <exception cref="ArgumentNullException">keyManager
 /// or
 /// defaultKeyResolver</exception>
 public KeyManagerWrapper(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, IProviderClient providerClient)
 {
     _keyManager         = keyManager ?? throw new ArgumentNullException(nameof(keyManager));
     _defaultKeyResolver = defaultKeyResolver ?? throw new ArgumentNullException(nameof(defaultKeyResolver));
     _providerClient     = providerClient ?? throw new ArgumentNullException(nameof(providerClient));
 }
Example #10
0
        private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions = null)
        {
            var mockEncryptorFactory = new Mock <IAuthenticatedEncryptorFactory>();

            mockEncryptorFactory.Setup(m => m.CreateEncryptorInstance(It.IsAny <IKey>())).Returns(new Mock <IAuthenticatedEncryptor>().Object);
            keyManagementOptions = keyManagementOptions ?? new KeyManagementOptions();
            keyManagementOptions.AuthenticatedEncryptorFactories.Add(mockEncryptorFactory.Object);

            return(new KeyRingProvider(
                       keyManager: keyManager,
                       keyManagementOptions: Options.Create(keyManagementOptions),
                       defaultKeyResolver: defaultKeyResolver,
                       loggerFactory: NullLoggerFactory.Instance));
        }
 private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions= null)
 {
     var serviceCollection = new ServiceCollection();
     serviceCollection.AddInstance<IDefaultKeyResolver>(defaultKeyResolver);
     return new KeyRingProvider(
         keyManager: keyManager,
         keyManagementOptions: keyManagementOptions,
         services: serviceCollection.BuildServiceProvider());
 }
Example #12
0
        private static ICacheableKeyRingProvider CreateKeyRingProvider(IKeyManager keyManager, IDefaultKeyResolver defaultKeyResolver, KeyManagementOptions keyManagementOptions = null)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IDefaultKeyResolver>(defaultKeyResolver);
            return(new KeyRingProvider(
                       keyManager: keyManager,
                       keyManagementOptions: keyManagementOptions,
                       services: serviceCollection.BuildServiceProvider()));
        }