21 changed files with 457 additions and 305 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,116 @@ |
|||
/* |
|||
* 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)); |
|||
} |
|||
|
|||
return builder.ReplaceService<IModelCustomizer, OpenIddictEntityFrameworkCoreCustomizer< |
|||
TApplication, TAuthorization, TScope, TToken, TKey>>(); |
|||
} |
|||
|
|||
/// <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)); |
|||
} |
|||
|
|||
return builder |
|||
.ApplyConfiguration(new OpenIddictApplicationConfiguration<TApplication, TAuthorization, TToken, TKey>()) |
|||
.ApplyConfiguration(new OpenIddictAuthorizationConfiguration<TAuthorization, TApplication, TToken, TKey>()) |
|||
.ApplyConfiguration(new OpenIddictScopeConfiguration<TScope, TKey>()) |
|||
.ApplyConfiguration(new OpenIddictTokenConfiguration<TToken, TApplication, TAuthorization, TKey>()); |
|||
} |
|||
} |
|||
} |
|||
@ -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,107 @@ |
|||
/* |
|||
* 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 Moq; |
|||
using OpenIddict.EntityFrameworkCore.Models; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.EntityFrameworkCore.Tests |
|||
{ |
|||
public class OpenIddictEntityFrameworkCoreHelpersTests |
|||
{ |
|||
[Fact] |
|||
public void UseOpenIddict_RegistersDefaultEntityConfigurations() |
|||
{ |
|||
// Arrange
|
|||
var builder = new Mock<ModelBuilder>(new ConventionSet()); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictApplication>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictAuthorization>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictScope>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictToken>>())) |
|||
.Returns(builder.Object); |
|||
|
|||
// Act
|
|||
builder.Object.UseOpenIddict(); |
|||
|
|||
// Assert
|
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictApplicationConfiguration<OpenIddictApplication, OpenIddictAuthorization, OpenIddictToken, string>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictAuthorizationConfiguration<OpenIddictAuthorization, OpenIddictApplication, OpenIddictToken, string>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictScopeConfiguration<OpenIddictScope, string>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictTokenConfiguration<OpenIddictToken, OpenIddictApplication, OpenIddictAuthorization, string>>()), Times.Once()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseOpenIddict_RegistersDefaultEntityConfigurationsWithCustomKeyType() |
|||
{ |
|||
// Arrange
|
|||
var builder = new Mock<ModelBuilder>(new ConventionSet()); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictApplication<long>>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictAuthorization<long>>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictScope<long>>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<OpenIddictToken<long>>>())) |
|||
.Returns(builder.Object); |
|||
|
|||
// Act
|
|||
builder.Object.UseOpenIddict<long>(); |
|||
|
|||
// Assert
|
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictApplicationConfiguration<OpenIddictApplication<long>, OpenIddictAuthorization<long>, OpenIddictToken<long>, long>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictAuthorizationConfiguration<OpenIddictAuthorization<long>, OpenIddictApplication<long>, OpenIddictToken<long>, long>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictScopeConfiguration<OpenIddictScope<long>, long>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictTokenConfiguration<OpenIddictToken<long>, OpenIddictApplication<long>, OpenIddictAuthorization<long>, long>>()), Times.Once()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void UseOpenIddict_RegistersCustomEntityConfigurations() |
|||
{ |
|||
// Arrange
|
|||
var builder = new Mock<ModelBuilder>(new ConventionSet()); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<CustomApplication>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<CustomAuthorization>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<CustomScope>>())) |
|||
.Returns(builder.Object); |
|||
builder.Setup(mock => mock.ApplyConfiguration(It.IsAny<IEntityTypeConfiguration<CustomToken>>())) |
|||
.Returns(builder.Object); |
|||
|
|||
// Act
|
|||
builder.Object.UseOpenIddict<CustomApplication, CustomAuthorization, CustomScope, CustomToken, Guid>(); |
|||
|
|||
// Assert
|
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictApplicationConfiguration<CustomApplication, CustomAuthorization, CustomToken, Guid>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictAuthorizationConfiguration<CustomAuthorization, CustomApplication, CustomToken, Guid>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictScopeConfiguration<CustomScope, Guid>>()), Times.Once()); |
|||
builder.Verify(mock => mock.ApplyConfiguration( |
|||
It.IsAny<OpenIddictTokenConfiguration<CustomToken, CustomApplication, CustomAuthorization, Guid>>()), Times.Once()); |
|||
} |
|||
|
|||
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