|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pterodactyl - Panel |
|
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]> |
|
5
|
|
|
* Some Modifications (c) 2015 Dylan Seidt <[email protected]>. |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Pterodactyl\Http\Controllers\Base; |
|
27
|
|
|
|
|
28
|
|
|
use Alert; |
|
29
|
|
|
use Google2FA; |
|
30
|
|
|
use Illuminate\Http\Request; |
|
31
|
|
|
use Pterodactyl\Models\Session; |
|
32
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
|
33
|
|
|
|
|
34
|
|
|
class SecurityController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Returns Security Management Page. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Http\Request $request |
|
40
|
|
|
* @return \Illuminate\View\View |
|
41
|
|
|
*/ |
|
42
|
|
|
public function index(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
return view('base.security', [ |
|
|
|
|
|
|
45
|
|
|
'sessions' => Session::where('user_id', $request->user()->id)->get(), |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Generates TOTP Secret and returns popup data for user to verify |
|
51
|
|
|
* that they can generate a valid response. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Illuminate\Http\Request $request |
|
54
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
55
|
|
|
*/ |
|
56
|
|
|
public function generateTotp(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$user = $request->user(); |
|
59
|
|
|
|
|
60
|
|
|
$user->totp_secret = Google2FA::generateSecretKey(); |
|
61
|
|
|
$user->save(); |
|
62
|
|
|
|
|
63
|
|
|
return response()->json([ |
|
64
|
|
|
'qrImage' => Google2FA::getQRCodeGoogleUrl( |
|
65
|
|
|
'Pterodactyl', |
|
66
|
|
|
$user->email, |
|
67
|
|
|
$user->totp_secret |
|
68
|
|
|
), |
|
69
|
|
|
'secret' => $user->totp_secret, |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Verifies that 2FA token recieved is valid and will work on the account. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \Illuminate\Http\Request $request |
|
77
|
|
|
* @return \Illuminate\Http\Response |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setTotp(Request $request) |
|
80
|
|
|
{ |
|
81
|
|
|
if (! $request->has('token')) { |
|
82
|
|
|
return response()->json([ |
|
|
|
|
|
|
83
|
|
|
'error' => 'Request is missing token parameter.', |
|
84
|
|
|
], 500); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$user = $request->user(); |
|
88
|
|
|
if ($user->toggleTotp($request->input('token'))) { |
|
89
|
|
|
return response('true'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return response('false'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Disables TOTP on an account. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \Illuminate\Http\Request $request |
|
99
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function disableTotp(Request $request) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
if (! $request->has('token')) { |
|
104
|
|
|
Alert::danger('Missing required `token` field in request.')->flash(); |
|
105
|
|
|
|
|
106
|
|
|
return redirect()->route('account.security'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$user = $request->user(); |
|
110
|
|
|
if ($user->toggleTotp($request->input('token'))) { |
|
111
|
|
|
return redirect()->route('account.security'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
Alert::danger('The TOTP token provided was invalid.')->flash(); |
|
115
|
|
|
|
|
116
|
|
|
return redirect()->route('account.security'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Revokes a user session. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Illuminate\Http\Request $request |
|
123
|
|
|
* @param int $id |
|
124
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
125
|
|
|
*/ |
|
126
|
|
|
public function revoke(Request $request, $id) |
|
127
|
|
|
{ |
|
128
|
|
|
Session::where('user_id', $request->user()->id)->findOrFail($id)->delete(); |
|
129
|
|
|
|
|
130
|
|
|
return redirect()->route('account.security'); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|