|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pterodactyl - Panel |
|
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
|
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\Repositories; |
|
26
|
|
|
|
|
27
|
|
|
use DB; |
|
28
|
|
|
use Auth; |
|
29
|
|
|
use Crypt; |
|
30
|
|
|
use Validator; |
|
31
|
|
|
use IPTools\Network; |
|
32
|
|
|
use Pterodactyl\Models\User; |
|
33
|
|
|
use Pterodactyl\Models\APIKey as Key; |
|
34
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
|
35
|
|
|
use Pterodactyl\Models\APIPermission as Permission; |
|
36
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
|
37
|
|
|
|
|
38
|
|
|
class APIRepository |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Holder for listing of allowed IPs when creating a new key. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $allowed = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The eloquent model for a user. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \Pterodactyl\Models\User |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $user; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Constructor for API Repository. |
|
56
|
|
|
* |
|
57
|
|
|
* @param null|\Pterodactyl\Models\User $user |
|
58
|
|
|
* @return void |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(User $user = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->user = is_null($user) ? Auth::user() : $user; |
|
63
|
|
|
if (is_null($this->user)) { |
|
64
|
|
|
throw new \Exception('Unable to initialize user for API repository instance.'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create a New API Keypair on the system. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $data |
|
72
|
|
|
* @return string |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
|
75
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function create(array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
$validator = Validator::make($data, [ |
|
80
|
|
|
'memo' => 'string|max:500', |
|
81
|
|
|
'allowed_ips' => 'sometimes|string', |
|
82
|
|
|
'permissions' => 'sometimes|required|array', |
|
83
|
|
|
'admin_permissions' => 'sometimes|required|array', |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$validator->after(function ($validator) use ($data) { |
|
87
|
|
|
if (array_key_exists('allowed_ips', $data) && ! empty($data['allowed_ips'])) { |
|
88
|
|
|
foreach (explode("\n", $data['allowed_ips']) as $ip) { |
|
89
|
|
|
$ip = trim($ip); |
|
90
|
|
|
try { |
|
91
|
|
|
Network::parse($ip); |
|
92
|
|
|
array_push($this->allowed, $ip); |
|
93
|
|
|
} catch (\Exception $ex) { |
|
94
|
|
|
$validator->errors()->add('allowed_ips', 'Could not parse IP <' . $ip . '> because it is in an invalid format.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
// Run validator, throw catchable and displayable exception if it fails. |
|
101
|
|
|
// Exception includes a JSON result of failed validation rules. |
|
102
|
|
|
if ($validator->fails()) { |
|
103
|
|
|
throw new DisplayValidationException(json_encode($validator->errors())); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
DB::beginTransaction(); |
|
107
|
|
|
try { |
|
108
|
|
|
$secretKey = str_random(16) . '.' . str_random(7) . '.' . str_random(7); |
|
109
|
|
|
$key = Key::create([ |
|
110
|
|
|
'user_id' => $this->user->id, |
|
111
|
|
|
'public' => str_random(16), |
|
112
|
|
|
'secret' => Crypt::encrypt($secretKey), |
|
113
|
|
|
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed), |
|
114
|
|
|
'memo' => $data['memo'], |
|
115
|
|
|
'expires_at' => null, |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
|
|
$totalPermissions = 0; |
|
119
|
|
|
$pNodes = Permission::permissions(); |
|
120
|
|
|
|
|
121
|
|
|
if (isset($data['permissions'])) { |
|
122
|
|
|
foreach ($data['permissions'] as $permission) { |
|
123
|
|
|
$parts = explode('-', $permission); |
|
124
|
|
|
|
|
125
|
|
|
if (count($parts) !== 2) { |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
list($block, $search) = $parts; |
|
130
|
|
|
|
|
131
|
|
|
if (! array_key_exists($block, $pNodes['_user'])) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (! in_array($search, $pNodes['_user'][$block])) { |
|
136
|
|
|
continue; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$totalPermissions++; |
|
140
|
|
|
Permission::create([ |
|
141
|
|
|
'key_id' => $key->id, |
|
142
|
|
|
'permission' => 'user.' . $permission, |
|
143
|
|
|
]); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($this->user->isRootAdmin() && isset($data['admin_permissions'])) { |
|
148
|
|
|
unset($pNodes['_user']); |
|
149
|
|
|
|
|
150
|
|
|
foreach ($data['admin_permissions'] as $permission) { |
|
151
|
|
|
$parts = explode('-', $permission); |
|
152
|
|
|
|
|
153
|
|
|
if (count($parts) !== 2) { |
|
154
|
|
|
continue; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
list($block, $search) = $parts; |
|
158
|
|
|
|
|
159
|
|
|
if (! array_key_exists($block, $pNodes)) { |
|
160
|
|
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (! in_array($search, $pNodes[$block])) { |
|
164
|
|
|
continue; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$totalPermissions++; |
|
168
|
|
|
Permission::create([ |
|
169
|
|
|
'key_id' => $key->id, |
|
170
|
|
|
'permission' => $permission, |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($totalPermissions < 1) { |
|
176
|
|
|
throw new DisplayException('No valid permissions were passed.'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
DB::commit(); |
|
180
|
|
|
|
|
181
|
|
|
return $secretKey; |
|
182
|
|
|
} catch (\Exception $ex) { |
|
183
|
|
|
DB::rollBack(); |
|
184
|
|
|
throw $ex; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Revokes an API key and associated permissions. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $key |
|
192
|
|
|
* @return void |
|
193
|
|
|
* |
|
194
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function revoke($key) |
|
197
|
|
|
{ |
|
198
|
|
|
DB::transaction(function () use ($key) { |
|
199
|
|
|
$model = Key::with('permissions')->where('public', $key)->where('user_id', $this->user->id)->firstOrFail(); |
|
|
|
|
|
|
200
|
|
|
foreach ($model->permissions as &$permission) { |
|
201
|
|
|
$permission->delete(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$model->delete(); |
|
205
|
|
|
}); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.