11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ using System . Collections . Generic ;
45using Microsoft . IdentityModel . Protocols ;
56using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
67using Microsoft . IdentityModel . Tokens ;
78using Microsoft . Owin . Security . Jwt ;
8- using System . Collections . Generic ;
9- using System . Threading ;
10- using System . Threading . Tasks ;
119
1210namespace Microsoft . Identity . Web
1311{
1412 // This class is necessary because the OAuthBearer Middleware does not leverage
1513 // the OpenID Connect metadata endpoint exposed by the STS by default.
1614 internal class OpenIdConnectCachingSecurityTokenProvider : IIssuerSecurityKeyProvider
1715 {
18- public ConfigurationManager < OpenIdConnectConfiguration > _configManager ;
19- private string ? _issuer ;
20- private IEnumerable < SecurityKey > ? _keys ;
21- private readonly string _metadataEndpoint ;
22-
23- private readonly ReaderWriterLockSlim _synclock = new ReaderWriterLockSlim ( ) ;
16+ public readonly ConfigurationManager < OpenIdConnectConfiguration > _configManager ;
2417
2518 public OpenIdConnectCachingSecurityTokenProvider ( string metadataEndpoint )
2619 {
27- _metadataEndpoint = metadataEndpoint ;
2820 _configManager = new ConfigurationManager < OpenIdConnectConfiguration > ( metadataEndpoint , new OpenIdConnectConfigurationRetriever ( ) ) ;
2921
3022 RetrieveMetadata ( ) ;
@@ -36,61 +28,23 @@ public OpenIdConnectCachingSecurityTokenProvider(string metadataEndpoint)
3628 /// <value>
3729 /// The issuer the credentials are for.
3830 /// </value>
39- public string ? Issuer
40- {
41- get
42- {
43- RetrieveMetadata ( ) ;
44- _synclock . EnterReadLock ( ) ;
45- try
46- {
47- return _issuer ;
48- }
49- finally
50- {
51- _synclock . ExitReadLock ( ) ;
52- }
53- }
54- }
31+ public string ? Issuer => RetrieveMetadata ( ) . Issuer ;
5532
5633 /// <summary>
5734 /// Gets all known security keys.
5835 /// </summary>
5936 /// <value>
6037 /// All known security keys.
6138 /// </value>
62- public IEnumerable < SecurityKey > ? SecurityKeys
63- {
64- get
65- {
66- RetrieveMetadata ( ) ;
67- _synclock . EnterReadLock ( ) ;
68- try
69- {
70- return _keys ;
71- }
72- finally
73- {
74- _synclock . ExitReadLock ( ) ;
75- }
76- }
77- }
39+ public IEnumerable < SecurityKey > ? SecurityKeys => RetrieveMetadata ( ) . SigningKeys ;
7840
79- private void RetrieveMetadata ( )
41+ private OpenIdConnectConfiguration RetrieveMetadata ( )
8042 {
81- _synclock . EnterWriteLock ( ) ;
82- try
83- {
43+ // ConfigurationManager will return the same cached config unless enough time has passed,
44+ // then the return value will be a new object.
8445#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
85- OpenIdConnectConfiguration config = Task . Run ( _configManager . GetConfigurationAsync ) . Result ;
46+ return _configManager . GetConfigurationAsync ( ) . Result ;
8647#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
87- _issuer = config . Issuer ;
88- _keys = config . SigningKeys ;
89- }
90- finally
91- {
92- _synclock . ExitWriteLock ( ) ;
93- }
9448 }
9549 }
9650}
0 commit comments