Browse Source

Add a SetProductInformation() overload accepting an assembly from which the product information is derived

pull/1519/head
Kévin Chalet 4 years ago
parent
commit
421a668948
  1. 6
      sandbox/OpenIddict.Sandbox.AspNet.Client/Startup.cs
  2. 6
      sandbox/OpenIddict.Sandbox.AspNet.Server/Startup.cs
  3. 6
      sandbox/OpenIddict.Sandbox.AspNetCore.Client/Startup.cs
  4. 7
      sandbox/OpenIddict.Sandbox.AspNetCore.Server/Startup.cs
  5. 3
      src/OpenIddict.Abstractions/OpenIddictResources.resx
  6. 53
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs
  7. 2
      src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs
  8. 2
      src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs
  9. 2
      src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs
  10. 2
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs
  11. 2
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs
  12. 4
      src/OpenIddict.Server/OpenIddictServerBuilder.cs
  13. 55
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs
  14. 2
      src/OpenIddict.Validation/OpenIddictValidationBuilder.cs

6
sandbox/OpenIddict.Sandbox.AspNet.Client/Startup.cs

@ -92,9 +92,11 @@ namespace OpenIddict.Sandbox.AspNet.Client
.EnableRedirectionEndpointPassthrough() .EnableRedirectionEndpointPassthrough()
.EnablePostLogoutRedirectionEndpointPassthrough(); .EnablePostLogoutRedirectionEndpointPassthrough();
// Register the System.Net.Http integration and configure the HTTP options. // Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp() options.UseSystemNetHttp()
.SetProductInformation("DemoApp", "1.0.0"); .SetProductInformation(typeof(Startup).Assembly);
// Add a client registration matching the client application definition in the server project. // Add a client registration matching the client application definition in the server project.
options.AddRegistration(new OpenIddictClientRegistration options.AddRegistration(new OpenIddictClientRegistration

6
sandbox/OpenIddict.Sandbox.AspNet.Server/Startup.cs

@ -140,9 +140,11 @@ namespace OpenIddict.Sandbox.AspNet.Server
options.UseOwin() options.UseOwin()
.EnableRedirectionEndpointPassthrough(); .EnableRedirectionEndpointPassthrough();
// Register the System.Net.Http integration and configure the HTTP options. // Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp() options.UseSystemNetHttp()
.SetProductInformation("DemoApp", "1.0.0"); .SetProductInformation(typeof(Startup).Assembly);
// Register the Web providers integrations. // Register the Web providers integrations.
options.UseWebProviders() options.UseWebProviders()

6
sandbox/OpenIddict.Sandbox.AspNetCore.Client/Startup.cs

@ -102,9 +102,11 @@ public class Startup
.EnableRedirectionEndpointPassthrough() .EnableRedirectionEndpointPassthrough()
.EnablePostLogoutRedirectionEndpointPassthrough(); .EnablePostLogoutRedirectionEndpointPassthrough();
// Register the System.Net.Http integration and configure the HTTP options. // Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp() options.UseSystemNetHttp()
.SetProductInformation("DemoApp", "1.0.0"); .SetProductInformation(typeof(Startup).Assembly);
// Add a client registration matching the client application definition in the server project. // Add a client registration matching the client application definition in the server project.
options.AddRegistration(new OpenIddictClientRegistration options.AddRegistration(new OpenIddictClientRegistration

7
sandbox/OpenIddict.Sandbox.AspNetCore.Server/Startup.cs

@ -1,4 +1,3 @@
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OpenIddict.Sandbox.AspNetCore.Server.Models; using OpenIddict.Sandbox.AspNetCore.Server.Models;
@ -87,9 +86,11 @@ public class Startup
.EnableStatusCodePagesIntegration() .EnableStatusCodePagesIntegration()
.EnableRedirectionEndpointPassthrough(); .EnableRedirectionEndpointPassthrough();
// Register the System.Net.Http integration and configure the HTTP options. // Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp() options.UseSystemNetHttp()
.SetProductInformation("DemoApp", "1.0.0"); .SetProductInformation(typeof(Startup).Assembly);
// Register the Web providers integrations. // Register the Web providers integrations.
options.UseWebProviders() options.UseWebProviders()

3
src/OpenIddict.Abstractions/OpenIddictResources.resx

@ -1331,6 +1331,9 @@ Alternatively, you can disable the token storage feature by calling 'services.Ad
<data name="ID0344" xml:space="preserve"> <data name="ID0344" xml:space="preserve">
<value>The request forgery protection claim cannot be resolved from the sign-out context.</value> <value>The request forgery protection claim cannot be resolved from the sign-out context.</value>
</data> </data>
<data name="ID0345" xml:space="preserve">
<value>The product name cannot be null or empty.</value>
</data>
<data name="ID2000" xml:space="preserve"> <data name="ID2000" xml:space="preserve">
<value>The security token is missing.</value> <value>The security token is missing.</value>
</data> </data>

53
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs

@ -6,6 +6,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Reflection;
using OpenIddict.Client.SystemNetHttp; using OpenIddict.Client.SystemNetHttp;
using Polly; using Polly;
@ -52,8 +53,15 @@ public class OpenIddictClientSystemNetHttpBuilder
/// </summary> /// </summary>
/// <param name="policy">The HTTP Polly error policy.</param> /// <param name="policy">The HTTP Polly error policy.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns>
public OpenIddictClientSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy<HttpResponseMessage>? policy) public OpenIddictClientSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy<HttpResponseMessage> policy)
=> Configure(options => options.HttpErrorPolicy = policy); {
if (policy is null)
{
throw new ArgumentNullException(nameof(policy));
}
return Configure(options => options.HttpErrorPolicy = policy);
}
/// <summary> /// <summary>
/// Sets the product information used in the user agent header that is attached /// Sets the product information used in the user agent header that is attached
@ -61,8 +69,15 @@ public class OpenIddictClientSystemNetHttpBuilder
/// </summary> /// </summary>
/// <param name="information">The product information.</param> /// <param name="information">The product information.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns>
public OpenIddictClientSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue? information) public OpenIddictClientSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue information)
=> Configure(options => options.ProductInformation = information); {
if (information is null)
{
throw new ArgumentNullException(nameof(information));
}
return Configure(options => options.ProductInformation = information);
}
/// <summary> /// <summary>
/// Sets the product information used in the user agent header that is attached /// Sets the product information used in the user agent header that is attached
@ -71,8 +86,34 @@ public class OpenIddictClientSystemNetHttpBuilder
/// <param name="name">The product name.</param> /// <param name="name">The product name.</param>
/// <param name="version">The product version.</param> /// <param name="version">The product version.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns>
public OpenIddictClientSystemNetHttpBuilder SetProductInformation(string? name, string? version) public OpenIddictClientSystemNetHttpBuilder SetProductInformation(string name, string? version)
=> SetProductInformation(!string.IsNullOrEmpty(name) ? new ProductInfoHeaderValue(name, version) : null); {
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0345), nameof(name));
}
return SetProductInformation(new ProductInfoHeaderValue(name, version));
}
/// <summary>
/// Sets the product information used in the user agent header that is attached
/// to the backchannel HTTP requests sent to the authorization server based
/// on the identity of the specified .NET assembly (name and version).
/// </summary>
/// <param name="assembly">The assembly from which the product information is created.</param>
/// <returns>The <see cref="OpenIddictClientSystemNetHttpBuilder"/>.</returns>
public OpenIddictClientSystemNetHttpBuilder SetProductInformation(Assembly assembly)
{
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
return SetProductInformation(new ProductInfoHeaderValue(
productName: assembly.GetName().Name!,
productVersion: assembly.GetName().Version!.ToString()));
}
/// <inheritdoc/> /// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]

2
src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs

@ -160,7 +160,7 @@ public class OpenIddictScopeCache<TScope> : IOpenIddictScopeCache<TScope>, IDisp
/// <inheritdoc/> /// <inheritdoc/>
public IAsyncEnumerable<TScope> FindByNamesAsync(ImmutableArray<string> names, CancellationToken cancellationToken) public IAsyncEnumerable<TScope> FindByNamesAsync(ImmutableArray<string> names, CancellationToken cancellationToken)
{ {
if (names.Any(name => string.IsNullOrEmpty(name))) if (names.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names)); throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names));
} }

2
src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs

@ -274,7 +274,7 @@ public class OpenIddictScopeManager<TScope> : IOpenIddictScopeManager where TSco
public virtual IAsyncEnumerable<TScope> FindByNamesAsync( public virtual IAsyncEnumerable<TScope> FindByNamesAsync(
ImmutableArray<string> names, CancellationToken cancellationToken = default) ImmutableArray<string> names, CancellationToken cancellationToken = default)
{ {
if (names.Any(name => string.IsNullOrEmpty(name))) if (names.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names)); throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names));
} }

2
src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs

@ -161,7 +161,7 @@ public class OpenIddictEntityFrameworkScopeStore<TScope, TContext, TKey> : IOpen
public virtual IAsyncEnumerable<TScope> FindByNamesAsync( public virtual IAsyncEnumerable<TScope> FindByNamesAsync(
ImmutableArray<string> names, CancellationToken cancellationToken) ImmutableArray<string> names, CancellationToken cancellationToken)
{ {
if (names.Any(name => string.IsNullOrEmpty(name))) if (names.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names)); throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names));
} }

2
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs

@ -177,7 +177,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore<TScope, TContext, TKey> : I
public virtual IAsyncEnumerable<TScope> FindByNamesAsync( public virtual IAsyncEnumerable<TScope> FindByNamesAsync(
ImmutableArray<string> names, CancellationToken cancellationToken) ImmutableArray<string> names, CancellationToken cancellationToken)
{ {
if (names.Any(name => string.IsNullOrEmpty(name))) if (names.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names)); throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names));
} }

2
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs

@ -129,7 +129,7 @@ public class OpenIddictMongoDbScopeStore<TScope> : IOpenIddictScopeStore<TScope>
/// <inheritdoc/> /// <inheritdoc/>
public virtual IAsyncEnumerable<TScope> FindByNamesAsync(ImmutableArray<string> names, CancellationToken cancellationToken) public virtual IAsyncEnumerable<TScope> FindByNamesAsync(ImmutableArray<string> names, CancellationToken cancellationToken)
{ {
if (names.Any(name => string.IsNullOrEmpty(name))) if (names.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names)); throw new ArgumentException(SR.GetResourceString(SR.ID0203), nameof(names));
} }

4
src/OpenIddict.Server/OpenIddictServerBuilder.cs

@ -1676,7 +1676,7 @@ public class OpenIddictServerBuilder
throw new ArgumentNullException(nameof(claims)); throw new ArgumentNullException(nameof(claims));
} }
if (claims.Any(claim => string.IsNullOrEmpty(claim))) if (claims.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0073), nameof(claims)); throw new ArgumentException(SR.GetResourceString(SR.ID0073), nameof(claims));
} }
@ -1697,7 +1697,7 @@ public class OpenIddictServerBuilder
throw new ArgumentNullException(nameof(scopes)); throw new ArgumentNullException(nameof(scopes));
} }
if (scopes.Any(scope => string.IsNullOrEmpty(scope))) if (scopes.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0074), nameof(scopes)); throw new ArgumentException(SR.GetResourceString(SR.ID0074), nameof(scopes));
} }

55
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs

@ -6,6 +6,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Reflection;
using OpenIddict.Validation.SystemNetHttp; using OpenIddict.Validation.SystemNetHttp;
using Polly; using Polly;
@ -48,12 +49,19 @@ public class OpenIddictValidationSystemNetHttpBuilder
} }
/// <summary> /// <summary>
/// Replaces the default HTTP error policy used by the OpenIddict validation services. /// Replaces the default HTTP error policy used by the OpenIddict client services.
/// </summary> /// </summary>
/// <param name="policy">The HTTP Polly error policy.</param> /// <param name="policy">The HTTP Polly error policy.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns>
public OpenIddictValidationSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy<HttpResponseMessage>? policy) public OpenIddictValidationSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy<HttpResponseMessage> policy)
=> Configure(options => options.HttpErrorPolicy = policy); {
if (policy is null)
{
throw new ArgumentNullException(nameof(policy));
}
return Configure(options => options.HttpErrorPolicy = policy);
}
/// <summary> /// <summary>
/// Sets the product information used in the user agent header that is attached /// Sets the product information used in the user agent header that is attached
@ -61,8 +69,15 @@ public class OpenIddictValidationSystemNetHttpBuilder
/// </summary> /// </summary>
/// <param name="information">The product information.</param> /// <param name="information">The product information.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns>
public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue? information) public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue information)
=> Configure(options => options.ProductInformation = information); {
if (information is null)
{
throw new ArgumentNullException(nameof(information));
}
return Configure(options => options.ProductInformation = information);
}
/// <summary> /// <summary>
/// Sets the product information used in the user agent header that is attached /// Sets the product information used in the user agent header that is attached
@ -71,8 +86,34 @@ public class OpenIddictValidationSystemNetHttpBuilder
/// <param name="name">The product name.</param> /// <param name="name">The product name.</param>
/// <param name="version">The product version.</param> /// <param name="version">The product version.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns> /// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns>
public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(string? name, string? version) public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(string name, string? version)
=> SetProductInformation(!string.IsNullOrEmpty(name) ? new ProductInfoHeaderValue(name, version) : null); {
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0345), nameof(name));
}
return SetProductInformation(new ProductInfoHeaderValue(name, version));
}
/// <summary>
/// Sets the product information used in the user agent header that is attached
/// to the backchannel HTTP requests sent to the authorization server based
/// on the identity of the specified .NET assembly (name and version).
/// </summary>
/// <param name="assembly">The assembly from which the product information is created.</param>
/// <returns>The <see cref="OpenIddictValidationSystemNetHttpBuilder"/>.</returns>
public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(Assembly assembly)
{
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
return SetProductInformation(new ProductInfoHeaderValue(
productName: assembly.GetName().Name!,
productVersion: assembly.GetName().Version!.ToString()));
}
/// <inheritdoc/> /// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]

2
src/OpenIddict.Validation/OpenIddictValidationBuilder.cs

@ -358,7 +358,7 @@ public class OpenIddictValidationBuilder
throw new ArgumentNullException(nameof(audiences)); throw new ArgumentNullException(nameof(audiences));
} }
if (audiences.Any(audience => string.IsNullOrEmpty(audience))) if (audiences.Any(string.IsNullOrEmpty))
{ {
throw new ArgumentException(SR.GetResourceString(SR.ID0123), nameof(audiences)); throw new ArgumentException(SR.GetResourceString(SR.ID0123), nameof(audiences));
} }

Loading…
Cancel
Save