Skip to content

Commit b6f0194

Browse files
authored
Merge pull request #1 from izanhzh/dev
Fix: cache expiration time setting bug
2 parents 2d99ff9 + c6f3991 commit b6f0194

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Copyright>Amos Huang</Copyright>
55
<Authors>Amos Huang</Authors>
6-
<Version>1.0.1</Version>
6+
<Version>1.0.2</Version>
77
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
88
<PackageReadmeFile>README.md</PackageReadmeFile>
99
<PackageProjectUrl>https://github.com/izanhzh/pow-cap-server</PackageProjectUrl>

src/PowCapServer.Core/DefaultCaptchaService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ public virtual async Task<RedeemChallengeResult> RedeemChallengeAsync(ChallengeS
8080
var hash = DigestUtil.Sha256Hex(vertoken);
8181
var id = RandomUtil.ToHexString(RandomUtil.RandomBytes(8));
8282

83-
await _captchaStore.SaveCaptchaTokenInfoAsync(new CaptchaTokenInfo($"{id}:{hash}", expires), cancellationToken).ConfigureAwait(false);
83+
await _captchaStore.SaveCaptchaTokenInfoAsync(new CaptchaTokenInfo($"{id}_{hash}", expires), cancellationToken).ConfigureAwait(false);
8484

85-
return RedeemChallengeResult.Ok($"{id}:{vertoken}", expires);
85+
return RedeemChallengeResult.Ok($"{id}_{vertoken}", expires);
8686
}
8787

8888
public virtual async Task<bool> ValidateCaptchaTokenAsync(string captchaToken, CancellationToken cancellationToken = default)
8989
{
9090
await _captchaStore.CleanExpiredTokensAsync(cancellationToken).ConfigureAwait(false);
9191

92-
var idAndVertoken = captchaToken?.Split(':');
92+
var idAndVertoken = captchaToken?.Split('_');
9393
if (idAndVertoken == null || idAndVertoken.Length != 2)
9494
{
9595
return false;
9696
}
9797

9898
var id = idAndVertoken[0];
9999
var hash = DigestUtil.Sha256Hex(idAndVertoken[1]);
100-
var actualToken = $"{id}:{hash}";
100+
var actualToken = $"{id}_{hash}";
101101

102102
var captchaTokenInfo = await _captchaStore.GetCaptchaTokenInfoAsync(actualToken, cancellationToken).ConfigureAwait(false);
103103
if (captchaTokenInfo == null)

src/PowCapServer.Core/DefaultCaptchaStore.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace PowCapServer;
1111

1212
public class DefaultCaptchaStore : ICaptchaStore
1313
{
14+
public const string ChallengeTokenCachePrefix = "Captcha:ChallengeToken:";
15+
public const string CaptchaTokenCachePrefix = "Captcha:CaptchaToken:";
16+
1417
private readonly IDistributedCache _distributedCache;
1518

1619
public DefaultCaptchaStore(IDistributedCache distributedCache)
@@ -37,16 +40,16 @@ public virtual async Task SaveChallengeTokenInfoAsync(ChallengeTokenInfo challen
3740

3841
var options = new DistributedCacheEntryOptions
3942
{
40-
AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(challengeTokenInfo.Expires)
43+
AbsoluteExpiration = DateTimeOffset.FromUnixTimeMilliseconds(challengeTokenInfo.Expires)
4144
};
4245
using var stream = new MemoryStream();
4346
await JsonSerializer.SerializeAsync(stream, challengeTokenInfo, cancellationToken: cancellationToken).ConfigureAwait(false);
44-
await _distributedCache.SetAsync(challengeTokenInfo.Token, stream.ToArray(), options, cancellationToken).ConfigureAwait(false);
47+
await _distributedCache.SetAsync($"{ChallengeTokenCachePrefix}{challengeTokenInfo.Token}", stream.ToArray(), options, cancellationToken).ConfigureAwait(false);
4548
}
4649

4750
public virtual async Task<ChallengeTokenInfo?> GetChallengeTokenInfoAsync(string challengeToken, CancellationToken cancellationToken = default)
4851
{
49-
var value = await _distributedCache.GetAsync(challengeToken, cancellationToken).ConfigureAwait(false);
52+
var value = await _distributedCache.GetAsync($"{ChallengeTokenCachePrefix}{challengeToken}", cancellationToken).ConfigureAwait(false);
5053
if (value == null)
5154
{
5255
return null;
@@ -62,7 +65,7 @@ public virtual async Task DeleteChallengeTokenInfoAsync(ChallengeTokenInfo chall
6265
throw new ArgumentNullException(nameof(challengeTokenInfo));
6366
}
6467

65-
await _distributedCache.RemoveAsync(challengeTokenInfo.Token, cancellationToken).ConfigureAwait(false);
68+
await _distributedCache.RemoveAsync($"{ChallengeTokenCachePrefix}{challengeTokenInfo.Token}", cancellationToken).ConfigureAwait(false);
6669
}
6770

6871
public virtual async Task SaveCaptchaTokenInfoAsync(CaptchaTokenInfo captchaTokenInfo, CancellationToken cancellationToken = default)
@@ -74,17 +77,17 @@ public virtual async Task SaveCaptchaTokenInfoAsync(CaptchaTokenInfo captchaToke
7477

7578
var options = new DistributedCacheEntryOptions
7679
{
77-
AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(captchaTokenInfo.Expires)
80+
AbsoluteExpiration = DateTimeOffset.FromUnixTimeMilliseconds(captchaTokenInfo.Expires)
7881
};
7982

8083
using var stream = new MemoryStream();
8184
await JsonSerializer.SerializeAsync(stream, captchaTokenInfo, cancellationToken: cancellationToken).ConfigureAwait(false);
82-
await _distributedCache.SetAsync(captchaTokenInfo.Token, stream.ToArray(), options, cancellationToken).ConfigureAwait(false);
85+
await _distributedCache.SetAsync($"{CaptchaTokenCachePrefix}{captchaTokenInfo.Token}", stream.ToArray(), options, cancellationToken).ConfigureAwait(false);
8386
}
8487

8588
public virtual async Task<CaptchaTokenInfo?> GetCaptchaTokenInfoAsync(string captchaToken, CancellationToken cancellationToken = default)
8689
{
87-
var value = await _distributedCache.GetAsync(captchaToken, cancellationToken).ConfigureAwait(false);
90+
var value = await _distributedCache.GetAsync($"{CaptchaTokenCachePrefix}{captchaToken}", cancellationToken).ConfigureAwait(false);
8891
if (value == null)
8992
{
9093
return null;
@@ -100,6 +103,6 @@ public virtual async Task DeleteCaptchaTokenInfoAsync(CaptchaTokenInfo captchaTo
100103
throw new ArgumentNullException(nameof(captchaTokenInfo));
101104
}
102105

103-
await _distributedCache.RemoveAsync(captchaTokenInfo.Token, cancellationToken).ConfigureAwait(false);
106+
await _distributedCache.RemoveAsync($"{CaptchaTokenCachePrefix}{captchaTokenInfo.Token}", cancellationToken).ConfigureAwait(false);
104107
}
105108
}

src/PowCapServer.Core/Microsoft/Extensions/DependencyInjection/PowCapServerServiceCollectionExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Microsoft.Extensions.DependencyInjection.Extensions;
32
using PowCapServer;
43
using PowCapServer.Abstractions;
54

@@ -23,8 +22,8 @@ public static IServiceCollection AddPowCapServer(this IServiceCollection service
2322
public static IServiceCollection AddPowCapServer(this IServiceCollection services, Action<PowCapServerOptions> options)
2423
{
2524
services.AddDistributedMemoryCache();
26-
services.TryAddSingleton<ICaptchaService, DefaultCaptchaService>();
27-
services.TryAddSingleton<ICaptchaStore, DefaultCaptchaStore>();
25+
services.AddSingleton<ICaptchaService, DefaultCaptchaService>();
26+
services.AddSingleton<ICaptchaStore, DefaultCaptchaStore>();
2827
services.Configure(options);
2928
return services;
3029
}

0 commit comments

Comments
 (0)