1212use Illuminate \Http \Client \ResponseSequence ;
1313use Illuminate \Http \Request ;
1414use Illuminate \Support \DateFactory ;
15+ use Laragear \Turnstile \Enums \SecretKey ;
16+ use Laragear \Turnstile \Enums \SiteKey ;
1517use Laragear \Turnstile \Exceptions \InvalidChallengeException ;
1618use function array_filter ;
1719use 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 /**
0 commit comments