Skip to content

Commit 939c86e

Browse files
buehlerjosephdecock
authored andcommitted
feat: add possibility to ignore missing "exp" claim in introspection response for caching
1 parent a258d1b commit 939c86e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Infrastructure/CacheExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ public static async Task<IEnumerable<Claim>> GetClaimsAsync(this IDistributedCac
4646
public static async Task SetClaimsAsync(this IDistributedCache cache, OAuth2IntrospectionOptions options, string token, IEnumerable<Claim> claims, TimeSpan duration, ILogger logger)
4747
{
4848
var expClaim = claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Expiration);
49-
if (expClaim == null)
49+
if (expClaim == null && !options.CacheIgnoreMissingExp)
5050
{
5151
Log.NoExpClaimFound(logger, null);
5252
return;
5353
}
5454

5555
var now = DateTimeOffset.UtcNow;
56-
var expiration = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expClaim.Value));
56+
var expiration = expClaim == null
57+
? now + duration
58+
: DateTimeOffset.FromUnixTimeSeconds(long.Parse(expClaim.Value));
5759
Log.TokenExpiresOn(logger, expiration, null);
5860

5961
if (expiration <= now)
@@ -79,4 +81,4 @@ public static async Task SetClaimsAsync(this IDistributedCache cache, OAuth2Intr
7981
await cache.SetAsync(cacheKey, bytes, new DistributedCacheEntryOptions { AbsoluteExpiration = absoluteLifetime }).ConfigureAwait(false);
8082
}
8183
}
82-
}
84+
}

src/OAuth2IntrospectionOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ public OAuth2IntrospectionOptions()
114114
/// </summary>
115115
public string CacheKeyPrefix { get; set; } = string.Empty;
116116

117+
/// <summary>
118+
/// If set, the caching mechanism will ignore missing "exp" claims in the
119+
/// introspection response.
120+
/// </summary>
121+
public bool CacheIgnoreMissingExp { get; set; }
122+
117123
/// <summary>
118124
/// Specifies the method how to generate the cache key from the token
119125
/// </summary>

0 commit comments

Comments
 (0)