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