This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
DefaultGrantStore.cs
192 lines (171 loc) · 6.83 KB
/
DefaultGrantStore.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using IdentityServer4.Stores.Serialization;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace IdentityServer4.Stores
{
/// <summary>
/// Base class for persisting grants using the IPersistedGrantStore.
/// </summary>
/// <typeparam name="T"></typeparam>
public class DefaultGrantStore<T>
{
/// <summary>
/// The grant type being stored.
/// </summary>
protected string GrantType { get; }
/// <summary>
/// The logger.
/// </summary>
protected ILogger Logger { get; }
/// <summary>
/// The PersistedGrantStore.
/// </summary>
protected IPersistedGrantStore Store { get; }
/// <summary>
/// The PersistentGrantSerializer;
/// </summary>
protected IPersistentGrantSerializer Serializer { get; }
/// <summary>
/// The HandleGenerationService.
/// </summary>
protected IHandleGenerationService HandleGenerationService { get; }
/// <summary>
/// Initializes a new instance of the <see cref="DefaultGrantStore{T}"/> class.
/// </summary>
/// <param name="grantType">Type of the grant.</param>
/// <param name="store">The store.</param>
/// <param name="serializer">The serializer.</param>
/// <param name="handleGenerationService">The handle generation service.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="System.ArgumentNullException">grantType</exception>
protected DefaultGrantStore(string grantType,
IPersistedGrantStore store,
IPersistentGrantSerializer serializer,
IHandleGenerationService handleGenerationService,
ILogger logger)
{
if (grantType.IsMissing()) throw new ArgumentNullException(nameof(grantType));
GrantType = grantType;
Store = store;
Serializer = serializer;
HandleGenerationService = handleGenerationService;
Logger = logger;
}
private const string KeySeparator = ":";
/// <summary>
/// Gets the hashed key.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
protected virtual string GetHashedKey(string value)
{
return (value + KeySeparator + GrantType).Sha256();
}
/// <summary>
/// Gets the item.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
protected virtual async Task<T> GetItemAsync(string key)
{
var hashedKey = GetHashedKey(key);
var grant = await Store.GetAsync(hashedKey);
if (grant != null && grant.Type == GrantType)
{
try
{
return Serializer.Deserialize<T>(grant.Data);
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to deserialize JSON from grant store.");
}
}
else
{
Logger.LogDebug("{grantType} grant with value: {key} not found in store.", GrantType, key);
}
return default;
}
/// <summary>
/// Creates the item.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="clientId">The client identifier.</param>
/// <param name="subjectId">The subject identifier.</param>
/// <param name="sessionId">The session identifier.</param>
/// <param name="description">The description.</param>
/// <param name="created">The created.</param>
/// <param name="lifetime">The lifetime.</param>
/// <returns></returns>
protected virtual async Task<string> CreateItemAsync(T item, string clientId, string subjectId, string sessionId, string description, DateTime created, int lifetime)
{
var handle = await HandleGenerationService.GenerateAsync();
await StoreItemAsync(handle, item, clientId, subjectId, sessionId, description, created, created.AddSeconds(lifetime));
return handle;
}
/// <summary>
/// Stores the item.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="item">The item.</param>
/// <param name="clientId">The client identifier.</param>
/// <param name="subjectId">The subject identifier.</param>
/// <param name="sessionId">The session identifier.</param>
/// <param name="description">The description.</param>
/// <param name="created">The created time.</param>
/// <param name="expiration">The expiration.</param>
/// <param name="consumedTime">The consumed time.</param>
/// <returns></returns>
protected virtual async Task StoreItemAsync(string key, T item, string clientId, string subjectId, string sessionId, string description, DateTime created, DateTime? expiration, DateTime? consumedTime = null)
{
key = GetHashedKey(key);
var json = Serializer.Serialize(item);
var grant = new PersistedGrant
{
Key = key,
Type = GrantType,
ClientId = clientId,
SubjectId = subjectId,
SessionId = sessionId,
Description = description,
CreationTime = created,
Expiration = expiration,
ConsumedTime = consumedTime,
Data = json
};
await Store.StoreAsync(grant);
}
/// <summary>
/// Removes the item.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
protected virtual async Task RemoveItemAsync(string key)
{
key = GetHashedKey(key);
await Store.RemoveAsync(key);
}
/// <summary>
/// Removes all items for a subject id / cliend id combination.
/// </summary>
/// <param name="subjectId">The subject identifier.</param>
/// <param name="clientId">The client identifier.</param>
/// <returns></returns>
protected virtual async Task RemoveAllAsync(string subjectId, string clientId)
{
await Store.RemoveAllAsync(new PersistedGrantFilter
{
SubjectId = subjectId,
ClientId = clientId,
Type = GrantType
});
}
}
}