19 changed files with 421 additions and 288 deletions
@ -0,0 +1,59 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using JetBrains.Annotations; |
|||
using OpenIddict.EntityFramework; |
|||
using OpenIddict.EntityFramework.Models; |
|||
|
|||
namespace System.Data.Entity |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes extensions allowing to register the OpenIddict Entity Framework 6.x entity sets.
|
|||
/// </summary>
|
|||
public static class OpenIddictEntityFrameworkHelpers |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework 6.x context
|
|||
/// using the default OpenIddict models and the default key type (string).
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static DbModelBuilder UseOpenIddict([NotNull] this DbModelBuilder builder) |
|||
=> builder.UseOpenIddict<OpenIddictApplication, |
|||
OpenIddictAuthorization, |
|||
OpenIddictScope, |
|||
OpenIddictToken, string>(); |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework 6.x
|
|||
/// context using the specified entities and the specified key type.
|
|||
/// Note: using this method requires creating non-generic derived classes
|
|||
/// for all the OpenIddict entities (application, authorization, scope, token).
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static DbModelBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this DbModelBuilder builder) |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken> |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken> |
|||
where TScope : OpenIddictScope<TKey> |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization> |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
builder.Configurations |
|||
.Add(new OpenIddictApplicationConfiguration<TApplication, TAuthorization, TToken, TKey>()) |
|||
.Add(new OpenIddictAuthorizationConfiguration<TAuthorization, TApplication, TToken, TKey>()) |
|||
.Add(new OpenIddictScopeConfiguration<TScope, TKey>()) |
|||
.Add(new OpenIddictTokenConfiguration<TToken, TApplication, TAuthorization, TKey>()); |
|||
|
|||
return builder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,123 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using OpenIddict.EntityFrameworkCore; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
|
|||
namespace Microsoft.EntityFrameworkCore |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes extensions allowing to register the OpenIddict Entity Framework Core entity sets.
|
|||
/// </summary>
|
|||
public static class OpenIddictEntityFrameworkCoreHelpers |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core context
|
|||
/// using the default OpenIddict models and the default key type (string).
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static DbContextOptionsBuilder UseOpenIddict([NotNull] this DbContextOptionsBuilder builder) |
|||
=> builder.UseOpenIddict<OpenIddictApplication, |
|||
OpenIddictAuthorization, |
|||
OpenIddictScope, |
|||
OpenIddictToken, string>(); |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core
|
|||
/// context using the default OpenIddict models and the specified key type.
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static DbContextOptionsBuilder UseOpenIddict<TKey>([NotNull] this DbContextOptionsBuilder builder) |
|||
where TKey : IEquatable<TKey> |
|||
=> builder.UseOpenIddict<OpenIddictApplication<TKey>, |
|||
OpenIddictAuthorization<TKey>, |
|||
OpenIddictScope<TKey>, |
|||
OpenIddictToken<TKey>, TKey>(); |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core
|
|||
/// context using the specified entities and the specified key type.
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static DbContextOptionsBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this DbContextOptionsBuilder builder) |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken> |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken> |
|||
where TScope : OpenIddictScope<TKey> |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization> |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
var extension = new OpenIddictEntityFrameworkCoreExtension<TApplication, TAuthorization, TScope, TToken, TKey>(); |
|||
((IDbContextOptionsBuilderInfrastructure) builder).AddOrUpdateExtension(extension); |
|||
|
|||
return builder; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core context
|
|||
/// using the default OpenIddict models and the default key type (string).
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static ModelBuilder UseOpenIddict([NotNull] this ModelBuilder builder) |
|||
=> builder.UseOpenIddict<OpenIddictApplication, |
|||
OpenIddictAuthorization, |
|||
OpenIddictScope, |
|||
OpenIddictToken, string>(); |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core
|
|||
/// context using the default OpenIddict models and the specified key type.
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static ModelBuilder UseOpenIddict<TKey>([NotNull] this ModelBuilder builder) where TKey : IEquatable<TKey> |
|||
=> builder.UseOpenIddict<OpenIddictApplication<TKey>, |
|||
OpenIddictAuthorization<TKey>, |
|||
OpenIddictScope<TKey>, |
|||
OpenIddictToken<TKey>, TKey>(); |
|||
|
|||
/// <summary>
|
|||
/// Registers the OpenIddict entity sets in the Entity Framework Core
|
|||
/// context using the specified entities and the specified key type.
|
|||
/// </summary>
|
|||
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
|
|||
/// <returns>The Entity Framework context builder.</returns>
|
|||
public static ModelBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this ModelBuilder builder) |
|||
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken> |
|||
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken> |
|||
where TScope : OpenIddictScope<TKey> |
|||
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization> |
|||
where TKey : IEquatable<TKey> |
|||
{ |
|||
if (builder == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(builder)); |
|||
} |
|||
|
|||
new OpenIddictApplicationConfiguration<TApplication, TAuthorization, TToken, TKey>() |
|||
.Configure(builder.Entity<TApplication>()); |
|||
new OpenIddictAuthorizationConfiguration<TAuthorization, TApplication, TToken, TKey>() |
|||
.Configure(builder.Entity<TAuthorization>()); |
|||
new OpenIddictScopeConfiguration<TScope, TKey>() |
|||
.Configure(builder.Entity<TScope>()); |
|||
new OpenIddictTokenConfiguration<TToken, TApplication, TAuthorization, TKey>() |
|||
.Configure(builder.Entity<TToken>()); |
|||
|
|||
return builder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,89 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using AspNet.Security.OpenIdConnect.Extensions; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using OpenIddict.Abstractions; |
|||
|
|||
namespace OpenIddict.Server |
|||
{ |
|||
/// <summary>
|
|||
/// Exposes extensions allowing to store and retrieve
|
|||
/// OpenIddict-specific properties in authentication tickets.
|
|||
/// </summary>
|
|||
public static class OpenIddictServerHelpers |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the internal authorization identifier associated with the authentication ticket.
|
|||
/// Note: this identifier can be used to retrieve the authorization from the database.
|
|||
/// </summary>
|
|||
/// <param name="ticket">The authentication ticket.</param>
|
|||
/// <returns>The authorization identifier or <c>null</c> if it cannot be found.</returns>
|
|||
public static string GetInternalAuthorizationId([NotNull] this AuthenticationTicket ticket) |
|||
{ |
|||
if (ticket == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(ticket)); |
|||
} |
|||
|
|||
return ticket.GetProperty(OpenIddictConstants.Properties.InternalAuthorizationId); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the internal token identifier associated with the authentication ticket.
|
|||
/// Note: this identifier can be used to retrieve the token from the database.
|
|||
/// </summary>
|
|||
/// <param name="ticket">The authentication ticket.</param>
|
|||
/// <returns>The token identifier or <c>null</c> if it cannot be found.</returns>
|
|||
public static string GetInternalTokenId([NotNull] this AuthenticationTicket ticket) |
|||
{ |
|||
if (ticket == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(ticket)); |
|||
} |
|||
|
|||
return ticket.GetProperty(OpenIddictConstants.Properties.InternalTokenId); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the internal authorization identifier associated with the authentication ticket.
|
|||
/// Note: the identifier MUST correspond to a valid authorization entry in the database.
|
|||
/// </summary>
|
|||
/// <param name="ticket">The authentication ticket.</param>
|
|||
/// <param name="identifier">The internal authorization identifier.</param>
|
|||
/// <returns>The authentication ticket.</returns>
|
|||
public static AuthenticationTicket SetInternalAuthorizationId( |
|||
[NotNull] this AuthenticationTicket ticket, [CanBeNull] string identifier) |
|||
{ |
|||
if (ticket == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(ticket)); |
|||
} |
|||
|
|||
return ticket.SetProperty(OpenIddictConstants.Properties.InternalAuthorizationId, identifier); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the internal token identifier associated with the authentication ticket.
|
|||
/// Note: the identifier MUST correspond to a valid token entry in the database.
|
|||
/// </summary>
|
|||
/// <param name="ticket">The authentication ticket.</param>
|
|||
/// <param name="identifier">The internal token identifier.</param>
|
|||
/// <returns>The authentication ticket.</returns>
|
|||
public static AuthenticationTicket SetInternalTokenId( |
|||
[NotNull] this AuthenticationTicket ticket, [CanBeNull] string identifier) |
|||
{ |
|||
if (ticket == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(ticket)); |
|||
} |
|||
|
|||
return ticket.SetProperty(OpenIddictConstants.Properties.InternalTokenId, identifier); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Conventions; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictEntityFrameworkCoreHelpersTests |
|||
{ |
|||
[Fact] |
|||
public void UseOpenIddict_RegistersDefaultEntityConfigurations() |
|||
{ |
|||
// Arrange
|
|||
var builder = new ModelBuilder(new ConventionSet()); |
|||
|
|||
// Act
|
|||
builder.UseOpenIddict(); |
|||
|
|||
// Assert
|
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictApplication))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictAuthorization))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictScope))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictToken))); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseOpenIddict_RegistersDefaultEntityConfigurationsWithCustomKeyType() |
|||
{ |
|||
// Arrange
|
|||
var builder = new ModelBuilder(new ConventionSet()); |
|||
|
|||
// Act
|
|||
builder.UseOpenIddict<long>(); |
|||
|
|||
// Assert
|
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictApplication<long>))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictAuthorization<long>))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictScope<long>))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(OpenIddictToken<long>))); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseOpenIddict_RegistersCustomEntityConfigurations() |
|||
{ |
|||
// Arrange
|
|||
var builder = new ModelBuilder(new ConventionSet()); |
|||
|
|||
// Act
|
|||
builder.UseOpenIddict<CustomApplication, CustomAuthorization, CustomScope, CustomToken, Guid>(); |
|||
|
|||
// Assert
|
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(CustomApplication))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(CustomAuthorization))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(CustomScope))); |
|||
Assert.NotNull(builder.Model.FindEntityType(typeof(CustomToken))); |
|||
} |
|||
|
|||
public class CustomApplication : OpenIddictApplication<Guid, CustomAuthorization, CustomToken> { } |
|||
public class CustomAuthorization : OpenIddictAuthorization<Guid, CustomApplication, CustomToken> { } |
|||
public class CustomScope : OpenIddictScope<Guid> { } |
|||
public class CustomToken : OpenIddictToken<Guid, CustomApplication, CustomAuthorization> { } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue