-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from argon-chat/feature/captcha
Feature/captcha
- Loading branch information
Showing
10 changed files
with
115 additions
and
470 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
public static class CaptchaFeature | ||
{ | ||
public static IServiceCollection AddCaptchaFeature(this WebApplicationBuilder builder) | ||
{ | ||
var cfg = builder.Configuration.GetSection("Captcha"); | ||
builder.Services.Configure<CaptchaOptions>(cfg); | ||
var kind = cfg.GetValue<CaptchaKind>("Kind"); | ||
|
||
return kind switch | ||
{ | ||
CaptchaKind.NO_CAPTCHA => builder.Services.AddTransient<ICaptchaFeature, NullCaptcha>(), | ||
CaptchaKind.CLOUDFLARE => builder.Services.AddTransient<ICaptchaFeature, CloudflareCaptcha>(), | ||
CaptchaKind.YANDEX => builder.Services.AddTransient<ICaptchaFeature, YandexCaptcha>(), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
public enum CaptchaKind | ||
{ | ||
NO_CAPTCHA, | ||
CLOUDFLARE, | ||
YANDEX | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
public class CaptchaOptions | ||
{ | ||
public string SiteKey { get; set; } | ||
public string SiteSecret { get; set; } | ||
public string ChallengeEndpoint { get; set; } | ||
public CaptchaKind Kind { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
using Extensions; | ||
using Flurl.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class CloudflareCaptcha(HttpContext httpContext, ILogger<ICaptchaFeature> logger, IOptions<CaptchaOptions> options) : ICaptchaFeature | ||
{ | ||
public async ValueTask<bool> ValidateAsync(string token) | ||
{ | ||
if (string.IsNullOrEmpty(token)) | ||
return false; | ||
var config = options.Value; | ||
var remoteIp = httpContext.GetIpAddress(); | ||
try | ||
{ | ||
var response = await config.ChallengeEndpoint | ||
.PostMultipartAsync(content => content | ||
.AddString("secret", config.SiteSecret) | ||
.AddString("response", token) | ||
.AddString("remoteip", remoteIp)) | ||
.ReceiveJson<CloudflareTurnstileResponse>(); | ||
logger.LogInformation("Success validate captcha token {Challenge_ts} {Hostname}", response.Challenge_ts, response.Hostname); | ||
return response?.Success ?? false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogCritical(ex, "failed validate captcha token"); | ||
return false; | ||
} | ||
} | ||
|
||
public class CloudflareTurnstileResponse | ||
{ | ||
public bool Success { get; set; } | ||
public string Challenge_ts { get; set; } | ||
public string Hostname { get; set; } | ||
public string[] ErrorCodes { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
public interface ICaptchaFeature | ||
{ | ||
ValueTask<bool> ValidateAsync(string token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
public class NullCaptcha : ICaptchaFeature | ||
{ | ||
public ValueTask<bool> ValidateAsync(string token) => new(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Argon.Api.Features.Captcha; | ||
|
||
public class YandexCaptcha : ICaptchaFeature | ||
{ | ||
public ValueTask<bool> ValidateAsync(string token) => throw new NotImplementedException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters