mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
refactor: remove unnecessary interfaces for services; change user key from UID to UUID
40 lines
1.6 KiB
C#
40 lines
1.6 KiB
C#
using Microsoft.Extensions.Options;
|
|
using SharpbinV3.Server.DTOs;
|
|
using SharpbinV3.Server.Settings;
|
|
using System.Text.Json;
|
|
|
|
namespace SharpbinV3.Server.Services.Verification.Providers
|
|
{
|
|
public sealed class TurnstileVerificationProvider(IOptions<AuthSettings> options, HttpClient httpClient) : IVerificationProvider
|
|
{
|
|
private readonly AuthSettings _authSettings = options.Value;
|
|
private const string SiteverifyUrl = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
|
|
|
public int Priority => 1;
|
|
public bool IsConfigured => !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SecretKey) && !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SiteKey);
|
|
|
|
public async Task<bool> VerifyAsync(string token, string? ip)
|
|
{
|
|
if (_authSettings.CF_Turnstile_SecretKey is null)
|
|
throw new InvalidOperationException("Turnstile secret key is not configured.");
|
|
|
|
var parameters = new Dictionary<string, string>
|
|
{
|
|
{ "secret", _authSettings.CF_Turnstile_SecretKey },
|
|
{ "response", token }
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(ip))
|
|
parameters.Add("remoteip", ip);
|
|
|
|
var postContent = new FormUrlEncodedContent(parameters);
|
|
|
|
var response = await httpClient.PostAsync(SiteverifyUrl, postContent);
|
|
response.EnsureSuccessStatusCode();
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<TurnstileResponse>(json);
|
|
return result?.Success ?? false;
|
|
}
|
|
}
|
|
}
|