Browse Source

Declare OpenIddictClientRegistration.ConfigurationManager as a nullable property

pull/1931/head
Kévin Chalet 3 years ago
parent
commit
c8f7c2c5bb
  1. 3
      src/OpenIddict.Abstractions/OpenIddictResources.resx
  2. 3
      src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs
  3. 47
      src/OpenIddict.Client/OpenIddictClientHandlers.cs
  4. 2
      src/OpenIddict.Client/OpenIddictClientRegistration.cs
  5. 15
      src/OpenIddict.Client/OpenIddictClientService.cs

3
src/OpenIddict.Abstractions/OpenIddictResources.resx

@ -1572,6 +1572,9 @@ To apply post-logout redirection responses, create a class implementing 'IOpenId
<data name="ID0421" xml:space="preserve"> <data name="ID0421" xml:space="preserve">
<value>At least one subject type must be supported.</value> <value>At least one subject type must be supported.</value>
</data> </data>
<data name="ID0422" xml:space="preserve">
<value>A configuration manager must be attached to the client registration to be able to resolve the server configuration.</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>

3
src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs

@ -305,7 +305,8 @@ public static partial class OpenIddictClientHandlers
// If validation failed because of an unrecognized key identifier and a client // If validation failed because of an unrecognized key identifier and a client
// registration is available, inform the configuration manager that the configuration // registration is available, inform the configuration manager that the configuration
// MAY have be refreshed by sending a new discovery request to the authorization server. // MAY have be refreshed by sending a new discovery request to the authorization server.
if (context.Registration is not null && result.Exception is SecurityTokenSignatureKeyNotFoundException) if (result.Exception is SecurityTokenSignatureKeyNotFoundException &&
context.Registration.ConfigurationManager is not null)
{ {
context.Registration.ConfigurationManager.RequestRefresh(); context.Registration.ConfigurationManager.RequestRefresh();
} }

47
src/OpenIddict.Client/OpenIddictClientHandlers.cs

@ -423,10 +423,17 @@ public static partial class OpenIddictClientHandlers
throw new InvalidOperationException(SR.GetResourceString(SR.ID0408)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0408));
} }
// Resolve and attach the server configuration to the context if none has been set already.
if (context.Configuration is null)
{
if (context.Registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
try try
{ {
// Resolve and attach the server configuration to the context if none has been set already. context.Configuration = await context.Registration.ConfigurationManager
context.Configuration ??= await context.Registration.ConfigurationManager
.GetConfigurationAsync(context.CancellationToken) .GetConfigurationAsync(context.CancellationToken)
.WaitAsync(context.CancellationToken) ?? .WaitAsync(context.CancellationToken) ??
throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
@ -444,6 +451,7 @@ public static partial class OpenIddictClientHandlers
return; return;
} }
}
// Ensure the selected grant type, if explicitly set, is listed as supported in the configuration. // Ensure the selected grant type, if explicitly set, is listed as supported in the configuration.
if (!string.IsNullOrEmpty(context.GrantType) && if (!string.IsNullOrEmpty(context.GrantType) &&
@ -1019,7 +1027,15 @@ public static partial class OpenIddictClientHandlers
// Note: if the static registration cannot be found in the options, this may indicate // Note: if the static registration cannot be found in the options, this may indicate
// the client was removed after the authorization dance started and thus, can no longer // the client was removed after the authorization dance started and thus, can no longer
// be used to authenticate users. In this case, throw an exception to abort the flow. // be used to authenticate users. In this case, throw an exception to abort the flow.
context.Registration = await _service.GetClientRegistrationByIdAsync(context.RegistrationId, context.CancellationToken); context.Registration ??= await _service.GetClientRegistrationByIdAsync(context.RegistrationId, context.CancellationToken);
// Resolve and attach the server configuration to the context if none has been set already.
if (context.Configuration is null)
{
if (context.Registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
try try
{ {
@ -1044,6 +1060,7 @@ public static partial class OpenIddictClientHandlers
} }
} }
} }
}
/// <summary> /// <summary>
/// Contains the logic responsible for ensuring the issuer parameter, if available, matches the expected issuer. /// Contains the logic responsible for ensuring the issuer parameter, if available, matches the expected issuer.
@ -4209,10 +4226,17 @@ public static partial class OpenIddictClientHandlers
throw new InvalidOperationException(SR.GetResourceString(SR.ID0408)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0408));
} }
// Resolve and attach the server configuration to the context if none has been set already.
if (context.Configuration is null)
{
if (context.Registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
try try
{ {
// Resolve and attach the server configuration to the context if none has been set already. context.Configuration = await context.Registration.ConfigurationManager
context.Configuration ??= await context.Registration.ConfigurationManager
.GetConfigurationAsync(context.CancellationToken) .GetConfigurationAsync(context.CancellationToken)
.WaitAsync(context.CancellationToken) ?? .WaitAsync(context.CancellationToken) ??
throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
@ -4232,6 +4256,7 @@ public static partial class OpenIddictClientHandlers
} }
} }
} }
}
/// <summary> /// <summary>
/// Contains the logic responsible for negotiating the best flow /// Contains the logic responsible for negotiating the best flow
@ -5852,10 +5877,17 @@ public static partial class OpenIddictClientHandlers
throw new InvalidOperationException(SR.GetResourceString(SR.ID0408)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0408));
} }
// Resolve and attach the server configuration to the context if none has been set already.
if (context.Configuration is null)
{
if (context.Registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
try try
{ {
// Resolve and attach the server configuration to the context if none has been set already. context.Configuration = await context.Registration.ConfigurationManager
context.Configuration ??= await context.Registration.ConfigurationManager
.GetConfigurationAsync(context.CancellationToken) .GetConfigurationAsync(context.CancellationToken)
.WaitAsync(context.CancellationToken) ?? .WaitAsync(context.CancellationToken) ??
throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
@ -5875,6 +5907,7 @@ public static partial class OpenIddictClientHandlers
} }
} }
} }
}
/// <summary> /// <summary>
/// Contains the logic responsible for attaching the client identifier to the sign-out request. /// Contains the logic responsible for attaching the client identifier to the sign-out request.

2
src/OpenIddict.Client/OpenIddictClientRegistration.cs

@ -143,7 +143,7 @@ public sealed class OpenIddictClientRegistration
/// <summary> /// <summary>
/// Gets or sets the configuration manager used to retrieve and cache the server configuration. /// Gets or sets the configuration manager used to retrieve and cache the server configuration.
/// </summary> /// </summary>
public IConfigurationManager<OpenIddictConfiguration> ConfigurationManager { get; set; } = default!; public IConfigurationManager<OpenIddictConfiguration>? ConfigurationManager { get; set; }
/// <summary> /// <summary>
/// Gets or sets the URI of the configuration endpoint exposed by the server. /// Gets or sets the URI of the configuration endpoint exposed by the server.

15
src/OpenIddict.Client/OpenIddictClientService.cs

@ -154,6 +154,11 @@ public sealed class OpenIddictClientService
} }
var registration = await GetClientRegistrationAsync(issuer, cancellationToken); var registration = await GetClientRegistrationAsync(issuer, cancellationToken);
if (registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
return await registration.ConfigurationManager return await registration.ConfigurationManager
.GetConfigurationAsync(cancellationToken) .GetConfigurationAsync(cancellationToken)
.WaitAsync(cancellationToken) ?? .WaitAsync(cancellationToken) ??
@ -182,6 +187,11 @@ public sealed class OpenIddictClientService
} }
var registration = await GetClientRegistrationAsync(provider, cancellationToken); var registration = await GetClientRegistrationAsync(provider, cancellationToken);
if (registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
return await registration.ConfigurationManager return await registration.ConfigurationManager
.GetConfigurationAsync(cancellationToken) .GetConfigurationAsync(cancellationToken)
.WaitAsync(cancellationToken) ?? .WaitAsync(cancellationToken) ??
@ -207,6 +217,11 @@ public sealed class OpenIddictClientService
} }
var registration = await GetClientRegistrationByIdAsync(identifier, cancellationToken); var registration = await GetClientRegistrationByIdAsync(identifier, cancellationToken);
if (registration.ConfigurationManager is null)
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0422));
}
return await registration.ConfigurationManager return await registration.ConfigurationManager
.GetConfigurationAsync(cancellationToken) .GetConfigurationAsync(cancellationToken)
.WaitAsync(cancellationToken) ?? .WaitAsync(cancellationToken) ??

Loading…
Cancel
Save