/// <summary> /// Updates an existing token. /// </summary> /// <param name="token">The token to update.</param> /// <param name="operation">The delegate used to update the token based on the given descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation. /// </returns> public virtual async Task UpdateAsync([NotNull] TToken token, [NotNull] Func <OpenIddictTokenDescriptor, Task> operation, CancellationToken cancellationToken = default) { if (operation == null) { throw new ArgumentNullException(nameof(operation)); } var descriptor = new OpenIddictTokenDescriptor { ApplicationId = await Store.GetApplicationIdAsync(token, cancellationToken), AuthorizationId = await Store.GetAuthorizationIdAsync(token, cancellationToken), CreationDate = await Store.GetCreationDateAsync(token, cancellationToken), ExpirationDate = await Store.GetExpirationDateAsync(token, cancellationToken), Payload = await Store.GetPayloadAsync(token, cancellationToken), ReferenceId = await Store.GetReferenceIdAsync(token, cancellationToken), Status = await Store.GetStatusAsync(token, cancellationToken), Subject = await Store.GetSubjectAsync(token, cancellationToken), Type = await Store.GetTokenTypeAsync(token, cancellationToken) }; await operation(descriptor); await PopulateAsync(token, descriptor, cancellationToken); await UpdateAsync(token, cancellationToken); }
/// <summary> /// Populates the token using the specified descriptor. /// </summary> /// <param name="token">The token.</param> /// <param name="descriptor">The descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation. /// </returns> protected virtual async Task PopulateAsync([NotNull] TToken token, [NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } await Store.SetApplicationIdAsync(token, descriptor.ApplicationId, cancellationToken); await Store.SetAuthorizationIdAsync(token, descriptor.AuthorizationId, cancellationToken); await Store.SetCreationDateAsync(token, descriptor.CreationDate, cancellationToken); await Store.SetExpirationDateAsync(token, descriptor.ExpirationDate, cancellationToken); await Store.SetPayloadAsync(token, descriptor.Payload, cancellationToken); await Store.SetReferenceIdAsync(token, descriptor.ReferenceId, cancellationToken); await Store.SetStatusAsync(token, descriptor.Status, cancellationToken); await Store.SetSubjectAsync(token, descriptor.Subject, cancellationToken); await Store.SetTokenTypeAsync(token, descriptor.Type, cancellationToken); }
/// <summary> /// Validates the token descriptor to ensure it's in a consistent state. /// </summary> /// <param name="descriptor">The token descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation. /// </returns> protected virtual Task ValidateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } if (string.IsNullOrEmpty(descriptor.Type)) { throw new ArgumentException("The token type cannot be null or empty.", nameof(descriptor)); } if (!string.Equals(descriptor.Type, OpenIddictConstants.TokenTypes.AccessToken, StringComparison.OrdinalIgnoreCase) && !string.Equals(descriptor.Type, OpenIddictConstants.TokenTypes.AuthorizationCode, StringComparison.OrdinalIgnoreCase) && !string.Equals(descriptor.Type, OpenIddictConstants.TokenTypes.RefreshToken, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("The specified token type is not supported by the default token manager."); } if (string.IsNullOrEmpty(descriptor.Status)) { throw new ArgumentException("The status cannot be null or empty."); } if (string.IsNullOrEmpty(descriptor.Subject)) { throw new ArgumentException("The subject cannot be null or empty."); } return(Task.CompletedTask); }
/// <summary> /// Validates the token to ensure it's in a consistent state. /// </summary> /// <param name="token">The token.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation. /// </returns> protected virtual async Task ValidateAsync([NotNull] TToken token, CancellationToken cancellationToken) { if (token == null) { throw new ArgumentNullException(nameof(token)); } var descriptor = new OpenIddictTokenDescriptor { Status = await Store.GetStatusAsync(token, cancellationToken), Subject = await Store.GetSubjectAsync(token, cancellationToken), Type = await Store.GetTokenTypeAsync(token, cancellationToken) }; await ValidateAsync(descriptor, cancellationToken); }
/// <summary> /// Creates a new token based on the specified descriptor. /// </summary> /// <param name="descriptor">The token descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token. /// </returns> public virtual async Task <TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } var token = await Store.InstantiateAsync(cancellationToken); if (token == null) { throw new InvalidOperationException("An error occurred while trying to create a new token"); } await PopulateAsync(token, descriptor, cancellationToken); return(await CreateAsync(token, cancellationToken)); }
/// <summary> /// Creates a new token, which is associated with a particular subject. /// </summary> /// <param name="descriptor">The token descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token. /// </returns> public virtual async Task <TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken) { if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } await ValidateAsync(descriptor, cancellationToken); try { return(await Store.CreateAsync(descriptor, cancellationToken)); } catch (Exception exception) { Logger.LogError(exception, "An exception occurred while trying to create a new token."); throw; } }
/// <summary> /// Creates a new token. /// </summary> /// <param name="descriptor">The token descriptor.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns> /// A <see cref="Task"/> that can be used to monitor the asynchronous operation, whose result returns the token. /// </returns> public abstract Task <TToken> CreateAsync([NotNull] OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken);