Browse Source

Update OpenIddictValidationService to propagate the error details during introspection

pull/965/head
Kévin Chalet 6 years ago
parent
commit
4e006968f3
  1. 26
      src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs
  2. 76
      src/OpenIddict.Validation/OpenIddictValidationService.cs

26
src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs

@ -27,7 +27,7 @@ namespace OpenIddict.Validation
* Introspection response handling: * Introspection response handling:
*/ */
AttachCredentials.Descriptor, AttachCredentials.Descriptor,
AttachAccessToken.Descriptor, AttachToken.Descriptor,
/* /*
* Introspection response handling: * Introspection response handling:
@ -75,16 +75,16 @@ namespace OpenIddict.Validation
} }
/// <summary> /// <summary>
/// Contains the logic responsible of attaching the access token to the introspection request. /// Contains the logic responsible of attaching the token to the introspection request.
/// </summary> /// </summary>
public class AttachAccessToken : IOpenIddictValidationHandler<PrepareIntrospectionRequestContext> public class AttachToken : IOpenIddictValidationHandler<PrepareIntrospectionRequestContext>
{ {
/// <summary> /// <summary>
/// Gets the default descriptor definition assigned to this handler. /// Gets the default descriptor definition assigned to this handler.
/// </summary> /// </summary>
public static OpenIddictValidationHandlerDescriptor Descriptor { get; } public static OpenIddictValidationHandlerDescriptor Descriptor { get; }
= OpenIddictValidationHandlerDescriptor.CreateBuilder<PrepareIntrospectionRequestContext>() = OpenIddictValidationHandlerDescriptor.CreateBuilder<PrepareIntrospectionRequestContext>()
.UseSingletonHandler<AttachAccessToken>() .UseSingletonHandler<AttachToken>()
.SetOrder(AttachCredentials.Descriptor.Order + 100_000) .SetOrder(AttachCredentials.Descriptor.Order + 100_000)
.Build(); .Build();
@ -372,15 +372,19 @@ namespace OpenIddict.Validation
// OpenIddict-based authorization servers always return the actual token type using // OpenIddict-based authorization servers always return the actual token type using
// the special "token_usage" claim, that helps resource servers determine whether the // the special "token_usage" claim, that helps resource servers determine whether the
// introspected token is an access token and thus prevent token substitution attacks. // introspected token is of the expected type and prevent token substitution attacks.
var usage = (string) context.Response[Claims.TokenUsage]; if (!string.IsNullOrEmpty(context.TokenType))
if (!string.IsNullOrEmpty(usage) && !string.Equals(usage, context.TokenType, StringComparison.OrdinalIgnoreCase))
{ {
context.Reject( var usage = (string) context.Response[Claims.TokenUsage];
error: Errors.InvalidToken, if (!string.IsNullOrEmpty(usage) &&
description: "The introspected token is not an access token."); !string.Equals(usage, context.TokenType, StringComparison.OrdinalIgnoreCase))
{
context.Reject(
error: Errors.InvalidToken,
description: "The type of the introspection token doesn't match the expected type.");
return default; return default;
}
} }
return default; return default;

76
src/OpenIddict.Validation/OpenIddictValidationService.cs

@ -90,7 +90,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while preparing the configuration request.") .AppendLine("An error occurred while preparing the configuration request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -115,7 +117,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while sending the configuration request.") .AppendLine("An error occurred while sending the configuration request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -140,7 +144,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while extracting the configuration response.") .AppendLine("An error occurred while extracting the configuration response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -166,7 +172,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while handling the configuration response.") .AppendLine("An error occurred while handling the configuration response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -254,7 +262,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while preparing the cryptography request.") .AppendLine("An error occurred while preparing the cryptography request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -279,7 +289,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while sending the cryptography request.") .AppendLine("An error occurred while sending the cryptography request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -304,7 +316,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while extracting the cryptography response.") .AppendLine("An error occurred while extracting the cryptography response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -330,7 +344,9 @@ namespace OpenIddict.Validation
var message = new StringBuilder() var message = new StringBuilder()
.AppendLine("An error occurred while handling the cryptography response.") .AppendLine("An error occurred while handling the cryptography response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)") .AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)") .AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)") .AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString(); .ToString();
@ -433,6 +449,21 @@ namespace OpenIddict.Validation
await provider.DispatchAsync(context); await provider.DispatchAsync(context);
if (context.IsRejected)
{
var message = new StringBuilder()
.AppendLine("An error occurred while preparing the introspection request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString();
throw new OpenIddictExceptions.GenericException(message,
context.Error, context.ErrorDescription, context.ErrorUri);
}
return context.Request; return context.Request;
} }
@ -445,6 +476,21 @@ namespace OpenIddict.Validation
await provider.DispatchAsync(context); await provider.DispatchAsync(context);
if (context.IsRejected)
{
var message = new StringBuilder()
.AppendLine("An error occurred while sending the introspection request.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString();
throw new OpenIddictExceptions.GenericException(message,
context.Error, context.ErrorDescription, context.ErrorUri);
}
return context.Request; return context.Request;
} }
@ -457,6 +503,21 @@ namespace OpenIddict.Validation
await provider.DispatchAsync(context); await provider.DispatchAsync(context);
if (context.IsRejected)
{
var message = new StringBuilder()
.AppendLine("An error occurred while extracting the introspection response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString();
throw new OpenIddictExceptions.GenericException(message,
context.Error, context.ErrorDescription, context.ErrorUri);
}
return context.Response; return context.Response;
} }
@ -472,6 +533,21 @@ namespace OpenIddict.Validation
await provider.DispatchAsync(context); await provider.DispatchAsync(context);
if (context.IsRejected)
{
var message = new StringBuilder()
.AppendLine("An error occurred while handling the introspection response.")
.AppendFormat("Error: {0}", context.Error ?? "(not available)")
.AppendLine()
.AppendFormat("Error description: {0}", context.ErrorDescription ?? "(not available)")
.AppendLine()
.AppendFormat("Error URI: {0}", context.ErrorUri ?? "(not available)")
.ToString();
throw new OpenIddictExceptions.GenericException(message,
context.Error, context.ErrorDescription, context.ErrorUri);
}
return context.Principal; return context.Principal;
} }
} }

Loading…
Cancel
Save