|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\YubiAuth\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\NumericField; |
|
8
|
|
|
use SilverStripe\ORM\DataExtension; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class YubiAuthMemberExtension |
|
12
|
|
|
* |
|
13
|
|
|
* Enable yubikey authentication disabling temporarily |
|
14
|
|
|
* |
|
15
|
|
|
* @property \Firesphere\YubiAuth\Extensions\MemberExtension $owner |
|
16
|
|
|
* @property string $Yubikey |
|
17
|
|
|
* @property int $NoYubikeyCount |
|
18
|
|
|
*/ |
|
19
|
|
|
class MemberExtension extends DataExtension |
|
20
|
|
|
{ |
|
21
|
|
|
private static $db = [ |
|
|
|
|
|
|
22
|
|
|
'Yubikey' => 'Varchar(255)', |
|
23
|
|
|
'NoYubikeyCount' => 'Int' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
private static $indexes = [ |
|
|
|
|
|
|
27
|
|
|
'Yubikey' => 'unique("Yubikey")' // The Yubikey Signature is unique for every Yubikey. |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
* @param array $labels |
|
33
|
|
|
*/ |
|
34
|
|
|
public function updateFieldLabels(&$labels) |
|
35
|
|
|
{ |
|
36
|
|
|
$labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code'); |
|
37
|
|
|
$labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
* @param FieldList $fields |
|
43
|
|
|
*/ |
|
44
|
|
|
public function updateCMSFields(FieldList $fields) |
|
45
|
|
|
{ |
|
46
|
|
|
$fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']); |
|
47
|
|
|
$yubiCount = NumericField::create('NoYubikeyCount'); |
|
48
|
|
|
if ($this->owner->YubiAuthEnabled) { |
|
|
|
|
|
|
49
|
|
|
$yubiCount->setReadonly(true); |
|
50
|
|
|
} |
|
51
|
|
|
$yubiAuth = CheckboxField::create('YubiAuthEnabled'); |
|
52
|
|
|
$yubiAuth->setDescription( |
|
53
|
|
|
_t( |
|
54
|
|
|
'YubikeyAuthenticator.ENABLEDDESCRIPTION', |
|
55
|
|
|
'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily' |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
|
|
$fields->addFieldsToTab('Root.MFA', [$yubiCount, $yubiAuth]); |
|
59
|
|
|
|
|
60
|
|
|
return $fields; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function onBeforeWrite() |
|
67
|
|
|
{ |
|
68
|
|
|
// Empty the yubikey field on member write, if the yubiauth is not required |
|
69
|
|
|
// Maybe the user lost the key? So a new one will be set next time it's logged in with key |
|
70
|
|
|
if (!$this->owner->MFAEnabled) { |
|
|
|
|
|
|
71
|
|
|
$this->owner->Yubikey = ''; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|