|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 3 | + * See https://github.com/openiddict/openiddict-core for more information concerning |
| 4 | + * the license and the contributors participating to this project. |
| 5 | + */ |
| 6 | + |
| 7 | +using System.Collections.Immutable; |
| 8 | +using System.Text.Json; |
| 9 | +using OpenIddict.Extensions; |
| 10 | +using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants; |
| 11 | + |
| 12 | +namespace OpenIddict.Client.WebIntegration; |
| 13 | + |
| 14 | +public static partial class OpenIddictClientWebIntegrationHandlers |
| 15 | +{ |
| 16 | + public static class Introspection |
| 17 | + { |
| 18 | + public static ImmutableArray<OpenIddictClientHandlerDescriptor> DefaultHandlers { get; } = |
| 19 | + [ |
| 20 | + /* |
| 21 | + * Introspection response extraction: |
| 22 | + */ |
| 23 | + MapNonStandardResponseParameters.Descriptor |
| 24 | + ]; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Contains the logic responsible for mapping non-standard response parameters |
| 28 | + /// to their standard equivalent for the providers that require it. |
| 29 | + /// </summary> |
| 30 | + public sealed class MapNonStandardResponseParameters : IOpenIddictClientHandler<ExtractIntrospectionResponseContext> |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Gets the default descriptor definition assigned to this handler. |
| 34 | + /// </summary> |
| 35 | + public static OpenIddictClientHandlerDescriptor Descriptor { get; } |
| 36 | + = OpenIddictClientHandlerDescriptor.CreateBuilder<ExtractIntrospectionResponseContext>() |
| 37 | + .UseSingletonHandler<MapNonStandardResponseParameters>() |
| 38 | + .SetOrder(int.MaxValue - 50_000) |
| 39 | + .SetType(OpenIddictClientHandlerType.BuiltIn) |
| 40 | + .Build(); |
| 41 | + |
| 42 | + /// <inheritdoc/> |
| 43 | + public ValueTask HandleAsync(ExtractIntrospectionResponseContext context) |
| 44 | + { |
| 45 | + if (context is null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(context)); |
| 48 | + } |
| 49 | + |
| 50 | + if (context.Response is null) |
| 51 | + { |
| 52 | + return default; |
| 53 | + } |
| 54 | + |
| 55 | + // Note: NetSuite returns the "scope" parameter as a non-standard JSON array of strings, |
| 56 | + // which requires converting it to a space-separated string to ensure the response is |
| 57 | + // not rejected by OpenIddict when validating the type of the well-known "scope" claim. |
| 58 | + if (context.Registration.ProviderType is ProviderTypes.NetSuite && |
| 59 | + (JsonElement?) context.Response[Parameters.Scope] is { ValueKind: JsonValueKind.Array } element && |
| 60 | + OpenIddictHelpers.ValidateArrayElements(element, JsonValueKind.String)) |
| 61 | + { |
| 62 | + var scopes = new HashSet<string>(StringComparer.Ordinal); |
| 63 | + |
| 64 | + foreach (var item in element.EnumerateArray()) |
| 65 | + { |
| 66 | + var scope = item.GetString()?.ToLowerInvariant(); |
| 67 | + if (!string.IsNullOrEmpty(scope)) |
| 68 | + { |
| 69 | + scopes.Add(scope); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + context.Response.Scope = string.Join(" ", scopes); |
| 74 | + } |
| 75 | + |
| 76 | + return default; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments