diff --git a/src/IdentityServer/Validation/Default/AuthorizeRequestValidator.cs b/src/IdentityServer/Validation/Default/AuthorizeRequestValidator.cs index 6ff2b65a5..952b3a12e 100644 --- a/src/IdentityServer/Validation/Default/AuthorizeRequestValidator.cs +++ b/src/IdentityServer/Validation/Default/AuthorizeRequestValidator.cs @@ -463,26 +463,32 @@ private async Task ValidateScopeAndResourceAsy ////////////////////////////////////////////////////////// // check for resource indicators and valid format ////////////////////////////////////////////////////////// - var resourceIndicators = request.Raw.GetValues(OidcConstants.AuthorizeRequest.Resource) ?? Enumerable.Empty(); - - if (resourceIndicators?.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength) == true) + var resourceIndicators = request.Raw.GetValues(OidcConstants.AuthorizeRequest.Resource); + if (resourceIndicators == null) { - return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + request.RequestedResourceIndicators = []; } - - if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + else { - return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Invalid resource indicator format"); - } + if (resourceIndicators.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength)) + { + return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + } - // we don't want to allow resource indicators when "token" is requested to authorize endpoint - if (request.GrantType == GrantType.Implicit && resourceIndicators.Any()) - { - // todo: correct error? - return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicators not allowed for response_type 'token'."); + if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + { + return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Invalid resource indicator format"); + } + + // we don't want to allow resource indicators when "token" is requested to authorize endpoint + if (request.GrantType == GrantType.Implicit && resourceIndicators.Length != 0) + { + // todo: correct error? + return Invalid(request, OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicators not allowed for response_type 'token'."); + } + + request.RequestedResourceIndicators = resourceIndicators; } - - request.RequestedResourceIndicators = resourceIndicators; ////////////////////////////////////////////////////////// // check if scopes are valid/supported and check for resource scopes diff --git a/src/IdentityServer/Validation/Default/BackchannelAuthenticationRequestValidator.cs b/src/IdentityServer/Validation/Default/BackchannelAuthenticationRequestValidator.cs index e043000aa..a492233e6 100644 --- a/src/IdentityServer/Validation/Default/BackchannelAuthenticationRequestValidator.cs +++ b/src/IdentityServer/Validation/Default/BackchannelAuthenticationRequestValidator.cs @@ -136,19 +136,25 @@ public async Task ValidateRequ ////////////////////////////////////////////////////////// // check for resource indicators and valid format ////////////////////////////////////////////////////////// - var resourceIndicators = _validatedRequest.Raw.GetValues(OidcConstants.AuthorizeRequest.Resource) ?? Enumerable.Empty(); - - if (resourceIndicators?.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength) == true) + var resourceIndicators = _validatedRequest.Raw.GetValues(OidcConstants.AuthorizeRequest.Resource); + if (resourceIndicators == null) { - return Invalid(OidcConstants.BackchannelAuthenticationRequestErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + _validatedRequest.RequestedResourceIndicators = []; } - - if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + else { - return Invalid(OidcConstants.BackchannelAuthenticationRequestErrors.InvalidTarget, "Invalid resource indicator format"); - } + if (resourceIndicators.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength)) + { + return Invalid(OidcConstants.BackchannelAuthenticationRequestErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + } - _validatedRequest.RequestedResourceIndicators = resourceIndicators?.ToList(); + if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + { + return Invalid(OidcConstants.BackchannelAuthenticationRequestErrors.InvalidTarget, "Invalid resource indicator format"); + } + + _validatedRequest.RequestedResourceIndicators = resourceIndicators; + } ////////////////////////////////////////////////////////// // check if scopes are valid/supported and check for resource scopes diff --git a/src/IdentityServer/Validation/Default/TokenRequestValidator.cs b/src/IdentityServer/Validation/Default/TokenRequestValidator.cs index 4a8a516e1..0a4f9bc5e 100644 --- a/src/IdentityServer/Validation/Default/TokenRequestValidator.cs +++ b/src/IdentityServer/Validation/Default/TokenRequestValidator.cs @@ -152,24 +152,31 @@ public async Task ValidateRequestAsync(TokenReques ////////////////////////////////////////////////////////// // check for resource indicator and basic formatting ////////////////////////////////////////////////////////// - var resourceIndicators = parameters.GetValues(OidcConstants.TokenRequest.Resource) ?? Enumerable.Empty(); - - if (resourceIndicators?.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength) == true) + var resourceIndicators = parameters.GetValues(OidcConstants.TokenRequest.Resource); + if (resourceIndicators == null) { - return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + _validatedRequest.RequestedResourceIndicator = null; } - - if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + else { - return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, "Invalid resource indicator format"); - } + if (resourceIndicators.Any(x => x.Length > _options.InputLengthRestrictions.ResourceIndicatorMaxLength)) + { + return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, "Resource indicator maximum length exceeded"); + } - if (resourceIndicators.Count() > 1) - { - return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, "Multiple resource indicators not supported on token endpoint."); - } + if (!resourceIndicators.AreValidResourceIndicatorFormat(_logger)) + { + return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, "Invalid resource indicator format"); + } - _validatedRequest.RequestedResourceIndicator = resourceIndicators.SingleOrDefault(); + if (resourceIndicators.Length > 1) + { + return Invalid(OidcConstants.AuthorizeErrors.InvalidTarget, + "Multiple resource indicators not supported on token endpoint."); + } + + _validatedRequest.RequestedResourceIndicator = resourceIndicators.SingleOrDefault(); + } ////////////////////////////////////////////////////////// // proof token validation