-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introdduce a LKG configuration cache to store each valid base configu…
…ration instead of a single entry of configuration.
- Loading branch information
Showing
28 changed files
with
698 additions
and
701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...Microsoft.IdentityModel.Protocols/Configuration/LastKnownGoodConfigurationCacheOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Microsoft.IdentityModel.Tokens; | ||
using Microsoft.IdentityModel.Logging; | ||
|
||
namespace Microsoft.IdentityModel.Protocols.Configuration | ||
{ | ||
/// <summary> | ||
/// Specifies the LastKnownGoodConfigurationCacheOptions which can be used to configure the internal LKG configuration cache. | ||
/// See <see cref="EventBasedLRUCache{TKey, TValue}"/> for more details. | ||
/// </summary> | ||
public class LastKnownGoodConfigurationCacheOptions | ||
{ | ||
private IEqualityComparer<BaseConfiguration> _baseConfigurationComparer = new BaseConfigurationComparer(); | ||
private int _lastKnownGoodConfigurationSizeLimit = DefaultLastKnownGoodConfigurationSizeLimit; | ||
|
||
/// <summary> | ||
/// 10 is the default size limit of the cache (in number of items) for last known good configuration. | ||
/// </summary> | ||
public static readonly int DefaultLastKnownGoodConfigurationSizeLimit = 10; | ||
|
||
/// <summary> | ||
/// Gets or sets the BaseConfgiurationComparer that to compare <see cref="BaseConfiguration"/>. | ||
/// </summary> | ||
public IEqualityComparer<BaseConfiguration> BaseConfigurationComparer | ||
{ | ||
get { return _baseConfigurationComparer; } | ||
set | ||
{ | ||
_baseConfigurationComparer = value ?? throw LogHelper.LogExceptionMessage(new ArgumentNullException(nameof(value))); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The size limit of the cache (in number of items) for last known good configuration. | ||
/// </summary> | ||
public int LastKnownGoodConfigurationSizeLimit | ||
{ | ||
get { return _lastKnownGoodConfigurationSizeLimit; } | ||
set | ||
{ | ||
_lastKnownGoodConfigurationSizeLimit = (value > 0) ? value : throw LogHelper.LogExceptionMessage(new ArgumentOutOfRangeException(nameof(value))); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Microsoft.IdentityModel.Tokens/BaseConfigurationComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.IdentityModel.Tokens | ||
{ | ||
/// <summary> | ||
/// Comparison class for a <see cref="BaseConfiguration"/>. | ||
/// </summary> | ||
internal class BaseConfigurationComparer : IEqualityComparer<BaseConfiguration> | ||
{ | ||
public bool Equals(BaseConfiguration config1, BaseConfiguration config2) | ||
{ | ||
if (config1 == null && config2 == null) | ||
return true; | ||
else if (config1 == null || config2 == null) | ||
return false; | ||
else if (config1.Issuer == config2.Issuer && config1.SigningKeys.Count == config2.SigningKeys.Count | ||
&& !config1.SigningKeys.Select(x => x.InternalId).Except(config2.SigningKeys.Select(x => x.InternalId)).Any()) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
public int GetHashCode(BaseConfiguration config) | ||
{ | ||
int defaultHash = string.Empty.GetHashCode(); | ||
int hashCode = defaultHash; | ||
hashCode ^= string.IsNullOrEmpty(config.Issuer) ? defaultHash : config.Issuer.GetHashCode(); | ||
foreach(string internalId in config.SigningKeys.Select(x => x.InternalId)) | ||
{ | ||
hashCode ^= internalId.GetHashCode(); | ||
} | ||
|
||
return hashCode; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.