From 9c5c5b5798a633839b92520e15f484b09cfd2cdc Mon Sep 17 00:00:00 2001 From: Jayden Date: Mon, 25 Jul 2022 18:38:10 -0400 Subject: [PATCH] Add support for ReCaptcha in addition to HCaptcha (#372) * Bump YamlDotNet from 11.2.1 to 12.0.0 Bumps [YamlDotNet](https://github.com/aaubry/YamlDotNet) from 11.2.1 to 12.0.0. - [Release notes](https://github.com/aaubry/YamlDotNet/releases) - [Commits](https://github.com/aaubry/YamlDotNet/compare/v11.2.1...v12.0.0) --- updated-dependencies: - dependency-name: YamlDotNet dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] * Add support for ReCaptcha in addition to HCaptcha Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .../Pages/Partials/CaptchaPartial.cshtml | 15 +++++++-- .../Configuration/CaptchaType.cs | 16 ++++++++++ .../CaptchaConfiguration.cs | 5 ++- .../Configuration/ServerConfiguration.cs | 2 +- .../Extensions/RequestExtensions.cs | 32 +++++++++++++++---- 5 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 ProjectLighthouse/Configuration/CaptchaType.cs diff --git a/ProjectLighthouse.Servers.Website/Pages/Partials/CaptchaPartial.cshtml b/ProjectLighthouse.Servers.Website/Pages/Partials/CaptchaPartial.cshtml index d54cebce..3a202ba9 100644 --- a/ProjectLighthouse.Servers.Website/Pages/Partials/CaptchaPartial.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/Partials/CaptchaPartial.cshtml @@ -1,6 +1,17 @@ @using LBPUnion.ProjectLighthouse.Configuration @if (ServerConfiguration.Instance.Captcha.CaptchaEnabled) { -
- + @switch (ServerConfiguration.Instance.Captcha.Type) + { + case CaptchaType.HCaptcha: +
+ + break; + case CaptchaType.ReCaptcha: +
+ + break; + default: + throw new ArgumentOutOfRangeException(); + } } \ No newline at end of file diff --git a/ProjectLighthouse/Configuration/CaptchaType.cs b/ProjectLighthouse/Configuration/CaptchaType.cs new file mode 100644 index 00000000..564d99c9 --- /dev/null +++ b/ProjectLighthouse/Configuration/CaptchaType.cs @@ -0,0 +1,16 @@ +namespace LBPUnion.ProjectLighthouse.Configuration; + +/// +/// The service to be used for presenting captchas to the user. +/// +public enum CaptchaType +{ + /// + /// A privacy-first captcha. https://www.hcaptcha.com/ + /// + HCaptcha, + /// + /// A captcha service by Google. https://developers.google.com/recaptcha/ + /// + ReCaptcha, +} \ No newline at end of file diff --git a/ProjectLighthouse/Configuration/ConfigurationCategories/CaptchaConfiguration.cs b/ProjectLighthouse/Configuration/ConfigurationCategories/CaptchaConfiguration.cs index fad16ae6..fd4ea31d 100644 --- a/ProjectLighthouse/Configuration/ConfigurationCategories/CaptchaConfiguration.cs +++ b/ProjectLighthouse/Configuration/ConfigurationCategories/CaptchaConfiguration.cs @@ -2,11 +2,10 @@ namespace LBPUnion.ProjectLighthouse.Configuration.ConfigurationCategories; public class CaptchaConfiguration { - // TODO: support recaptcha, not just hcaptcha - // use an enum to define which captcha services can be used? - // LBPUnion.ProjectLighthouse.Types.Settings.CaptchaService public bool CaptchaEnabled { get; set; } + public CaptchaType Type { get; set; } = CaptchaType.HCaptcha; + public string SiteKey { get; set; } = ""; public string Secret { get; set; } = ""; diff --git a/ProjectLighthouse/Configuration/ServerConfiguration.cs b/ProjectLighthouse/Configuration/ServerConfiguration.cs index 3142339e..30c5ee41 100644 --- a/ProjectLighthouse/Configuration/ServerConfiguration.cs +++ b/ProjectLighthouse/Configuration/ServerConfiguration.cs @@ -23,7 +23,7 @@ public class ServerConfiguration // You can use an ObsoleteAttribute instead. Make sure you set it to error, though. // // Thanks for listening~ - public const int CurrentConfigVersion = 6; + public const int CurrentConfigVersion = 7; #region Meta diff --git a/ProjectLighthouse/Extensions/RequestExtensions.cs b/ProjectLighthouse/Extensions/RequestExtensions.cs index d375246c..c922945d 100644 --- a/ProjectLighthouse/Extensions/RequestExtensions.cs +++ b/ProjectLighthouse/Extensions/RequestExtensions.cs @@ -15,7 +15,21 @@ namespace LBPUnion.ProjectLighthouse.Extensions; public static class RequestExtensions { - + static RequestExtensions() + { + Uri captchaUri = ServerConfiguration.Instance.Captcha.Type switch + { + CaptchaType.HCaptcha => new Uri("https://hcaptcha.com"), + CaptchaType.ReCaptcha => new Uri("https://www.google.com/recaptcha/api/"), + _ => throw new ArgumentOutOfRangeException(), + }; + + client = new HttpClient + { + BaseAddress = captchaUri, + }; + } + #region Mobile Checking // yoinked and adapted from https://stackoverflow.com/a/68641796 @@ -32,10 +46,7 @@ public static class RequestExtensions #region Captcha - private static readonly HttpClient client = new() - { - BaseAddress = new Uri("https://hcaptcha.com"), - }; + private static readonly HttpClient client; [SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeNotEvident")] private static async Task verifyCaptcha(string token) @@ -48,7 +59,7 @@ public static class RequestExtensions new("response", token), }; - HttpResponseMessage response = await client.PostAsync("/siteverify", new FormUrlEncodedContent(payload)); + HttpResponseMessage response = await client.PostAsync("siteverify", new FormUrlEncodedContent(payload)); response.EnsureSuccessStatusCode(); @@ -63,7 +74,14 @@ public static class RequestExtensions { if (ServerConfiguration.Instance.Captcha.CaptchaEnabled) { - bool gotCaptcha = request.Form.TryGetValue("h-captcha-response", out StringValues values); + string keyName = ServerConfiguration.Instance.Captcha.Type switch + { + CaptchaType.HCaptcha => "h-captcha-response", + CaptchaType.ReCaptcha => "g-recaptcha-response", + _ => throw new ArgumentOutOfRangeException(), + }; + + bool gotCaptcha = request.Form.TryGetValue(keyName, out StringValues values); if (!gotCaptcha) return false; if (!await verifyCaptcha(values[0])) return false;