diff --git a/Directory.Packages.props b/Directory.Packages.props index 3892a028e..5406f289c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -355,6 +355,7 @@ + diff --git a/aspnet-core/LINGYUN.MicroService.Aspire.slnx b/aspnet-core/LINGYUN.MicroService.Aspire.slnx index b00ca5382..9988fa52a 100644 --- a/aspnet-core/LINGYUN.MicroService.Aspire.slnx +++ b/aspnet-core/LINGYUN.MicroService.Aspire.slnx @@ -130,6 +130,7 @@ + @@ -325,6 +326,10 @@ + + + + diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xml b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xml new file mode 100644 index 000000000..1715698cc --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xsd b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xsd new file mode 100644 index 000000000..3f3946e28 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xsd @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj new file mode 100644 index 000000000..33571e3ee --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj @@ -0,0 +1,19 @@ + + + + + + + netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0 + LINGYUN.Abp.Captcha.Abstractions + LINGYUN.Abp.Captcha.Abstractions + false + false + false + + + + + + + diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/AbpCaptchaAbstractionsModule.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/AbpCaptchaAbstractionsModule.cs new file mode 100644 index 000000000..3a8db146b --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/AbpCaptchaAbstractionsModule.cs @@ -0,0 +1,7 @@ +using Volo.Abp.Modularity; + +namespace LINGYUN.Abp.Captcha; + +public class AbpCaptchaAbstractionsModule : AbpModule +{ +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CaptchaKeywords.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CaptchaKeywords.cs new file mode 100644 index 000000000..4d11235ce --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CaptchaKeywords.cs @@ -0,0 +1,6 @@ +namespace LINGYUN.Abp.Captcha; + +public static class CaptchaKeywords +{ + public const string CaptchaIdCookieName = "__captcha_identity"; +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CodeCaptchaData.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CodeCaptchaData.cs new file mode 100644 index 000000000..d83330239 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CodeCaptchaData.cs @@ -0,0 +1,3 @@ +namespace LINGYUN.Abp.Captcha; + +public record CodeCaptchaData(string Id, string Code, byte[] Data); diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/ICodeCaptchaProvider.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/ICodeCaptchaProvider.cs new file mode 100644 index 000000000..151f93f59 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/ICodeCaptchaProvider.cs @@ -0,0 +1,14 @@ +using System.Threading.Tasks; + +namespace LINGYUN.Abp.Captcha; + +public interface ICodeCaptchaProvider +{ + string Name { get; } + + Task GenerateAsync(string captchaId); + + Task ValidateAsync(string captchaId, string code); + + Task RemoveAsync(string captchaId); +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/IsExternalInit.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/IsExternalInit.cs new file mode 100644 index 000000000..4351fad11 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/IsExternalInit.cs @@ -0,0 +1,3 @@ +namespace System.Runtime.CompilerServices; + +internal static class IsExternalInit { } diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xml b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xml new file mode 100644 index 000000000..1715698cc --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xsd b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xsd new file mode 100644 index 000000000..3f3946e28 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xsd @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj new file mode 100644 index 000000000..70d23524a --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj @@ -0,0 +1,26 @@ + + + + + + + netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0 + LINGYUN.Abp.Captcha.LazyCaptcha + LINGYUN.Abp.Captcha.LazyCaptcha + false + false + false + + + + + + + + + + + + + + diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpCaptchaStorage.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpCaptchaStorage.cs new file mode 100644 index 000000000..016c2814e --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpCaptchaStorage.cs @@ -0,0 +1,31 @@ +using Lazy.Captcha.Core.Storage; +using Microsoft.Extensions.Caching.Distributed; +using System; +using Volo.Abp.Caching; + +namespace LINGYUN.Abp.LazyCaptcha; + +public class AbpCaptchaStorage(IDistributedCache _cache) : IStorage +{ + public virtual string? Get(string key) + { + var cacheItem = _cache.Get(key); + return cacheItem?.Value; + } + + public virtual void Remove(string key) + { + _cache.Remove(key); + } + + public virtual void Set(string key, string value, DateTimeOffset absoluteExpiration) + { + _cache.Set( + key, + new CaptchaCacheItem(key, value), + new DistributedCacheEntryOptions + { + AbsoluteExpiration = absoluteExpiration, + }); + } +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpLazyCaptchaModule.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpLazyCaptchaModule.cs new file mode 100644 index 000000000..4610bbbde --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpLazyCaptchaModule.cs @@ -0,0 +1,28 @@ +using Lazy.Captcha.Core.Storage; +using LINGYUN.Abp.Captcha; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.Caching; +using Volo.Abp.Modularity; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Settings; + +namespace LINGYUN.Abp.LazyCaptcha; + +[DependsOn( + typeof(AbpCaptchaAbstractionsModule), + typeof(AbpCachingModule), + typeof(AbpMultiTenancyAbstractionsModule), + typeof(AbpSettingsModule))] +public class AbpLazyCaptchaModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddCaptcha(context.Configuration); + + context.Services.Replace( + ServiceDescriptor.Scoped()); + + context.Services.AddScoped(); + } +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/CaptchaCacheItem.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/CaptchaCacheItem.cs new file mode 100644 index 000000000..d062b0d39 --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/CaptchaCacheItem.cs @@ -0,0 +1,20 @@ +using Volo.Abp.Caching; + +namespace LINGYUN.Abp.LazyCaptcha; + +[CacheName("LazyCaptcha")] +public class CaptchaCacheItem +{ + public string Key { get; set; } = default!; + public string Value { get; set; } = default!; + public CaptchaCacheItem() + { + + } + + public CaptchaCacheItem(string key, string value) + { + Key = key; + Value = value; + } +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/ILazyCaptchaNormalizer.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/ILazyCaptchaNormalizer.cs new file mode 100644 index 000000000..4380d70bf --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/ILazyCaptchaNormalizer.cs @@ -0,0 +1,8 @@ +namespace LINGYUN.Abp.LazyCaptcha; + +public interface ILazyCaptchaNormalizer +{ + string NormalizeCaptchaId(string captchaId); + + string NormalizeRateLimitKey(string captchaId); +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCaptchaNormalizer.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCaptchaNormalizer.cs new file mode 100644 index 000000000..38ab7beaa --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCaptchaNormalizer.cs @@ -0,0 +1,47 @@ +using System; +using System.Text; +using Volo.Abp.Clients; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Users; + +namespace LINGYUN.Abp.LazyCaptcha; + +public class LazyCaptchaNormalizer( + ICurrentClient _currentClient, + ICurrentUser _currentUser, + ICurrentTenant _currentTenant) : ILazyCaptchaNormalizer, ISingletonDependency +{ + protected const char SeparatorChar = ';'; + public virtual string NormalizeCaptchaId(string captchaId) + { + return captchaId; + } + + public virtual string NormalizeRateLimitKey(string captchaId) + { + var rateLimitKeyBuilder = new StringBuilder(); + if (_currentTenant.Id.HasValue) + { + rateLimitKeyBuilder.Append("t:"); + rateLimitKeyBuilder.Append(_currentTenant.Id.Value.ToString("N")); + rateLimitKeyBuilder.Append(SeparatorChar); + } + + if (!_currentClient.Id.IsNullOrWhiteSpace()) + { + rateLimitKeyBuilder.Append("c:"); + rateLimitKeyBuilder.Append(_currentClient.Id); + rateLimitKeyBuilder.Append(SeparatorChar); + } + + if (_currentUser.Id.HasValue) + { + rateLimitKeyBuilder.Append("u:"); + rateLimitKeyBuilder.Append(_currentUser.Id.Value.ToString("N")); + rateLimitKeyBuilder.Append(SeparatorChar); + } + + return rateLimitKeyBuilder.ToString(); + } +} diff --git a/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCodeCaptchaProvider.cs b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCodeCaptchaProvider.cs new file mode 100644 index 000000000..9ed987beb --- /dev/null +++ b/aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCodeCaptchaProvider.cs @@ -0,0 +1,45 @@ +using Lazy.Captcha.Core; +using Lazy.Captcha.Core.Storage; +using LINGYUN.Abp.Captcha; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.LazyCaptcha; + +public class LazyCodeCaptchaProvider( + ICaptcha _captcha, + IStorage _storage, + ILazyCaptchaNormalizer _captchaIdNormalizer) : ICodeCaptchaProvider +{ + public const string ProviderName = "LazyCaptcha"; + public string Name => ProviderName; + + public async virtual Task GenerateAsync(string captchaId) + { + var captchData = _captcha.GenerateWithRateLimit( + NormalizeCaptchaId(captchaId), + NormalizeRateLimitKey(captchaId)); + return new CodeCaptchaData(captchData.Id, captchData.Code, captchData.Bytes); + } + + public virtual Task RemoveAsync(string captchaId) + { + _storage.Remove(NormalizeCaptchaId(captchaId)); + + return Task.FromResult(true); + } + + public async virtual Task ValidateAsync(string captchaId, string code) + { + return _captcha.Validate(NormalizeCaptchaId(captchaId), code); + } + + protected virtual string NormalizeCaptchaId(string captchaId) + { + return _captchaIdNormalizer.NormalizeCaptchaId(captchaId); + } + + protected virtual string NormalizeRateLimitKey(string captchaId) + { + return _captchaIdNormalizer.NormalizeRateLimitKey(captchaId); + } +}