2FAuth/app/Rules/IsBase32Encoded.php
2023-08-01 15:10:58 +02:00

24 lines
563 B
PHP

<?php
namespace App\Rules;
use App\Helpers\Helpers;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use ParagonIE\ConstantTime\Base32;
class IsBase32Encoded implements ValidationRule
{
/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
$secret = Base32::decodeUpper(Helpers::PadToBase32Format($value));
} catch (\Exception $e) {
$fail('validation.custom.secret.isBase32Encoded')->translate();
}
}
}