|
|
|
@ -5,9 +5,13 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Text; |
|
|
|
using OpenIddict.Extensions; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlerFilters; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlers; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlers.Exchange; |
|
|
|
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants; |
|
|
|
|
|
|
|
namespace OpenIddict.Client.WebIntegration; |
|
|
|
@ -20,6 +24,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers |
|
|
|
/* |
|
|
|
* Token request preparation: |
|
|
|
*/ |
|
|
|
AttachNonStandardBasicAuthenticationCredentials.Descriptor, |
|
|
|
AttachNonStandardQueryStringParameters.Descriptor, |
|
|
|
|
|
|
|
/* |
|
|
|
@ -27,6 +32,68 @@ public static partial class OpenIddictClientWebIntegrationHandlers |
|
|
|
*/ |
|
|
|
MapNonStandardResponseParameters.Descriptor); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains the logic responsible for attaching the client credentials to the HTTP Authorization
|
|
|
|
/// header using a non-standard construction logic for the providers that require it.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class AttachNonStandardBasicAuthenticationCredentials : IOpenIddictClientHandler<PrepareTokenRequestContext> |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
|
/// </summary>
|
|
|
|
public static OpenIddictClientHandlerDescriptor Descriptor { get; } |
|
|
|
= OpenIddictClientHandlerDescriptor.CreateBuilder<PrepareTokenRequestContext>() |
|
|
|
.AddFilter<RequireHttpMetadataUri>() |
|
|
|
.UseSingletonHandler<AttachNonStandardBasicAuthenticationCredentials>() |
|
|
|
.SetOrder(AttachBasicAuthenticationCredentials.Descriptor.Order + 500) |
|
|
|
.SetType(OpenIddictClientHandlerType.BuiltIn) |
|
|
|
.Build(); |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public ValueTask HandleAsync(PrepareTokenRequestContext context) |
|
|
|
{ |
|
|
|
if (context is null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
|
} |
|
|
|
|
|
|
|
// Some providers are known to incorrectly implement basic authentication support, either because
|
|
|
|
// an incorrect encoding scheme is used (e.g the credentials are not formURL-encoded as required
|
|
|
|
// by the OAuth 2.0 specification) or because basic authentication is required even for public
|
|
|
|
// clients, even though these clients don't have a secret (which requires using an empty password).
|
|
|
|
|
|
|
|
Debug.Assert(context.Request is not null, SR.GetResourceString(SR.ID4008)); |
|
|
|
|
|
|
|
// This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved,
|
|
|
|
// this may indicate that the request was incorrectly processed by another client stack.
|
|
|
|
var request = context.Transaction.GetHttpRequestMessage() ?? |
|
|
|
throw new InvalidOperationException(SR.GetResourceString(SR.ID0173)); |
|
|
|
|
|
|
|
// These providers require using basic authentication to flow the client_id
|
|
|
|
// for all types of client applications, even when there's no client_secret.
|
|
|
|
if (context.Registration.ProviderName is Providers.Reddit) |
|
|
|
{ |
|
|
|
// Important: the credentials MUST be formURL-encoded before being base64-encoded.
|
|
|
|
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(new StringBuilder() |
|
|
|
.Append(EscapeDataString(context.Request.ClientId)) |
|
|
|
.Append(':') |
|
|
|
.Append(EscapeDataString(context.Request.ClientSecret)) |
|
|
|
.ToString())); |
|
|
|
|
|
|
|
// Attach the authorization header containing the client credentials to the HTTP request.
|
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue(Schemes.Basic, credentials); |
|
|
|
|
|
|
|
// Remove the client credentials from the request payload to ensure they are not sent twice.
|
|
|
|
context.Request.ClientId = context.Request.ClientSecret = null; |
|
|
|
} |
|
|
|
|
|
|
|
return default; |
|
|
|
|
|
|
|
static string? EscapeDataString(string? value) |
|
|
|
=> value is not null ? Uri.EscapeDataString(value).Replace("%20", "+") : null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains the logic responsible for attaching non-standard query string
|
|
|
|
/// parameters to the token request for the providers that require it.
|
|
|
|
|