Browse Source

feat: Integrated LazyCaptcha

pull/1573/head
colin 2 months ago
parent
commit
fe4be4de35
  1. 1
      Directory.Packages.props
  2. 5
      aspnet-core/LINGYUN.MicroService.Aspire.slnx
  3. 3
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xml
  4. 30
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xsd
  5. 19
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj
  6. 7
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/AbpCaptchaAbstractionsModule.cs
  7. 6
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CaptchaKeywords.cs
  8. 3
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/CodeCaptchaData.cs
  9. 14
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/ICodeCaptchaProvider.cs
  10. 3
      aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN/Abp/Captcha/IsExternalInit.cs
  11. 3
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xml
  12. 30
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xsd
  13. 26
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj
  14. 31
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpCaptchaStorage.cs
  15. 28
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/AbpLazyCaptchaModule.cs
  16. 20
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/CaptchaCacheItem.cs
  17. 8
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/ILazyCaptchaNormalizer.cs
  18. 47
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCaptchaNormalizer.cs
  19. 45
      aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN/Abp/LazyCaptcha/LazyCodeCaptchaProvider.cs

1
Directory.Packages.props

@ -355,6 +355,7 @@
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="IP2Region.Net" Version="3.0.2" />
<PackageVersion Include="JetBrains.Annotations" Version="2025.2.2" />
<PackageVersion Include="Lazy.Captcha.Core" Version="2.2.5" />
<PackageVersion Include="Magicodes.IE.Excel" Version="2.9.0" />
<PackageVersion Include="MailKit" Version="4.17.0" />
<PackageVersion Include="Markdig" Version="1.3.2" />

5
aspnet-core/LINGYUN.MicroService.Aspire.slnx

@ -130,6 +130,7 @@
<Project Path="framework/data-protection/LINGYUN.Abp.DataProtection/LINGYUN.Abp.DataProtection.csproj" />
</Folder>
<Folder Name="/framework/dynamic-definition/">
<Project Path="framework/dynamic-definition/LINGYUN.Abp.Dynamic.Definitions.Core/LINGYUN.Abp.Dynamic.Definitions.Core.csproj" />
<Project Path="framework/dynamic-definition/LINGYUN.Abp.Dynamic.Definitions/LINGYUN.Abp.Dynamic.Definitions.csproj" />
<Project Path="framework/dynamic-definition/LINGYUN.Abp.Dynamic.Definitions.Core/LINGYUN.Abp.Dynamic.Definitions.Core.csproj" />
</Folder>
@ -325,6 +326,10 @@
<Project Path="modules/caching-management/LINGYUN.Abp.CachingManagement.HttpApi/LINGYUN.Abp.CachingManagement.HttpApi.csproj" />
<Project Path="modules/caching-management/LINGYUN.Abp.CachingManagement.StackExchangeRedis/LINGYUN.Abp.CachingManagement.StackExchangeRedis.csproj" />
</Folder>
<Folder Name="/modules/captcha/">
<Project Path="modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj" Id="06e085b5-82e8-426b-ac59-496a3d4d9ca3" />
<Project Path="modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj" />
</Folder>
<Folder Name="/modules/data-protection/">
<Project Path="modules/data-protection/LINGYUN.Abp.DataProtectionManagement.Application.Contracts/LINGYUN.Abp.DataProtectionManagement.Application.Contracts.csproj" />
<Project Path="modules/data-protection/LINGYUN.Abp.DataProtectionManagement.Application/LINGYUN.Abp.DataProtectionManagement.Application.csproj" />

3
aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xml

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>

30
aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/FodyWeavers.xsd

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

19
aspnet-core/modules/captcha/LINGYUN.Abp.Captcha.Abstractions/LINGYUN.Abp.Captcha.Abstractions.csproj

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\configureawait.props" />
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0</TargetFrameworks>
<AssemblyName>LINGYUN.Abp.Captcha.Abstractions</AssemblyName>
<PackageId>LINGYUN.Abp.Captcha.Abstractions</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Core" />
</ItemGroup>
</Project>

7
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
{
}

6
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";
}

3
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);

14
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<CodeCaptchaData> GenerateAsync(string captchaId);
Task<bool> ValidateAsync(string captchaId, string code);
Task<bool> RemoveAsync(string captchaId);
}

3
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 { }

3
aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xml

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>

30
aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/FodyWeavers.xsd

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

26
aspnet-core/modules/captcha/LINGYUN.Abp.LazyCaptcha/LINGYUN.Abp.LazyCaptcha.csproj

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\configureawait.props" />
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;net9.0;net10.0</TargetFrameworks>
<AssemblyName>LINGYUN.Abp.Captcha.LazyCaptcha</AssemblyName>
<PackageId>LINGYUN.Abp.Captcha.LazyCaptcha</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Caching" />
<PackageReference Include="Volo.Abp.MultiTenancy.Abstractions" />
<PackageReference Include="Volo.Abp.Settings" />
<PackageReference Include="Lazy.Captcha.Core" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LINGYUN.Abp.Captcha.Abstractions\LINGYUN.Abp.Captcha.Abstractions.csproj" />
</ItemGroup>
</Project>

31
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<CaptchaCacheItem> _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,
});
}
}

28
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<IStorage, AbpCaptchaStorage>());
context.Services.AddScoped<ICodeCaptchaProvider, LazyCodeCaptchaProvider>();
}
}

20
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;
}
}

8
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);
}

47
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();
}
}

45
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<CodeCaptchaData> GenerateAsync(string captchaId)
{
var captchData = _captcha.GenerateWithRateLimit(
NormalizeCaptchaId(captchaId),
NormalizeRateLimitKey(captchaId));
return new CodeCaptchaData(captchData.Id, captchData.Code, captchData.Bytes);
}
public virtual Task<bool> RemoveAsync(string captchaId)
{
_storage.Remove(NormalizeCaptchaId(captchaId));
return Task.FromResult(true);
}
public async virtual Task<bool> 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);
}
}
Loading…
Cancel
Save