Example #1
0
        public static void CreateClient_Creates_Client_From_Service_Provider_With_Locations()
        {
            // Arrange
            var options = new UserStoreOptions()
            {
                ServiceUri      = new Uri("https://cosmosdb.azure.local"),
                AccessKey       = "bpfYUKmfV0arChaIPI3hU3+bn3w=",
                DatabaseName    = "my-database",
                CollectionName  = "my-collection",
                CurrentLocation = "UK South",
            };

            var services = new ServiceCollection()
                           .AddHttpClient()
                           .AddSingleton(Options.Create(new JsonOptions()))
                           .AddSingleton(options);

            using var serviceProvider = services.BuildServiceProvider();

            // Act
            using CosmosClient actual = DocumentHelpers.CreateClient(serviceProvider);

            // Assert
            actual.ShouldNotBeNull();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentCollectionInitializer"/> class.
 /// </summary>
 /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
 /// <param name="logger">The <see cref="ILogger{DocumentCollectionInitializer}"/> to use.</param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="options"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// <paramref name="options"/> is invalid.
 /// </exception>
 public DocumentCollectionInitializer(
     UserStoreOptions options,
     ILogger <DocumentCollectionInitializer> logger)
 {
     _existingContainers = new ConcurrentDictionary <string, bool>();
     _logger             = logger;
     _databaseName       = options.DatabaseName;
 }
 public static void ConfigureUserContext(this ModelBuilder modelBuilder,
                                         UserStoreOptions storeOptions)
 {
     modelBuilder.Entity <CustomUser>(customUser =>
     {
         customUser.ToTable(storeOptions.CustomUser);
     });
 }
 public UserDbContext(DbContextOptions <UserDbContext> options, UserStoreOptions storeOptions)
     : base(options)
 {
     if (storeOptions == null)
     {
         throw new ArgumentNullException(nameof(storeOptions));
     }
     _storeOptions = storeOptions;
 }
Example #5
0
        public static void CreateClient_Throws_If_HttpClientFactory_Is_Null()
        {
            // Arrange
            var options = new UserStoreOptions();
            IHttpClientFactory?httpClientFactory = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>("httpClientFactory", () => DocumentHelpers.CreateClient(options, httpClientFactory !));
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentService"/> class.
 /// </summary>
 /// <param name="client">The <see cref="IDocumentClient"/> to use.</param>
 /// <param name="initializer">The <see cref="IDocumentCollectionInitializer"/> to use.</param>
 /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
 /// <param name="logger">The <see cref="ILogger{DocumentService}"/> to use.</param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="client"/> or <paramref name="options"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// <paramref name="options"/> is invalid.
 /// </exception>
 public DocumentService(
     CosmosClient client,
     IDocumentCollectionInitializer initializer,
     UserStoreOptions options,
     ILogger <DocumentService> logger)
 {
     _client      = client ?? throw new ArgumentNullException(nameof(client));
     _initializer = initializer ?? throw new ArgumentNullException(nameof(initializer));
     _options     = options;
     _logger      = logger;
 }
Example #7
0
        /// <summary>
        /// Creates a new instance of an <see cref="IDocumentClient"/> implementation.
        /// </summary>
        /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
        /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> to use.</param>
        /// <param name="serializer">The optional <see cref="CosmosSerializer"/> to use.</param>
        /// <returns>
        /// The created instance of <see cref="IDocumentClient"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> or <paramref name="httpClientFactory"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="options"/> is invalid.
        /// </exception>
        internal static CosmosClient CreateClient(
            UserStoreOptions options,
            IHttpClientFactory httpClientFactory,
            CosmosSerializer?serializer = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (httpClientFactory == null)
            {
                throw new ArgumentNullException(nameof(httpClientFactory));
            }

            if (options.ServiceUri == null)
            {
                throw new ArgumentException("No DocumentDB URI is configured.", nameof(options));
            }

            if (!options.ServiceUri.IsAbsoluteUri)
            {
                throw new ArgumentException("The configured DocumentDB URI is as it is not an absolute URI.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.AccessKey))
            {
                throw new ArgumentException("No DocumentDB access key is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.DatabaseName))
            {
                throw new ArgumentException("No DocumentDB database name is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.CollectionName))
            {
                throw new ArgumentException("No DocumentDB collection name is configured.", nameof(options));
            }

            var builder = new CosmosClientBuilder(options.ServiceUri.AbsoluteUri, options.AccessKey)
                          .WithApplicationName("london-travel")
                          .WithCustomSerializer(serializer)
                          .WithHttpClientFactory(httpClientFactory.CreateClient)
                          .WithRequestTimeout(TimeSpan.FromSeconds(15));

            if (!string.IsNullOrEmpty(options.CurrentLocation))
            {
                builder = builder.WithApplicationRegion(options.CurrentLocation);
            }

            return(builder.Build());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentCollectionInitializer"/> class.
        /// </summary>
        /// <param name="telemetry">The <see cref="TelemetryClient"/> to use.</param>
        /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
        /// <param name="logger">The <see cref="ILogger{DocumentCollectionInitializer}"/> to use.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="options"/> is invalid.
        /// </exception>
        public DocumentCollectionInitializer(
            TelemetryClient telemetry,
            UserStoreOptions options,
            ILogger <DocumentCollectionInitializer> logger)
        {
            _client = DocumentHelpers.CreateClient(options);
            _existingCollections = new ConcurrentDictionary <string, bool>();

            _telemetry    = telemetry;
            _logger       = logger;
            _databaseName = options.DatabaseName;
        }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentClientWrapper"/> class.
        /// </summary>
        /// <param name="initializer">The <see cref="IDocumentCollectionInitializer"/> to use.</param>
        /// <param name="telemetry">The <see cref="TelemetryClient"/> to use.</param>
        /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
        /// <param name="logger">The <see cref="ILogger{DocumentClientWrapper}"/> to use.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="options"/> is invalid.
        /// </exception>
        public DocumentClientWrapper(
            IDocumentCollectionInitializer initializer,
            TelemetryClient telemetry,
            UserStoreOptions options,
            ILogger <DocumentClientWrapper> logger)
        {
            _initializer = initializer ?? throw new ArgumentNullException(nameof(initializer));
            _client      = DocumentHelpers.CreateClient(options);

            _telemetry = telemetry;
            _options   = options;
            _logger    = logger;
        }
        public static void CreateClient_Throws_If_Options_Has_Relative_Service_Endpoint()
        {
            // Arrange
            var options = new UserStoreOptions()
            {
                ServiceUri     = new Uri("/", UriKind.Relative),
                AccessKey      = "bpfYUKmfV0arChaIPI3hU3+bn3w=",
                DatabaseName   = "my-database",
                CollectionName = "my-collection",
            };

            // Act and Assert
            Assert.Throws <ArgumentException>("options", () => DocumentHelpers.CreateClient(options));
        }
        public static void CreateClient_Throws_If_Options_Has_No_Database_Name(string databaseName)
        {
            // Arrange
            var options = new UserStoreOptions()
            {
                ServiceUri     = new Uri("https://cosmosdb.azure.local"),
                AccessKey      = "bpfYUKmfV0arChaIPI3hU3+bn3w=",
                DatabaseName   = databaseName,
                CollectionName = "my-collection",
            };

            // Act and Assert
            Assert.Throws <ArgumentException>("options", () => DocumentHelpers.CreateClient(options));
        }
        public static void CreateClient_Throws_If_Options_Has_No_Access_Key(string accessKey)
        {
            // Arrange
            var options = new UserStoreOptions()
            {
                ServiceUri     = new Uri("https://cosmosdb.azure.local"),
                AccessKey      = accessKey,
                DatabaseName   = "my-database",
                CollectionName = "my-collection",
            };

            // Act and Assert
            Assert.Throws <ArgumentException>("options", () => DocumentHelpers.CreateClient(options));
        }
Example #13
0
        public static void CreateClient_Throws_If_Options_Has_No_Service_Endpoint()
        {
            // Arrange
            var options = new UserStoreOptions()
            {
                ServiceUri     = null,
                AccessKey      = "bpfYUKmfV0arChaIPI3hU3+bn3w=",
                DatabaseName   = "my-database",
                CollectionName = "my-collection",
            };

            var httpClientFactory = Mock.Of <IHttpClientFactory>();

            // Act and Assert
            Assert.Throws <ArgumentException>("options", () => DocumentHelpers.CreateClient(options, httpClientFactory));
        }
        public InMemoryUserStore(IOptionsMonitor <UserStoreOptions> optionsMonitor)
        {
            _options = optionsMonitor.Get(nameof(InMemoryUserStore));

            optionsMonitor.OnChange((o, n) =>
            {
                if (n == nameof(InMemoryUserStore))
                {
                    _options = o;
                }
            });

            foreach (var user in _options.Users)
            {
                _store.AddOrUpdate(user.UserName, i => user, (i, u) => u);
            }
        }
Example #15
0
        /// <summary>
        /// Creates a new instance of the <see cref="DocumentClient"/> class.
        /// </summary>
        /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
        /// <returns>
        /// The created instance of <see cref="DocumentClient"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="options"/> is invalid.
        /// </exception>
        internal static DocumentClient CreateClient(UserStoreOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.ServiceUri == null)
            {
                throw new ArgumentException("No DocumentDB URI is configured.", nameof(options));
            }

            if (!options.ServiceUri.IsAbsoluteUri)
            {
                throw new ArgumentException("The configured DocumentDB URI is as it is not an absolute URI.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.AccessKey))
            {
                throw new ArgumentException("No DocumentDB access key is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.DatabaseName))
            {
                throw new ArgumentException("No DocumentDB database name is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.CollectionName))
            {
                throw new ArgumentException("No DocumentDB collection name is configured.", nameof(options));
            }

            ConnectionPolicy connectionPolicy = null;

            if (options.PreferredLocations?.Count > 0)
            {
                connectionPolicy = new ConnectionPolicy();

                foreach (string location in options.PreferredLocations)
                {
                    connectionPolicy.PreferredLocations.Add(location);
                }
            }

            return(new DocumentClient(options.ServiceUri, options.AccessKey, connectionPolicy));
        }
Example #16
0
        /// <summary>
        /// Creates a new instance of an <see cref="IDocumentClient"/> implementation.
        /// </summary>
        /// <param name="options">The <see cref="UserStoreOptions"/> to use.</param>
        /// <param name="serializer">The optional <see cref="CosmosSerializer"/> to use.</param>
        /// <returns>
        /// The created instance of <see cref="IDocumentClient"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="options"/> is invalid.
        /// </exception>
        internal static CosmosClient CreateClient(UserStoreOptions options, CosmosSerializer?serializer = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.ServiceUri == null)
            {
                throw new ArgumentException("No DocumentDB URI is configured.", nameof(options));
            }

            if (!options.ServiceUri.IsAbsoluteUri)
            {
                throw new ArgumentException("The configured DocumentDB URI is as it is not an absolute URI.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.AccessKey))
            {
                throw new ArgumentException("No DocumentDB access key is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.DatabaseName))
            {
                throw new ArgumentException("No DocumentDB database name is configured.", nameof(options));
            }

            if (string.IsNullOrEmpty(options.CollectionName))
            {
                throw new ArgumentException("No DocumentDB collection name is configured.", nameof(options));
            }

            var cosmosOptions = new CosmosClientOptions()
            {
                ApplicationName = "london-travel",
                RequestTimeout  = TimeSpan.FromSeconds(15),
                Serializer      = serializer,
            };

            if (!string.IsNullOrEmpty(options.CurrentLocation))
            {
                cosmosOptions.ApplicationRegion = options.CurrentLocation;
            }

            return(new CosmosClient(options.ServiceUri.AbsoluteUri, options.AccessKey, cosmosOptions));
        }
Example #17
0
        public static IIdentityServerBuilder AddCustomUserStore(
            this IIdentityServerBuilder builder,
            Action <DbContextOptionsBuilder> dbContextOptionsAction = null,
            Action <UserStoreOptions> storeOptionsAction            = null)
        {
            builder.Services.AddDbContext <UserDbContext>(dbContextOptionsAction);
            builder.Services.AddScoped <IUserDbContext, UserDbContext>();

            builder.Services.AddTransient <IUserStore, UserStore>();
            builder.AddProfileService <CustomProfileService>();
            builder.AddResourceOwnerValidator <CustomResourceOwnerPasswordValidator>();

            builder.AddExtensionGrantValidator <GoogleAuthValidator>();
            builder.AddExtensionGrantValidator <FacebookAuthValidator>();

            var options = new UserStoreOptions();

            storeOptionsAction?.Invoke(options);
            builder.Services.AddSingleton(options);

            return(builder);
        }
 public InMemoryDocumentStore(UserStoreOptions options)
 {
     _collections   = new Dictionary <string, DocumentCollection>();
     CollectionName = options.CollectionName;
 }