@@ -29,9 +29,11 @@ Task<string> GetTokenAsync(ServerContext server, string clientId, string secret,
2929 internal class TokenStore : ITokenStore
3030 {
3131 private readonly ILogger < TokenStore > _logger ;
32- private readonly MemoryCache _cache = new MemoryCache ( new MemoryCacheOptions ( ) ) ;
32+ private readonly MemoryCache _cache = new ( new MemoryCacheOptions ( ) ) ;
3333 private readonly ITokenProvider _tokenProvider ;
3434
35+ private const int DefaultCacheDurationInMinutes = 30 ;
36+
3537 public TokenStore ( ITokenProvider tokenProvider , ILogger < TokenStore > logger )
3638 {
3739 _tokenProvider = tokenProvider ;
@@ -42,12 +44,12 @@ public async Task<string> GetTokenAsync(ServerContext server, string clientId, s
4244 CancellationToken cancel = default )
4345 {
4446 // look for the token in the cache
45- var tokenCacheKey = "TK:" + server . Url ;
47+ var tokenCacheKey = $ "TK- { server . Url } - { clientId } " . GetHashCode ( ) ;
4648 if ( _cache . TryGetValue ( tokenCacheKey , out string accessToken ) )
4749 return accessToken ;
4850
4951 // look for auth info in the cache
50- var authInfoCacheKey = "AI:" + server . Url ;
52+ var authInfoCacheKey = $ "AI- { server . Url } - { clientId } " . GetHashCode ( ) ;
5153 if ( ! _cache . TryGetValue ( authInfoCacheKey , out AuthorityInfo authInfo ) )
5254 {
5355 _logger ? . LogTrace ( $ "Getting authority info from { server . Url } .") ;
@@ -59,7 +61,7 @@ public async Task<string> GetTokenAsync(ServerContext server, string clientId, s
5961 authInfo . ClientId = clientId ;
6062
6163 if ( ! string . IsNullOrEmpty ( authInfo . Authority ) )
62- _cache . Set ( authInfoCacheKey , authInfo , TimeSpan . FromMinutes ( 30 ) ) ;
64+ _cache . Set ( authInfoCacheKey , authInfo , TimeSpan . FromMinutes ( DefaultCacheDurationInMinutes ) ) ;
6365 else
6466 _logger ? . LogTrace ( $ "Authority info is empty for server { server . Url } ") ;
6567 }
@@ -74,24 +76,28 @@ public async Task<string> GetTokenAsync(ServerContext server, string clientId, s
7476
7577 accessToken = tokenInfo ? . AccessToken ;
7678
77- //TODO: determine access token cache expiration based on the token expiration
78- if ( ! string . IsNullOrEmpty ( accessToken ) )
79+ if ( string . IsNullOrEmpty ( accessToken ) )
80+ return accessToken ;
81+
82+ // Maximize token expiration to the received token expiration (if given) or a fixed short time.
83+ var tokenExpiration = tokenInfo . ExpiresIn > 0
84+ ? TimeSpan . FromSeconds ( Math . Min ( tokenInfo . ExpiresIn , DefaultCacheDurationInMinutes * 60 ) )
85+ : TimeSpan . FromMinutes ( DefaultCacheDurationInMinutes ) ;
86+
87+ _cache . Set ( tokenCacheKey , accessToken , tokenExpiration ) ;
88+
89+ try
90+ {
91+ // parse token and log user
92+ var handler = new JwtSecurityTokenHandler ( ) ;
93+ var token = handler . ReadJwtToken ( accessToken ) ;
94+ var sub = token . Claims . FirstOrDefault ( c => c . Type == "client_sub" ) ? . Value ?? "unknown" ;
95+
96+ _logger ? . LogTrace ( $ "Token acquired for user { sub } . Expires in { tokenExpiration } .") ;
97+ }
98+ catch ( Exception ex )
7999 {
80- _cache . Set ( tokenCacheKey , accessToken , TimeSpan . FromMinutes ( 10 ) ) ;
81-
82- try
83- {
84- // parse token and log user
85- var handler = new JwtSecurityTokenHandler ( ) ;
86- var token = handler . ReadJwtToken ( accessToken ) ;
87- var sub = token . Claims . FirstOrDefault ( c => c . Type == "client_sub" ) ? . Value ?? "unknown" ;
88-
89- _logger ? . LogTrace ( $ "Token acquired for user { sub } .") ;
90- }
91- catch ( Exception ex )
92- {
93- _logger ? . LogWarning ( ex , $ "Could not read access token: { ex . Message } ") ;
94- }
100+ _logger ? . LogWarning ( ex , $ "Could not read access token: { ex . Message } ") ;
95101 }
96102
97103 return accessToken ;
0 commit comments