GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#286)
by Dane
04:53 queued 02:03
created

RemoteController::getConfiguration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 11
nc 4
nop 2
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\Http\Controllers\Remote;
26
27
use Carbon\Carbon;
28
use Pterodactyl\Models;
29
use Illuminate\Http\Request;
30
use Pterodactyl\Http\Controllers\Controller;
31
32
class RemoteController extends Controller
33
{
34
    /**
35
     * Controller Constructor.
36
     */
37
    public function __construct()
38
    {
39
        // No middleware for this route.
40
    }
41
42
    public function postDownload(Request $request)
43
    {
44
        $download = Models\Download::where('token', $request->input('token'))->first();
45
        if (! $download) {
46
            return response()->json([
47
                'error' => 'An invalid request token was recieved with this request.',
48
            ], 403);
49
        }
50
51
        $download->delete();
52
53
        return response()->json([
54
            'path' => $download->path,
55
            'server' => $download->server,
56
        ]);
57
    }
58
59
    public function postInstall(Request $request)
60
    {
61
        $server = Models\Server::where('uuid', $request->input('server'))->with('node')->first();
62
        if (! $server) {
63
            return response()->json([
64
                'error' => 'No server by that ID was found on the system.',
65
            ], 422);
66
        }
67
68
        $hmac = $request->input('signed');
69
        $status = $request->input('installed');
70
71 View Code Duplication
        if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            return response()->json([
73
                'error' => 'Signed HMAC was invalid.',
74
            ], 403);
75
        }
76
77
        $server->installed = ($status === 'installed') ? 1 : 2;
78
        $server->save();
79
80
        return response()->json([
81
            'message' => 'Recieved!',
82
        ], 200);
83
    }
84
85
    public function event(Request $request)
86
    {
87
        $server = Models\Server::where('uuid', $request->input('server'))->with('node')->first();
88
        if (! $server) {
89
            return response()->json([
90
                'error' => 'No server by that ID was found on the system.',
91
            ], 422);
92
        }
93
94
        $hmac = $request->input('signed');
95 View Code Duplication
        if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            return response()->json([
97
                'error' => 'Signed HMAC was invalid.',
98
            ], 403);
99
        }
100
101
        // Passes Validation, Setup Notifications
102
        $notify = new NotificationService($server);
103
        $notify->pass($request->input('notification'));
104
105
        return response('', 201);
106
    }
107
108
    public function getConfiguration(Request $request, $tokenString)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110
        // Try to query the token and the node from the database
111
        try {
112
            $token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail();
113
            $node = Models\Node::findOrFail($token->node);
114
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
115
            return response()->json(['error' => 'token_invalid'], 403);
116
        }
117
118
        // Check if token is expired
119
        if ($token->expires_at->lt(Carbon::now())) {
120
            $token->delete();
121
122
            return response()->json(['error' => 'token_expired'], 403);
123
        }
124
125
        // Delete the token, it's one-time use
126
        $token->delete();
127
128
        // Manually as getConfigurationAsJson() returns it in correct format already
129
        return response()->json($node->getConfigurationAsJson(), 200);
130
    }
131
}
132