2021-06-30 17:10:02 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Access\Mfa;
|
2021-06-30 17:10:02 -04:00
|
|
|
|
2024-03-17 12:52:19 -04:00
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2021-06-30 17:10:02 -04:00
|
|
|
|
2024-03-17 12:52:19 -04:00
|
|
|
class TotpValidationRule implements ValidationRule
|
2021-06-30 17:10:02 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new rule instance.
|
|
|
|
* Takes the TOTP secret that must be system provided, not user provided.
|
|
|
|
*/
|
2024-03-17 12:52:19 -04:00
|
|
|
public function __construct(
|
|
|
|
protected string $secret,
|
|
|
|
protected TotpService $totpService,
|
|
|
|
) {
|
2021-06-30 17:10:02 -04:00
|
|
|
}
|
|
|
|
|
2024-03-17 12:52:19 -04:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
2021-06-30 17:10:02 -04:00
|
|
|
{
|
2024-03-17 12:52:19 -04:00
|
|
|
$passes = $this->totpService->verifyCode($value, $this->secret);
|
|
|
|
if (!$passes) {
|
|
|
|
$fail(trans('validation.totp'));
|
|
|
|
}
|
2021-06-30 17:10:02 -04:00
|
|
|
}
|
|
|
|
}
|