-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1027 from colinin/open-api-validation
refactor(open-api): get api key from the request header
- Loading branch information
Showing
18 changed files
with
216 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
aspnet-core/framework/open-api/LINGYUN.Abp.OpenApi/LINGYUN/Abp/OpenApi/AbpOpenApiOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
namespace LINGYUN.Abp.OpenApi; | ||
using System; | ||
|
||
namespace LINGYUN.Abp.OpenApi; | ||
|
||
public class AbpOpenApiOptions | ||
{ | ||
/// <summary> | ||
/// 启用Api签名检查 | ||
/// </summary> | ||
/// <remarks> | ||
/// 默认: true | ||
/// </remarks> | ||
public bool IsEnabled { get; set; } | ||
/// <summary> | ||
/// 请求随机数过期时间 | ||
/// </summary> | ||
/// <remarks> | ||
/// 默认: 10分钟 | ||
/// </remarks> | ||
public TimeSpan RequestNonceExpireIn { get; set; } | ||
|
||
public AbpOpenApiOptions() | ||
{ | ||
IsEnabled = true; | ||
|
||
RequestNonceExpireIn = TimeSpan.FromMinutes(10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
aspnet-core/framework/open-api/LINGYUN.Abp.OpenApi/LINGYUN/Abp/OpenApi/DefaultNonceStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
aspnet-core/framework/open-api/LINGYUN.Abp.OpenApi/LINGYUN/Abp/OpenApi/INonceStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.