Skip to content

Commit

Permalink
Merge pull request #89 from EasyAbp/abp-wechat-230
Browse files Browse the repository at this point in the history
Upgrade to Abp.WeChat 2.3.0 to share tokens and tickets
  • Loading branch information
gdlcf88 committed Jan 13, 2023
2 parents 8f14e11 + 76da4bd commit f1568da
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Expand Up @@ -3,7 +3,7 @@

<AbpVersion>7.0.0</AbpVersion>
<EasyAbpAbpTagHelperPlusModuleVersion>1.3.0</EasyAbpAbpTagHelperPlusModuleVersion>
<EasyAbpAbpWeChatModuleVersion>2.2.0</EasyAbpAbpWeChatModuleVersion>
<EasyAbpAbpWeChatModuleVersion>2.3.0</EasyAbpAbpWeChatModuleVersion>

</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion common.props
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>2.0.1</Version>
<Version>2.1.0</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Expand Up @@ -15,12 +15,12 @@ public class AuthorizerRefreshTokenStore : IAuthorizerRefreshTokenStore, ITransi
{
private readonly IStringEncryptionService _stringEncryptionService;
private readonly IAuthorizerSecretRepository _authorizerSecretRepository;
private readonly IDistributedCache<string> _cache;
private readonly CacheAuthorizerRefreshTokenStore _cache;

public AuthorizerRefreshTokenStore(
IStringEncryptionService stringEncryptionService,
IAuthorizerSecretRepository authorizerSecretRepository,
IDistributedCache<string> cache)
CacheAuthorizerRefreshTokenStore cache)
{
_stringEncryptionService = stringEncryptionService;
_authorizerSecretRepository = authorizerSecretRepository;
Expand All @@ -29,30 +29,26 @@ public class AuthorizerRefreshTokenStore : IAuthorizerRefreshTokenStore, ITransi

public virtual async Task<string> GetOrNullAsync(string componentAppId, string authorizerAppId)
{
var cacheKey = await GetCacheKeyAsync(componentAppId, authorizerAppId);
var cache = await _cache.GetAsync(cacheKey);
var cachedValue = await _cache.GetOrNullAsync(componentAppId, authorizerAppId);

if (cache != null)
if (cachedValue != null)
{
return cache;
return cachedValue;
}

var authorizerSecret = await _authorizerSecretRepository.FindAsync(x =>
x.ComponentAppId == componentAppId && x.AuthorizerAppId == authorizerAppId);

cache = authorizerSecret?.GetRefreshToken(_stringEncryptionService);
cachedValue = authorizerSecret?.GetRefreshToken(_stringEncryptionService);

await _cache.SetAsync(cacheKey, cache);
await _cache.SetAsync(componentAppId, authorizerAppId, cachedValue);

return cache;
return cachedValue;
}

public virtual async Task SetAsync(string componentAppId, string authorizerAppId, string authorizerRefreshToken)
{
// Set only the cache value.
await _cache.SetAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId), authorizerRefreshToken);
await _cache.SetAsync(componentAppId, authorizerAppId, authorizerRefreshToken);
}

protected virtual async Task<string> GetCacheKeyAsync(string componentAppId, string authorizerAppId) =>
$"WeChatAuthorizerRefreshToken:{componentAppId}:{authorizerAppId}";
}
@@ -1,8 +1,5 @@
using System;
using System.Threading.Tasks;
using EasyAbp.WeChatManagement.Common.WeChatApps;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Encryption;
using Volo.Abp.Uow;
Expand All @@ -17,13 +14,13 @@ public class ComponentVerifyTicketStore : IComponentVerifyTicketStore, ITransien
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IStringEncryptionService _stringEncryptionService;
private readonly IWeChatAppRepository _weChatAppRepository;
private readonly IDistributedCache<string> _cache;
private readonly CacheComponentVerifyTicketStore _cache;

public ComponentVerifyTicketStore(
IUnitOfWorkManager unitOfWorkManager,
IStringEncryptionService stringEncryptionService,
IWeChatAppRepository weChatAppRepository,
IDistributedCache<string> cache)
CacheComponentVerifyTicketStore cache)
{
_unitOfWorkManager = unitOfWorkManager;
_stringEncryptionService = stringEncryptionService;
Expand All @@ -34,21 +31,20 @@ public class ComponentVerifyTicketStore : IComponentVerifyTicketStore, ITransien
[UnitOfWork]
public virtual async Task<string> GetOrNullAsync(string componentAppId)
{
var cacheKey = await GetCacheKeyAsync(componentAppId);
var cache = await _cache.GetAsync(cacheKey);
var cachedValue = await _cache.GetOrNullAsync(componentAppId);

if (cache != null)
if (cachedValue != null)
{
return cache;
return cachedValue;
}

var weChatApp = await _weChatAppRepository.FindThirdPartyPlatformAppByAppIdAsync(componentAppId);

cache = weChatApp?.GetVerifyTicketOrNullAsync(_stringEncryptionService);
cachedValue = weChatApp?.GetVerifyTicketOrNullAsync(_stringEncryptionService);

await _cache.SetAsync(cacheKey, cache);
await _cache.SetAsync(componentAppId, cachedValue);

return cache;
return cachedValue;
}

public virtual async Task SetAsync(string componentAppId, string componentVerifyTicket)
Expand All @@ -63,13 +59,6 @@ public virtual async Task SetAsync(string componentAppId, string componentVerify

await uow.CompleteAsync();

await _cache.SetAsync(await GetCacheKeyAsync(componentAppId), componentVerifyTicket,
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(715)
});
await _cache.SetAsync(componentAppId, componentVerifyTicket);
}

protected virtual async Task<string> GetCacheKeyAsync(string componentAppId) =>
$"WeChatComponentVerifyTicket:{componentAppId}";
}

0 comments on commit f1568da

Please sign in to comment.