Firesphere /
silverstripe-bootstrapmfa
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Firesphere\BootstrapMFA\Tests\Mocks; |
||
| 4 | |||
| 5 | use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator; |
||
| 6 | use Firesphere\BootstrapMFA\Forms\BootstrapMFALoginForm; |
||
| 7 | use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler; |
||
| 8 | use Firesphere\BootstrapMFA\Interfaces\MFAAuthenticator; |
||
| 9 | use SilverStripe\Control\HTTPRequest; |
||
| 10 | use SilverStripe\Dev\TestOnly; |
||
| 11 | use SilverStripe\Forms\FieldList; |
||
| 12 | use SilverStripe\Forms\FormAction; |
||
| 13 | use SilverStripe\Forms\TextField; |
||
| 14 | use SilverStripe\ORM\ValidationResult; |
||
| 15 | use SilverStripe\Security\Member; |
||
| 16 | |||
| 17 | class MockAuthenticator extends BootstrapMFAAuthenticator implements TestOnly, MFAAuthenticator |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Get the MFA form |
||
| 22 | * |
||
| 23 | * @param BootstrapMFALoginHandler $controller |
||
| 24 | * @param string $name |
||
| 25 | * @return BootstrapMFALoginForm |
||
| 26 | */ |
||
| 27 | public function getMFAForm($controller, $name) |
||
| 28 | { |
||
| 29 | $fields = FieldList::create([ |
||
| 30 | TextField::create('token', 'Token') |
||
| 31 | ]); |
||
| 32 | $actions = FieldList::create([ |
||
| 33 | FormAction::create('verifyMFA', 'Verify') |
||
| 34 | ]); |
||
| 35 | |||
| 36 | $form = BootstrapMFALoginForm::create($controller, $name, $fields, $actions); |
||
| 37 | $form->setAuthenticatorClass(self::class); |
||
| 38 | |||
| 39 | return $form; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Verify the MFA code. Or just fake it |
||
| 44 | * |
||
| 45 | * @param array $data |
||
| 46 | * @param HTTPRequest $request |
||
| 47 | * @param string $token |
||
| 48 | * @param ValidationResult $result |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function verifyMFA($data, $request, $token, $result) |
||
| 52 | { |
||
| 53 | if (!$result) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 54 | $result = ValidationResult::create(); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($token === 'success') { |
||
| 58 | $id = $request->getSession()->get(BootstrapMFAAuthenticator::SESSION_KEY . '.MemberID'); |
||
| 59 | |||
| 60 | return Member::get()->byID($id); |
||
| 61 | } |
||
| 62 | |||
| 63 | $result->addError('This is not correct'); |
||
| 64 | |||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getTokenField() |
||
| 69 | { |
||
| 70 | return 'token'; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |