|
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()); |
|
|
|
|
|
|
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')) |
|
|
|
|
|
|
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'))); |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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')) |
|
|
|
|
|
|
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'))); |
|
|
|
|
|
|
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
|
|
|
|
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: