Skip to content

Commit

Permalink
✨ Adds an ApiAuthorizationDbContext with exposed TRole and TKey generics
Browse files Browse the repository at this point in the history
  • Loading branch information
akritikos committed Feb 4, 2021
1 parent 3b8be04 commit 3e1f0b9
Show file tree
Hide file tree
Showing 4 changed files with 828 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Configuration.Persistence.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Configuration.Persistence",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Configuration.PersistenceTests", "tests\Configuration.PersistenceTests\Configuration.PersistenceTests.csproj", "{470E9DF1-37DE-4B95-9F32-B2ACEACB1C8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Configuration.Peristence.IdentityServer", "src\Configuration.Peristence.IdentityServer\Configuration.Peristence.IdentityServer.csproj", "{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,13 +59,26 @@ Global
{470E9DF1-37DE-4B95-9F32-B2ACEACB1C8F}.Release|x64.Build.0 = Release|Any CPU
{470E9DF1-37DE-4B95-9F32-B2ACEACB1C8F}.Release|x86.ActiveCfg = Release|Any CPU
{470E9DF1-37DE-4B95-9F32-B2ACEACB1C8F}.Release|x86.Build.0 = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|x64.ActiveCfg = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|x64.Build.0 = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|x86.ActiveCfg = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Debug|x86.Build.0 = Debug|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|Any CPU.Build.0 = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|x64.ActiveCfg = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|x64.Build.0 = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|x86.ActiveCfg = Release|Any CPU
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F48B66E0-9768-46C3-AEFD-E04807D4CC2B} = {52270E06-D3BB-4B8E-A261-377E460B5FD5}
{470E9DF1-37DE-4B95-9F32-B2ACEACB1C8F} = {4FAAB5E4-2D81-4062-90D2-6F68DF746BD2}
{7FE53649-E01F-4E1C-A0A9-AD0984FE9F78} = {52270E06-D3BB-4B8E-A261-377E460B5FD5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51F9BF65-6DFB-4A56-AC1C-EA87B6678C8B}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#nullable disable
namespace Kritikos.Configuration.Peristence.IdentityServer
{
using System;
using System.Threading;
using System.Threading.Tasks;

using IdentityServer4.EntityFramework.Entities;
using IdentityServer4.EntityFramework.Extensions;
using IdentityServer4.EntityFramework.Interfaces;
using IdentityServer4.EntityFramework.Options;

using Kritikos.Configuration.Persistence.Abstractions;

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

/// <summary>
/// Database abstraction for a combined <see cref="DbContext"/> using ASP.NET Identity and Identity Server.
/// </summary>
/// <typeparam name="TUser">The type of user objects.</typeparam>
/// <typeparam name="TRole">The type of role objects.</typeparam>
/// <typeparam name="TKey">The type of the primary key for users and roles.</typeparam>
/// <seealso cref="DbContext"/>
/// <seealso cref="IPersistedGrantDbContext"/>
/// <seealso cref="IConfigurationDbContext"/>
public abstract class ApiAuthorizationDbContext<TUser, TRole, TKey> :
IdentityDbContext<TUser, TRole, TKey>,
IPersistedGrantDbContext,
IConfigurationDbContext
where TUser : IdentityUser<TKey>, IEntity<TKey>
where TRole : IdentityRole<TKey>, IEntity<TKey>
where TKey : IComparable, IComparable<TKey>, IEquatable<TKey>
{
private readonly IOptions<ConfigurationStoreOptions> configurationStoreOptions;
private readonly IOptions<OperationalStoreOptions> operationalStoreOptions;

/// <summary>
/// Initializes a new instance of the <see cref="ApiAuthorizationDbContext{TUser,TRole,TKey}"/> class.
/// </summary>
/// <param name="options">The <see cref="DbContextOptions"/>.</param>
/// <param name="configurationStoreOptions">The <see cref="IOptions{ConfigurationStoreOptions}"/>.</param>
/// <param name="operationalStoreOptions">The <see cref="IOptions{OperationalStoreOptions}"/>.</param>
protected ApiAuthorizationDbContext(
DbContextOptions options,
IOptions<ConfigurationStoreOptions> configurationStoreOptions,
IOptions<OperationalStoreOptions> operationalStoreOptions)
: base(options)
{
this.configurationStoreOptions = configurationStoreOptions;
this.operationalStoreOptions = operationalStoreOptions;
}

#region Implementation of IConfigurationDbContext

/// <summary>
/// Gets or sets the clients.
/// </summary>
/// <value>
/// The clients.
/// </value>
public DbSet<Client> Clients { get; set; }

/// <summary>
/// Gets or sets the clients' CORS origins.
/// </summary>
/// <value>
/// The clients CORS origins.
/// </value>
public DbSet<ClientCorsOrigin> ClientCorsOrigins { get; set; }

/// <summary>
/// Gets or sets the identity resources.
/// </summary>
/// <value>
/// The identity resources.
/// </value>
public DbSet<IdentityResource> IdentityResources { get; set; }

/// <summary>
/// Gets or sets the API resources.
/// </summary>
/// <value>
/// The API resources.
/// </value>
public DbSet<ApiResource> ApiResources { get; set; }

/// <summary>
/// Gets or sets the API scopes.
/// </summary>
/// <value>
/// The API resources.
/// </value>
public DbSet<ApiScope> ApiScopes { get; set; }

#endregion Implementation of IConfigurationDbContext

#region Implementation of IPersistedGrantDbContext

/// <summary>
/// Gets or sets the persisted grants.
/// </summary>
/// <value>
/// The persisted grants.
/// </value>
public DbSet<PersistedGrant> PersistedGrants { get; set; }

/// <summary>
/// Gets or sets the device codes.
/// </summary>
/// <value>
/// The device codes.
/// </value>
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }

/// <inheritdoc cref="DbContext.SaveChangesAsync(CancellationToken)"/>
public Task<int> SaveChangesAsync() => base.SaveChangesAsync();

#endregion Implementation of IPersistedGrantDbContext

#region Overrides of IdentityDbContext<TUser,TRole,TKey,IdentityUserClaim<TKey>,IdentityUserRole<TKey>,IdentityUserLogin<TKey>,IdentityRoleClaim<TKey>,IdentityUserToken<TKey>>

/// <summary>
/// Override this method to further configure the model that was discovered by convention from the entity types
/// exposed in <see cref="T:Microsoft.EntityFrameworkCore.DbSet`1" /> properties on your derived context. The resulting model may be cached
/// and re-used for subsequent instances of your derived context.
/// </summary>
/// /// <param name="modelBuilder">The builder being used to construct the model for this context. Databases (and other extensions) typically
/// define extension methods on this object that allow you to configure aspects of the model that are specific
/// to a given database.</param>
/// <remarks>
/// /// If a model is explicitly set on the options for this context (via <see cref="M:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.UseModel(Microsoft.EntityFrameworkCore.Metadata.IModel)" />)
/// then this method will not be run.
/// </remarks>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigurePersistedGrantContext(operationalStoreOptions.Value);
modelBuilder.ConfigurePersistedGrantContext(operationalStoreOptions.Value);
modelBuilder.ConfigureResourcesContext(configurationStoreOptions.Value);
}

#endregion Overrides of IdentityDbContext<TUser,TRole,TKey,IdentityUserClaim<TKey>,IdentityUserRole<TKey>,IdentityUserLogin<TKey>,IdentityRoleClaim<TKey>,IdentityUserToken<TKey>>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Configuration.Persistence\Configuration.Persistence.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3e1f0b9

Please sign in to comment.