47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Support;
|
|
|
|
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
|
|
use AltchaOrg\Altcha\Altcha;
|
|
use AltchaOrg\Altcha\CreateChallengeOptions;
|
|
use AltchaOrg\Altcha\SolveChallengeOptions;
|
|
|
|
/**
|
|
* Builds a valid altcha payload for tests using the fixed ALTCHA_HMAC_KEY,
|
|
* mirroring the payload shape produced by the browser widget (base64 JSON with
|
|
* a challenge array and a solution array).
|
|
*/
|
|
final class AltchaPayload
|
|
{
|
|
public static function valid(?int $counter = null): string
|
|
{
|
|
$pbkdf2 = new Pbkdf2;
|
|
$altcha = new Altcha(
|
|
hmacSignatureSecret: config('captcha.hmac_key'),
|
|
);
|
|
|
|
$counter ??= random_int(1, 100);
|
|
|
|
$challenge = $altcha->createChallenge(new CreateChallengeOptions(
|
|
algorithm: $pbkdf2,
|
|
cost: 5000,
|
|
counter: $counter,
|
|
expiresAt: time() + 600,
|
|
));
|
|
|
|
$solution = $altcha->solveChallenge(new SolveChallengeOptions(
|
|
challenge: $challenge,
|
|
algorithm: $pbkdf2,
|
|
start: $counter,
|
|
step: 1,
|
|
timeout: 5,
|
|
));
|
|
|
|
return base64_encode(json_encode([
|
|
'challenge' => $challenge->toArray(),
|
|
'solution' => $solution->toArray(),
|
|
]));
|
|
}
|
|
}
|