Skip to content

Commit

Permalink
chore : fix dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-vortex committed Nov 17, 2024
1 parent 01a9b7e commit 55d3828
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 46 deletions.
35 changes: 10 additions & 25 deletions NeoCaptcha.AspnetCore/Attributes/VerifyNeoCaptchaAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MyAspNetCoreExtensions.Enitities;
using NeoCaptcha.AspnetCore.Entities;
using Microsoft.Extensions.DependencyInjection;

using NeoCaptcha.AspnetCore.Interfaces;


namespace NeoCaptcha.AspnetCore.Attributes;

public class VerifyNeoCaptchaAttribute(ICaptchaGenerator captchaGenerator) : ActionFilterAttribute
public class VerifyNeoCaptchaAttribute : Attribute, IFilterFactory
{
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// Find the model that implements RecaptchaCapableModel

if (context.ActionArguments.Values
.FirstOrDefault(arg => arg is NeoCaptchaCapableModel) is not NeoCaptchaCapableModel model)
{
context.Result = new BadRequestObjectResult("Invalid model for captcha validation.");
return;
}
public bool IsReusable => false;

// Perform captcha validation
var validationResult = await captchaGenerator.ValidateCaptcha(model.CaptchaId, model.CaptchaChallenge);

if (validationResult != CaptchaValidationResult.OK)
{
context.Result = new BadRequestObjectResult("Captcha validation failed : captcha result :" + validationResult);
return;
}

// Continue to the action if validation succeeds
await next();
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
// Resolve ICaptchaGenerator from the service provider
var captchaGenerator = serviceProvider.GetRequiredService<ICaptchaGenerator>();
return new VerifyNeoCaptchaFilter(captchaGenerator);
}
}
14 changes: 2 additions & 12 deletions NeoCaptcha.AspnetCore/NeoCaptcha.AspnetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Jitbit.FastCache" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="NeoCaptcha" Version="1.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Mvc.Abstractions">
<HintPath>..\..\..\..\..\usr\share\dotnet\shared\Microsoft.AspNetCore.App\8.0.11\Microsoft.AspNetCore.Mvc.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Mvc.Core">
<HintPath>..\..\..\..\..\usr\share\dotnet\shared\Microsoft.AspNetCore.App\8.0.11\Microsoft.AspNetCore.Mvc.Core.dll</HintPath>
</Reference>
</ItemGroup>


</Project>
4 changes: 1 addition & 3 deletions NeoCaptcha.AspnetCore/NeoCaptchaGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using NeoCaptcha.AspnetCore.Attributes;
using Microsoft.Extensions.DependencyInjection;
using NeoCaptcha.AspnetCore.Entities;
using NeoCaptcha.AspnetCore.Interfaces;

Expand Down
31 changes: 31 additions & 0 deletions NeoCaptcha.AspnetCore/VerifyNeoCaptchaFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MyAspNetCoreExtensions.Enitities;
using NeoCaptcha.AspnetCore.Entities;
using NeoCaptcha.AspnetCore.Interfaces;

namespace NeoCaptcha.AspnetCore;

public class VerifyNeoCaptchaFilter(ICaptchaGenerator captchaGenerator) : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// Locate the model that implements NeoCaptchaCapableModel
if (context.ActionArguments.Values.FirstOrDefault(arg => arg is NeoCaptchaCapableModel) is not NeoCaptchaCapableModel model)
{
context.Result = new UnauthorizedObjectResult("Invalid captcha model");
return;
}

// Validate the captcha
var validationResult = await captchaGenerator.ValidateCaptcha(model.CaptchaId, model.CaptchaChallenge);

if (validationResult != CaptchaValidationResult.OK)
{
context.Result = new UnauthorizedObjectResult($"Captcha validation failed: {validationResult}");
return;
}

await next(); // Proceed if validation succeeds
}
}
19 changes: 13 additions & 6 deletions NeoCaptcha.AspnetCore/VerifyNeoCaptchaFilterFactory.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using Microsoft.AspNetCore.Mvc.Filters;
using NeoCaptcha.AspnetCore.Attributes;
using Microsoft.Extensions.DependencyInjection;
using NeoCaptcha.AspnetCore;
using NeoCaptcha.AspnetCore.Interfaces;

namespace NeoCaptcha.AspnetCore;

public class VerifyNeoCaptchaFilterFactory(ICaptchaGenerator captchaGenerator) : IFilterFactory
public class VerifyNeoCaptchaFilterFactory : IFilterFactory
{
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
private readonly ICaptchaGenerator _captchaGenerator;

public VerifyNeoCaptchaFilterFactory(ICaptchaGenerator captchaGenerator)
{
return new VerifyNeoCaptchaAttribute(captchaGenerator);
_captchaGenerator = captchaGenerator;
}

public bool IsReusable => false;

public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
// Create the filter instance, passing the required service
return new VerifyNeoCaptchaFilter(_captchaGenerator);
}
}
2 changes: 2 additions & 0 deletions NeoCaptcha.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AActionResult_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa105b4a31e844e8895058fb1b02b9bc81dea00_003F3f_003Fa13a27c0_003FActionResult_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AControllerContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F3cff62e5ef214e58897fd0f1eafd3beb6cb60_003F35_003Fd590ee27_003FControllerContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=076b12b3_002Dd90d_002D4aa8_002Da9f6_002D1091fde4cefb/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;NeoCaptcha.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Project Location="/home/neo/Projects/NeoCaptcha/NeoCaptcha.Tests" Presentation="&amp;lt;NeoCaptcha.Tests&amp;gt;" /&gt;
&lt;/SessionState&gt;</s:String>
Expand Down

0 comments on commit 55d3828

Please sign in to comment.