Skip to content

Commit c46cfe6

Browse files
1.x
1 parent cfeb68b commit c46cfe6

16 files changed

Lines changed: 350 additions & 144 deletions

README.md

Lines changed: 135 additions & 68 deletions
Large diffs are not rendered by default.

src/Enums/SecretKey.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Laragear\Turnstile\Enums;
4+
5+
enum SecretKey: string
6+
{
7+
case Passing = '1x0000000000000000000000000000000AA';
8+
case Fails = '2x0000000000000000000000000000000AA';
9+
case Spent = '3x0000000000000000000000000000000AA';
10+
}

src/Enums/SiteKey.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Laragear\Turnstile\Enums;
4+
5+
enum SiteKey: string
6+
{
7+
case VisiblePassing = '1x00000000000000000000AA';
8+
case VisibleBlocks = '2x00000000000000000000AB';
9+
case InvisiblePassing = '1x00000000000000000000BB';
10+
case InvisibleBlocks = '2x00000000000000000000BB';
11+
case ForceInteraction = '3x00000000000000000000FF';
12+
}

src/Facades/Turnstile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @method static \Laragear\Turnstile\Challenge getChallenge(string $token, string $ip = '', string $idempotencyKey = '', array $options = [], bool $save = true)
99
* @method static \Laragear\Turnstile\Challenge getChallengeFromRequest(\Illuminate\Http\Request|null $request = null, string $key = '', string $idempotencyKey = '', array $options = [], bool $store = true)
1010
* @method static \Laragear\Turnstile\Challenge challenge()
11+
* @method static \Laragear\Turnstile\Turnstile useTestingSiteKey(\Laragear\Turnstile\Enums\SiteKey $key)
12+
* @method static \Laragear\Turnstile\Turnstile useTestingSecretKey(\Laragear\Turnstile\Enums\SecretKey $key)
1113
* @method static string key()
1214
* @method static string rule()
1315
* @method static array rules()

src/Http/Middleware/TurnstileMiddleware.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
use Illuminate\Support\Str;
1010
use Illuminate\Validation\ValidationException;
1111
use Laragear\Turnstile\Turnstile;
12-
use function array_map;
13-
use function array_pad;
1412
use function explode;
15-
use function implode;
16-
use function in_array;
1713
use function is_string;
18-
use function trim;
1914

2015
/**
2116
* @method static \Laragear\Turnstile\Http\Middleware\TurnstileMiddlewareDefinition auth($guards = null)

src/Http/Middleware/TurnstileMiddlewareDefinition.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Support\Arr;
66
use Stringable;
77
use function implode;
8-
use function in_array;
98

109
class TurnstileMiddlewareDefinition implements Stringable
1110
{

src/Turnstile.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Illuminate\Http\Client\ResponseSequence;
1313
use Illuminate\Http\Request;
1414
use Illuminate\Support\DateFactory;
15+
use Laragear\Turnstile\Enums\SecretKey;
16+
use Laragear\Turnstile\Enums\SiteKey;
1517
use Laragear\Turnstile\Exceptions\InvalidChallengeException;
1618
use function array_filter;
1719
use function array_merge;
@@ -37,20 +39,6 @@ class Turnstile
3739
*/
3840
public const KEY = 'cf-turnstile-response';
3941

40-
/**
41-
* Testing site key that always resolves to true
42-
*
43-
* @const string
44-
*/
45-
public const SITE_KEY = '1x00000000000000000000AA';
46-
47-
/**
48-
* Testing secret key that always returns successful challenges.
49-
*
50-
* @const string
51-
*/
52-
public const SECRET_KEY = '1x0000000000000000000000000000000AA';
53-
5442
/**
5543
* The Cloudflare Turnstile Site Verify endpoint.
5644
*
@@ -73,12 +61,37 @@ public function __construct(
7361
protected readonly Factory $http,
7462
protected readonly Repository $config,
7563
protected readonly DateFactory $date,
64+
protected Enums\SiteKey $testingSiteKey = SiteKey::VisiblePassing,
65+
protected Enums\SecretKey $testingSecretKey = SecretKey::Passing,
7666
protected array $fakedResponse = [],
7767
protected bool $shouldFake = false
7868
) {
7969
// ...
8070
}
8171

72+
/**
73+
* Use the given Testing Site Key for rendering challenges on the frontend.
74+
*
75+
* @return $this
76+
*/
77+
public function useTestingSiteKey(SiteKey $key): static
78+
{
79+
$this->testingSiteKey = $key;
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* USe the given Testing Secret Key for retrieving challenges from the backend.
86+
* @return $this
87+
*/
88+
public function useTestingSecretKey(SecretKey $key): static
89+
{
90+
$this->testingSecretKey = $key;
91+
92+
return $this;
93+
}
94+
8295
/**
8396
* Returns the default key to use to check Cloudflare Turnstile responses.
8497
*/
@@ -162,7 +175,6 @@ public function getChallenge(
162175
/**
163176
* Retrieves the Challenge response from Turnstile servers.
164177
*
165-
* @throws \Illuminate\Contracts\Container\BindingResolutionException
166178
* @throws \Illuminate\Http\Client\ConnectionException
167179
* @throws \Illuminate\Http\Client\RequestException
168180
*/
@@ -176,7 +188,7 @@ protected function getResponse(string $token, string $ip, string $idempotencyKey
176188
->asJson()
177189
->acceptJson()
178190
->withOptions(array_merge($this->config->get('turnstile.client'), $options))
179-
->post(static::ENDPOINT, array_filter([ // @phpstan-ignore-line
191+
->post(static::ENDPOINT, array_filter([
180192
'secret' => $this->getSecretKey(),
181193
'response' => $token,
182194
'remoteip' => $ip,
@@ -256,7 +268,7 @@ public function getSiteKey(): string
256268
$key = $this->config->get('turnstile.site_key');
257269

258270
if ($this->currentEnvironment() !== 'production') {
259-
$key ??= static::SITE_KEY;
271+
$key ??= $this->testingSiteKey->value;
260272
}
261273

262274
return $key;
@@ -270,7 +282,7 @@ protected function getSecretKey(): string
270282
$key = $this->config->get('turnstile.secret_key');
271283

272284
if ($this->currentEnvironment() !== 'production') {
273-
$key ??= static::SECRET_KEY;
285+
$key ??= $this->testingSecretKey->value;
274286
}
275287

276288
return $key;
@@ -309,7 +321,7 @@ public function missingChallenge(): bool
309321
*/
310322
public function flushChallenge(): void
311323
{
312-
$this->container->instance(Challenge::class, null);
324+
unset($this->container[Challenge::class]); // @phpstan-ignore-line
313325
}
314326

315327
/**

src/TurnstileServiceProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Illuminate\Contracts\Foundation\Application;
66
use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
7-
use Illuminate\Http\Client\Factory;
8-
use Illuminate\Http\Request;
97
use Illuminate\Routing\Router;
108
use Illuminate\Support\ServiceProvider;
119
use Illuminate\View\Compilers\BladeCompiler;
@@ -31,7 +29,7 @@ public function register(): void
3129

3230
// Remove the challenge when the application lifecycle ends.
3331
$this->app->terminating(static function (Application $app) {
34-
$app->instance(Challenge::class, null);
32+
unset($app[Challenge::class]);
3533
});
3634
}
3735

@@ -45,7 +43,6 @@ public function boot(Router $router): void
4543
);
4644

4745
if ($this->app->runningInConsole()) {
48-
// @phpstan-ignore-next-line
4946
$this->publishes([static::CONFIG => $this->app->configPath('turnstile.php')], 'config');
5047
$this->publishes([static::LANG => $this->app->langPath('vendor/turnstile')], 'lang');
5148
}

src/Validation/TurnstileRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Laragear\Turnstile\Turnstile;
1010
use Stringable;
1111
use function array_pad;
12-
use function blank;
1312
use function in_array;
1413
use function is_string;
1514

src/Views/Components/Script.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Script extends Component
2222
public function __construct(
2323
protected Turnstile $turnstile,
2424
public bool $explicit = false,
25-
public string $onload = '',
25+
public ?string $onload = null,
2626
public bool $async = true,
2727
public bool $defer = true,
2828
)

0 commit comments

Comments
 (0)