Add captcha to register form

This commit is contained in:
jvyden 2022-02-05 01:00:35 -05:00
commit 2c7922ccc1
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 35 additions and 14 deletions

View file

@ -1,5 +1,9 @@
#nullable enable
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace LBPUnion.ProjectLighthouse.Helpers.Extensions;
@ -11,4 +15,17 @@ public static class RequestExtensions
("Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
public static bool IsMobile(this HttpRequest request) => mobileCheck.IsMatch(request.Headers[HeaderNames.UserAgent].ToString());
public static async Task<bool> CheckCaptchaValidity(this HttpRequest request)
{
if (ServerSettings.Instance.HCaptchaEnabled)
{
bool gotCaptcha = request.Form.TryGetValue("h-captcha-response", out StringValues values);
if (!gotCaptcha) return false;
if (!await CaptchaHelper.Verify(values[0])) return false;
}
return true;
}
}

View file

@ -3,13 +3,12 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Primitives;
namespace LBPUnion.ProjectLighthouse.Pages;
@ -35,17 +34,10 @@ public class LoginForm : BaseLayout
return this.Page();
}
if (ServerSettings.Instance.HCaptchaEnabled)
if (!await Request.CheckCaptchaValidity())
{
// && (!this.Request.Form.TryGetValue("h-captcha-response", out StringValues values) || !await CaptchaHelper.Verify(values[0])))
bool gotCaptcha = this.Request.Form.TryGetValue("h-captcha-response", out StringValues values);
string? token = gotCaptcha ? values[0] : null;
if (token == null || !await CaptchaHelper.Verify(token))
{
this.Error = "You must solve the captcha correctly.";
return this.Page();
}
this.Error = "You must complete the captcha correctly.";
return this.Page();
}
User? user = await this.Database.Users.FirstOrDefaultAsync(u => u.Username == username);

View file

@ -1,4 +1,5 @@
@page "/register"
@using LBPUnion.ProjectLighthouse.Types.Settings
@model LBPUnion.ProjectLighthouse.Pages.RegisterForm
@{
@ -60,5 +61,10 @@
</div>
</div>
@if (ServerSettings.Instance.HCaptchaEnabled)
{
@await Html.PartialAsync("Partials/CaptchaPartial")
}
<input type="submit" value="Register" id="submit" class="ui green button">
</form>

View file

@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using JetBrains.Annotations;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Settings;
@ -42,13 +43,18 @@ public class RegisterForm : BaseLayout
return this.Page();
}
bool userExists = await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null;
if (userExists)
if (await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null)
{
this.Error = "The username you've chosen is already taken.";
return this.Page();
}
if (!await Request.CheckCaptchaValidity())
{
this.Error = "You must complete the captcha correctly.";
return this.Page();
}
User user = await this.Database.CreateUser(username, HashHelper.BCryptHash(password));
WebToken webToken = new()