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] <support@github.com>

* Add support for ReCaptcha in addition to HCaptcha

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
Jayden 2022-07-25 18:38:10 -04:00 committed by GitHub
commit 9c5c5b5798
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 13 deletions

View file

@ -1,6 +1,17 @@
@using LBPUnion.ProjectLighthouse.Configuration
@if (ServerConfiguration.Instance.Captcha.CaptchaEnabled)
{
<div class="h-captcha" data-sitekey="@ServerConfiguration.Instance.Captcha.SiteKey"></div>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
@switch (ServerConfiguration.Instance.Captcha.Type)
{
case CaptchaType.HCaptcha:
<div class="h-captcha" data-sitekey="@ServerConfiguration.Instance.Captcha.SiteKey"></div>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
break;
case CaptchaType.ReCaptcha:
<div class="g-recaptcha" data-sitekey="@ServerConfiguration.Instance.Captcha.SiteKey"></div>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
break;
default:
throw new ArgumentOutOfRangeException();
}
}

View file

@ -0,0 +1,16 @@
namespace LBPUnion.ProjectLighthouse.Configuration;
/// <summary>
/// The service to be used for presenting captchas to the user.
/// </summary>
public enum CaptchaType
{
/// <summary>
/// A privacy-first captcha. https://www.hcaptcha.com/
/// </summary>
HCaptcha,
/// <summary>
/// A captcha service by Google. https://developers.google.com/recaptcha/
/// </summary>
ReCaptcha,
}

View file

@ -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; } = "";

View file

@ -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

View file

@ -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<bool> 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;