Registration gates: proof-of-work CAPTCHA and training-notice consent receipt (#54)
This commit is contained in:
parent
fc8242f655
commit
33be966b7d
28 changed files with 1253 additions and 56 deletions
|
|
@ -403,7 +403,7 @@ public sealed class OidcLogoutFactory : OidcWebApplicationFactory
|
|||
{
|
||||
using var client = CreateClient(HostClientOptions());
|
||||
var identity = $"member-{Guid.NewGuid():N}";
|
||||
using (var register = await client.PostAsJsonAsync("/auth/bifrost/register", new { identity, displayName = identity }))
|
||||
using (var register = await RegistrationKit.RegisterAsync(client, identity))
|
||||
{
|
||||
Assert.Equal(HttpStatusCode.OK, register.StatusCode);
|
||||
}
|
||||
|
|
|
|||
63
tests/Bifrost.Web.Tests/Support/RegistrationKit.cs
Normal file
63
tests/Bifrost.Web.Tests/Support/RegistrationKit.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System.Net.Http.Json;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Bifrost.Web.Features.Registration;
|
||||
|
||||
namespace Bifrost.Web.Tests.Support;
|
||||
|
||||
/// <summary>Registers through the real gate: fetch a challenge, solve it, post with the notice version.</summary>
|
||||
public static class RegistrationKit
|
||||
{
|
||||
public static async Task<(string Challenge, string Nonce)> SolveAsync(HttpClient client)
|
||||
{
|
||||
var issued = await client.GetFromJsonAsync<JsonElement>("/auth/bifrost/challenge");
|
||||
var challenge = issued.GetProperty("challenge").GetString()!;
|
||||
return (challenge, Solve(challenge, issued.GetProperty("difficulty").GetInt32()));
|
||||
}
|
||||
|
||||
public static string Solve(string challenge, int difficulty)
|
||||
{
|
||||
for (long nonce = 0; ; nonce++)
|
||||
{
|
||||
var text = nonce.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (RegistrationChallengeService.LeadingZeroBits(SHA256.HashData(Encoding.UTF8.GetBytes(challenge + ":" + text))) >= difficulty)
|
||||
{
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<HttpResponseMessage> RegisterAsync(
|
||||
HttpClient client,
|
||||
string identity,
|
||||
string? displayName = null,
|
||||
string? returnUrl = null,
|
||||
bool anonymous = false)
|
||||
{
|
||||
var (challenge, nonce) = await SolveAsync(client);
|
||||
return await PostAsync(client, new
|
||||
{
|
||||
identity,
|
||||
displayName = displayName ?? identity,
|
||||
returnUrl,
|
||||
challenge,
|
||||
nonce,
|
||||
acceptedNoticeVersion = TrainingNotice.Version
|
||||
}, anonymous);
|
||||
}
|
||||
|
||||
public static async Task<HttpResponseMessage> PostAsync(HttpClient client, object body, bool anonymous = false)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "/auth/bifrost/register")
|
||||
{
|
||||
Content = JsonContent.Create(body)
|
||||
};
|
||||
if (anonymous)
|
||||
{
|
||||
request.Headers.Add("X-Test-Anonymous", "true");
|
||||
}
|
||||
|
||||
return await client.SendAsync(request);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,16 @@ public sealed class TestAuthHandler(
|
|||
return Task.FromResult(AuthenticateResult.NoResult());
|
||||
}
|
||||
|
||||
// A test may sign in as a native account by naming its Bifrost identity; only that claim is sent, so no GitHub claim also matches.
|
||||
if (Request.Headers.TryGetValue("X-Test-Bifrost-Identity", out var nativeIdentity))
|
||||
{
|
||||
var native = new ClaimsIdentity(
|
||||
[new Claim(BifrostClaimTypes.BifrostIdentity, nativeIdentity.ToString())],
|
||||
SchemeName);
|
||||
return Task.FromResult(AuthenticateResult.Success(
|
||||
new AuthenticationTicket(new ClaimsPrincipal(native), SchemeName)));
|
||||
}
|
||||
|
||||
// A test may sign in as another account by naming its GitHub id and login.
|
||||
var gitHubId = Request.Headers["X-Test-GitHub-Id"].FirstOrDefault() ?? "12345";
|
||||
var login = Request.Headers["X-Test-GitHub-Login"].FirstOrDefault() ?? "test-admin";
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@ public class TestWebApplicationFactory : WebApplicationFactory<Program>
|
|||
["ConceptVotes:CastPermits"] = "100000",
|
||||
["ConceptVotes:MintPermits"] = "100000",
|
||||
["MindIntake:Permits"] = "100000",
|
||||
["Bifrost:Registration:Captcha:Difficulty"] = "4",
|
||||
["Bifrost:Registration:Limits:PerAddressPermits"] = "100000",
|
||||
["Host:PublicBaseUrl"] = "https://localhost",
|
||||
["Host:ExpectedHost"] = "localhost",
|
||||
["Host:RequireStrictHostValidation"] = "false",
|
||||
|
|
|
|||
Loading…
Reference in a new issue