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.

SecurityController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 17.17 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 17
loc 99
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A generateTotp() 0 16 1
A setTotp() 0 15 3
A revoke() 0 6 1
A disableTotp() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('base.security', ar...->user()->id)->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 44 which is incompatible with the return type documented by Pterodactyl\Http\Control...curityController::index of type Illuminate\View\View.
Loading history...
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([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return response()->json(...ken parameter.'), 500); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Pterodactyl\Http\Control...rityController::setTotp of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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