|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodingLibs\MFA; |
| 4 | + |
| 5 | +use CodingLibs\MFA\Channels\EmailChannel; |
| 6 | +use CodingLibs\MFA\Channels\SmsChannel; |
| 7 | +use CodingLibs\MFA\Models\MfaChallenge; |
| 8 | +use CodingLibs\MFA\Models\MfaMethod; |
| 9 | +use CodingLibs\MFA\Totp\GoogleTotp; |
| 10 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 11 | +use Illuminate\Support\Arr; |
| 12 | +use Illuminate\Support\Carbon; |
| 13 | +use Illuminate\Support\Facades\DB; |
| 14 | + |
| 15 | +class MFA |
| 16 | +{ |
| 17 | + protected array $config; |
| 18 | + |
| 19 | + public function __construct(array $config) |
| 20 | + { |
| 21 | + $this->config = $config; |
| 22 | + } |
| 23 | + |
| 24 | + public function setupTotp(Authenticatable $user, ?string $issuer = null, ?string $label = null): array |
| 25 | + { |
| 26 | + $secret = GoogleTotp::generateSecret(); |
| 27 | + $issuer = $issuer ?: Arr::get($this->config, 'totp.issuer', 'Laravel'); |
| 28 | + $label = $label ?: method_exists($user, 'getEmailForVerification') ? $user->getEmailForVerification() : ($user->email ?? (string) $user->getAuthIdentifier()); |
| 29 | + $otpauth = GoogleTotp::buildOtpAuthUrl($secret, $label, $issuer, Arr::get($this->config, 'totp.digits', 6)); |
| 30 | + |
| 31 | + $this->enableMethod($user, 'totp', ['secret' => $secret]); |
| 32 | + |
| 33 | + return [ |
| 34 | + 'secret' => $secret, |
| 35 | + 'otpauth_url' => $otpauth, |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + public function verifyTotp(Authenticatable $user, string $code): bool |
| 40 | + { |
| 41 | + $method = $this->getMethod($user, 'totp'); |
| 42 | + if (! $method || ! $method->secret) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + $digits = Arr::get($this->config, 'totp.digits', 6); |
| 47 | + $period = Arr::get($this->config, 'totp.period', 30); |
| 48 | + $window = Arr::get($this->config, 'totp.window', 1); |
| 49 | + |
| 50 | + $verified = GoogleTotp::verifyCode($method->secret, $code, $digits, $period, $window); |
| 51 | + if ($verified) { |
| 52 | + $method->last_used_at = Carbon::now(); |
| 53 | + $method->save(); |
| 54 | + } |
| 55 | + return $verified; |
| 56 | + } |
| 57 | + |
| 58 | + public function issueChallenge(Authenticatable $user, string $method): ?MfaChallenge |
| 59 | + { |
| 60 | + $method = strtolower($method); |
| 61 | + if (! in_array($method, ['email', 'sms'], true)) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + $codeLength = Arr::get($this->config, 'code_length', 6); |
| 66 | + $ttlSeconds = Arr::get($this->config, 'code_ttl_seconds', 300); |
| 67 | + $code = str_pad((string) random_int(0, (10 ** $codeLength) - 1), $codeLength, '0', STR_PAD_LEFT); |
| 68 | + |
| 69 | + $challenge = new MfaChallenge(); |
| 70 | + $challenge->user_type = get_class($user); |
| 71 | + $challenge->user_id = $user->getAuthIdentifier(); |
| 72 | + $challenge->method = $method; |
| 73 | + $challenge->code = $code; |
| 74 | + $challenge->expires_at = Carbon::now()->addSeconds($ttlSeconds); |
| 75 | + $challenge->save(); |
| 76 | + |
| 77 | + if ($method === 'email') { |
| 78 | + (new EmailChannel($this->config['email'] ?? []))->send($user, $code); |
| 79 | + } else if ($method === 'sms') { |
| 80 | + (new SmsChannel($this->config['sms'] ?? []))->send($user, $code); |
| 81 | + } |
| 82 | + |
| 83 | + return $challenge; |
| 84 | + } |
| 85 | + |
| 86 | + public function verifyChallenge(Authenticatable $user, string $method, string $code): bool |
| 87 | + { |
| 88 | + $now = Carbon::now(); |
| 89 | + |
| 90 | + $challenge = MfaChallenge::query() |
| 91 | + ->where('user_type', get_class($user)) |
| 92 | + ->where('user_id', $user->getAuthIdentifier()) |
| 93 | + ->where('method', strtolower($method)) |
| 94 | + ->whereNull('consumed_at') |
| 95 | + ->where('expires_at', '>', $now) |
| 96 | + ->latest('id') |
| 97 | + ->first(); |
| 98 | + |
| 99 | + if (! $challenge) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (hash_equals($challenge->code, $code)) { |
| 104 | + $challenge->consumed_at = $now; |
| 105 | + $challenge->save(); |
| 106 | + |
| 107 | + $this->enableMethod($user, $challenge->method); |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + public function enableMethod(Authenticatable $user, string $method, array $attributes = []): MfaMethod |
| 116 | + { |
| 117 | + $record = $this->getMethod($user, $method); |
| 118 | + if (! $record) { |
| 119 | + $record = new MfaMethod(); |
| 120 | + $record->user_type = get_class($user); |
| 121 | + $record->user_id = $user->getAuthIdentifier(); |
| 122 | + $record->method = strtolower($method); |
| 123 | + } |
| 124 | + |
| 125 | + foreach ($attributes as $key => $value) { |
| 126 | + $record->setAttribute($key, $value); |
| 127 | + } |
| 128 | + |
| 129 | + $record->enabled_at = Carbon::now(); |
| 130 | + $record->save(); |
| 131 | + |
| 132 | + return $record; |
| 133 | + } |
| 134 | + |
| 135 | + public function disableMethod(Authenticatable $user, string $method): bool |
| 136 | + { |
| 137 | + $record = $this->getMethod($user, $method); |
| 138 | + if (! $record) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + $record->enabled_at = null; |
| 142 | + return $record->save(); |
| 143 | + } |
| 144 | + |
| 145 | + public function isEnabled(Authenticatable $user, string $method): bool |
| 146 | + { |
| 147 | + $record = $this->getMethod($user, $method); |
| 148 | + return (bool) ($record && $record->enabled_at); |
| 149 | + } |
| 150 | + |
| 151 | + public function getMethod(Authenticatable $user, string $method): ?MfaMethod |
| 152 | + { |
| 153 | + return MfaMethod::query() |
| 154 | + ->where('user_type', get_class($user)) |
| 155 | + ->where('user_id', $user->getAuthIdentifier()) |
| 156 | + ->where('method', strtolower($method)) |
| 157 | + ->first(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments