| 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\Forms\ReadonlyField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\ORM\DataExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class YubiAuthMemberExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Enable yubikey authentication disabling temporarily | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property \Firesphere\YubiAuth\Extensions\MemberExtension $owner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property boolean $YubiAuthEnabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property string $Yubikey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property int $NoYubikeyCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class MemberExtension extends DataExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private static $db = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'YubiAuthEnabled' => 'Boolean(true)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'Yubikey'         => 'Varchar(16)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'NoYubikeyCount'  => 'Int' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private static $defaults = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'YubiAuthEnabled' => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private static $indexes = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'Yubikey' => 'unique("Yubikey")' // The Yubikey Signature is unique for every Yubikey. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param array $labels | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function updateFieldLabels(&$labels) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         parent::updateFieldLabels($labels); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $labels['YubiAuthEnabled'] = _t('YubikeyAuthenticator.ENABLED', 'Yubikey Authentication Enabled'); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param FieldList $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function updateCMSFields(FieldList $fields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $yubiCount = NumericField::create('NoYubikeyCount', $this->owner->fieldLabel('NoYubikeyCount')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if ($this->owner->YubiAuthEnabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $yubiCount->setReadonly(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $yubiField = ReadonlyField::create('Yubikey', $this->owner->fieldLabel('Yubikey')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $yubiField->setDescription( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             _t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'YubikeyAuthenticator.YUBIKEYDESCRIPTION', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'Unique identifier string for the Yubikey. Will reset when the Yubikey Authentication is disabled' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $yubiAuth = CheckboxField::create('YubiAuthEnabled', $this->owner->fieldLabel('YubiAuthEnabled')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $yubiAuth->setDescription( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             _t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 'YubikeyAuthenticator.ENABLEDDESCRIPTION', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $fields->addFieldsToTab('Root.Main', [$yubiCount, $yubiField, $yubiAuth], 'DirectGroups'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function onBeforeWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // Empty the yubikey field on member write, if the yubiauth is not required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Maybe the user lost the key? So a new one will be set next time it's logged in with key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (!$this->owner->YubiAuthEnabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->owner->Yubikey = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |