diff --git a/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs b/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs index 292de19e..a34603f9 100644 --- a/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs +++ b/src/OpenIddict.Core/Descriptors/OpenIddictAuthorizationDescriptor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Security.Claims; namespace OpenIddict.Core { @@ -13,6 +14,19 @@ namespace OpenIddict.Core /// public string ApplicationId { get; set; } + /// + /// Gets or sets the optional principal associated with the authorization. + /// Note: this property is not stored by the default authorization stores. + /// + public ClaimsPrincipal Principal { get; set; } + + /// + /// Gets the optional authentication properties associated with the authorization. + /// Note: this property is not stored by the default authorization stores. + /// + public IDictionary Properties { get; } = + new Dictionary(StringComparer.Ordinal); + /// /// Gets the scopes associated with the authorization. /// diff --git a/src/OpenIddict.Core/Descriptors/OpenIddictScopeDescriptor.cs b/src/OpenIddict.Core/Descriptors/OpenIddictScopeDescriptor.cs index edcc69d4..9900a704 100644 --- a/src/OpenIddict.Core/Descriptors/OpenIddictScopeDescriptor.cs +++ b/src/OpenIddict.Core/Descriptors/OpenIddictScopeDescriptor.cs @@ -5,6 +5,12 @@ /// public class OpenIddictScopeDescriptor { + /// + /// Gets or sets the description + /// associated with the scope. + /// + public virtual string Description { get; set; } + /// /// Gets or sets the unique name /// associated with the scope. diff --git a/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs b/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs index 258713a9..370ccd07 100644 --- a/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs +++ b/src/OpenIddict.Core/Descriptors/OpenIddictTokenDescriptor.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Security.Claims; namespace OpenIddict.Core { @@ -37,6 +39,19 @@ namespace OpenIddict.Core /// public string Hash { get; set; } + /// + /// Gets or sets the optional principal associated with the token. + /// Note: this property is not stored by the default token stores. + /// + public ClaimsPrincipal Principal { get; set; } + + /// + /// Gets the optional authentication properties associated with the token. + /// Note: this property is not stored by the default token stores. + /// + public IDictionary Properties { get; } = + new Dictionary(StringComparer.Ordinal); + /// /// Gets or sets the status associated with the token. /// diff --git a/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs index 0d08b0ff..308eaa6f 100644 --- a/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.Core/Stores/OpenIddictAuthorizationStore.cs @@ -380,7 +380,7 @@ namespace OpenIddict.Core authorization.Type = type; - return Task.FromResult(0); + return Task.CompletedTask; } /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs index ae9a1652..e473b984 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs @@ -141,36 +141,17 @@ namespace OpenIddict.EntityFramework /// /// A that can be used to monitor the asynchronous operation, whose result returns the application. /// - public override Task CreateAsync([NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) + public override async Task CreateAsync([NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } - var application = new TApplication - { - ClientId = descriptor.ClientId, - ClientSecret = descriptor.ClientSecret, - DisplayName = descriptor.DisplayName, - Type = descriptor.Type - }; - - if (descriptor.PostLogoutRedirectUris.Count != 0) - { - application.PostLogoutRedirectUris = string.Join( - OpenIddictConstants.Separators.Space, - descriptor.PostLogoutRedirectUris.Select(uri => uri.OriginalString)); - } - - if (descriptor.RedirectUris.Count != 0) - { - application.RedirectUris = string.Join( - OpenIddictConstants.Separators.Space, - descriptor.RedirectUris.Select(uri => uri.OriginalString)); - } + var application = new TApplication(); - return CreateAsync(application, cancellationToken); + await BindAsync(application, descriptor, cancellationToken); + return await CreateAsync(application, cancellationToken); } /// @@ -304,5 +285,48 @@ namespace OpenIddict.EntityFramework return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the application properties based on the specified descriptor. + /// + /// The application to update. + /// The application descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual Task BindAsync([NotNull] TApplication application, [NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) + { + if (application == null) + { + throw new ArgumentNullException(nameof(application)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + application.ClientId = descriptor.ClientId; + application.ClientSecret = descriptor.ClientSecret; + application.DisplayName = descriptor.DisplayName; + application.Type = descriptor.Type; + + if (descriptor.PostLogoutRedirectUris.Count != 0) + { + application.PostLogoutRedirectUris = string.Join( + OpenIddictConstants.Separators.Space, + descriptor.PostLogoutRedirectUris.Select(uri => uri.OriginalString)); + } + + if (descriptor.RedirectUris.Count != 0) + { + application.RedirectUris = string.Join( + OpenIddictConstants.Separators.Space, + descriptor.RedirectUris.Select(uri => uri.OriginalString)); + } + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs index f93d8a60..1778ca94 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs @@ -148,30 +148,9 @@ namespace OpenIddict.EntityFramework throw new ArgumentNullException(nameof(descriptor)); } - var authorization = new TAuthorization - { - Status = descriptor.Status, - Subject = descriptor.Subject, - Type = descriptor.Type - }; - - if (descriptor.Scopes.Count != 0) - { - authorization.Scopes = string.Join(OpenIddictConstants.Separators.Space, descriptor.Scopes); - } - - // Bind the authorization to the specified application, if applicable. - if (!string.IsNullOrEmpty(descriptor.ApplicationId)) - { - var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId)); - if (application == null) - { - throw new InvalidOperationException("The application associated with the authorization cannot be found."); - } - - authorization.Application = application; - } + var authorization = new TAuthorization(); + await BindAsync(authorization, descriptor, cancellationToken); return await CreateAsync(authorization, cancellationToken); } @@ -356,5 +335,48 @@ namespace OpenIddict.EntityFramework return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the authorization properties based on the specified descriptor. + /// + /// The authorization to update. + /// The authorization descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual async Task BindAsync([NotNull] TAuthorization authorization, [NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + authorization.Status = descriptor.Status; + authorization.Subject = descriptor.Subject; + authorization.Type = descriptor.Type; + + if (descriptor.Scopes.Count != 0) + { + authorization.Scopes = string.Join(OpenIddictConstants.Separators.Space, descriptor.Scopes); + } + + // Bind the authorization to the specified application, if applicable. + if (!string.IsNullOrEmpty(descriptor.ApplicationId)) + { + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); + if (application == null) + { + throw new InvalidOperationException("The application associated with the authorization cannot be found."); + } + + authorization.Application = application; + } + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs index 4732f7b8..992e8d82 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs @@ -122,14 +122,12 @@ namespace OpenIddict.EntityFramework /// /// A that can be used to monitor the asynchronous operation, whose result returns the scope. /// - public override Task CreateAsync([NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) + public override async Task CreateAsync([NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) { - var scope = new TScope - { - Name = descriptor.Name - }; + var scope = new TScope(); - return CreateAsync(scope, cancellationToken); + await BindAsync(scope, descriptor, cancellationToken); + return await CreateAsync(scope, cancellationToken); } /// @@ -212,5 +210,32 @@ namespace OpenIddict.EntityFramework return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the scope properties based on the specified descriptor. + /// + /// The scope to update. + /// The scope descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual Task BindAsync([NotNull] TScope scope, [NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) + { + if (scope == null) + { + throw new ArgumentNullException(nameof(scope)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + scope.Description = descriptor.Description; + scope.Name = descriptor.Name; + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs index 5d7c4f06..1e6135b8 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs @@ -148,41 +148,9 @@ namespace OpenIddict.EntityFramework throw new ArgumentNullException(nameof(descriptor)); } - var token = new TToken - { - Ciphertext = descriptor.Ciphertext, - CreationDate = descriptor.CreationDate, - ExpirationDate = descriptor.ExpirationDate, - Hash = descriptor.Hash, - Status = descriptor.Status, - Subject = descriptor.Subject, - Type = descriptor.Type - }; - - // Bind the token to the specified client application, if applicable. - if (!string.IsNullOrEmpty(descriptor.ApplicationId)) - { - var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId)); - if (application == null) - { - throw new InvalidOperationException("The application associated with the token cannot be found."); - } - - token.Application = application; - } - - // Bind the token to the specified authorization, if applicable. - if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) - { - var authorization = await Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.AuthorizationId)); - if (authorization == null) - { - throw new InvalidOperationException("The authorization associated with the token cannot be found."); - } - - token.Authorization = authorization; - } + var token = new TToken(); + await BindAsync(token, descriptor, cancellationToken); return await CreateAsync(token, cancellationToken); } @@ -429,5 +397,59 @@ namespace OpenIddict.EntityFramework return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the token properties based on the specified descriptor. + /// + /// The token to update. + /// The token descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual async Task BindAsync([NotNull] TToken token, [NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) + { + if (token == null) + { + throw new ArgumentNullException(nameof(token)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + token.Ciphertext = descriptor.Ciphertext; + token.CreationDate = descriptor.CreationDate; + token.ExpirationDate = descriptor.ExpirationDate; + token.Hash = descriptor.Hash; + token.Status = descriptor.Status; + token.Subject = descriptor.Subject; + token.Type = descriptor.Type; + + // Bind the token to the specified client application, if applicable. + if (!string.IsNullOrEmpty(descriptor.ApplicationId)) + { + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); + if (application == null) + { + throw new InvalidOperationException("The application associated with the token cannot be found."); + } + + token.Application = application; + } + + // Bind the token to the specified authorization, if applicable. + if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) + { + var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.AuthorizationId) }, cancellationToken); + if (authorization == null) + { + throw new InvalidOperationException("The authorization associated with the token cannot be found."); + } + + token.Authorization = authorization; + } + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs index 92c8affb..4db5055a 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs @@ -141,36 +141,17 @@ namespace OpenIddict.EntityFrameworkCore /// /// A that can be used to monitor the asynchronous operation, whose result returns the application. /// - public override Task CreateAsync([NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) + public override async Task CreateAsync([NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } - var application = new TApplication - { - ClientId = descriptor.ClientId, - ClientSecret = descriptor.ClientSecret, - DisplayName = descriptor.DisplayName, - Type = descriptor.Type - }; - - if (descriptor.PostLogoutRedirectUris.Count != 0) - { - application.PostLogoutRedirectUris = string.Join( - OpenIddictConstants.Separators.Space, - descriptor.PostLogoutRedirectUris.Select(uri => uri.OriginalString)); - } - - if (descriptor.RedirectUris.Count != 0) - { - application.RedirectUris = string.Join( - OpenIddictConstants.Separators.Space, - descriptor.RedirectUris.Select(uri => uri.OriginalString)); - } + var application = new TApplication(); - return CreateAsync(application, cancellationToken); + await BindAsync(application, descriptor, cancellationToken); + return await CreateAsync(application, cancellationToken); } /// @@ -304,5 +285,48 @@ namespace OpenIddict.EntityFrameworkCore return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the application properties based on the specified descriptor. + /// + /// The application to update. + /// The application descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual Task BindAsync([NotNull] TApplication application, [NotNull] OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken) + { + if (application == null) + { + throw new ArgumentNullException(nameof(application)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + application.ClientId = descriptor.ClientId; + application.ClientSecret = descriptor.ClientSecret; + application.DisplayName = descriptor.DisplayName; + application.Type = descriptor.Type; + + if (descriptor.PostLogoutRedirectUris.Count != 0) + { + application.PostLogoutRedirectUris = string.Join( + OpenIddictConstants.Separators.Space, + descriptor.PostLogoutRedirectUris.Select(uri => uri.OriginalString)); + } + + if (descriptor.RedirectUris.Count != 0) + { + application.RedirectUris = string.Join( + OpenIddictConstants.Separators.Space, + descriptor.RedirectUris.Select(uri => uri.OriginalString)); + } + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs index de874fe3..90c0d2a3 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs @@ -148,30 +148,9 @@ namespace OpenIddict.EntityFrameworkCore throw new ArgumentNullException(nameof(descriptor)); } - var authorization = new TAuthorization - { - Status = descriptor.Status, - Subject = descriptor.Subject, - Type = descriptor.Type - }; - - if (descriptor.Scopes.Count != 0) - { - authorization.Scopes = string.Join(OpenIddictConstants.Separators.Space, descriptor.Scopes); - } - - // Bind the authorization to the specified application, if applicable. - if (!string.IsNullOrEmpty(descriptor.ApplicationId)) - { - var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); - if (application == null) - { - throw new InvalidOperationException("The application associated with the authorization cannot be found."); - } - - authorization.Application = application; - } + var authorization = new TAuthorization(); + await BindAsync(authorization, descriptor, cancellationToken); return await CreateAsync(authorization, cancellationToken); } @@ -356,5 +335,48 @@ namespace OpenIddict.EntityFrameworkCore return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the authorization properties based on the specified descriptor. + /// + /// The authorization to update. + /// The authorization descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual async Task BindAsync([NotNull] TAuthorization authorization, [NotNull] OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken) + { + if (authorization == null) + { + throw new ArgumentNullException(nameof(authorization)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + authorization.Status = descriptor.Status; + authorization.Subject = descriptor.Subject; + authorization.Type = descriptor.Type; + + if (descriptor.Scopes.Count != 0) + { + authorization.Scopes = string.Join(OpenIddictConstants.Separators.Space, descriptor.Scopes); + } + + // Bind the authorization to the specified application, if applicable. + if (!string.IsNullOrEmpty(descriptor.ApplicationId)) + { + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); + if (application == null) + { + throw new InvalidOperationException("The application associated with the authorization cannot be found."); + } + + authorization.Application = application; + } + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs index 4520f532..918ed9b7 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs @@ -122,14 +122,12 @@ namespace OpenIddict.EntityFrameworkCore /// /// A that can be used to monitor the asynchronous operation, whose result returns the scope. /// - public override Task CreateAsync([NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) + public override async Task CreateAsync([NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) { - var scope = new TScope - { - Name = descriptor.Name - }; + var scope = new TScope(); - return CreateAsync(scope, cancellationToken); + await BindAsync(scope, descriptor, cancellationToken); + return await CreateAsync(scope, cancellationToken); } /// @@ -212,5 +210,32 @@ namespace OpenIddict.EntityFrameworkCore return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the scope properties based on the specified descriptor. + /// + /// The scope to update. + /// The scope descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual Task BindAsync([NotNull] TScope scope, [NotNull] OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken) + { + if (scope == null) + { + throw new ArgumentNullException(nameof(scope)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + scope.Description = descriptor.Description; + scope.Name = descriptor.Name; + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs index b2596619..fab5b859 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs @@ -148,41 +148,9 @@ namespace OpenIddict.EntityFrameworkCore throw new ArgumentNullException(nameof(descriptor)); } - var token = new TToken - { - Ciphertext = descriptor.Ciphertext, - CreationDate = descriptor.CreationDate, - ExpirationDate = descriptor.ExpirationDate, - Hash = descriptor.Hash, - Status = descriptor.Status, - Subject = descriptor.Subject, - Type = descriptor.Type - }; - - // Bind the token to the specified client application, if applicable. - if (!string.IsNullOrEmpty(descriptor.ApplicationId)) - { - var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); - if (application == null) - { - throw new InvalidOperationException("The application associated with the token cannot be found."); - } - - token.Application = application; - } - - // Bind the token to the specified authorization, if applicable. - if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) - { - var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.AuthorizationId) }, cancellationToken); - if (authorization == null) - { - throw new InvalidOperationException("The authorization associated with the token cannot be found."); - } - - token.Authorization = authorization; - } + var token = new TToken(); + await BindAsync(token, descriptor, cancellationToken); return await CreateAsync(token, cancellationToken); } @@ -431,5 +399,59 @@ namespace OpenIddict.EntityFrameworkCore return Context.SaveChangesAsync(cancellationToken); } + + /// + /// Sets the token properties based on the specified descriptor. + /// + /// The token to update. + /// The token descriptor. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation. + /// + protected virtual async Task BindAsync([NotNull] TToken token, [NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) + { + if (token == null) + { + throw new ArgumentNullException(nameof(token)); + } + + if (descriptor == null) + { + throw new ArgumentNullException(nameof(descriptor)); + } + + token.Ciphertext = descriptor.Ciphertext; + token.CreationDate = descriptor.CreationDate; + token.ExpirationDate = descriptor.ExpirationDate; + token.Hash = descriptor.Hash; + token.Status = descriptor.Status; + token.Subject = descriptor.Subject; + token.Type = descriptor.Type; + + // Bind the token to the specified client application, if applicable. + if (!string.IsNullOrEmpty(descriptor.ApplicationId)) + { + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); + if (application == null) + { + throw new InvalidOperationException("The application associated with the token cannot be found."); + } + + token.Application = application; + } + + // Bind the token to the specified authorization, if applicable. + if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) + { + var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.AuthorizationId) }, cancellationToken); + if (authorization == null) + { + throw new InvalidOperationException("The authorization associated with the token cannot be found."); + } + + token.Authorization = authorization; + } + } } } \ No newline at end of file diff --git a/src/OpenIddict/OpenIddictProvider.Helpers.cs b/src/OpenIddict/OpenIddictProvider.Helpers.cs index 22cce5f8..7bc501f8 100644 --- a/src/OpenIddict/OpenIddictProvider.Helpers.cs +++ b/src/OpenIddict/OpenIddictProvider.Helpers.cs @@ -33,12 +33,18 @@ namespace OpenIddict { var descriptor = new OpenIddictAuthorizationDescriptor { + Principal = ticket.Principal, Status = OpenIddictConstants.Statuses.Valid, Subject = ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject), Type = OpenIddictConstants.AuthorizationTypes.AdHoc }; - foreach (var scope in request.GetScopes()) + foreach (var property in ticket.Properties.Items) + { + descriptor.Properties.Add(property); + } + + foreach (var scope in ticket.GetScopes()) { descriptor.Scopes.Add(scope); } @@ -115,11 +121,17 @@ namespace OpenIddict AuthorizationId = ticket.GetProperty(OpenIddictConstants.Properties.AuthorizationId), CreationDate = ticket.Properties.IssuedUtc, ExpirationDate = ticket.Properties.ExpiresUtc, + Principal = ticket.Principal, Status = OpenIddictConstants.Statuses.Valid, Subject = ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject), Type = type }; + foreach (var property in ticket.Properties.Items) + { + descriptor.Properties.Add(property); + } + string result = null; // When reference tokens are enabled or when the token is an authorization code or a