-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add for Turnstile: environment variables, Context, Service
- Loading branch information
1 parent
d805fb4
commit 1db2d1a
Showing
19 changed files
with
141 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading.Tasks; | ||
using BackendFramework.Interfaces; | ||
|
||
namespace Backend.Tests.Mocks | ||
{ | ||
sealed internal class TurnstileServiceMock : ITurnstileService | ||
{ | ||
public Task<bool> VerifyToken(string token) | ||
{ | ||
return Task.FromResult(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using BackendFramework.Interfaces; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BackendFramework.Contexts | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class TurnstileContext : ITurnstileContext | ||
{ | ||
public string? TurnstileSecretKey { get; } | ||
public string? TurnstileVerifyUrl { get; } | ||
|
||
public TurnstileContext(IOptions<Startup.Settings> options) | ||
{ | ||
TurnstileSecretKey = options.Value.TurnstileSecretKey; | ||
TurnstileVerifyUrl = options.Value.TurnstileVerifyUrl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BackendFramework.Interfaces | ||
{ | ||
public interface ITurnstileContext | ||
{ | ||
string? TurnstileSecretKey { get; } | ||
string? TurnstileVerifyUrl { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace BackendFramework.Interfaces | ||
{ | ||
public interface ITurnstileService | ||
{ | ||
Task<bool> VerifyToken(string token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using BackendFramework.Interfaces; | ||
|
||
namespace BackendFramework.Services | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class TurnstileService : ITurnstileService | ||
{ | ||
private readonly ITurnstileContext _turnstileContext; | ||
|
||
public TurnstileService(ITurnstileContext turnstileContext) | ||
{ | ||
_turnstileContext = turnstileContext; | ||
} | ||
|
||
public async Task<bool> VerifyToken(string token) | ||
{ | ||
var secret = _turnstileContext.TurnstileSecretKey; | ||
var verifyUrl = _turnstileContext.TurnstileVerifyUrl; | ||
if (string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(verifyUrl)) | ||
{ | ||
return false; | ||
} | ||
var httpContent = new FormUrlEncodedContent(new Dictionary<string, string>{ | ||
{"response", token}, | ||
{"secret", secret}, | ||
}); | ||
using var result = await new HttpClient().PostAsync(verifyUrl, httpContent); | ||
var contentString = await result.Content.ReadAsStringAsync(); | ||
Console.WriteLine($"content: {contentString}"); | ||
return contentString.Contains("\"success\":true"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters