-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAPTCHA] Replace ReCAPTCHA with Turnstile (#3249)
- Loading branch information
1 parent
63563d9
commit e71754a
Showing
51 changed files
with
512 additions
and
217 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,6 @@ | |
"piptools", | ||
"Prenoun", | ||
"Preverb", | ||
"recaptcha", | ||
"reportgenerator", | ||
"sched", | ||
"signup", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading.Tasks; | ||
using BackendFramework.Interfaces; | ||
|
||
namespace Backend.Tests.Mocks | ||
{ | ||
sealed internal class CaptchaServiceMock : ICaptchaService | ||
{ | ||
public Task<bool> VerifyToken(string token) | ||
{ | ||
return Task.FromResult(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,21 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using BackendFramework.Interfaces; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BackendFramework.Contexts | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class CaptchaContext : ICaptchaContext | ||
{ | ||
public bool CaptchaEnabled { get; } | ||
public string? CaptchaSecretKey { get; } | ||
public string? CaptchaVerifyUrl { get; } | ||
|
||
public CaptchaContext(IOptions<Startup.Settings> options) | ||
{ | ||
CaptchaEnabled = options.Value.CaptchaEnabled; | ||
CaptchaSecretKey = options.Value.CaptchaSecretKey; | ||
CaptchaVerifyUrl = options.Value.CaptchaVerifyUrl; | ||
} | ||
} | ||
} |
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
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 BackendFramework.Interfaces | ||
{ | ||
public interface ICaptchaContext | ||
{ | ||
bool CaptchaEnabled { get; } | ||
string? CaptchaSecretKey { get; } | ||
string? CaptchaVerifyUrl { get; } | ||
} | ||
} |
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 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace BackendFramework.Interfaces | ||
{ | ||
public interface ICaptchaService | ||
{ | ||
Task<bool> VerifyToken(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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using BackendFramework.Interfaces; | ||
|
||
namespace BackendFramework.Services | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class CaptchaService : ICaptchaService | ||
{ | ||
private readonly ICaptchaContext _captchaContext; | ||
|
||
public CaptchaService(ICaptchaContext captchaContext) | ||
{ | ||
_captchaContext = captchaContext; | ||
} | ||
|
||
public async Task<bool> VerifyToken(string token) | ||
{ | ||
if (!_captchaContext.CaptchaEnabled) | ||
{ | ||
throw new CaptchaNotEnabledException(); | ||
} | ||
|
||
var secret = _captchaContext.CaptchaSecretKey; | ||
var verifyUrl = _captchaContext.CaptchaVerifyUrl; | ||
if (string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(verifyUrl)) | ||
{ | ||
return false; | ||
} | ||
var httpContent = new FormUrlEncodedContent(new Dictionary<string, string>{ | ||
{"response", token}, | ||
{"secret", secret}, | ||
}); | ||
using var result = await new HttpClient().PostAsync(verifyUrl, httpContent); | ||
var contentString = await result.Content.ReadAsStringAsync(); | ||
return contentString.Contains("\"success\":true"); | ||
} | ||
|
||
private sealed class CaptchaNotEnabledException : Exception { } | ||
} | ||
} |
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
Oops, something went wrong.