mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-07 12:28:39 +00:00
Add captcha to register form
This commit is contained in:
parent
df257b38c4
commit
2c7922ccc1
4 changed files with 35 additions and 14 deletions
|
@ -1,5 +1,9 @@
|
||||||
|
#nullable enable
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Helpers.Extensions;
|
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);
|
("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 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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -3,13 +3,12 @@ using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Kettu;
|
using Kettu;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
|
||||||
using LBPUnion.ProjectLighthouse.Logging;
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Pages;
|
namespace LBPUnion.ProjectLighthouse.Pages;
|
||||||
|
|
||||||
|
@ -35,17 +34,10 @@ public class LoginForm : BaseLayout
|
||||||
return this.Page();
|
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])))
|
this.Error = "You must complete the captcha correctly.";
|
||||||
bool gotCaptcha = this.Request.Form.TryGetValue("h-captcha-response", out StringValues values);
|
return this.Page();
|
||||||
string? token = gotCaptcha ? values[0] : null;
|
|
||||||
|
|
||||||
if (token == null || !await CaptchaHelper.Verify(token))
|
|
||||||
{
|
|
||||||
this.Error = "You must solve the captcha correctly.";
|
|
||||||
return this.Page();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User? user = await this.Database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
User? user = await this.Database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
@page "/register"
|
@page "/register"
|
||||||
|
@using LBPUnion.ProjectLighthouse.Types.Settings
|
||||||
@model LBPUnion.ProjectLighthouse.Pages.RegisterForm
|
@model LBPUnion.ProjectLighthouse.Pages.RegisterForm
|
||||||
|
|
||||||
@{
|
@{
|
||||||
|
@ -60,5 +61,10 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@if (ServerSettings.Instance.HCaptchaEnabled)
|
||||||
|
{
|
||||||
|
@await Html.PartialAsync("Partials/CaptchaPartial")
|
||||||
|
}
|
||||||
|
|
||||||
<input type="submit" value="Register" id="submit" class="ui green button">
|
<input type="submit" value="Register" id="submit" class="ui green button">
|
||||||
</form>
|
</form>
|
|
@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
|
||||||
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||||
|
@ -42,13 +43,18 @@ public class RegisterForm : BaseLayout
|
||||||
return this.Page();
|
return this.Page();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool userExists = await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null;
|
if (await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null)
|
||||||
if (userExists)
|
|
||||||
{
|
{
|
||||||
this.Error = "The username you've chosen is already taken.";
|
this.Error = "The username you've chosen is already taken.";
|
||||||
return this.Page();
|
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));
|
User user = await this.Database.CreateUser(username, HashHelper.BCryptHash(password));
|
||||||
|
|
||||||
WebToken webToken = new()
|
WebToken webToken = new()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue