Skip to content

Commit 8d4eaf1

Browse files
Entity Framework.
1 parent 1f4afd8 commit 8d4eaf1

26 files changed

+4420
-1693
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/openiddict/openiddict-core for more information concerning
4+
* the license and the contributors participating to this project.
5+
*/
6+
7+
using System.ComponentModel;
8+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
9+
using OpenIddict.EntityFrameworkCore.Models;
10+
11+
namespace OpenIddict.EntityFrameworkCore;
12+
13+
/// <summary>
14+
/// Defines a relational mapping for the Authorization entity.
15+
/// </summary>
16+
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
17+
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
18+
/// <typeparam name="TKey">The type of the Key entity.</typeparam>
19+
[EditorBrowsable(EditorBrowsableState.Never)]
20+
public sealed class OpenIddictEntityFrameworkCoreAuthorizationConfiguration<TAuthorization, TToken, TKey> : IEntityTypeConfiguration<TAuthorization>
21+
where TAuthorization : OpenIddictEntityFrameworkCoreAuthorization<TKey, TToken>
22+
where TToken : OpenIddictEntityFrameworkCoreToken<TKey, TAuthorization>
23+
where TKey : notnull, IEquatable<TKey>
24+
{
25+
public void Configure(EntityTypeBuilder<TAuthorization> builder)
26+
{
27+
if (builder is null)
28+
{
29+
throw new ArgumentNullException(nameof(builder));
30+
}
31+
32+
// Warning: optional foreign keys MUST NOT be added as CLR properties because
33+
// Entity Framework would throw an exception due to the TKey generic parameter
34+
// being non-nullable when using value types like short, int, long or Guid.
35+
36+
builder.HasKey(authorization => authorization.Id);
37+
38+
builder.HasIndex(
39+
nameof(OpenIddictEntityFrameworkCoreAuthorization.ApplicationId),
40+
nameof(OpenIddictEntityFrameworkCoreAuthorization.Status),
41+
nameof(OpenIddictEntityFrameworkCoreAuthorization.Subject),
42+
nameof(OpenIddictEntityFrameworkCoreAuthorization.Type));
43+
44+
builder.Property(authorization => authorization.ConcurrencyToken)
45+
.HasMaxLength(50)
46+
.IsConcurrencyToken();
47+
48+
builder.Property(authorization => authorization.Id)
49+
.ValueGeneratedOnAdd();
50+
51+
builder.Property(authorization => authorization.Status)
52+
.HasMaxLength(50);
53+
54+
builder.Property(authorization => authorization.Subject)
55+
.HasMaxLength(400);
56+
57+
builder.Property(authorization => authorization.Type)
58+
.HasMaxLength(50);
59+
60+
builder.HasMany(authorization => authorization.Tokens)
61+
.WithOne(token => token.Authorization!)
62+
.HasForeignKey(nameof(OpenIddictEntityFrameworkCoreToken.Authorization) +
63+
nameof(OpenIddictEntityFrameworkCoreAuthorization.Id))
64+
.IsRequired(required: false);
65+
66+
builder.ToTable("OpenIddictAuthorizations");
67+
}
68+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/openiddict/openiddict-core for more information concerning
4+
* the license and the contributors participating to this project.
5+
*/
6+
7+
using System.ComponentModel;
8+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
9+
using OpenIddict.EntityFrameworkCore.Models;
10+
11+
namespace OpenIddict.EntityFrameworkCore;
12+
13+
/// <summary>
14+
/// Defines a relational mapping for the Token entity.
15+
/// </summary>
16+
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
17+
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
18+
/// <typeparam name="TKey">The type of the Key entity.</typeparam>
19+
[EditorBrowsable(EditorBrowsableState.Never)]
20+
public sealed class OpenIddictEntityFrameworkCoreTokenConfiguration<TToken, TAuthorization, TKey> : IEntityTypeConfiguration<TToken>
21+
where TToken : OpenIddictEntityFrameworkCoreToken<TKey, TAuthorization>
22+
where TAuthorization : OpenIddictEntityFrameworkCoreAuthorization<TKey, TToken>
23+
where TKey : notnull, IEquatable<TKey>
24+
{
25+
public void Configure(EntityTypeBuilder<TToken> builder)
26+
{
27+
if (builder is null)
28+
{
29+
throw new ArgumentNullException(nameof(builder));
30+
}
31+
32+
// Warning: optional foreign keys MUST NOT be added as CLR properties because
33+
// Entity Framework would throw an exception due to the TKey generic parameter
34+
// being non-nullable when using value types like short, int, long or Guid.
35+
36+
builder.HasKey(token => token.Id);
37+
38+
// Warning: the non-generic overlord is deliberately used to work around
39+
// a breaking change introduced in Entity Framework Core 3.x (where a
40+
// generic entity type builder is now returned by the HasIndex() method).
41+
builder.HasIndex(nameof(OpenIddictEntityFrameworkCoreToken.ReferenceId))
42+
.IsUnique();
43+
44+
builder.HasIndex(
45+
nameof(OpenIddictEntityFrameworkCoreToken.ApplicationId),
46+
nameof(OpenIddictEntityFrameworkCoreToken.Status),
47+
nameof(OpenIddictEntityFrameworkCoreToken.Subject),
48+
nameof(OpenIddictEntityFrameworkCoreToken.Type));
49+
50+
builder.Property(token => token.ConcurrencyToken)
51+
.HasMaxLength(50)
52+
.IsConcurrencyToken();
53+
54+
builder.Property(token => token.Id)
55+
.ValueGeneratedOnAdd();
56+
57+
builder.Property(token => token.ReferenceId)
58+
.HasMaxLength(100);
59+
60+
builder.Property(token => token.Status)
61+
.HasMaxLength(50);
62+
63+
builder.Property(token => token.Subject)
64+
.HasMaxLength(400);
65+
66+
builder.Property(token => token.Type)
67+
.HasMaxLength(50);
68+
69+
builder.ToTable("OpenIddictTokens");
70+
}
71+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/openiddict/openiddict-core for more information concerning
4+
* the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
9+
10+
namespace OpenIddict.EntityFrameworkCore.Models;
11+
12+
/// <summary>
13+
/// Represents an OpenIddict authorization.
14+
/// </summary>
15+
public class OpenIddictEntityFrameworkCoreAuthorization : OpenIddictEntityFrameworkCoreAuthorization<string, OpenIddictEntityFrameworkCoreToken>
16+
{
17+
public OpenIddictEntityFrameworkCoreAuthorization()
18+
{
19+
// Generate a new string identifier.
20+
Id = Guid.NewGuid().ToString();
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Represents an OpenIddict authorization.
26+
/// </summary>
27+
public class OpenIddictEntityFrameworkCoreAuthorization<TKey> : OpenIddictEntityFrameworkCoreAuthorization<TKey, OpenIddictEntityFrameworkCoreToken<TKey>>
28+
where TKey : notnull, IEquatable<TKey>
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Represents an OpenIddict authorization.
34+
/// </summary>
35+
[DebuggerDisplay("Id = {Id.ToString(),nq} ; Subject = {Subject,nq} ; Type = {Type,nq} ; Status = {Status,nq}")]
36+
public class OpenIddictEntityFrameworkCoreAuthorization<TKey, TToken>
37+
where TKey : notnull, IEquatable<TKey>
38+
where TToken : class
39+
{
40+
/// <summary>
41+
/// Gets or sets the application associated with the current authorization.
42+
/// </summary>
43+
public virtual string ApplicationId { get; set; } = string.Empty;
44+
45+
/// <summary>
46+
/// Gets or sets the concurrency token.
47+
/// </summary>
48+
public virtual string? ConcurrencyToken { get; set; } = Guid.NewGuid().ToString();
49+
50+
/// <summary>
51+
/// Gets or sets the UTC creation date of the current authorization.
52+
/// </summary>
53+
public virtual DateTime? CreationDate { get; set; }
54+
55+
/// <summary>
56+
/// Gets or sets the unique identifier associated with the current authorization.
57+
/// </summary>
58+
public virtual TKey? Id { get; set; }
59+
60+
/// <summary>
61+
/// Gets or sets the additional properties serialized as a JSON object,
62+
/// or <see langword="null"/> if no bag was associated with the current authorization.
63+
/// </summary>
64+
[StringSyntax(StringSyntaxAttribute.Json)]
65+
public virtual string? Properties { get; set; }
66+
67+
/// <summary>
68+
/// Gets or sets the scopes associated with the current
69+
/// authorization, serialized as a JSON array.
70+
/// </summary>
71+
public virtual string? Scopes { get; set; }
72+
73+
/// <summary>
74+
/// Gets or sets the status of the current authorization.
75+
/// </summary>
76+
public virtual string? Status { get; set; }
77+
78+
/// <summary>
79+
/// Gets or sets the subject associated with the current authorization.
80+
/// </summary>
81+
public virtual string? Subject { get; set; }
82+
83+
/// <summary>
84+
/// Gets the list of tokens associated with the current authorization.
85+
/// </summary>
86+
public virtual ICollection<TToken> Tokens { get; } = new HashSet<TToken>();
87+
88+
/// <summary>
89+
/// Gets or sets the type of the current authorization.
90+
/// </summary>
91+
public virtual string? Type { get; set; }
92+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/openiddict/openiddict-core for more information concerning
4+
* the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
9+
10+
namespace OpenIddict.EntityFrameworkCore.Models;
11+
12+
/// <summary>
13+
/// Represents an OpenIddict token.
14+
/// </summary>
15+
public class OpenIddictEntityFrameworkCoreToken : OpenIddictEntityFrameworkCoreToken<string, OpenIddictEntityFrameworkCoreAuthorization>
16+
{
17+
public OpenIddictEntityFrameworkCoreToken()
18+
{
19+
// Generate a new string identifier.
20+
Id = Guid.NewGuid().ToString();
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Represents an OpenIddict token.
26+
/// </summary>
27+
public class OpenIddictEntityFrameworkCoreToken<TKey> : OpenIddictEntityFrameworkCoreToken<TKey, OpenIddictEntityFrameworkCoreAuthorization<TKey>>
28+
where TKey : notnull, IEquatable<TKey>
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Represents an OpenIddict token.
34+
/// </summary>
35+
[DebuggerDisplay("Id = {Id.ToString(),nq} ; Subject = {Subject,nq} ; Type = {Type,nq} ; Status = {Status,nq}")]
36+
public class OpenIddictEntityFrameworkCoreToken<TKey, TAuthorization>
37+
where TKey : notnull, IEquatable<TKey>
38+
where TAuthorization : class
39+
{
40+
/// <summary>
41+
/// Gets or sets the application associated with the current token.
42+
/// </summary>
43+
public virtual string? ApplicationId { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the authorization associated with the current token.
47+
/// </summary>
48+
public virtual TAuthorization? Authorization { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the concurrency token.
52+
/// </summary>
53+
public virtual string? ConcurrencyToken { get; set; } = Guid.NewGuid().ToString();
54+
55+
/// <summary>
56+
/// Gets or sets the UTC creation date of the current token.
57+
/// </summary>
58+
public virtual DateTime? CreationDate { get; set; }
59+
60+
/// <summary>
61+
/// Gets or sets the UTC expiration date of the current token.
62+
/// </summary>
63+
public virtual DateTime? ExpirationDate { get; set; }
64+
65+
/// <summary>
66+
/// Gets or sets the unique identifier associated with the current token.
67+
/// </summary>
68+
public virtual TKey? Id { get; set; }
69+
70+
/// <summary>
71+
/// Gets or sets the payload of the current token, if applicable.
72+
/// Note: this property is only used for reference tokens
73+
/// and may be encrypted for security reasons.
74+
/// </summary>
75+
public virtual string? Payload { get; set; }
76+
77+
/// <summary>
78+
/// Gets or sets the additional properties serialized as a JSON object,
79+
/// or <see langword="null"/> if no bag was associated with the current token.
80+
/// </summary>
81+
[StringSyntax(StringSyntaxAttribute.Json)]
82+
public virtual string? Properties { get; set; }
83+
84+
/// <summary>
85+
/// Gets or sets the UTC redemption date of the current token.
86+
/// </summary>
87+
public virtual DateTime? RedemptionDate { get; set; }
88+
89+
/// <summary>
90+
/// Gets or sets the reference identifier associated
91+
/// with the current token, if applicable.
92+
/// Note: this property is only used for reference tokens
93+
/// and may be hashed or encrypted for security reasons.
94+
/// </summary>
95+
public virtual string? ReferenceId { get; set; }
96+
97+
/// <summary>
98+
/// Gets or sets the status of the current token.
99+
/// </summary>
100+
public virtual string? Status { get; set; }
101+
102+
/// <summary>
103+
/// Gets or sets the subject associated with the current token.
104+
/// </summary>
105+
public virtual string? Subject { get; set; }
106+
107+
/// <summary>
108+
/// Gets or sets the type of the current token.
109+
/// </summary>
110+
public virtual string? Type { get; set; }
111+
}

0 commit comments

Comments
 (0)