1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\BootstrapMFA\Authenticators; |
4
|
|
|
|
5
|
|
|
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler; |
6
|
|
|
use Firesphere\BootstrapMFA\Interfaces\MFAAuthenticator; |
7
|
|
|
use Firesphere\BootstrapMFA\Providers\BootstrapMFAProvider; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\ORM\ValidationException; |
10
|
|
|
use SilverStripe\ORM\ValidationResult; |
11
|
|
|
use SilverStripe\Security\Member; |
12
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; |
13
|
|
|
use SilverStripe\Security\PasswordEncryptor_NotFoundException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class BootstrapMFAAuthenticator |
17
|
|
|
* It needs to be instantiable, therefore it can't be an Abstract. |
18
|
|
|
* |
19
|
|
|
* @todo Interface! |
20
|
|
|
* |
21
|
|
|
* @package Firesphere\BootstrapMFA\Authenticators |
22
|
|
|
*/ |
23
|
|
|
class BootstrapMFAAuthenticator extends MemberAuthenticator implements MFAAuthenticator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Key for array to be stored in between steps in the session |
27
|
|
|
*/ |
28
|
|
|
const SESSION_KEY = 'MFALogin'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Member $member |
32
|
|
|
* @param string $token |
33
|
|
|
* @param ValidationResult|null $result |
34
|
|
|
* @return bool|Member |
35
|
|
|
* @throws ValidationException |
36
|
|
|
* @throws PasswordEncryptor_NotFoundException |
37
|
|
|
*/ |
38
|
|
|
public function validateBackupCode($member, $token, &$result = null) |
39
|
|
|
{ |
40
|
|
|
if (!$result) { |
41
|
|
|
$result = new ValidationResult(); |
42
|
|
|
} |
43
|
|
|
$token = $member->encryptWithUserSettings($token); |
44
|
|
|
|
45
|
|
|
/** @var BootstrapMFAProvider $provider */ |
46
|
|
|
$provider = Injector::inst()->get(BootstrapMFAProvider::class); |
47
|
|
|
$provider->setMember($member); |
48
|
|
|
|
49
|
|
|
$backupCode = $provider->fetchToken($token); |
50
|
|
|
|
51
|
|
|
if ($backupCode && $backupCode->exists()) { |
52
|
|
|
$backupCode->expire(); |
53
|
|
|
// Reset the subclass authenticator results |
54
|
|
|
$result = Injector::inst()->get(ValidationResult::class, false); |
55
|
|
|
|
56
|
|
|
/** @var Member $member */ |
57
|
|
|
return $member; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$member->registerFailedLogin(); |
61
|
|
|
$result->addError(_t(self::class . '.INVALIDTOKEN', 'Invalid token')); |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $link |
68
|
|
|
* @return BootstrapMFALoginHandler|static |
69
|
|
|
*/ |
70
|
|
|
public function getLoginHandler($link) |
71
|
|
|
{ |
72
|
|
|
return BootstrapMFALoginHandler::create($link, $this); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $member |
77
|
|
|
* @param $token |
78
|
|
|
* @param $result |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function verifyMFA($member, $token, &$result) |
82
|
|
|
{ |
83
|
|
|
throw new \LogicException('No token verification implemented'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function getMFAForm() |
90
|
|
|
{ |
91
|
|
|
throw new \LogicException('No MFA Form implementation found'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|