|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Pterodactyl\Http\Middleware; |
| 26 | + |
| 27 | +use Closure; |
| 28 | +use Krucas\Settings\Settings; |
| 29 | +use Prologue\Alerts\AlertsMessageBag; |
| 30 | + |
| 31 | +class RequireTwoFactorAuthentication |
| 32 | +{ |
| 33 | + const LEVEL_NONE = 0; |
| 34 | + const LEVEL_ADMIN = 1; |
| 35 | + const LEVEL_ALL = 2; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 39 | + */ |
| 40 | + protected $alert; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Krucas\Settings\Settings |
| 44 | + */ |
| 45 | + protected $settings; |
| 46 | + |
| 47 | + /** |
| 48 | + * All TOTP related routes. |
| 49 | + * |
| 50 | + * @var array |
| 51 | + */ |
| 52 | + protected $ignoreRoutes = [ |
| 53 | + 'account.security', |
| 54 | + 'account.security.revoke', |
| 55 | + 'account.security.totp', |
| 56 | + 'account.security.totp.set', |
| 57 | + 'account.security.totp.disable', |
| 58 | + 'auth.totp', |
| 59 | + 'auth.logout', |
| 60 | + ]; |
| 61 | + |
| 62 | + /** |
| 63 | + * RequireTwoFactorAuthentication constructor. |
| 64 | + * |
| 65 | + * @param \Prologue\Alerts\AlertsMessageBag $alert |
| 66 | + * @param \Krucas\Settings\Settings $settings |
| 67 | + */ |
| 68 | + public function __construct(AlertsMessageBag $alert, Settings $settings) |
| 69 | + { |
| 70 | + $this->alert = $alert; |
| 71 | + $this->settings = $settings; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Handle an incoming request. |
| 76 | + * |
| 77 | + * @param \Illuminate\Http\Request $request |
| 78 | + * @param \Closure $next |
| 79 | + * @return mixed |
| 80 | + */ |
| 81 | + public function handle($request, Closure $next) |
| 82 | + { |
| 83 | + // Ignore non-users |
| 84 | + if (! $request->user()) { |
| 85 | + return $next($request); |
| 86 | + } |
| 87 | + |
| 88 | + // Skip the 2FA pages |
| 89 | + if (in_array($request->route()->getName(), $this->ignoreRoutes)) { |
| 90 | + return $next($request); |
| 91 | + } |
| 92 | + |
| 93 | + // Get the setting |
| 94 | + switch ((int) $this->settings->get('2fa', 0)) { |
| 95 | + case self::LEVEL_NONE: |
| 96 | + return $next($request); |
| 97 | + |
| 98 | + case self::LEVEL_ADMIN: |
| 99 | + if (! $request->user()->root_admin) { |
| 100 | + return $next($request); |
| 101 | + } |
| 102 | + break; |
| 103 | + |
| 104 | + case self::LEVEL_ALL: |
| 105 | + if ($request->user()->use_totp) { |
| 106 | + return $next($request); |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + $this->alert->danger('The administrator has required 2FA to be enabled. You must enable it before you can do any other action.')->flash(); |
| 112 | + |
| 113 | + return redirect()->route('account.security'); |
| 114 | + } |
| 115 | +} |
0 commit comments