mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +00:00
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:
parent
2391eb14d0
commit
9c5c5b5798
5 changed files with 57 additions and 13 deletions
|
@ -1,6 +1,17 @@
|
||||||
@using LBPUnion.ProjectLighthouse.Configuration
|
@using LBPUnion.ProjectLighthouse.Configuration
|
||||||
@if (ServerConfiguration.Instance.Captcha.CaptchaEnabled)
|
@if (ServerConfiguration.Instance.Captcha.CaptchaEnabled)
|
||||||
{
|
{
|
||||||
<div class="h-captcha" data-sitekey="@ServerConfiguration.Instance.Captcha.SiteKey"></div>
|
@switch (ServerConfiguration.Instance.Captcha.Type)
|
||||||
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
16
ProjectLighthouse/Configuration/CaptchaType.cs
Normal file
16
ProjectLighthouse/Configuration/CaptchaType.cs
Normal 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,
|
||||||
|
}
|
|
@ -2,11 +2,10 @@ namespace LBPUnion.ProjectLighthouse.Configuration.ConfigurationCategories;
|
||||||
|
|
||||||
public class CaptchaConfiguration
|
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 bool CaptchaEnabled { get; set; }
|
||||||
|
|
||||||
|
public CaptchaType Type { get; set; } = CaptchaType.HCaptcha;
|
||||||
|
|
||||||
public string SiteKey { get; set; } = "";
|
public string SiteKey { get; set; } = "";
|
||||||
|
|
||||||
public string Secret { get; set; } = "";
|
public string Secret { get; set; } = "";
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class ServerConfiguration
|
||||||
// You can use an ObsoleteAttribute instead. Make sure you set it to error, though.
|
// You can use an ObsoleteAttribute instead. Make sure you set it to error, though.
|
||||||
//
|
//
|
||||||
// Thanks for listening~
|
// Thanks for listening~
|
||||||
public const int CurrentConfigVersion = 6;
|
public const int CurrentConfigVersion = 7;
|
||||||
|
|
||||||
#region Meta
|
#region Meta
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,21 @@ namespace LBPUnion.ProjectLighthouse.Extensions;
|
||||||
|
|
||||||
public static class RequestExtensions
|
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
|
#region Mobile Checking
|
||||||
|
|
||||||
// yoinked and adapted from https://stackoverflow.com/a/68641796
|
// yoinked and adapted from https://stackoverflow.com/a/68641796
|
||||||
|
@ -32,10 +46,7 @@ public static class RequestExtensions
|
||||||
|
|
||||||
#region Captcha
|
#region Captcha
|
||||||
|
|
||||||
private static readonly HttpClient client = new()
|
private static readonly HttpClient client;
|
||||||
{
|
|
||||||
BaseAddress = new Uri("https://hcaptcha.com"),
|
|
||||||
};
|
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeNotEvident")]
|
[SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeNotEvident")]
|
||||||
private static async Task<bool> verifyCaptcha(string token)
|
private static async Task<bool> verifyCaptcha(string token)
|
||||||
|
@ -48,7 +59,7 @@ public static class RequestExtensions
|
||||||
new("response", token),
|
new("response", token),
|
||||||
};
|
};
|
||||||
|
|
||||||
HttpResponseMessage response = await client.PostAsync("/siteverify", new FormUrlEncodedContent(payload));
|
HttpResponseMessage response = await client.PostAsync("siteverify", new FormUrlEncodedContent(payload));
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
@ -63,7 +74,14 @@ public static class RequestExtensions
|
||||||
{
|
{
|
||||||
if (ServerConfiguration.Instance.Captcha.CaptchaEnabled)
|
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 (!gotCaptcha) return false;
|
||||||
|
|
||||||
if (!await verifyCaptcha(values[0])) return false;
|
if (!await verifyCaptcha(values[0])) return false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue