@@ -5,73 +5,112 @@ namespace Masa.Contrib.Authentication.OpenIdConnect.EFCore.Caches;
5
5
6
6
public class SyncCache
7
7
{
8
- IClientCache _clientCache ;
9
- IApiResourceCache _apiResourceCache ;
10
- IApiScopeCache _apiScopeCache ;
11
- IIdentityResourceCache _identityResourceCache ;
8
+ IClientCache ? _clientCache ;
9
+ IApiResourceCache ? _apiResourceCache ;
10
+ IApiScopeCache ? _apiScopeCache ;
11
+ IIdentityResourceCache ? _identityResourceCache ;
12
12
DbContext _context ;
13
13
14
- public SyncCache ( IClientCache clientCache , IApiResourceCache apiResourceCache , IApiScopeCache apiScopeCache , IIdentityResourceCache identityResourceCache , OidcDbContext context )
14
+ public SyncCache ( OidcDbContext context , IServiceProvider serviceProvider )
15
15
{
16
- _clientCache = clientCache ;
17
- _apiResourceCache = apiResourceCache ;
18
- _apiScopeCache = apiScopeCache ;
19
- _identityResourceCache = identityResourceCache ;
16
+ _clientCache = serviceProvider . GetService < IClientCache > ( ) ;
17
+ _apiResourceCache = serviceProvider . GetService < IApiResourceCache > ( ) ;
18
+ _apiScopeCache = serviceProvider . GetService < IApiScopeCache > ( ) ;
19
+ _identityResourceCache = serviceProvider . GetService < IIdentityResourceCache > ( ) ;
20
20
_context = context ;
21
21
}
22
22
23
23
public async Task SyncApiResourceCacheAsync ( Guid id )
24
24
{
25
+ if ( _apiResourceCache is null ) return ;
26
+
25
27
var apiResource = await ApiResourceQuery ( ) . FirstOrDefaultAsync ( apiResource => apiResource . Id == id ) ;
26
28
if ( apiResource is null ) return ;
27
29
await _apiResourceCache . SetAsync ( apiResource ) ;
28
30
}
29
31
30
32
public async Task SyncApiScopeCacheAsync ( Guid id )
31
33
{
34
+ if ( _apiScopeCache is null ) return ;
35
+
32
36
var apiScope = await ApiScopeQuery ( ) . FirstOrDefaultAsync ( apiScope => apiScope . Id == id ) ;
33
37
if ( apiScope is null ) return ;
34
38
await _apiScopeCache . SetAsync ( apiScope ) ;
35
39
}
36
40
37
41
public async Task SyncIdentityResourceCacheAsync ( params Guid [ ] ids )
38
42
{
43
+ if ( _identityResourceCache is null ) return ;
44
+
39
45
var identityResources = await IdentityResourceQuery ( ) . Where ( idrs => ids . Contains ( idrs . Id ) ) . ToListAsync ( ) ;
40
46
if ( identityResources . Count == 0 ) return ;
41
47
await _identityResourceCache . SetRangeAsync ( identityResources ) ;
42
48
}
43
49
50
+ public async Task SyncClientCacheAsync ( Guid id )
51
+ {
52
+ if ( _clientCache is null ) return ;
53
+
54
+ var client = await ClientQuery ( ) . FirstOrDefaultAsync ( client => client . Id == id ) ;
55
+ if ( client is null ) return ;
56
+ await _clientCache . SetAsync ( client ) ;
57
+ }
58
+
44
59
public async Task RemoveApiResourceCacheAsync ( ApiResource apiResource )
45
60
{
61
+ if ( _apiResourceCache is null ) return ;
62
+
46
63
await _apiResourceCache . RemoveAsync ( apiResource ) ;
47
64
}
48
65
49
66
public async Task RemoveApiScopeCacheAsync ( ApiScope apiScope )
50
67
{
68
+ if ( _apiScopeCache is null ) return ;
69
+
51
70
await _apiScopeCache . RemoveAsync ( apiScope ) ;
52
71
}
53
72
54
73
public async Task RemoveIdentityResourceCacheAsync ( IdentityResource identityResource )
55
74
{
75
+ if ( _identityResourceCache is null ) return ;
76
+
56
77
await _identityResourceCache . RemoveAsync ( identityResource ) ;
57
78
}
58
79
80
+ public async Task RemoveClientCacheAsync ( Client client )
81
+ {
82
+ if ( _clientCache is null ) return ;
83
+
84
+ await _clientCache . RemoveAsync ( client ) ;
85
+ }
86
+
59
87
public async Task ResetAsync ( )
60
88
{
61
- var clients = await ClientQueryAsync ( ) ;
62
- var apiScopes = await ApiScopeQuery ( ) . ToListAsync ( ) ;
63
- var apiResources = await ApiResourceQuery ( ) . ToListAsync ( ) ;
64
- var identityResource = await IdentityResourceQuery ( ) . ToListAsync ( ) ;
65
-
66
- await _clientCache . ResetAsync ( clients ) ;
67
- await _apiScopeCache . ResetAsync ( apiScopes ) ;
68
- await _apiResourceCache . ResetAsync ( apiResources ) ;
69
- await _identityResourceCache . ResetAsync ( identityResource ) ;
89
+ if ( _clientCache is not null )
90
+ {
91
+ var clients = await ClientQueryAsync ( ) ;
92
+ await _clientCache . ResetAsync ( clients ) ;
93
+ }
94
+ if ( _apiScopeCache is not null )
95
+ {
96
+ var apiScopes = await ApiScopeQuery ( ) . ToListAsync ( ) ;
97
+ await _apiScopeCache . ResetAsync ( apiScopes ) ;
98
+ }
99
+ if ( _apiResourceCache is not null )
100
+ {
101
+ var apiResources = await ApiResourceQuery ( ) . ToListAsync ( ) ;
102
+ await _apiResourceCache . ResetAsync ( apiResources ) ;
103
+ }
104
+ if ( _identityResourceCache is not null )
105
+ {
106
+ var identityResource = await IdentityResourceQuery ( ) . ToListAsync ( ) ;
107
+ await _identityResourceCache . ResetAsync ( identityResource ) ;
108
+ }
70
109
}
71
110
72
111
public async Task < IEnumerable < Client > > ClientQueryAsync ( )
73
112
{
74
- var clients = await _context . Set < Client > ( ) . ToListAsync ( ) ;
113
+ var clients = await _context . Set < Client > ( ) . ToListAsync ( ) ;
75
114
var clientPropertys = await _context . Set < ClientProperty > ( ) . ToListAsync ( ) ;
76
115
var clientClaims = await _context . Set < ClientClaim > ( ) . ToListAsync ( ) ;
77
116
var clientIdPRestrictions = await _context . Set < ClientIdPRestriction > ( ) . ToListAsync ( ) ;
@@ -82,7 +121,7 @@ public async Task<IEnumerable<Client>> ClientQueryAsync()
82
121
var clientPostLogoutRedirectUris = await _context . Set < ClientPostLogoutRedirectUri > ( ) . ToListAsync ( ) ;
83
122
var clientScopes = await _context . Set < ClientScope > ( ) . ToListAsync ( ) ;
84
123
85
- foreach ( var client in clients )
124
+ foreach ( var client in clients )
86
125
{
87
126
client . AllowedGrantTypes . AddRange ( clientGrantTypes . Where ( item => item . ClientId == client . Id ) ) ;
88
127
client . RedirectUris . AddRange ( clientRedirectUris . Where ( item => item . ClientId == client . Id ) ) ;
@@ -123,4 +162,19 @@ private IQueryable<ApiResource> ApiResourceQuery()
123
162
. Include ( apiResource => apiResource . ApiScopes )
124
163
. ThenInclude ( apiScope => apiScope . ApiScope ) ;
125
164
}
165
+
166
+ private IQueryable < Client > ClientQuery ( )
167
+ {
168
+ return _context . Set < Client > ( )
169
+ . Include ( c => c . AllowedGrantTypes )
170
+ . Include ( c => c . RedirectUris )
171
+ . Include ( c => c . PostLogoutRedirectUris )
172
+ . Include ( c => c . Properties )
173
+ . Include ( c => c . Claims )
174
+ . Include ( c => c . IdentityProviderRestrictions )
175
+ . Include ( c => c . AllowedCorsOrigins )
176
+ . Include ( c => c . ClientSecrets )
177
+ . Include ( c => c . AllowedScopes )
178
+ . AsSplitQuery ( ) ;
179
+ }
126
180
}
0 commit comments