From 2b8211a8fc4bcbaa2239961788866da5263a3619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 5 Mar 2023 21:27:46 +0100 Subject: [PATCH] Introduce dynamic options to disable nonce validation and add Dropbox to the list of supported providers --- .../OpenIddictClientWebIntegrationHandlers.cs | 45 +++- ...penIddictClientWebIntegrationProviders.xml | 19 ++ .../OpenIddictClientEvents.cs | 208 +++++++++++------- .../OpenIddictClientExtensions.cs | 2 + .../OpenIddictClientHandlerFilters.cs | 34 +++ .../OpenIddictClientHandlers.cs | 2 + .../OpenIddictServerEvents.cs | 200 ++++++++++------- .../OpenIddictValidationEvents.cs | 20 +- 8 files changed, 365 insertions(+), 165 deletions(-) diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs index 4bc71604..f65fa7ce 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs @@ -26,6 +26,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers AttachTokenRequestNonStandardClientCredentials.Descriptor, AdjustRedirectUriInTokenRequest.Descriptor, OverrideValidatedBackchannelTokens.Descriptor, + DisableBackchannelIdentityTokenNonceValidation.Descriptor, AttachAdditionalUserinfoRequestParameters.Descriptor, PopulateUserinfoTokenPrincipalFromTokenResponse.Descriptor, @@ -313,6 +314,48 @@ public static partial class OpenIddictClientWebIntegrationHandlers } } + /// + /// Contains the logic responsible for disabling the backchannel + /// identity token nonce validation for the providers that require it. + /// + public sealed class DisableBackchannelIdentityTokenNonceValidation : IOpenIddictClientHandler + { + /// + /// Gets the default descriptor definition assigned to this handler. + /// + public static OpenIddictClientHandlerDescriptor Descriptor { get; } + = OpenIddictClientHandlerDescriptor.CreateBuilder() + .UseSingletonHandler() + .SetOrder(ValidateBackchannelIdentityTokenNonce.Descriptor.Order - 500) + .SetType(OpenIddictClientHandlerType.BuiltIn) + .Build(); + + /// + public ValueTask HandleAsync(ProcessAuthenticationContext context) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + // Note: despite implementing OpenID Connect, some providers are known to implement the + // specification incorrectly and either don't support the "nonce" authorization request + // parameter, don't include it in the issued identity tokens or flow an unexpected value. + // + // Despite being an important security feature, nonce validation is explicitly disabled + // for the providers that are known to cause errors when nonce validation is enforced. + + context.DisableBackchannelIdentityTokenNonceValidation = context.Registration.ProviderName switch + { + Providers.Dropbox => true, // Dropbox doesn't include the nonce in the identity tokens. + + _ => context.DisableBackchannelIdentityTokenNonceValidation + }; + + return default; + } + } + /// /// Contains the logic responsible for attaching additional parameters /// to the userinfo request for the providers that require it. @@ -517,7 +560,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers } /// - /// Contains the logic responsible for overriding response mode for providers that require it. + /// Contains the logic responsible for overriding the response mode for the providers that require it. /// public sealed class OverrideResponseMode : IOpenIddictClientHandler { diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml index 7ce9f39e..3e820776 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml @@ -159,6 +159,25 @@ + + + + + + + + + +