]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Mfa/MfaValue.php
cba90dcac2b14dcab9769bfbdbbfbd5077f129eb
[bookstack] / app / Auth / Access / Mfa / MfaValue.php
1 <?php
2
3 namespace BookStack\Auth\Access\Mfa;
4
5 use BookStack\Auth\User;
6 use Carbon\Carbon;
7 use Illuminate\Database\Eloquent\Model;
8
9 /**
10  * @property int $id
11  * @property int $user_id
12  * @property string $method
13  * @property string $value
14  * @property Carbon $created_at
15  * @property Carbon $updated_at
16  */
17 class MfaValue extends Model
18 {
19     protected static $unguarded = true;
20
21     const METHOD_TOTP = 'totp';
22     const METHOD_BACKUP_CODES = 'backup_codes';
23
24     /**
25      * Upsert a new MFA value for the given user and method
26      * using the provided value.
27      */
28     public static function upsertWithValue(User $user, string $method, string $value): void
29     {
30         /** @var MfaValue $mfaVal */
31         $mfaVal = static::query()->firstOrNew([
32             'user_id' => $user->id,
33             'method' => $method
34         ]);
35         $mfaVal->setValue($value);
36         $mfaVal->save();
37     }
38
39     /**
40      * Decrypt the value attribute upon access.
41      */
42     public function getValue(): string
43     {
44         return decrypt($this->value);
45     }
46
47     /**
48      * Encrypt the value attribute upon access.
49      */
50     public function setValue($value): void
51     {
52         $this->value = encrypt($value);
53     }
54 }