Skip to content

Commit

Permalink
feat: mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Apr 17, 2021
1 parent daf03c5 commit b1b1906
Show file tree
Hide file tree
Showing 34 changed files with 826 additions and 126 deletions.
15 changes: 15 additions & 0 deletions TheIdServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.IdentityServer.R
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aguacongas.IdentityServer.MongoDb.Store", "src\IdentityServer\Aguacongas.IdentityServer.MongoDb.Store\Aguacongas.IdentityServer.MongoDb.Store.csproj", "{40AFC1C5-8A1A-488F-9CDA-F2D29BE0BAEF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aguacongas.IdentityServer.MongoDb.Store.Test", "test\Aguacongas.IdentityServer.MongoDb.Store.Test\Aguacongas.IdentityServer.MongoDb.Store.Test.csproj", "{F24C3E47-0279-4A83-9AB5-75D67F628102}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1013,6 +1015,18 @@ Global
{40AFC1C5-8A1A-488F-9CDA-F2D29BE0BAEF}.Release|x64.Build.0 = Release|Any CPU
{40AFC1C5-8A1A-488F-9CDA-F2D29BE0BAEF}.Release|x86.ActiveCfg = Release|Any CPU
{40AFC1C5-8A1A-488F-9CDA-F2D29BE0BAEF}.Release|x86.Build.0 = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|x64.ActiveCfg = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|x64.Build.0 = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|x86.ActiveCfg = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Debug|x86.Build.0 = Debug|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|Any CPU.Build.0 = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|x64.ActiveCfg = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|x64.Build.0 = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|x86.ActiveCfg = Release|Any CPU
{F24C3E47-0279-4A83-9AB5-75D67F628102}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1091,6 +1105,7 @@ Global
{D887CCE0-A999-4CE8-92B1-AE83587CDC2F} = {DF368D6C-D11F-49F9-BF41-090AE1D10C6D}
{C1560F0F-7B46-4DF1-AC04-01AF1E884DDB} = {DE50F426-4409-4573-8502-93364ED12E0C}
{40AFC1C5-8A1A-488F-9CDA-F2D29BE0BAEF} = {DF368D6C-D11F-49F9-BF41-090AE1D10C6D}
{F24C3E47-0279-4A83-9AB5-75D67F628102} = {DE50F426-4409-4573-8502-93364ED12E0C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5283BE0B-F6F2-4458-B12F-64C78CFF8CBA}
Expand Down
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ branches:
- /release\/*/
image:
- Visual Studio 2019 Preview
services:
- mongodb
environment:
GH_TOKEN:
secure: 0NJdORJRFjpB0dwUYv7bVNsbkldkoBhnvWik/CTOwAF/k9kP+/uTWMFnDcpEpt8E
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "5.0.200"
"version": "5.0.202"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@ public static IdentityBuilder AddTheIdServerStores(this IdentityBuilder builder,
builder.Services.AddTheIdServerStores(builder.UserType, builder.RoleType, getHttpClient);
return builder;
}

/// <summary>
/// Adds the identifier server stores.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns></returns>
public static IdentityBuilder AddTheIdServerStores(this IdentityBuilder builder)
{
builder.Services.AddTheIdServerStores(builder.UserType, builder.RoleType);
return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace Microsoft.Extensions.DependencyInjection
public static class ServiceCollectionExtentsions
{
public static IServiceCollection AddTheIdServerStores(this IServiceCollection services, Type userType, Type roleType, Func<IServiceProvider, Task<HttpClient>> getHttpClient)
{
services.AddTheIdServerStores(userType, roleType);
services.AddIdentityServer4AdminHttpStores(getHttpClient);

return services;
}

public static void AddTheIdServerStores(this IServiceCollection services, Type userType, Type roleType)
{
var userOnlyStoreType = typeof(UserOnlyStore<>).MakeGenericType(userType);

Expand All @@ -33,10 +41,6 @@ public static IServiceCollection AddTheIdServerStores(this IServiceCollection se
services.TryAddScoped(typeof(IUserStore<>)
.MakeGenericType(userType), provider => provider.CreateUserOnlyStore(userOnlyStoreType));
}

services.AddIdentityServer4AdminHttpStores(getHttpClient);

return services;
}
}
}
1 change: 1 addition & 0 deletions src/Aguacongas.TheIdServer/Aguacongas.TheIdServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<ProjectReference Include="..\IdentityServer\Aguacongas.IdentityServer.Admin\Aguacongas.IdentityServer.Admin.csproj" />
<ProjectReference Include="..\IdentityServer\Aguacongas.IdentityServer.EntityFramework.Store\Aguacongas.IdentityServer.EntityFramework.Store.csproj" />
<ProjectReference Include="..\IdentityServer\Aguacongas.IdentityServer.Http.Store\Aguacongas.IdentityServer.Http.Store.csproj" />
<ProjectReference Include="..\IdentityServer\Aguacongas.IdentityServer.MongoDb.Store\Aguacongas.IdentityServer.MongoDb.Store.csproj" />
<ProjectReference Include="..\IdentityServer\Aguacongas.IdentityServer.RavenDb.Store\Aguacongas.IdentityServer.RavenDb.Store.csproj" />
<ProjectReference Include="..\IdentityServer\Migrations\Aguacongas.TheIdServer.Migrations.MySql\Aguacongas.TheIdServer.Migrations.MySql.csproj" />
<ProjectReference Include="..\IdentityServer\Migrations\Aguacongas.TheIdServer.Migrations.Oracle\Aguacongas.TheIdServer.Migrations.Oracle.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static IDataProtectionBuilder ConfigureDataProtection(this IDataProtectio
case StorageKind.RavenDb:
builder.PersistKeysToRavenDb();
break;
case StorageKind.MongoDb:
builder.PersistKeysToMongoDb();
break;
case StorageKind.FileSystem:
builder.PersistKeysToFileSystem(new DirectoryInfo(dataProtectionsOptions.StorageConnectionString));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public static IIdentityServerBuilder ConfigureKey(this IIdentityServerBuilder id
builder.PersistKeysToDbContext<OperationalDbContext>();
break;
case StorageKind.RavenDb:
builder.PersistKeysToRavenDb<DocumentSessionWrapper>();
builder.PersistKeysToRavenDb();
break;
case StorageKind.MongoDb:
builder.PersistKeysToMongoDb();
break;
case StorageKind.FileSystem:
builder.PersistKeysToFileSystem(new DirectoryInfo(dataProtectionsOptions.StorageConnectionString));
Expand Down
3 changes: 2 additions & 1 deletion src/Aguacongas.TheIdServer/Models/DataProtectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum StorageKind
AzureStorage,
FileSystem,
Registry,
RavenDb
RavenDb,
MongoDb
}

public enum KeyProtectionKind
Expand Down
3 changes: 2 additions & 1 deletion src/Aguacongas.TheIdServer/Models/DbTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum DbTypes
MySql,
Oracle,
PostgreSQL,
RavenDb
RavenDb,
MongoDb
}
}
2 changes: 1 addition & 1 deletion src/Aguacongas.TheIdServer/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void EnsureSeedData(IConfiguration configuration)
using var scope = serviceProvider.CreateScope();

var dbType = configuration.GetValue<DbTypes>("DbType");
if (dbType != DbTypes.InMemory && dbType != DbTypes.RavenDb)
if (dbType != DbTypes.InMemory && dbType != DbTypes.RavenDb && dbType != DbTypes.MongoDb)
{
var configContext = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
configContext.Database.Migrate();
Expand Down
24 changes: 22 additions & 2 deletions src/Aguacongas.TheIdServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ void configureOptions(IdentityServerOptions options)
mvcBuilder.AddIdentityServerAdmin<ApplicationUser, RavenDbStore.SchemeDefinition>()
.AddRavenDbStore();
}
else if (DbType == DbTypes.MongoDb)
{
mvcBuilder.AddIdentityServerAdmin<ApplicationUser, Auth.SchemeDefinition>()
.AddMongoDbStore();
}
else
{
mvcBuilder.AddIdentityServerAdmin<ApplicationUser, SchemeDefinition>()
Expand Down Expand Up @@ -296,7 +301,12 @@ public void Configure(IApplicationBuilder app)
endpoints.MapFallbackToPage("/_Host");
});

if (isProxy)
LoadDynamicConfiguration(app, isProxy);
}

private void LoadDynamicConfiguration(IApplicationBuilder app, bool isProxy)
{
if (isProxy || DbType == DbTypes.MongoDb)
{
app.LoadDynamicAuthenticationConfiguration<Auth.SchemeDefinition>();
return;
Expand Down Expand Up @@ -446,6 +456,16 @@ private void AddDefaultServices(IServiceCollection services)

identityBuilder.AddRavenDbStores();
}
if (DbType == DbTypes.MongoDb)
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddTransient<ISchemeChangeSubscriber, SchemeChangeSubscriber<Auth.SchemeDefinition>>()
.AddIdentityServer4AdminMongoDbStores(connectionString)
.AddConfigurationStores()
.AddOperationalStores();

identityBuilder.AddTheIdServerStores();
}
else
{
services.AddTransient<ISchemeChangeSubscriber, SchemeChangeSubscriber<SchemeDefinition>>()
Expand Down Expand Up @@ -487,7 +507,7 @@ private void ConfigureInitialData(IApplicationBuilder app)
{
var dbType = Configuration.GetValue<DbTypes>("DbType");
if (Configuration.GetValue<bool>("Migrate") &&
dbType != DbTypes.InMemory && dbType != DbTypes.RavenDb)
dbType != DbTypes.InMemory && dbType != DbTypes.RavenDb && dbType != DbTypes.MongoDb)
{
using var scope = app.ApplicationServices.CreateScope();
var configContext = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

<ItemGroup>
<PackageReference Include="Aguacongas.AspNetCore.Authentication.EntityFramework" Version="4.0.3" />
<PackageReference Include="Community.OData.Linq" Version="1.4.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.5" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.9" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.10.0" />
<PackageReference Include="MongoDB.Driver" Version="2.12.2" />
<PackageReference Include="RavenDB.Client" Version="5.1.5" />
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public static IDataProtectionBuilder PersistKeysToMongoDb(this IDataProtectionBu
options.XmlRepository = new mongoDb.MongoDbXmlRepository<mongoDb.DataProtectionKey>(services, loggerFactory);
});
})
.AddTransient(p => getCollection(p))
.AddTransient<mongoDb.MongoCollectionWrapper<mongoDb.DataProtectionKey>>();
.AddTransient(p => new mongoDb.MongoCollectionWrapper<mongoDb.DataProtectionKey>(getCollection(p)));

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using Aguacongas.IdentityServer.KeysRotation.RavenDb;
using Raven.Client.Documents.Session;
using Raven.Client.Documents;
using mongoDb = Aguacongas.IdentityServer.KeysRotation.MongoDb;
using MongoDB.Driver;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -253,7 +255,6 @@ public static IKeyRotationBuilder PersistKeysToDbContext<TContext>(this IKeyRota
/// <summary>
/// Configures the key rotation system to persist keys to a RavenDb datasstore
/// </summary>
/// <typeparam name="TWrapper">The type of the wrapper.</typeparam>
/// <param name="builder">The <see cref="IDataProtectionBuilder" /> instance to modify.</param>
/// <param name="getSession">The get session.</param>
/// <returns>
Expand Down Expand Up @@ -288,6 +289,40 @@ public static IKeyRotationBuilder PersistKeysToRavenDb(this IKeyRotationBuilder
return builder;
}

/// <summary>
/// Configures the key rotation system to persist keys to a MongoDb datasstore
/// </summary>
/// <param name="builder">The <see cref="IDataProtectionBuilder" /> instance to modify.</param>
/// <param name="getSession">The get session.</param>
/// <returns>
/// The value <paramref name="builder" />.
/// </returns>
/// <exception cref="ArgumentNullException">builder</exception>
public static IKeyRotationBuilder PersistKeysToMongoDb(this IKeyRotationBuilder builder, Func<IServiceProvider, IMongoCollection<mongoDb.KeyRotationKey>> getCollection = null)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (getCollection == null)
{
getCollection = p => p.GetRequiredService<IMongoDatabase>().GetCollection<mongoDb.KeyRotationKey>(nameof(mongoDb.KeyRotationKey));
}

builder.Services.AddSingleton<IConfigureOptions<KeyRotationOptions>>(services =>
{
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
return new ConfigureOptions<KeyRotationOptions>(options =>
{
options.XmlRepository = new mongoDb.MongoDbXmlRepository<mongoDb.KeyRotationKey>(services, loggerFactory);
});
})
.AddTransient(p => new mongoDb.MongoCollectionWrapper<mongoDb.KeyRotationKey>(getCollection(p)));

return builder;
}

/// <summary>
/// Configures the key rotation system to persist keys to the specified directory.
/// This path may be on the local machine or may point to a UNC share.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
namespace Aguacongas.IdentityServer.KeysRotation.MongoDb
{
/// <summary>
/// Code first model used by <see cref="EntityFrameworkCoreXmlRepository{TContext}"/>.
/// </summary>
public class DataProtectionKey : IXmlKey
{
/// <summary>
/// The entity identifier of the <see cref="KeyRotationKey"/>.
/// </summary>
public string Id { get; set; }

/// <summary>
/// The friendly name of the <see cref="KeyRotationKey"/>.
/// </summary>
public string FriendlyName { get; set; }

/// <summary>
/// The XML representation of the <see cref="KeyRotationKey"/>.
/// </summary>
public string Xml { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
namespace Aguacongas.IdentityServer.KeysRotation.MongoDb
{
public interface IXmlKey
{
string Id { get; set; }

string Xml { get; set; }
string FriendlyName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
namespace Aguacongas.IdentityServer.KeysRotation.MongoDb
{
public class KeyRotationKey : IXmlKey
{
/// <summary>
/// The entity identifier of the <see cref="KeyRotationKey"/>.
/// </summary>
public string Id { get; set; }

/// <summary>
/// The friendly name of the <see cref="KeyRotationKey"/>.
/// </summary>
public string FriendlyName { get; set; }

/// <summary>
/// The XML representation of the <see cref="KeyRotationKey"/>.
/// </summary>
public string Xml { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2021 @Olivier Lefebvre
using System;
using MongoDB.Driver;

namespace Aguacongas.IdentityServer.KeysRotation.MongoDb
{
public class MongoCollectionWrapper<TKey>
where TKey : IXmlKey
{
public IMongoCollection<TKey> Collection { get; }

public MongoCollectionWrapper(IMongoCollection<TKey> collection)
{
Collection = collection ?? throw new ArgumentNullException(nameof(collection));
}
}
}
Loading

0 comments on commit b1b1906

Please sign in to comment.