U2f::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php namespace Lahaxearnaud\U2f\Http\Middleware;
2
3
use Closure;
4
use Illuminate\Config\Repository as Config;
5
use Illuminate\Support\Facades\Auth;
6
use Lahaxearnaud\U2f\Models\U2fKey;
7
use Lahaxearnaud\U2f\U2f as LaravelU2f;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
10
/**
11
 * Class U2f
12
 *
13
 *
14
 *
15
 * @package Lahaxearnaud\U2f\Http\Middleware
16
 * @author  LAHAXE Arnaud
17
 */
18
class U2f
19
{
20
21
    /**
22
     * @var LaravelU2f
23
     */
24
    protected $u2f;
25
26
    /**
27
     * @var Config
28
     */
29
    protected  $config;
30
31
    public function __construct(\Lahaxearnaud\U2f\U2f $u2f, Config $config)
32
    {
33
        $this->u2f = $u2f;
34
        $this->config = $config;
35
    }
36
37
    /**
38
     * Handle an incoming request.
39
     *
40
     * @param  \Illuminate\Http\Request $request
41
     * @param  \Closure                 $next
42
     *
43
     * @return mixed
44
     */
45
    public function handle($request, Closure $next)
46
    {
47
        if(!$this->config->get('u2f.enable')) {
48
            return $next($request);
49
        }
50
51
        if (!$this->u2f->check()) {
52
            if(!Auth::guest()){
53
                if(
54
                    U2fKey::where('user_id', '=', Auth::user()->getAuthIdentifier())->count()  === 0
55
                    && $this->config->get('u2f.byPassUserWithoutKey')
56
                ) {
57
                    return $next($request);
58
                } else {
59
                    return redirect()->guest('u2f/auth');
60
                }
61
62
            } else {
63
                throw new HttpException(401, 'You need to log in before an u2f authentication');
64
            }
65
        }
66
67
        return $next($request);
68
    }
69
}
70