|
|
@ -40,7 +40,73 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
.UseSingletonHandler<PrepareGetHttpRequest<TContext>>() |
|
|
.UseSingletonHandler<PrepareGetHttpRequest<TContext>>() |
|
|
.SetOrder(int.MaxValue - 100_000) |
|
|
.SetOrder(int.MinValue + 100_000) |
|
|
|
|
|
.Build(); |
|
|
|
|
|
|
|
|
|
|
|
public ValueTask HandleAsync([NotNull] TContext context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (context == null) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, context.Address); |
|
|
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
|
|
|
|
|
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8")); |
|
|
|
|
|
|
|
|
|
|
|
// Store the HttpRequestMessage in the transaction properties.
|
|
|
|
|
|
context.Transaction.Properties[typeof(HttpRequestMessage).FullName] = request; |
|
|
|
|
|
|
|
|
|
|
|
return default; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Contains the logic responsible of preparing an HTTP POST request message.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PreparePostHttpRequest<TContext> : IOpenIddictValidationHandler<TContext> where TContext : BaseExternalContext |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static OpenIddictValidationHandlerDescriptor Descriptor { get; } |
|
|
|
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
|
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
|
|
|
.UseSingletonHandler<PreparePostHttpRequest<TContext>>() |
|
|
|
|
|
.SetOrder(PrepareGetHttpRequest<TContext>.Descriptor.Order + 1_000) |
|
|
|
|
|
.Build(); |
|
|
|
|
|
|
|
|
|
|
|
public ValueTask HandleAsync([NotNull] TContext context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (context == null) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, context.Address); |
|
|
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
|
|
|
|
|
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8")); |
|
|
|
|
|
|
|
|
|
|
|
// Store the HttpRequestMessage in the transaction properties.
|
|
|
|
|
|
context.Transaction.Properties[typeof(HttpRequestMessage).FullName] = request; |
|
|
|
|
|
|
|
|
|
|
|
return default; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Contains the logic responsible of attaching the query string parameters to the HTTP request.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AttachQueryStringParameters<TContext> : IOpenIddictValidationHandler<TContext> where TContext : BaseExternalContext |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static OpenIddictValidationHandlerDescriptor Descriptor { get; } |
|
|
|
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
|
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
|
|
|
.UseSingletonHandler<AttachQueryStringParameters<TContext>>() |
|
|
|
|
|
.SetOrder(AttachFormParameters<TContext>.Descriptor.Order - 100_000) |
|
|
.Build(); |
|
|
.Build(); |
|
|
|
|
|
|
|
|
public async ValueTask HandleAsync([NotNull] TContext context) |
|
|
public async ValueTask HandleAsync([NotNull] TContext context) |
|
|
@ -50,6 +116,14 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved,
|
|
|
|
|
|
// this may indicate that the request was incorrectly processed by another client stack.
|
|
|
|
|
|
var request = context.Transaction.GetHttpRequestMessage(); |
|
|
|
|
|
if (request == null) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new InvalidOperationException("The System.Net.Http request cannot be resolved."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Note: System.Net.Http doesn't expose convenient methods allowing to create
|
|
|
// Note: System.Net.Http doesn't expose convenient methods allowing to create
|
|
|
// query strings from existing key/value pairs. To work around this limitation,
|
|
|
// query strings from existing key/value pairs. To work around this limitation,
|
|
|
// a FormUrlEncodedContent is instantiated and used to manually create the URL.
|
|
|
// a FormUrlEncodedContent is instantiated and used to manually create the URL.
|
|
|
@ -60,24 +134,19 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
from value in values |
|
|
from value in values |
|
|
select new KeyValuePair<string, string>(parameter.Key, value)); |
|
|
select new KeyValuePair<string, string>(parameter.Key, value)); |
|
|
|
|
|
|
|
|
var builder = new UriBuilder(context.Address) |
|
|
var builder = new UriBuilder(request.RequestUri) |
|
|
{ |
|
|
{ |
|
|
Query = await content.ReadAsStringAsync() |
|
|
Query = await content.ReadAsStringAsync() |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, builder.Uri); |
|
|
request.RequestUri = builder.Uri; |
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
|
|
|
|
|
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8")); |
|
|
|
|
|
|
|
|
|
|
|
// Store the HttpRequestMessage in the transaction properties.
|
|
|
|
|
|
context.Transaction.Properties[typeof(HttpRequestMessage).FullName] = request; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Contains the logic responsible of preparing an HTTP POST request message.
|
|
|
/// Contains the logic responsible of attaching the form parameters to the HTTP request.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class PreparePostHttpRequest<TContext> : IOpenIddictValidationHandler<TContext> where TContext : BaseExternalContext |
|
|
public class AttachFormParameters<TContext> : IOpenIddictValidationHandler<TContext> where TContext : BaseExternalContext |
|
|
{ |
|
|
{ |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
/// Gets the default descriptor definition assigned to this handler.
|
|
|
@ -85,8 +154,8 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
public static OpenIddictValidationHandlerDescriptor Descriptor { get; } |
|
|
public static OpenIddictValidationHandlerDescriptor Descriptor { get; } |
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
= OpenIddictValidationHandlerDescriptor.CreateBuilder<TContext>() |
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
.AddFilter<RequireHttpMetadataAddress>() |
|
|
.UseSingletonHandler<PreparePostHttpRequest<TContext>>() |
|
|
.UseSingletonHandler<AttachFormParameters<TContext>>() |
|
|
.SetOrder(PrepareGetHttpRequest<TContext>.Descriptor.Order - 1_000) |
|
|
.SetOrder(int.MaxValue - 100_000) |
|
|
.Build(); |
|
|
.Build(); |
|
|
|
|
|
|
|
|
public ValueTask HandleAsync([NotNull] TContext context) |
|
|
public ValueTask HandleAsync([NotNull] TContext context) |
|
|
@ -96,9 +165,13 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
throw new ArgumentNullException(nameof(context)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, context.Address); |
|
|
// This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved,
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
|
|
// this may indicate that the request was incorrectly processed by another client stack.
|
|
|
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8")); |
|
|
var request = context.Transaction.GetHttpRequestMessage(); |
|
|
|
|
|
if (request == null) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new InvalidOperationException("The System.Net.Http request cannot be resolved."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
request.Content = new FormUrlEncodedContent( |
|
|
request.Content = new FormUrlEncodedContent( |
|
|
from parameter in context.Request.GetParameters() |
|
|
from parameter in context.Request.GetParameters() |
|
|
@ -107,9 +180,6 @@ namespace OpenIddict.Validation.SystemNetHttp |
|
|
from value in values |
|
|
from value in values |
|
|
select new KeyValuePair<string, string>(parameter.Key, value)); |
|
|
select new KeyValuePair<string, string>(parameter.Key, value)); |
|
|
|
|
|
|
|
|
// Store the HttpRequestMessage in the transaction properties.
|
|
|
|
|
|
context.Transaction.Properties[typeof(HttpRequestMessage).FullName] = request; |
|
|
|
|
|
|
|
|
|
|
|
return default; |
|
|
return default; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|