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 * Decrypt the value attribute upon access.
50 public function getValue(): string
52 return decrypt($this->value);
56 * Encrypt the value attribute upon access.
58 public function setValue($value): void
60 $this->value = encrypt($value);