Reauthenticate::handle()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 6
nop 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Middleware;
6
7
use Closure;
8
9
class Reauthenticate
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     * @param \Closure                 $next
16
     * @param string                   $type
17
     * @param string                   $sessionName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sessionName not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
18
     * @param int                      $timeout
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timeout not be null|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
19
     * @param bool                     $renew
20
     *
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next, string $type = 'password', string $sessionName = null, int $timeout = null, bool $renew = false)
24
    {
25
        $timeout = $timeout ?? config('cortex.auth.reauthentication.timeout');
26
        $sessionName = $sessionName ? 'cortex.auth.reauthentication.'.$sessionName : 'cortex.auth.reauthentication.'.$request->route()->getName();
27
28
        if (is_null(session($sessionName)) || time() - session($sessionName) >= $timeout) {
29
            session()->forget($sessionName);
30
            session()->put('cortex.auth.reauthentication.intended', $request->url());
31
            session()->put('cortex.auth.reauthentication.session_name', $sessionName);
32
33
            return view('cortex/auth::'.$request->route('accessarea').'.pages.reauthentication-'.$type);
34
        }
35
36
        ! $renew || session()->put($sessionName, time());
37
38
        return $next($request);
39
    }
40
}
41