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 Log; |
29
|
|
|
use Alert; |
30
|
|
|
use Illuminate\Http\Request; |
31
|
|
|
use Pterodactyl\Models\APIKey; |
32
|
|
|
use Pterodactyl\Models\APIPermission; |
33
|
|
|
use Pterodactyl\Repositories\APIRepository; |
34
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
35
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
36
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
37
|
|
|
|
38
|
|
|
class APIController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Display base API index page. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Http\Request $request |
44
|
|
|
* @return \Illuminate\View\View |
45
|
|
|
*/ |
46
|
|
|
public function index(Request $request) |
47
|
|
|
{ |
48
|
|
|
return view('base.api.index', [ |
|
|
|
|
49
|
|
|
'keys' => APIKey::where('user_id', $request->user()->id)->get(), |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Display API key creation page. |
55
|
|
|
* |
56
|
|
|
* @param \Illuminate\Http\Request $request |
57
|
|
|
* @return \Illuminate\View\View |
58
|
|
|
*/ |
59
|
|
|
public function create(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return view('base.api.new', [ |
|
|
|
|
62
|
|
|
'permissions' => [ |
63
|
|
|
'user' => collect(APIPermission::permissions())->pull('_user'), |
64
|
|
|
'admin' => collect(APIPermission::permissions())->except('_user')->toArray(), |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Handle saving new API key. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Http\Request $request |
73
|
|
|
* @return \Illuminate\Http\RedirectResponse |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function store(Request $request) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$repo = new APIRepository($request->user()); |
79
|
|
|
$secret = $repo->create($request->intersect([ |
80
|
|
|
'memo', 'allowed_ips', |
81
|
|
|
'admin_permissions', 'permissions', |
82
|
|
|
])); |
83
|
|
|
Alert::success('An API Key-Pair has successfully been generated. The API secret for this public key is shown below and will not be shown again.<br /><br /><code>' . $secret . '</code>')->flash(); |
84
|
|
|
|
85
|
|
|
return redirect()->route('account.api'); |
86
|
|
|
} catch (DisplayValidationException $ex) { |
87
|
|
|
return redirect()->route('account.api.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
88
|
|
|
} catch (DisplayException $ex) { |
89
|
|
|
Alert::danger($ex->getMessage())->flash(); |
90
|
|
|
} catch (\Exception $ex) { |
91
|
|
|
Log::error($ex); |
92
|
|
|
Alert::danger('An unhandled exception occured while attempting to add this API key.')->flash(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return redirect()->route('account.api.new')->withInput(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Handle revoking API key. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Http\Request $request |
102
|
|
|
* @param string $key |
103
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response |
104
|
|
|
*/ |
105
|
|
|
public function revoke(Request $request, $key) |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$repo = new APIRepository($request->user()); |
109
|
|
|
$repo->revoke($key); |
110
|
|
|
|
111
|
|
|
return response('', 204); |
112
|
|
|
} catch (\Exception $ex) { |
113
|
|
|
Log::error($ex); |
114
|
|
|
|
115
|
|
|
return response()->json([ |
116
|
|
|
'error' => 'An error occured while attempting to remove this key.', |
117
|
|
|
], 503); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|