Completed
Pull Request — develop (#50)
by Marcus
01:49
created

ReauthenticationController::setSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Frontarea;
6
7
use Cortex\Fort\Traits\TwoFactorAuthenticatesUsers;
8
use Illuminate\Support\Facades\Hash;
9
use Illuminate\Http\Request;
10
use Cortex\Foundation\Http\Controllers\AuthenticatedController;
11
12
class ReauthenticationController extends AuthenticatedController
13
{
14
15
    use TwoFactorAuthenticatesUsers;
16
17
    /**
18
     * @param Request $request
19
     *
20
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
21
     */
22
    public function processPassword( Request $request ) {
23
24
        $session_name = session(config('cortex.fort.reauthentication.prefix').'.session_name');
25
        $redirect_url = session(config('cortex.fort.reauthentication.prefix').'.intended');
26
27
        if( Hash::check($request->input('password'), request()->user()->password) ) {
28
            $this->setSession($session_name);
29
30
            return intend([
31
                'intended' => url($redirect_url)
32
            ]);
33
        } else {
34
            return intend([
35
                'intended' => url($redirect_url),
36
                'withErrors' => ['password' => trans('cortex/fort::messages.auth.failed')
37
                ]
38
            ]);
39
        }
40
    }
41
42
    /**
43
     * @param Request $request
44
     *
45
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
46
     */
47
    public function processTwofactor( Request $request ) {
48
49
        $session_name = session(config('cortex.fort.reauthentication.prefix').'.session_name');
50
        $redirect_url = session(config('cortex.fort.reauthentication.prefix').'.intended');
51
52
        $guard = $this->getGuard();
53
        $token = $request->input('token');
54
        $user = $request->user($guard);
55
56
        if( $this->attemptTwoFactor($user, $token) ) {
0 ignored issues
show
Bug introduced by
It seems like $token defined by $request->input('token') on line 53 can also be of type array; however, Cortex\Fort\Traits\TwoFa...ers::attemptTwoFactor() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
57
            $this->setSession($session_name);
58
59
            return intend([
60
                'intended' => url($redirect_url)
61
            ]);
62
        } else {
63
            return intend([
64
                'intended' => url($redirect_url),
65
                'withErrors' => ['token' => trans('cortex/fort::messages.verification.twofactor.invalid_token')],
66
            ]);
67
        }
68
    }
69
70
    /**
71
     * @param $session_name
72
     */
73
    protected  function setSession($session_name) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $session_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
74
        session()->put($session_name, time());
75
        session()->forget(config('cortex.fort.reauthentication.prefix').'.session_name');
76
        session()->forget(config('cortex.fort.reauthentication.prefix').'.intended');
77
        session()->forget('rinvex.fort.twofactor.totp');
78
    }
79
80
}
81