1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\BootstrapMFA\Extensions; |
4
|
|
|
|
5
|
|
|
use Firesphere\BootstrapMFA\Models\BackupCode; |
6
|
|
|
use Firesphere\BootstrapMFA\Providers\BootstrapMFAProvider; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Forms\CheckboxField; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\LiteralField; |
13
|
|
|
use SilverStripe\Forms\Tab; |
14
|
|
|
use SilverStripe\ORM\DataExtension; |
15
|
|
|
use SilverStripe\ORM\DataList; |
16
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class MemberExtension |
20
|
|
|
* |
21
|
|
|
* @package Firesphere\BootstrapMFA |
22
|
|
|
* @property Member|MemberExtension $owner |
23
|
|
|
* @property boolean $MFAEnabled |
24
|
|
|
* @method DataList|BackupCode[] BackupCodes() |
25
|
|
|
*/ |
26
|
|
|
class MemberExtension extends DataExtension |
27
|
|
|
{ |
28
|
|
|
use Configurable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $db = [ |
|
|
|
|
34
|
|
|
'MFAEnabled' => 'Boolean(false)', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $has_many = [ |
|
|
|
|
41
|
|
|
'BackupCodes' => BackupCode::class |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $updateMFA = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param FieldList $fields |
51
|
|
|
*/ |
52
|
|
|
public function updateCMSFields(FieldList $fields) |
53
|
|
|
{ |
54
|
|
|
$this->updateMFA = 0; |
|
|
|
|
55
|
|
|
$fields->removeByName(['BackupCodes']); |
56
|
|
|
$session = Controller::curr()->getRequest()->getSession(); |
57
|
|
|
$rootTabSet = $fields->fieldByName("Root"); |
58
|
|
|
$field = LiteralField::create('tokens', $session->get('tokens')); |
59
|
|
|
$tab = Tab::create( |
60
|
|
|
'MFA', |
61
|
|
|
_t(self::class . '.MFATAB', 'Multi Factor Authentication') |
62
|
|
|
); |
63
|
|
|
$rootTabSet->push( |
64
|
|
|
$tab |
65
|
|
|
); |
66
|
|
|
$fields->addFieldToTab( |
67
|
|
|
'Root.MFA', |
68
|
|
|
$enabled = CheckboxField::create('MFAEnabled', _t(self::class . '.MFAEnabled', 'MFA Enabled')) |
69
|
|
|
); |
70
|
|
|
$fields->addFieldToTab( |
71
|
|
|
'Root.MFA', |
72
|
|
|
CheckboxField::create('updateMFA', _t(self::class . '.RESETMFA', 'Reset MFA codes')) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ($session->get('tokens')) { |
76
|
|
|
$fields->addFieldToTab('Root.MFA', $field); |
77
|
|
|
$session->clear('tokens'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
|
|
public function onBeforeWrite() |
85
|
|
|
{ |
86
|
|
|
if (!$this->owner->MFAEnabled && SiteConfig::current_site_config()->ForceMFA) { |
87
|
|
|
$this->owner->MFAEnabled = true; |
88
|
|
|
$this->owner->updateMFA = true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
95
|
|
|
*/ |
96
|
|
|
public function onAfterWrite() |
97
|
|
|
{ |
98
|
|
|
parent::onAfterWrite(); |
99
|
|
|
if ($this->owner->updateMFA) { |
100
|
|
|
$provider = Injector::inst()->get(BootstrapMFAProvider::class); |
101
|
|
|
$provider->setMember($this->owner); |
102
|
|
|
$provider->updateTokens(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getBackupcodes() |
107
|
|
|
{ |
108
|
|
|
return $this->owner->BackupCodes(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isInGracePeriod() |
112
|
|
|
{ |
113
|
|
|
$member = $this->owner; |
114
|
|
|
|
115
|
|
|
// If MFA is enabled on the member, we're always using it |
116
|
|
|
if ($member->MFAEnabled) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$config = SiteConfig::current_site_config(); |
121
|
|
|
// If MFA is not enforced, we're in an endless grace period |
122
|
|
|
if (!$config->ForceMFA) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$graceStartDay = ($member->Created > $config->ForceMFA) ? $member->Created : $config->ForceMFA; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|