2FAuth/app/Rules/IsBase32Encoded.php

24 lines
564 B
PHP
Raw Normal View History

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