U2f   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getRegisterData() 0 4 1
A doRegister() 0 7 1
A getAuthenticateData() 0 4 1
A doAuthenticate() 0 24 2
A check() 0 4 1
1
<?php namespace Lahaxearnaud\U2f;
2
3
use Lahaxearnaud\U2f\Models\U2fKey;
4
use Illuminate\Support\Facades\Request;
5
use Illuminate\Config\Repository as Config;
6
use Illuminate\Session\SessionManager as Session;
7
use Illuminate\Contracts\Auth\Authenticatable as User;
8
9
/**
10
 * Class LaravelU2f
11
 *
12
 *
13
 *
14
 * @package Lahaxearnaud\U2f
15
 * @author  LAHAXE Arnaud
16
 */
17
class U2f {
18
    /**
19
     * @var \u2flib_server\U2F
20
     */
21
    protected $u2f;
22
23
    /**
24
     * @var Config
25
     */
26
    protected  $config;
27
28
    /**
29
     * @var Session
30
     */
31
    protected  $session;
32
33
    /**
34
     * @param \Illuminate\Config\Repository $config
35
     */
36
    public function __construct(Config $config, Session $session)
37
    {
38
        $scheme = Request::isSecure() ? "https://" : "http://";
0 ignored issues
show
Bug introduced by
The method isSecure() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean secure()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
39
        $this->u2f = new \u2flib_server\U2F($scheme . Request::getHttpHost());
40
        $this->config = $config;
41
        $this->session = $session;
42
    }
43
44
    /**
45
     * @author LAHAXE Arnaud
46
     *
47
     * @param User $user
48
     *
49
     * @return mixed
50
     */
51
    public function getRegisterData(User $user)
52
    {
53
        return $this->u2f->getRegisterData(U2fKey::where('user_id', $user->getAuthIdentifier())->get()->all());
54
    }
55
56
    /**
57
     * @author LAHAXE Arnaud
58
     *
59
     * @param User $user
60
     * @param           $registerData
61
     * @param           $keyData
62
     *
63
     * @return mixed
64
     */
65
    public function doRegister(User $user, $registerData, $keyData)
66
    {
67
        $reg = $this->u2f->doRegister($registerData, $keyData);
68
        $reg->user_id = $user->getAuthIdentifier();
69
70
        return U2fKey::create((array) $reg);
71
    }
72
73
    /**
74
     * @author LAHAXE Arnaud
75
     *
76
     * @param User $user
77
     *
78
     * @return mixed
79
     */
80
    public function getAuthenticateData(User $user)
81
    {
82
        return $this->u2f->getAuthenticateData(U2fKey::where('user_id', $user->getAuthIdentifier())->get()->all());
83
    }
84
85
    /**
86
     * @author LAHAXE Arnaud
87
     *
88
     * @param User $user
89
     * @param           $authData
90
     * @param           $keyData
91
     *
92
     * @return bool
93
     */
94
    public function doAuthenticate(User $user, $authData, $keyData)
95
    {
96
        $reg = $this->u2f->doAuthenticate(
97
            $authData,
98
            U2fKey::where('user_id', $user->getAuthIdentifier())->get()->all(),
99
            $keyData
100
        );
101
102
        $U2fKey = U2fKey::where([
103
            'user_id' => $user->getAuthIdentifier(),
104
            'publicKey' => $reg->publicKey
105
        ])->first();
106
107
        if (is_null($U2fKey)) {
108
            return false;
109
        }
110
111
        $U2fKey->counter = $reg->counter;
112
        $U2fKey->save();
113
114
        session([$this->config->get('u2f.sessionU2fName') => true]);
115
116
        return $U2fKey;
117
    }
118
119
    /**
120
     * @author LAHAXE Arnaud
121
     *
122
     *
123
     * @return mixed
124
     */
125
    public function check()
126
    {
127
        return $this->session->get($this->config->get('u2f.sessionU2fName'), false);
128
    }
129
}
130