]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Mfa/TotpValidationRule.php
Merge pull request #2827 from BookStackApp/mfa
[bookstack] / app / Auth / Access / Mfa / TotpValidationRule.php
1 <?php
2
3 namespace BookStack\Auth\Access\Mfa;
4
5 use Illuminate\Contracts\Validation\Rule;
6
7 class TotpValidationRule implements Rule
8 {
9
10     protected $secret;
11     protected $totpService;
12
13     /**
14      * Create a new rule instance.
15      * Takes the TOTP secret that must be system provided, not user provided.
16      */
17     public function __construct(string $secret)
18     {
19         $this->secret = $secret;
20         $this->totpService = app()->make(TotpService::class);
21     }
22
23     /**
24      * Determine if the validation rule passes.
25      */
26     public function passes($attribute, $value)
27     {
28         return $this->totpService->verifyCode($value, $this->secret);
29     }
30
31     /**
32      * Get the validation error message.
33      */
34     public function message()
35     {
36         return trans('validation.totp');
37     }
38 }