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