Scenario:
Persist authorization grants in DB
Solution:
1. Implement custom IPersistedGrantStore
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Stores;
namespace MyCoreSolution
{
public class GrantStore : IPersistedGrantStore
{
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter)
{
List<GrantInfo> grantInfos = null;
await Task.Run(() =>
{
//get grant by filters like subjectid, type, client etc from DB
}).ConfigureAwait(false);
return grantInfos
.Select(GrantInfoToGrant).ToList();
}
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
List<GrantInfo> grantInfos = null;
await Task.Run(() =>
{
//get grant by subjectId from DB
}).ConfigureAwait(false);
return grantInfos
.Select(GrantInfoToGrant).ToList();
}
public async Task<PersistedGrant> GetAsync(string key)
{
List<GrantInfo> grantInfos = null;
await Task.Run(() =>
{
//get grant by subjectId from Key
}).ConfigureAwait(false);
return grantInfos
.Select(GrantInfoToGrant).FirstOrDefault();
}
public async Task RemoveAllAsync(string subjectId, string clientId, string type)
{
await Task.Run(() =>
{
//remove grant by filters
}).ConfigureAwait(false);
}
public async Task RemoveAsync(string key)
{
await Task.Run(() =>
{
//remove grant by key
}).ConfigureAwait(false);
}
public Task RemoveAllAsync(PersistedGrantFilter filter)
{
//remove grant by filters
return Task.CompletedTask;
}
public async Task StoreAsync(PersistedGrant grant)
{
var grantInfo = new GrantInfo
{
Key = grant.Key,
ClientId = grant.ClientId,
SubjectId = grant.SubjectId,
Type = grant.Type,
Data = grant.Data,
CreationTime = grant.CreationTime,
Expiration = grant.Expiration
};
//add grants
}
private PersistedGrant GrantInfoToGrant(GrantInfo grantInfo)
{
return new PersistedGrant()
{
Key = grantInfo.Key,
ClientId = grantInfo.ClientId,
CreationTime = grantInfo.CreationTime ?? DateTime.Now,
Data = grantInfo.Data,
Expiration = grantInfo.Expiration,
SubjectId = grantInfo.SubjectId,
Type = grantInfo.Type
};
}
}
}
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 | using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using IdentityServer4.Models; using IdentityServer4.Stores; namespace MyCoreSolution { public class GrantStore : IPersistedGrantStore { public async Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter) { List<GrantInfo> grantInfos = null; await Task.Run(() => { //get grant by filters like subjectid, type, client etc from DB }).ConfigureAwait(false); return grantInfos .Select(GrantInfoToGrant).ToList(); } public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId) { List<GrantInfo> grantInfos = null; await Task.Run(() => { //get grant by subjectId from DB }).ConfigureAwait(false); return grantInfos .Select(GrantInfoToGrant).ToList(); } public async Task<PersistedGrant> GetAsync(string key) { List<GrantInfo> grantInfos = null; await Task.Run(() => { //get grant by subjectId from Key }).ConfigureAwait(false); return grantInfos .Select(GrantInfoToGrant).FirstOrDefault(); } public async Task RemoveAllAsync(string subjectId, string clientId, string type) { await Task.Run(() => { //remove grant by filters }).ConfigureAwait(false); } public async Task RemoveAsync(string key) { await Task.Run(() => { //remove grant by key }).ConfigureAwait(false); } public Task RemoveAllAsync(PersistedGrantFilter filter) { //remove grant by filters return Task.CompletedTask; } public async Task StoreAsync(PersistedGrant grant) { var grantInfo = new GrantInfo { Key = grant.Key, ClientId = grant.ClientId, SubjectId = grant.SubjectId, Type = grant.Type, Data = grant.Data, CreationTime = grant.CreationTime, Expiration = grant.Expiration }; //add grants } private PersistedGrant GrantInfoToGrant(GrantInfo grantInfo) { return new PersistedGrant() { Key = grantInfo.Key, ClientId = grantInfo.ClientId, CreationTime = grantInfo.CreationTime ?? DateTime.Now, Data = grantInfo.Data, Expiration = grantInfo.Expiration, SubjectId = grantInfo.SubjectId, Type = grantInfo.Type }; } } } |
2.Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddTransient<IPersistedGrantStore, GrantStore>();
}
public void ConfigureServices(IServiceCollection services) { ... services.AddTransient<IPersistedGrantStore, GrantStore>(); } |
No comments:
Post a Comment