-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheExtensions.cs
More file actions
86 lines (72 loc) · 3.1 KB
/
CacheExtensions.cs
File metadata and controls
86 lines (72 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Duende.IdentityModel;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace IdentityModel.AspNetCore.OAuth2Introspection
{
internal static class CacheExtensions
{
private static readonly JsonSerializerOptions Options;
static CacheExtensions()
{
Options = new JsonSerializerOptions
{
IgnoreReadOnlyFields = true,
IgnoreReadOnlyProperties = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
Options.Converters.Add(new ClaimConverter());
}
public static async Task<IEnumerable<Claim>> GetClaimsAsync(this IDistributedCache cache, OAuth2IntrospectionOptions options, string token)
{
var cacheKey = options.CacheKeyGenerator(options, token);
var bytes = await cache.GetAsync(cacheKey).ConfigureAwait(false);
if (bytes == null)
{
return null;
}
var json = Encoding.UTF8.GetString(bytes);
return JsonSerializer.Deserialize<IEnumerable<Claim>>(json, Options);
}
public static async Task SetClaimsAsync(this IDistributedCache cache, OAuth2IntrospectionOptions options, string token, IEnumerable<Claim> claims, TimeSpan duration, ILogger logger)
{
var expClaim = claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Expiration);
if (expClaim == null)
{
logger.LogWarning("No exp claim found on introspection response, can't cache.");
return;
}
var now = DateTimeOffset.UtcNow;
var expiration = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expClaim.Value));
logger.LogDebug("Token will expire in {expiration}", expiration);
if (expiration <= now)
{
return;
}
// if the lifetime of the token is shorter than the duration, use the remaining token lifetime
DateTimeOffset absoluteLifetime;
if (expiration <= now.Add(duration))
{
absoluteLifetime = expiration;
}
else
{
absoluteLifetime = now.Add(duration);
}
var json = JsonSerializer.Serialize(claims, Options);
var bytes = Encoding.UTF8.GetBytes(json);
logger.LogDebug("Setting cache item expiration to {expiration}", absoluteLifetime);
var cacheKey = options.CacheKeyGenerator(options, token);
await cache.SetAsync(cacheKey, bytes, new DistributedCacheEntryOptions { AbsoluteExpiration = absoluteLifetime }).ConfigureAwait(false);
}
}
}