]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Mfa/MfaValue.php
9f9ab29a5012d0fa2eb5b4d2a1c8cf84f673ac9b
[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      * Get all the MFA methods available.
26      */
27     public static function allMethods(): array
28     {
29         return [self::METHOD_TOTP, self::METHOD_BACKUP_CODES];
30     }
31
32     /**
33      * Upsert a new MFA value for the given user and method
34      * using the provided value.
35      */
36     public static function upsertWithValue(User $user, string $method, string $value): void
37     {
38         /** @var MfaValue $mfaVal */
39         $mfaVal = static::query()->firstOrNew([
40             'user_id' => $user->id,
41             'method' => $method
42         ]);
43         $mfaVal->setValue($value);
44         $mfaVal->save();
45     }
46
47     /**
48      * Decrypt the value attribute upon access.
49      */
50     public function getValue(): string
51     {
52         return decrypt($this->value);
53     }
54
55     /**
56      * Encrypt the value attribute upon access.
57      */
58     public function setValue($value): void
59     {
60         $this->value = encrypt($value);
61     }
62 }