Skip to content

Commit 74932fa

Browse files
authored
Feature(oidc): optimize OIDC (#408)
* optimize oidc * refactor code
1 parent 96c6c2f commit 74932fa

File tree

6 files changed

+96
-37
lines changed

6 files changed

+96
-37
lines changed

src/Contrib/Authentication/OpenIdConnect/Masa.Contrib.Authentication.OpenIdConnect.Cache.Storage/ServiceCollectionExtensions.cs

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@ public static IServiceCollection AddOidcCacheStorage(this IServiceCollection ser
1414
return services;
1515
}
1616

17-
public static IServiceCollection AddOidcCacheStorage(this IServiceCollection services, RedisConfigurationOptions options)
17+
public static IServiceCollection AddOidcCacheStorage(this IServiceCollection services, RedisConfigurationOptions? options = null)
1818
{
1919
services.AddOidcCache(options);
20-
services.AddOidcCacheStorage();
21-
22-
return services;
23-
}
24-
25-
static IServiceCollection AddOidcCacheStorage(this IServiceCollection services)
26-
{
2720
services.AddSingleton<IClientStore, ClientStore>();
2821
services.AddSingleton<IResourceStore, ResourceStore>();
2922
services.AddSingleton<IPersistedGrantStore, PersistedGrantStore>();

src/Contrib/Authentication/OpenIdConnect/Masa.Contrib.Authentication.OpenIdConnect.Cache/ServiceCollectionExtensions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ public static IServiceCollection AddOidcCache(this IServiceCollection services,
1414
return services;
1515
}
1616

17-
public static IServiceCollection AddOidcCache(this IServiceCollection services, RedisConfigurationOptions options)
17+
public static IServiceCollection AddOidcCache(this IServiceCollection services, RedisConfigurationOptions? options = null)
1818
{
1919
services.AddMultilevelCache(
2020
Constants.DEFAULT_CLIENT_NAME,
21-
distributedCacheOptions => distributedCacheOptions.UseStackExchangeRedisCache(options),
21+
distributedCacheOptions =>
22+
{
23+
if (options is null) distributedCacheOptions.UseStackExchangeRedisCache();
24+
else distributedCacheOptions.UseStackExchangeRedisCache(options);
25+
},
2226
multilevelCacheOptions =>
2327
{
2428
multilevelCacheOptions.SubscribeKeyType = SubscribeKeyType.SpecificPrefix;

src/Contrib/Authentication/OpenIdConnect/Masa.Contrib.Authentication.OpenIdConnect.EFCore/Caches/SyncCache.cs

+74-20
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,112 @@ namespace Masa.Contrib.Authentication.OpenIdConnect.EFCore.Caches;
55

66
public class SyncCache
77
{
8-
IClientCache _clientCache;
9-
IApiResourceCache _apiResourceCache;
10-
IApiScopeCache _apiScopeCache;
11-
IIdentityResourceCache _identityResourceCache;
8+
IClientCache? _clientCache;
9+
IApiResourceCache? _apiResourceCache;
10+
IApiScopeCache? _apiScopeCache;
11+
IIdentityResourceCache? _identityResourceCache;
1212
DbContext _context;
1313

14-
public SyncCache(IClientCache clientCache, IApiResourceCache apiResourceCache, IApiScopeCache apiScopeCache, IIdentityResourceCache identityResourceCache, OidcDbContext context)
14+
public SyncCache(OidcDbContext context, IServiceProvider serviceProvider)
1515
{
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>();
2020
_context = context;
2121
}
2222

2323
public async Task SyncApiResourceCacheAsync(Guid id)
2424
{
25+
if (_apiResourceCache is null) return;
26+
2527
var apiResource = await ApiResourceQuery().FirstOrDefaultAsync(apiResource => apiResource.Id == id);
2628
if (apiResource is null) return;
2729
await _apiResourceCache.SetAsync(apiResource);
2830
}
2931

3032
public async Task SyncApiScopeCacheAsync(Guid id)
3133
{
34+
if (_apiScopeCache is null) return;
35+
3236
var apiScope = await ApiScopeQuery().FirstOrDefaultAsync(apiScope => apiScope.Id == id);
3337
if (apiScope is null) return;
3438
await _apiScopeCache.SetAsync(apiScope);
3539
}
3640

3741
public async Task SyncIdentityResourceCacheAsync(params Guid[] ids)
3842
{
43+
if (_identityResourceCache is null) return;
44+
3945
var identityResources = await IdentityResourceQuery().Where(idrs => ids.Contains(idrs.Id)).ToListAsync();
4046
if (identityResources.Count == 0) return;
4147
await _identityResourceCache.SetRangeAsync(identityResources);
4248
}
4349

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+
4459
public async Task RemoveApiResourceCacheAsync(ApiResource apiResource)
4560
{
61+
if (_apiResourceCache is null) return;
62+
4663
await _apiResourceCache.RemoveAsync(apiResource);
4764
}
4865

4966
public async Task RemoveApiScopeCacheAsync(ApiScope apiScope)
5067
{
68+
if (_apiScopeCache is null) return;
69+
5170
await _apiScopeCache.RemoveAsync(apiScope);
5271
}
5372

5473
public async Task RemoveIdentityResourceCacheAsync(IdentityResource identityResource)
5574
{
75+
if (_identityResourceCache is null) return;
76+
5677
await _identityResourceCache.RemoveAsync(identityResource);
5778
}
5879

80+
public async Task RemoveClientCacheAsync(Client client)
81+
{
82+
if (_clientCache is null) return;
83+
84+
await _clientCache.RemoveAsync(client);
85+
}
86+
5987
public async Task ResetAsync()
6088
{
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+
}
70109
}
71110

72111
public async Task<IEnumerable<Client>> ClientQueryAsync()
73112
{
74-
var clients = await _context.Set<Client>().ToListAsync();
113+
var clients = await _context.Set<Client>().ToListAsync();
75114
var clientPropertys = await _context.Set<ClientProperty>().ToListAsync();
76115
var clientClaims = await _context.Set<ClientClaim>().ToListAsync();
77116
var clientIdPRestrictions = await _context.Set<ClientIdPRestriction>().ToListAsync();
@@ -82,7 +121,7 @@ public async Task<IEnumerable<Client>> ClientQueryAsync()
82121
var clientPostLogoutRedirectUris = await _context.Set<ClientPostLogoutRedirectUri>().ToListAsync();
83122
var clientScopes = await _context.Set<ClientScope>().ToListAsync();
84123

85-
foreach(var client in clients)
124+
foreach (var client in clients)
86125
{
87126
client.AllowedGrantTypes.AddRange(clientGrantTypes.Where(item => item.ClientId == client.Id));
88127
client.RedirectUris.AddRange(clientRedirectUris.Where(item => item.ClientId == client.Id));
@@ -123,4 +162,19 @@ private IQueryable<ApiResource> ApiResourceQuery()
123162
.Include(apiResource => apiResource.ApiScopes)
124163
.ThenInclude(apiScope => apiScope.ApiScope);
125164
}
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+
}
126180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Authentication.OpenIdConnect.EFCore;
5+
6+
public static class OpenIdConnectEFCore
7+
{
8+
public static Assembly Assembly => typeof(OpenIdConnectEFCore).Assembly;
9+
}

src/Contrib/Authentication/OpenIdConnect/Masa.Contrib.Authentication.OpenIdConnect.EFCore/Repositories/ClientRepository.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace Masa.Contrib.Authentication.OpenIdConnect.EFCore.Repositories;
55

66
public class ClientRepository : IClientRepository
77
{
8-
IClientCache _cache;
8+
SyncCache _cache;
99
DbContext _context;
1010
IRepository<Client> _repository;
1111

12-
public ClientRepository(IClientCache cache, OidcDbContext context, IRepository<Client> repository)
12+
public ClientRepository(SyncCache cache, OidcDbContext context, IRepository<Client> repository)
1313
{
1414
_cache = cache;
1515
_context = context;
@@ -70,24 +70,22 @@ public async ValueTask<Client> AddAsync(Client client)
7070
{
7171
var newClient = await _repository.AddAsync(client);
7272
await _context.SaveChangesAsync();
73-
var detail = await GetDetailAsync(client.Id);
74-
await _cache.SetAsync(detail!);
73+
await _cache.SyncClientCacheAsync(client.Id);
7574
return newClient;
7675
}
7776

7877
public async Task<Client> UpdateAsync(Client client)
7978
{
8079
var newClient = await _repository.UpdateAsync(client);
8180
await _context.SaveChangesAsync();
82-
var detail = await GetDetailAsync(client.Id);
83-
await _cache.SetAsync(detail!);
81+
await _cache.SyncClientCacheAsync(client.Id);
8482
return newClient;
8583
}
8684

8785
public async Task RemoveAsync(Client client)
8886
{
8987
await _repository.RemoveAsync(client);
9088
await _context.SaveChangesAsync();
91-
await _cache.RemoveAsync(client);
89+
await _cache.RemoveClientCacheAsync(client);
9290
}
9391
}

src/Contrib/Authentication/OpenIdConnect/Masa.Contrib.Authentication.OpenIdConnect.EFCore/_Imports.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
global using Microsoft.Extensions.DependencyInjection;
1818
global using System.Data;
1919
global using System.Linq.Expressions;
20+
global using System.Reflection;

0 commit comments

Comments
 (0)