-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathWebauthn2faTrait.php
More file actions
147 lines (133 loc) · 5.08 KB
/
Copy pathWebauthn2faTrait.php
File metadata and controls
147 lines (133 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use Cake\Core\Configure;
use Cake\Http\Exception\BadRequestException;
use Cake\Log\Log;
use Cake\Routing\Router;
use CakeDC\Auth\Authenticator\TwoFactorAuthenticator;
use CakeDC\Users\Webauthn\AuthenticateAdapter;
use CakeDC\Users\Webauthn\RegisterAdapter;
trait Webauthn2faTrait
{
/**
* Main page tof webauthn 2fa
*
* @param \CakeDC\Users\Webauthn\RegisterAdapter|null $adapter
* @return void
*/
public function webauthn2fa(?RegisterAdapter $adapter = null)
{
$adapter = $adapter ?? $this->getWebauthn2faRegisterAdapter();
$user = $adapter->getUser();
$this->set('isRegister', !$adapter->hasCredential());
$this->set('username', $user->webauthn_username ?? $user->username);
}
/**
* Action to provide register options to frontend (from js requests)
*
* @param \CakeDC\Users\Webauthn\RegisterAdapter|null $adapter
* @return \Cake\Http\Response
*/
public function webauthn2faRegisterOptions(?RegisterAdapter $adapter = null)
{
$adapter = $adapter ?? $this->getWebauthn2faRegisterAdapter();
if (!$adapter->hasCredential()) {
return $this->getResponse()
->withStringBody(json_encode($adapter->getOptions()));
}
throw new BadRequestException(
__d('cake_d_c/users', 'User already has configured webauthn2fa'),
);
}
/**
* Action to verify and save the new credential based on the webauthn register response.
*
* @param \CakeDC\Users\Webauthn\RegisterAdapter|null $adapter
* @return \Cake\Http\Response
* @throws \Throwable
*/
public function webauthn2faRegister(?RegisterAdapter $adapter = null): \Cake\Http\Response
{
try {
$adapter = $adapter ?? $this->getWebauthn2faRegisterAdapter();
if (!$adapter->hasCredential()) {
$adapter->verifyResponse();
return $this->getResponse()->withStringBody(json_encode(['success' => true]));
}
throw new BadRequestException(
__d('cake_d_c/users', 'User already has configured webauthn2fa'),
);
} catch (\Throwable $e) {
$user = $this->request->getSession()->read('Webauthn2fa.User');
Log::debug(__d('cake_d_c/users', 'Register error with webauthn for user id: {0}', $user['id'] ?? 'empty'));
throw $e;
}
}
/**
* Action to provide authenticate options to frontend (from js requests)
*
* @param \CakeDC\Users\Webauthn\AuthenticateAdapter|null $adapter
* @return \Cake\Http\Response
*/
public function webauthn2faAuthenticateOptions(?AuthenticateAdapter $adapter = null): \Cake\Http\Response
{
$adapter = $adapter ?? $this->getWebauthn2faAuthenticateAdapter();
return $this->getResponse()->withStringBody(
json_encode($adapter->getOptions()),
);
}
/**
* Action to authenticate user based on the webauthn authenticate response.
*
* @param \CakeDC\Users\Webauthn\AuthenticateAdapter|null $adapter
* @return \Cake\Http\Response
* @throws \Throwable
*/
public function webauthn2faAuthenticate(?AuthenticateAdapter $adapter = null): \Cake\Http\Response
{
try {
$adapter = $adapter ?? $this->getWebauthn2faAuthenticateAdapter();
$adapter->verifyResponse();
$redirectUrl = Configure::read('Auth.AuthenticationComponent.loginAction') + [
'?' => $this->getRequest()->getQueryParams(),
];
$this->getRequest()->getSession()->delete('Webauthn2fa');
$this->getRequest()->getSession()->write(
TwoFactorAuthenticator::USER_SESSION_KEY,
$adapter->getUser(),
);
return $this->getResponse()->withStringBody(json_encode([
'success' => true,
'redirectUrl' => Router::url($redirectUrl),
]));
} catch (\Throwable $e) {
$user = $this->request->getSession()->read('Webauthn2fa.User');
Log::debug(__d('cake_d_c/users', 'Register error with webauthn for user id: {0}', $user['id'] ?? 'empty'));
throw $e;
}
}
/**
* @return \CakeDC\Users\Webauthn\RegisterAdapter
*/
protected function getWebauthn2faRegisterAdapter(): RegisterAdapter
{
return new RegisterAdapter($this->getRequest(), $this->getUsersTable());
}
/**
* @return \CakeDC\Users\Webauthn\AuthenticateAdapter
*/
protected function getWebauthn2faAuthenticateAdapter(): AuthenticateAdapter
{
return new AuthenticateAdapter($this->getRequest());
}
}