|
|
|
@ -5,8 +5,12 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Net.Http.Json; |
|
|
|
using OpenIddict.Client.SystemNetHttp; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpConstants; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlerFilters; |
|
|
|
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlers; |
|
|
|
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants; |
|
|
|
@ -24,12 +28,61 @@ public static partial class OpenIddictClientWebIntegrationHandlers |
|
|
|
MapNonStandardRequestParameters.Descriptor, |
|
|
|
OverrideHttpMethod.Descriptor, |
|
|
|
AttachBearerAccessToken.Descriptor, |
|
|
|
AttachNonStandardRequestPayload.Descriptor, |
|
|
|
|
|
|
|
/* |
|
|
|
* Revocation response extraction: |
|
|
|
*/ |
|
|
|
NormalizeContentType.Descriptor |
|
|
|
]); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains the logic responsible for attaching a non-standard payload for the providers that require it.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class AttachNonStandardRequestPayload : IOpenIddictClientHandler<PrepareRevocationRequestContext> |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
|
/// </summary>
|
|
|
|
public static OpenIddictClientHandlerDescriptor Descriptor { get; } |
|
|
|
= OpenIddictClientHandlerDescriptor.CreateBuilder<PrepareRevocationRequestContext>() |
|
|
|
.AddFilter<RequireHttpUri>() |
|
|
|
.UseSingletonHandler<AttachNonStandardRequestPayload>() |
|
|
|
.SetOrder(AttachHttpParameters<PrepareRevocationRequestContext>.Descriptor.Order + 500) |
|
|
|
.SetType(OpenIddictClientHandlerType.BuiltIn) |
|
|
|
.Build(); |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public ValueTask HandleAsync(PrepareRevocationRequestContext context) |
|
|
|
{ |
|
|
|
if (context is null) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
|
} |
|
|
|
|
|
|
|
Debug.Assert(context.Transaction.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)); |
|
|
|
|
|
|
|
request.Content = context.Registration.ProviderType switch |
|
|
|
{ |
|
|
|
// The token revocation endpoints exposed by these providers
|
|
|
|
// requires sending the request parameters as a JSON payload:
|
|
|
|
ProviderTypes.Miro => JsonContent.Create(context.Transaction.Request, |
|
|
|
new MediaTypeHeaderValue(MediaTypes.Json) |
|
|
|
{ |
|
|
|
CharSet = Charsets.Utf8 |
|
|
|
}), |
|
|
|
|
|
|
|
_ => request.Content |
|
|
|
}; |
|
|
|
|
|
|
|
return default; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains the logic responsible for mapping non-standard request parameters
|
|
|
|
@ -55,15 +108,39 @@ public static partial class OpenIddictClientWebIntegrationHandlers |
|
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
|
} |
|
|
|
|
|
|
|
// Weibo, VK ID and Yandex don't support the standard "token" parameter and
|
|
|
|
// These providers don't support the standard "token" parameter and
|
|
|
|
// require using the non-standard "access_token" parameter instead.
|
|
|
|
if (context.Registration.ProviderType is ProviderTypes.Weibo or ProviderTypes.VkId or ProviderTypes.Yandex) |
|
|
|
if (context.Registration.ProviderType is |
|
|
|
ProviderTypes.VkId or ProviderTypes.Webflow or |
|
|
|
ProviderTypes.Weibo or ProviderTypes.Yandex) |
|
|
|
{ |
|
|
|
context.Request.AccessToken = context.Token; |
|
|
|
context.Request.Token = null; |
|
|
|
context.Request.TokenTypeHint = null; |
|
|
|
} |
|
|
|
|
|
|
|
// Linear requires only the access_token and no other parameters.
|
|
|
|
else if (context.Registration.ProviderType is ProviderTypes.Linear) |
|
|
|
{ |
|
|
|
context.Request.AccessToken = context.Token; |
|
|
|
context.Request.Token = null; |
|
|
|
context.Request.TokenTypeHint = null; |
|
|
|
context.Request.ClientId = null; |
|
|
|
} |
|
|
|
|
|
|
|
// Miro uses a JSON payload that expects the non-standard
|
|
|
|
// "accessToken", "clientId" and "clientSecret" properties.
|
|
|
|
else if (context.Registration.ProviderType is ProviderTypes.Miro) |
|
|
|
{ |
|
|
|
context.Request["accessToken"] = context.Token; |
|
|
|
context.Request["clientId"] = context.Request.ClientId; |
|
|
|
context.Request["clientSecret"] = context.Request.ClientSecret; |
|
|
|
context.Request.Token = null; |
|
|
|
context.Request.TokenTypeHint = null; |
|
|
|
context.Request.ClientId = null; |
|
|
|
context.Request.ClientSecret = null; |
|
|
|
} |
|
|
|
|
|
|
|
return default; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -147,6 +224,15 @@ public static partial class OpenIddictClientWebIntegrationHandlers |
|
|
|
context.Request.Token = null; |
|
|
|
} |
|
|
|
|
|
|
|
// Miro requires using bearer authentication with the token that is going to be revoked.
|
|
|
|
//
|
|
|
|
// Note: the token property CANNOT be used here as the token parameter is mapped to "accessToken".
|
|
|
|
else if (context.Registration.ProviderType is ProviderTypes.Miro && |
|
|
|
(string?) context.Request["accessToken"] is { Length: > 0 } token) |
|
|
|
{ |
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue(Schemes.Bearer, token); |
|
|
|
} |
|
|
|
|
|
|
|
return default; |
|
|
|
} |
|
|
|
} |
|
|
|
|