19 changed files with 329 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -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> |
|||
@ -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> |
|||
@ -0,0 +1,7 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace LINGYUN.Abp.Captcha; |
|||
|
|||
public class AbpCaptchaAbstractionsModule : AbpModule |
|||
{ |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
namespace LINGYUN.Abp.Captcha; |
|||
|
|||
public static class CaptchaKeywords |
|||
{ |
|||
public const string CaptchaIdCookieName = "__captcha_identity"; |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
namespace LINGYUN.Abp.Captcha; |
|||
|
|||
public record CodeCaptchaData(string Id, string Code, byte[] Data); |
|||
@ -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); |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
namespace System.Runtime.CompilerServices; |
|||
|
|||
internal static class IsExternalInit { } |
|||
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -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> |
|||
@ -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> |
|||
@ -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, |
|||
}); |
|||
} |
|||
} |
|||
@ -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>(); |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
namespace LINGYUN.Abp.LazyCaptcha; |
|||
|
|||
public interface ILazyCaptchaNormalizer |
|||
{ |
|||
string NormalizeCaptchaId(string captchaId); |
|||
|
|||
string NormalizeRateLimitKey(string captchaId); |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
@ -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…
Reference in new issue