3 namespace BookStack\Auth\Access\Mfa;
5 use BookStack\Auth\User;
7 use Illuminate\Database\Eloquent\Model;
11 * @property int $user_id
12 * @property string $method
13 * @property string $value
14 * @property Carbon $created_at
15 * @property Carbon $updated_at
17 class MfaValue extends Model
19 protected static $unguarded = true;
21 const METHOD_TOTP = 'totp';
22 const METHOD_BACKUP_CODES = 'backup_codes';
25 * Get all the MFA methods available.
27 public static function allMethods(): array
29 return [self::METHOD_TOTP, self::METHOD_BACKUP_CODES];
33 * Upsert a new MFA value for the given user and method
34 * using the provided value.
36 public static function upsertWithValue(User $user, string $method, string $value): void
38 /** @var MfaValue $mfaVal */
39 $mfaVal = static::query()->firstOrNew([
40 'user_id' => $user->id,
43 $mfaVal->setValue($value);
48 * Easily get the decrypted MFA value for the given user and method.
50 public static function getValueForUser(User $user, string $method): ?string
52 /** @var MfaValue $mfaVal */
53 $mfaVal = static::query()
54 ->where('user_id', '=', $user->id)
55 ->where('method', '=', $method)
58 return $mfaVal ? $mfaVal->getValue() : null;
62 * Decrypt the value attribute upon access.
64 protected function getValue(): string
66 return decrypt($this->value);
70 * Encrypt the value attribute upon access.
72 protected function setValue($value): void
74 $this->value = encrypt($value);