|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ElliotSawyer\TOTPAuthenticator; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
10
|
|
|
use SilverStripe\ORM\ValidationException; |
|
11
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
12
|
|
|
use SilverStripe\Security\Member; |
|
13
|
|
|
use SilverStripe\Security\PasswordEncryptor_NotFoundException; |
|
14
|
|
|
use SilverStripe\Security\Security; |
|
15
|
|
|
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class TOTPLoginHandler |
|
19
|
|
|
* @package ElliotSawyer\TOTPAuthenticator |
|
20
|
|
|
*/ |
|
21
|
|
|
class TOTPLoginHandler extends BootstrapMFALoginHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
27
|
|
|
'MFAForm', |
|
28
|
|
|
'validateTOTP' |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $data |
|
33
|
|
|
* @param $form |
|
34
|
|
|
* @param HTTPRequest $request |
|
35
|
|
|
* @return HTTPResponse |
|
36
|
|
|
* @throws NotFoundExceptionInterface |
|
37
|
|
|
* @throws ValidationException |
|
38
|
|
|
* @throws PasswordEncryptor_NotFoundException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function validateTOTP($data, $form, $request) |
|
41
|
|
|
{ |
|
42
|
|
|
$result = Injector::inst()->get(ValidationResult::class); |
|
43
|
|
|
$session = $request->getSession(); |
|
44
|
|
|
|
|
45
|
|
|
$this->request['BackURL'] = !empty($session->get('MFALogin.BackURL')) ? $session->get('MFALogin.BackURL') : ''; |
|
46
|
|
|
$member = $this->authenticator->validateTOTP($data, $request, $result); |
|
47
|
|
|
|
|
48
|
|
|
if (!$member instanceof Member) { |
|
49
|
|
|
$member = parent::validate($data, $form, $request, $result); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($member instanceof Member && $result->isValid()) { |
|
53
|
|
|
$member->MFAEnabled = true; |
|
54
|
|
|
$member->write(); |
|
55
|
|
|
$memberData = $session->get('MFALogin'); |
|
56
|
|
|
|
|
57
|
|
|
$this->performLogin($member, $memberData, $request); |
|
58
|
|
|
Security::setCurrentUser($member); |
|
59
|
|
|
$session->clear('MFAForm'); |
|
60
|
|
|
|
|
61
|
|
|
return $this->redirectAfterSuccessfulLogin(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->redirect($this->link()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return static|TOTPForm |
|
69
|
|
|
*/ |
|
70
|
|
|
public function MFAForm() |
|
71
|
|
|
{ |
|
72
|
|
|
return TOTPForm::create( |
|
73
|
|
|
$this, |
|
74
|
|
|
get_class($this->authenticator), |
|
75
|
|
|
'MFAForm' |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|