committed by
GitHub
18 changed files with 216 additions and 47 deletions
@ -1,10 +1,28 @@ |
|||||
namespace LINGYUN.Abp.OpenApi; |
using System; |
||||
|
|
||||
|
namespace LINGYUN.Abp.OpenApi; |
||||
|
|
||||
public class AbpOpenApiOptions |
public class AbpOpenApiOptions |
||||
{ |
{ |
||||
|
/// <summary>
|
||||
|
/// 启用Api签名检查
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// 默认: true
|
||||
|
/// </remarks>
|
||||
public bool IsEnabled { get; set; } |
public bool IsEnabled { get; set; } |
||||
|
/// <summary>
|
||||
|
/// 请求随机数过期时间
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// 默认: 10分钟
|
||||
|
/// </remarks>
|
||||
|
public TimeSpan RequestNonceExpireIn { get; set; } |
||||
|
|
||||
public AbpOpenApiOptions() |
public AbpOpenApiOptions() |
||||
{ |
{ |
||||
IsEnabled = true; |
IsEnabled = true; |
||||
|
|
||||
|
RequestNonceExpireIn = TimeSpan.FromMinutes(10); |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,45 @@ |
|||||
|
using Microsoft.Extensions.Caching.Distributed; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace LINGYUN.Abp.OpenApi; |
||||
|
public class DefaultNonceStore : INonceStore, ITransientDependency |
||||
|
{ |
||||
|
private const string CacheKeyFormat = "open-api,n:{0}"; |
||||
|
|
||||
|
private readonly IDistributedCache<NonceStateCacheItem> _nonceCache; |
||||
|
private readonly AbpOpenApiOptions _options; |
||||
|
|
||||
|
public DefaultNonceStore( |
||||
|
IDistributedCache<NonceStateCacheItem> nonceCache, |
||||
|
IOptions<AbpOpenApiOptions> options) |
||||
|
{ |
||||
|
_nonceCache = nonceCache; |
||||
|
_options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public async virtual Task<bool> TrySetAsync(string nonce, CancellationToken cancellationToken = default) |
||||
|
{ |
||||
|
var cacheKey = string.Format(CacheKeyFormat, nonce); |
||||
|
|
||||
|
var cacheItem = await _nonceCache.GetAsync(cacheKey, token: cancellationToken); |
||||
|
if (cacheItem == null) |
||||
|
{ |
||||
|
await _nonceCache.SetAsync( |
||||
|
cacheKey, |
||||
|
new NonceStateCacheItem(nonce), |
||||
|
options: new DistributedCacheEntryOptions |
||||
|
{ |
||||
|
AbsoluteExpirationRelativeToNow = _options.RequestNonceExpireIn, |
||||
|
}, |
||||
|
token: cancellationToken); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace LINGYUN.Abp.OpenApi; |
||||
|
public interface INonceStore |
||||
|
{ |
||||
|
Task<bool> TrySetAsync(string nonce, CancellationToken cancellationToken = default); |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace LINGYUN.Abp.OpenApi; |
||||
|
|
||||
|
[Serializable] |
||||
|
public class NonceStateCacheItem |
||||
|
{ |
||||
|
private const string CacheKeyFormat = "open-api,nonce:{0}"; |
||||
|
|
||||
|
public string Nonce { get; set; } |
||||
|
|
||||
|
public NonceStateCacheItem() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public NonceStateCacheItem(string nonce) |
||||
|
{ |
||||
|
Nonce = nonce; |
||||
|
} |
||||
|
|
||||
|
public static string CalculateCacheKey(string nonce) |
||||
|
{ |
||||
|
return string.Format(CacheKeyFormat, nonce); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue