|
8 | 8 | namespace OxidEsales\SecurityModule\TwoFA\Controller; |
9 | 9 |
|
10 | 10 | use OxidEsales\Eshop\Application\Controller\FrontendController; |
11 | | -use OxidEsales\Eshop\Core\Exception\UserException; |
| 11 | +use OxidEsales\Eshop\Application\Model\User; |
12 | 12 | use OxidEsales\Eshop\Core\Registry; |
13 | 13 | use OxidEsales\SecurityModule\TwoFA\Service\TwoFactorAuthInterface; |
14 | 14 | use PragmaRX\Google2FA\Google2FA; |
15 | 15 |
|
16 | 16 | class TwoFactorAuthController extends FrontendController |
17 | 17 | { |
18 | | - protected $_sThisTemplate = '@oe_security_module/templates/2fa/two-factor-auth'; |
| 18 | + protected $template = '@oe_security_module/templates/2fa/two-factor-auth'; |
19 | 19 |
|
20 | | - public function render() |
| 20 | + public function render(): string |
21 | 21 | { |
22 | 22 | $template = parent::render(); |
23 | 23 |
|
24 | 24 | $code = Registry::getRequest()->getRequestEscapedParameter('code'); |
25 | 25 | if ($code) { |
26 | | - $secret = $this->getService(TwoFactorAuthInterface::class)->secretGenerate(); |
| 26 | + $this->handleOTP($code); |
27 | 27 |
|
28 | | - $GA = new Google2FA(); |
29 | | - $valid = $GA->verify( |
30 | | - $code, |
31 | | - $secret, |
32 | | - ); |
| 28 | + $this->handleTOTP($code); |
| 29 | + } |
| 30 | + |
| 31 | + return $template; |
| 32 | + } |
33 | 33 |
|
34 | | - if ($valid) { |
35 | | - //todo: set correct cookies so user is logged in after verification |
| 34 | + private function handleOTP(string $code): void |
| 35 | + { |
| 36 | + //In case OTP is used |
| 37 | + $sessionUser = Registry::getSession()->getVariable('usr'); |
| 38 | + $user = oxNew(User::class); |
| 39 | + $user->load($sessionUser); |
36 | 40 |
|
37 | | - Registry::getUtils()->redirect( |
38 | | - Registry::getConfig()->getShopHomeUrl() . '?cl=account' |
39 | | - ); |
40 | | - } |
| 41 | + //Todo: create own field for OTP password |
| 42 | + if ( |
| 43 | + $user->getFieldData('oxaddinfo') == $code |
| 44 | + ) { |
| 45 | + $this->redirectToAccount(); |
41 | 46 | } |
| 47 | + } |
42 | 48 |
|
43 | | - return $template; |
| 49 | + |
| 50 | + private function handleTOTP(string $code): void |
| 51 | + { |
| 52 | + //In case TOTP is used |
| 53 | + $secret = $this->getService(TwoFactorAuthInterface::class)->secretGenerate(); |
| 54 | + |
| 55 | + $GA = new Google2FA(); |
| 56 | + $valid = $GA->verify( |
| 57 | + $code, |
| 58 | + $secret, |
| 59 | + ); |
| 60 | + |
| 61 | + if ($valid) { |
| 62 | + $this->redirectToAccount(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private function redirectToAccount(): void |
| 67 | + { |
| 68 | + //todo: set correct cookies so user is logged in after verification |
| 69 | + |
| 70 | + Registry::getUtils()->redirect( |
| 71 | + Registry::getConfig()->getShopHomeUrl() . '?cl=account' |
| 72 | + ); |
44 | 73 | } |
45 | 74 | } |
0 commit comments