Replace INonceCache by IDistributedCache
This commit is contained in:
parent
6ae37717e8
commit
102f113e2b
|
|
@ -12,6 +12,7 @@ using Microsoft.AspNet.Authentication.Notifications;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Authentication;
|
using Microsoft.AspNet.Http.Authentication;
|
||||||
using Microsoft.AspNet.Http.Features.Authentication;
|
using Microsoft.AspNet.Http.Features.Authentication;
|
||||||
|
using Microsoft.Framework.Caching.Distributed;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Microsoft.IdentityModel.Protocols;
|
using Microsoft.IdentityModel.Protocols;
|
||||||
|
|
@ -149,13 +150,18 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
if (Options.ProtocolValidator.RequireNonce)
|
if (Options.ProtocolValidator.RequireNonce)
|
||||||
{
|
{
|
||||||
message.Nonce = Options.ProtocolValidator.GenerateNonce();
|
message.Nonce = Options.ProtocolValidator.GenerateNonce();
|
||||||
if (Options.NonceCache != null)
|
if (Options.CacheNonces)
|
||||||
{
|
{
|
||||||
if (!Options.NonceCache.TryAddNonce(message.Nonce))
|
if (await Options.NonceCache.GetAsync(message.Nonce) != null)
|
||||||
{
|
{
|
||||||
Logger.LogError(Resources.OIDCH_0033_TryAddNonceFailed, message.Nonce);
|
Logger.LogError(Resources.OIDCH_0033_NonceAlreadyExists, message.Nonce);
|
||||||
throw new OpenIdConnectProtocolException(string.Format(CultureInfo.InvariantCulture, Resources.OIDCH_0033_TryAddNonceFailed, message.Nonce));
|
throw new OpenIdConnectProtocolException(string.Format(CultureInfo.InvariantCulture, Resources.OIDCH_0033_NonceAlreadyExists, message.Nonce));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Options.NonceCache.SetAsync(message.Nonce, new byte[0], new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = Options.ProtocolValidator.NonceLifetime
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -389,11 +395,16 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
}
|
}
|
||||||
|
|
||||||
string nonce = jwt.Payload.Nonce;
|
string nonce = jwt.Payload.Nonce;
|
||||||
if (Options.NonceCache != null)
|
if (Options.CacheNonces)
|
||||||
{
|
{
|
||||||
// if the nonce cannot be removed, it was used
|
if (await Options.NonceCache.GetAsync(nonce) != null)
|
||||||
if (!Options.NonceCache.TryRemoveNonce(nonce))
|
|
||||||
{
|
{
|
||||||
|
await Options.NonceCache.RemoveAsync(nonce);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If the nonce cannot be removed, it was
|
||||||
|
// already used and MUST be rejected.
|
||||||
nonce = null;
|
nonce = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,13 @@ using Microsoft.AspNet.Authentication.DataHandler.Serializer;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.DataProtection;
|
using Microsoft.AspNet.DataProtection;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.Framework.Caching.Distributed;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
using Microsoft.Framework.Internal;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Microsoft.Framework.OptionsModel;
|
using Microsoft.Framework.OptionsModel;
|
||||||
using Microsoft.IdentityModel.Protocols;
|
|
||||||
using Microsoft.Framework.Internal;
|
|
||||||
using Microsoft.Framework.WebEncoders;
|
using Microsoft.Framework.WebEncoders;
|
||||||
|
using Microsoft.IdentityModel.Protocols;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
{
|
{
|
||||||
|
|
@ -42,6 +44,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
[NotNull] IDataProtectionProvider dataProtectionProvider,
|
[NotNull] IDataProtectionProvider dataProtectionProvider,
|
||||||
[NotNull] ILoggerFactory loggerFactory,
|
[NotNull] ILoggerFactory loggerFactory,
|
||||||
[NotNull] IUrlEncoder encoder,
|
[NotNull] IUrlEncoder encoder,
|
||||||
|
[NotNull] IServiceProvider services,
|
||||||
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
|
[NotNull] IOptions<ExternalAuthenticationOptions> externalOptions,
|
||||||
[NotNull] IOptions<OpenIdConnectAuthenticationOptions> options,
|
[NotNull] IOptions<OpenIdConnectAuthenticationOptions> options,
|
||||||
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions = null)
|
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions = null)
|
||||||
|
|
@ -125,6 +128,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
Options.ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(Options.MetadataAddress, httpClient);
|
Options.ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(Options.MetadataAddress, httpClient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Options.CacheNonces && Options.NonceCache == null)
|
||||||
|
{
|
||||||
|
// Use the global distributed cache if the user has not provided his own instance.
|
||||||
|
// Note: GetRequiredService will throw an exception if caching services have not been registered.
|
||||||
|
Options.NonceCache = services.GetRequiredService<IDistributedCache>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ using System.IdentityModel.Tokens;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Authentication;
|
using Microsoft.AspNet.Http.Authentication;
|
||||||
|
using Microsoft.Framework.Caching.Distributed;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
using Microsoft.IdentityModel.Protocols;
|
using Microsoft.IdentityModel.Protocols;
|
||||||
|
|
||||||
|
|
@ -173,7 +174,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
/// recommends adding a nonce to a request as a mitigation against replay attacks when requesting id_tokens.
|
/// recommends adding a nonce to a request as a mitigation against replay attacks when requesting id_tokens.
|
||||||
/// By default the runtime uses cookies with unique names generated from a hash of the nonce.
|
/// By default the runtime uses cookies with unique names generated from a hash of the nonce.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public INonceCache NonceCache { get; set; }
|
public IDistributedCache NonceCache { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the value indicating whether nonces should be stored in the distributed cache or not.
|
||||||
|
/// The default value, <c>false</c>, is used to store nonces in client cookies.
|
||||||
|
/// </summary>
|
||||||
|
public bool CacheNonces { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the <see cref="OpenIdConnectAuthenticationNotifications"/> to notify when processing OpenIdConnect messages.
|
/// Gets or sets the <see cref="OpenIdConnectAuthenticationNotifications"/> to notify when processing OpenIdConnect messages.
|
||||||
|
|
|
||||||
|
|
@ -141,11 +141,11 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OIDCH_0033: ProtocolValidator.RequireNonce == true. Options.NonceCache.TryAddNonce returned false. This usually indicates the nonce is not unique or has been used. The nonce is: '{0}'.
|
/// OIDCH_0033: ProtocolValidator.RequireNonce == true. The generated nonce already exists: this usually indicates the nonce is not unique or has been used. The nonce is: '{0}'.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static string OIDCH_0033_TryAddNonceFailed
|
internal static string OIDCH_0033_NonceAlreadyExists
|
||||||
{
|
{
|
||||||
get { return ResourceManager.GetString("OIDCH_0033_TryAddNonceFailed"); }
|
get { return ResourceManager.GetString("OIDCH_0033_NonceAlreadyExists"); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -147,8 +147,8 @@
|
||||||
<data name="OIDCH_0032_UsingCurrentUriRedirectUri" xml:space="preserve">
|
<data name="OIDCH_0032_UsingCurrentUriRedirectUri" xml:space="preserve">
|
||||||
<value>OIDCH_0032: using the CurrentUri for 'local redirect' post authentication: '{0}'.</value>
|
<value>OIDCH_0032: using the CurrentUri for 'local redirect' post authentication: '{0}'.</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="OIDCH_0033_TryAddNonceFailed" xml:space="preserve">
|
<data name="OIDCH_0033_NonceAlreadyExists" xml:space="preserve">
|
||||||
<value>OIDCH_0033: ProtocolValidator.RequireNonce == true. Options.NonceCache.TryAddNonce returned false. This usually indicates the nonce is not unique or has been used. The nonce is: '{0}'.</value>
|
<value>OIDCH_0033: ProtocolValidator.RequireNonce == true. The generated nonce already exists: this usually indicates the nonce is not unique or has been used. The nonce is: '{0}'.</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="OIDCH_0034_RedirectToIdentityProviderNotificationHandledResponse" xml:space="preserve">
|
<data name="OIDCH_0034_RedirectToIdentityProviderNotificationHandledResponse" xml:space="preserve">
|
||||||
<value>OIDCH_0034: redirectToIdentityProviderNotification.HandledResponse</value>
|
<value>OIDCH_0034: redirectToIdentityProviderNotification.HandledResponse</value>
|
||||||
|
|
@ -222,4 +222,4 @@
|
||||||
<data name="OIDCH_0020_IdTokenReceived" xml:space="preserve">
|
<data name="OIDCH_0020_IdTokenReceived" xml:space="preserve">
|
||||||
<value>OIDCH_0020: 'id_token' received: '{0}'</value>
|
<value>OIDCH_0020: 'id_token' received: '{0}'</value>
|
||||||
</data>
|
</data>
|
||||||
</root>
|
</root>
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
"description": "ASP.NET 5 middleware that enables an application to support OpenIdConnect authentication workflow.",
|
"description": "ASP.NET 5 middleware that enables an application to support OpenIdConnect authentication workflow.",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Authentication": "1.0.0-*",
|
"Microsoft.AspNet.Authentication": "1.0.0-*",
|
||||||
|
"Microsoft.Framework.Caching.Abstractions": "1.0.0-*",
|
||||||
"Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" },
|
"Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" },
|
||||||
"Microsoft.IdentityModel.Protocol.Extensions": "2.0.0-beta5-*"
|
"Microsoft.IdentityModel.Protocol.Extensions": "2.0.0-beta5-*"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,5 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
{
|
{
|
||||||
return services.Configure<ClaimsTransformationOptions>(o => o.Transformer = new ClaimsTransformer { TransformAsyncDelegate = asyncTransform });
|
return services.Configure<ClaimsTransformationOptions>(o => o.Transformer = new ClaimsTransformer { TransformAsyncDelegate = asyncTransform });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -437,6 +437,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
|
||||||
},
|
},
|
||||||
services =>
|
services =>
|
||||||
{
|
{
|
||||||
|
services.AddAuthentication();
|
||||||
services.AddWebEncoders();
|
services.AddWebEncoders();
|
||||||
services.AddDataProtection();
|
services.AddDataProtection();
|
||||||
}
|
}
|
||||||
|
|
@ -456,6 +457,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
|
||||||
},
|
},
|
||||||
services =>
|
services =>
|
||||||
{
|
{
|
||||||
|
services.AddAuthentication();
|
||||||
services.AddWebEncoders();
|
services.AddWebEncoders();
|
||||||
services.AddDataProtection();
|
services.AddDataProtection();
|
||||||
}
|
}
|
||||||
|
|
@ -571,11 +573,12 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
|
||||||
IDataProtectionProvider dataProtectionProvider,
|
IDataProtectionProvider dataProtectionProvider,
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
IUrlEncoder encoder,
|
IUrlEncoder encoder,
|
||||||
|
IServiceProvider services,
|
||||||
IOptions<ExternalAuthenticationOptions> externalOptions,
|
IOptions<ExternalAuthenticationOptions> externalOptions,
|
||||||
IOptions<OpenIdConnectAuthenticationOptions> options,
|
IOptions<OpenIdConnectAuthenticationOptions> options,
|
||||||
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions = null
|
ConfigureOptions<OpenIdConnectAuthenticationOptions> configureOptions = null
|
||||||
)
|
)
|
||||||
: base(next, dataProtectionProvider, loggerFactory, encoder, externalOptions, options, configureOptions)
|
: base(next, dataProtectionProvider, loggerFactory, encoder, services, externalOptions, options, configureOptions)
|
||||||
{
|
{
|
||||||
Logger = (loggerFactory as CustomLoggerFactory).Logger;
|
Logger = (loggerFactory as CustomLoggerFactory).Logger;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue