You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
6.6 KiB
142 lines
6.6 KiB
/*
|
|
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|
* the license and the contributors participating to this project.
|
|
*/
|
|
|
|
using System.Collections.Immutable;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace OpenIddict.Client;
|
|
|
|
public static partial class OpenIddictClientHandlers
|
|
{
|
|
public static class Exchange
|
|
{
|
|
public static ImmutableArray<OpenIddictClientHandlerDescriptor> DefaultHandlers { get; } = ImmutableArray.Create(
|
|
/*
|
|
* Token response handling:
|
|
*/
|
|
ValidateWellKnownParameters.Descriptor,
|
|
HandleErrorResponse.Descriptor);
|
|
|
|
/// <summary>
|
|
/// Contains the logic responsible for validating the well-known parameters contained in the token response.
|
|
/// </summary>
|
|
public sealed class ValidateWellKnownParameters : IOpenIddictClientHandler<HandleTokenResponseContext>
|
|
{
|
|
/// <summary>
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
/// </summary>
|
|
public static OpenIddictClientHandlerDescriptor Descriptor { get; }
|
|
= OpenIddictClientHandlerDescriptor.CreateBuilder<HandleTokenResponseContext>()
|
|
.UseSingletonHandler<ValidateWellKnownParameters>()
|
|
.SetOrder(int.MinValue + 100_000)
|
|
.SetType(OpenIddictClientHandlerType.BuiltIn)
|
|
.Build();
|
|
|
|
/// <inheritdoc/>
|
|
public ValueTask HandleAsync(HandleTokenResponseContext context)
|
|
{
|
|
if (context is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(context));
|
|
}
|
|
|
|
foreach (var parameter in context.Response.GetParameters())
|
|
{
|
|
if (!ValidateParameterType(parameter.Key, parameter.Value))
|
|
{
|
|
context.Reject(
|
|
error: Errors.ServerError,
|
|
description: SR.FormatID2107(parameter.Key),
|
|
uri: SR.FormatID8000(SR.ID2107));
|
|
|
|
return default;
|
|
}
|
|
}
|
|
|
|
return default;
|
|
|
|
// Note: in the typical case, the response parameters should be deserialized from a
|
|
// JSON response and thus natively stored as System.Text.Json.JsonElement instances.
|
|
//
|
|
// In the rare cases where the underlying value wouldn't be a JsonElement instance
|
|
// (e.g when custom parameters are manually added to the response), the static
|
|
// conversion operator would take care of converting the underlying value to a
|
|
// JsonElement instance using the same value type as the original parameter value.
|
|
static bool ValidateParameterType(string name, OpenIddictParameter value) => name switch
|
|
{
|
|
// Error parameters MUST be formatted as unique strings:
|
|
Parameters.Error or Parameters.ErrorDescription or Parameters.ErrorUri
|
|
=> ((JsonElement) value).ValueKind is JsonValueKind.String,
|
|
|
|
// The following parameters MUST be formatted as unique strings:
|
|
Parameters.AccessToken or Parameters.IdToken or Parameters.RefreshToken
|
|
=> ((JsonElement) value).ValueKind is JsonValueKind.String,
|
|
|
|
// The following parameters MUST be formatted as numeric dates:
|
|
Parameters.ExpiresIn => (JsonElement) value is { ValueKind: JsonValueKind.Number } element &&
|
|
element.TryGetDecimal(out decimal result) && result is >= 0,
|
|
|
|
// Parameters that are not in the well-known list can be of any type.
|
|
_ => true
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Contains the logic responsible for surfacing potential errors from the token response.
|
|
/// </summary>
|
|
public sealed class HandleErrorResponse : IOpenIddictClientHandler<HandleTokenResponseContext>
|
|
{
|
|
/// <summary>
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
/// </summary>
|
|
public static OpenIddictClientHandlerDescriptor Descriptor { get; }
|
|
= OpenIddictClientHandlerDescriptor.CreateBuilder<HandleTokenResponseContext>()
|
|
.UseSingletonHandler<HandleErrorResponse>()
|
|
.SetOrder(ValidateWellKnownParameters.Descriptor.Order + 1_000)
|
|
.SetType(OpenIddictClientHandlerType.BuiltIn)
|
|
.Build();
|
|
|
|
/// <inheritdoc/>
|
|
public ValueTask HandleAsync(HandleTokenResponseContext context)
|
|
{
|
|
if (context is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(context));
|
|
}
|
|
|
|
// For more information, see https://datatracker.ietf.org/doc/html/rfc6749#section-5.2.
|
|
if (!string.IsNullOrEmpty(context.Response.Error))
|
|
{
|
|
context.Logger.LogInformation(SR.GetResourceString(SR.ID6206), context.Response);
|
|
|
|
context.Reject(
|
|
error: context.Response.Error switch
|
|
{
|
|
Errors.AccessDenied => Errors.AccessDenied,
|
|
Errors.AuthorizationPending => Errors.AuthorizationPending,
|
|
Errors.ExpiredToken => Errors.ExpiredToken,
|
|
Errors.InvalidClient => Errors.InvalidClient,
|
|
Errors.InvalidGrant => Errors.InvalidGrant,
|
|
Errors.InvalidScope => Errors.InvalidScope,
|
|
Errors.InvalidRequest => Errors.InvalidRequest,
|
|
Errors.SlowDown => Errors.SlowDown,
|
|
Errors.UnauthorizedClient => Errors.UnauthorizedClient,
|
|
Errors.UnsupportedGrantType => Errors.UnsupportedGrantType,
|
|
_ => Errors.ServerError
|
|
},
|
|
description: SR.GetResourceString(SR.ID2147),
|
|
uri: SR.FormatID8000(SR.ID2147));
|
|
|
|
return default;
|
|
}
|
|
|
|
return default;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|