Browse Source

Rework the entities to allow exposing a services.AddOpenIddict<TUser, TContext, TKey>() extension

pull/130/head
Kévin Chalet 10 years ago
parent
commit
8b4b63a63b
  1. 15
      src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs
  2. 7
      src/OpenIddict.EntityFramework/Models/OpenIddictScope.cs
  3. 15
      src/OpenIddict.EntityFramework/Models/OpenIddictUser.cs
  4. 31
      src/OpenIddict.EntityFramework/OpenIddictContext.cs
  5. 2
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  6. 33
      src/OpenIddict/OpenIddictExtensions.cs

15
src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs

@ -11,12 +11,7 @@ namespace OpenIddict {
/// <summary>
/// Represents an OpenIddict authorization.
/// </summary>
public class OpenIddictAuthorization : OpenIddictAuthorization<OpenIddictToken> { }
/// <summary>
/// Represents an OpenIddict authorization.
/// </summary>
public class OpenIddictAuthorization<TToken> : OpenIddictAuthorization<TToken, string> {
public class OpenIddictAuthorization : OpenIddictAuthorization<string, OpenIddictToken> {
public OpenIddictAuthorization() {
// Generate a new string identifier.
Id = Guid.NewGuid().ToString();
@ -26,7 +21,13 @@ namespace OpenIddict {
/// <summary>
/// Represents an OpenIddict authorization.
/// </summary>
public class OpenIddictAuthorization<TToken, TKey> where TKey : IEquatable<TKey> {
public class OpenIddictAuthorization<TKey> : OpenIddictAuthorization<TKey, OpenIddictToken<TKey>>
where TKey : IEquatable<TKey> { }
/// <summary>
/// Represents an OpenIddict authorization.
/// </summary>
public class OpenIddictAuthorization<TKey, TToken> where TKey : IEquatable<TKey> {
/// <summary>
/// Gets or sets the unique identifier
/// associated with the current authorization.

7
src/OpenIddict.EntityFramework/Models/OpenIddictScope.cs

@ -10,7 +10,12 @@ namespace OpenIddict {
/// <summary>
/// Represents an OpenIddict scope.
/// </summary>
public class OpenIddictScope : OpenIddictScope<string> { }
public class OpenIddictScope : OpenIddictScope<string> {
public OpenIddictScope() {
// Generate a new string identifier.
Id = Guid.NewGuid().ToString();
}
}
/// <summary>
/// Represents an OpenIddict scope.

15
src/OpenIddict.EntityFramework/Models/OpenIddictUser.cs

@ -12,12 +12,7 @@ namespace OpenIddict {
/// <summary>
/// Represents an OpenIddict user.
/// </summary>
public class OpenIddictUser : OpenIddictUser<OpenIddictAuthorization, OpenIddictToken> { }
/// <summary>
/// Represents an OpenIddict user.
/// </summary>
public class OpenIddictUser<TAuthorization, TToken> : OpenIddictUser<TAuthorization, TToken, string> {
public class OpenIddictUser : OpenIddictUser<string, OpenIddictAuthorization, OpenIddictToken> {
public OpenIddictUser() {
// Generate a new string identifier.
Id = Guid.NewGuid().ToString();
@ -27,7 +22,13 @@ namespace OpenIddict {
/// <summary>
/// Represents an OpenIddict user.
/// </summary>
public class OpenIddictUser<TAuthorization, TToken, TKey> : IdentityUser<TKey> where TKey : IEquatable<TKey> {
public class OpenIddictUser<TKey> : OpenIddictUser<TKey, OpenIddictAuthorization<TKey>, OpenIddictToken<TKey>>
where TKey : IEquatable<TKey> { }
/// <summary>
/// Represents an OpenIddict user.
/// </summary>
public class OpenIddictUser<TKey, TAuthorization, TToken> : IdentityUser<TKey> where TKey : IEquatable<TKey> {
/// <summary>
/// Gets the list of the authorizations associated with this user profile.
/// </summary>

31
src/OpenIddict.EntityFramework/OpenIddictContext.cs

@ -32,7 +32,8 @@ namespace OpenIddict {
public class OpenIddictContext<TUser> : OpenIddictContext<TUser, OpenIddictApplication,
OpenIddictAuthorization,
OpenIddictScope,
OpenIddictToken, IdentityRole, string>
OpenIddictToken,
IdentityRole, string>
where TUser : OpenIddictUser {
/// <summary>
/// Initializes a new OpenIddict context without configuring the Entity Framework options.
@ -46,6 +47,30 @@ namespace OpenIddict {
public OpenIddictContext(DbContextOptions options) : base(options) { }
}
/// <summary>
/// Represents an OpenIddict-powered Entity Framework context.
/// </summary>
/// <typeparam name="TUser">The type of the User entity.</typeparam>
/// <typeparam name="TKey">The type of the primary key used by the Identity/OpenIddict entities.</typeparam>
public class OpenIddictContext<TUser, TKey> : OpenIddictContext<TUser, OpenIddictApplication<TKey>,
OpenIddictAuthorization<TKey>,
OpenIddictScope<TKey>,
OpenIddictToken<TKey>,
IdentityRole<TKey>, TKey>
where TUser : OpenIddictUser<TKey>
where TKey : IEquatable<TKey> {
/// <summary>
/// Initializes a new OpenIddict context without configuring the Entity Framework options.
/// </summary>
protected OpenIddictContext() { }
/// <summary>
/// Initializes a new OpenIddict context.
/// </summary>
/// <param name="options">The options used to configure the Entity Framework context.</param>
public OpenIddictContext(DbContextOptions options) : base(options) { }
}
/// <summary>
/// Represents an OpenIddict-powered Entity Framework context.
/// </summary>
@ -57,9 +82,9 @@ namespace OpenIddict {
/// <typeparam name="TRole">The type of the Role entity.</typeparam>
/// <typeparam name="TKey">The type of the primary key used by the Identity/OpenIddict entities.</typeparam>
public class OpenIddictContext<TUser, TApplication, TAuthorization, TScope, TToken, TRole, TKey> : IdentityDbContext<TUser, TRole, TKey>
where TUser : OpenIddictUser<TAuthorization, TToken, TKey>
where TUser : OpenIddictUser<TKey, TAuthorization, TToken>
where TApplication : OpenIddictApplication<TKey>
where TAuthorization : OpenIddictAuthorization<TToken, TKey>
where TAuthorization : OpenIddictAuthorization<TKey, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey>
where TRole : IdentityRole<TKey>

2
src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs

@ -16,7 +16,7 @@ namespace OpenIddict {
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictAuthorizationStore<TAuthorization, TToken, TContext, TKey> : IOpenIddictAuthorizationStore<TAuthorization>
where TAuthorization : OpenIddictAuthorization<TToken, TKey>
where TAuthorization : OpenIddictAuthorization<TKey, TToken>
where TToken : OpenIddictToken<TKey>
where TContext : DbContext
where TKey : IEquatable<TKey> {

33
src/OpenIddict/OpenIddictExtensions.cs

@ -61,6 +61,35 @@ namespace Microsoft.AspNetCore.Builder {
OpenIddictToken, TContext, string>();
}
/// <summary>
/// Registers the default OpenIddict services in the DI container,
/// including the Entity Framework stores and the specified entities.
/// </summary>
/// <typeparam name="TUser">The type of the User entity.</typeparam>
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
/// <param name="services">The services collection.</param>
/// <remarks>
/// Note: the core services include native support for the non-interactive flows
/// (resource owner password credentials, client credentials, refresh token).
/// To support interactive flows like authorization code or implicit/hybrid,
/// consider adding the MVC module or creating your own authorization controller.
/// </remarks>
/// <returns>The <see cref="OpenIddictBuilder"/>.</returns>
public static OpenIddictBuilder AddOpenIddict<TUser, TContext, TKey>([NotNull] this IServiceCollection services)
where TUser : OpenIddictUser<TKey>
where TContext : DbContext
where TKey : IEquatable<TKey> {
if (services == null) {
throw new ArgumentNullException(nameof(services));
}
return services.AddOpenIddict<TUser, OpenIddictApplication<TKey>,
OpenIddictAuthorization<TKey>,
OpenIddictScope<TKey>,
OpenIddictToken<TKey>, TContext, TKey>();
}
/// <summary>
/// Registers the default OpenIddict services in the DI container,
/// including the Entity Framework stores and the specified entities.
@ -82,9 +111,9 @@ namespace Microsoft.AspNetCore.Builder {
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static OpenIddictBuilder AddOpenIddict<TUser, TApplication, TAuthorization, TScope, TToken, TContext, TKey>(
[NotNull] this IServiceCollection services)
where TUser : OpenIddictUser<TAuthorization, TToken, TKey>
where TUser : OpenIddictUser<TKey, TAuthorization, TToken>
where TApplication : OpenIddictApplication<TKey>
where TAuthorization : OpenIddictAuthorization<TToken, TKey>
where TAuthorization : OpenIddictAuthorization<TKey, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey>
where TContext : DbContext

Loading…
Cancel
Save