U2fController::redirectAfterSuccessAuth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php namespace Lahaxearnaud\U2f\Http\Controllers;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Config\Repository as Config;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Event;
8
use Illuminate\Support\Facades\Redirect;
9
use Lahaxearnaud\U2f\U2f as LaravelU2f;
10
11
class U2fController extends Controller
12
{
13
    /**
14
     * @var LaravelU2f
15
     */
16
    protected $u2f;
17
18
    /**
19
     * @var Config
20
     */
21
    protected  $config;
22
23
    /**
24
     * @param LaravelU2f $u2f
25
     * @param Config $config
26
     */
27
    public function __construct(LaravelU2f $u2f, Config $config)
28
    {
29
        $this->u2f = $u2f;
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * @author LAHAXE Arnaud
35
     *
36
     *
37
     * @return mixed
38
     */
39
    public function registerData()
40
    {
41
        list($req, $sigs) = $this->u2f->getRegisterData(Auth::user());
0 ignored issues
show
Bug introduced by
It seems like \Illuminate\Support\Facades\Auth::user() can be null; however, getRegisterData() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
        Event::dispatch('u2f.register.data', [ 'user' => Auth::user() ]);
43
44
        session()->put('u2f.registerData', $req);
45
46
        return view($this->config->get('u2f.register.view'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
            ->with('currentKeys', $sigs)
48
            ->with('registerData', $req);
49
    }
50
51
    /**
52
     * @author LAHAXE Arnaud
53
     *
54
     *
55
     * @return \Illuminate\Http\RedirectResponse
56
     */
57
    public function register(Request $request)
58
    {
59
        try {
60
            $key = $this->u2f->doRegister(Auth::user(), session('u2f.registerData'), json_decode($request->get('register')));
0 ignored issues
show
Bug introduced by
It seems like \Illuminate\Support\Facades\Auth::user() can be null; however, doRegister() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
61
            Event::dispatch('u2f.register', [ 'u2fKey' => $key, 'user' => Auth::user() ]);
62
            session()->forget('u2f.registerData');
63
64
            session()->put($this->config->get('u2f.sessionU2fName'), true);
65
66
            if ($this->config->get('u2f.register.postSuccessRedirectRoute')) {
67
68
                return Redirect::route($this->config->get('u2f.register.postSuccessRedirectRoute'));
69
            } else {
70
                return redirect('/');
71
            }
72
73
        } catch (\Exception $e) {
74
75
            return Redirect::route('u2f.register.data');
76
        }
77
    }
78
79
    /**
80
     * @author LAHAXE Arnaud
81
     *
82
     *
83
     * @return mixed
84
     */
85
    public function authData()
86
    {
87
        if ($this->u2f->check()) {
88
89
            return $this->redirectAfterSuccessAuth();
90
        }
91
92
        $req = $this->u2f->getAuthenticateData(Auth::user());
0 ignored issues
show
Bug introduced by
It seems like \Illuminate\Support\Facades\Auth::user() can be null; however, getAuthenticateData() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
93
        Event::dispatch('u2f.authentication.data', [ 'user' => Auth::user() ]);
94
95
        session()->put('u2f.authenticationData', $req);
96
97
        return view($this->config->get('u2f.authenticate.view'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
            ->with('authenticationData', $req);
99
    }
100
101
    /**
102
     * @author LAHAXE Arnaud
103
     *
104
     *
105
     * @return mixed
106
     */
107
    public function auth(Request $request)
108
    {
109
        try {
110
            $key = $this->u2f->doAuthenticate(Auth::user(), session('u2f.authenticationData'), json_decode($request->get('authentication')));
0 ignored issues
show
Bug introduced by
It seems like \Illuminate\Support\Facades\Auth::user() can be null; however, doAuthenticate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
111
            Event::dispatch('u2f.authentication', [ 'u2fKey' => $key, 'user' => Auth::user() ]);
112
            session()->forget('u2f.authenticationData');
113
114
            return $this->redirectAfterSuccessAuth();
115
116
        } catch (\Exception $e) {
117
            session()->flash('error', $e->getMessage());
118
119
            return Redirect::route('u2f.auth.data');
120
        }
121
    }
122
123
    /**
124
     * @author LAHAXE Arnaud
125
     *
126
     * @return mixed
127
     */
128
    protected function redirectAfterSuccessAuth()
129
    {
130
        if (strlen($this->config->get('u2f.authenticate.postSuccessRedirectRoute'))) {
131
132
            return Redirect::intended($this->config->get('u2f.authenticate.postSuccessRedirectRoute'));
133
        } else {
134
135
            return Redirect::intended('/');
136
        }
137
    }
138
}
139