Skip to content

Commit 3dcc147

Browse files
OXDEV-5014 Add OTP and send as mail
1 parent 30c936c commit 3dcc147

9 files changed

Lines changed: 107 additions & 35 deletions

File tree

src/OAuth/Controller/OAuthController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ public function render()
2424
$provider->authenticate();
2525
}
2626
}
27-
2827
}
2928
}

src/OAuth/Service/Provider/Google.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function isActive(): bool
2525
public function authenticate(): void
2626
{
2727
$provider = new GoogleProvider([
28-
'clientId' => '{google-app-id}',
29-
'clientSecret' => '{google-app-secret}',
30-
'redirectUri' => 'http://127.0.0.1/?cl=captcha&fnc=access'
28+
'clientId' => '29518356239-stt1somdnqn4hkoojuneerotqbdetq5u.apps.googleusercontent.com',
29+
'clientSecret' => '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',
30+
'redirectUri' => 'http://localhost.local/?cl=oauth&fnc=register'
3131
]);
3232

3333
if (!empty($_GET['error'])) {

src/OAuth/Service/ProviderCollector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class ProviderCollector implements ProviderCollectorInterface
1212
public function __construct(
1313
protected iterable $providers,
1414
) {
15-
1615
}
1716

1817
public function getProviders(): iterable

src/Shared/Model/User.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function login($userName, $password, $setSessionCookie = false): bool
6363
return $login;
6464
}
6565

66+
Registry::getSession()->setVariable(
67+
'usr',
68+
$this->getUser()->getId()
69+
);
6670
Registry::getUtils()->redirect(
6771
Registry::getConfig()->getShopHomeUrl() . 'cl=2fa&setsessioncookie=' . $setSessionCookie
6872
);

src/TwoFA/Component/UserComponent.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,53 @@
88
namespace OxidEsales\SecurityModule\TwoFA\Component;
99

1010
use OxidEsales\Eshop\Core\Registry;
11+
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
12+
use OxidEsales\SecurityModule\TwoFA\Service\TwoFactorAuthInterface;
1113

1214
class UserComponent extends UserComponent_parent
1315
{
1416
public function registerUser()
1517
{
1618
$registration = parent::registerUser();
1719

20+
//todo: module setting to use OTP (sms, email) or TOTP (time based password)
1821
if ($registration) {
19-
Registry::getUtils()->redirect(
20-
Registry::getConfig()->getShopHomeUrl() . 'cl=2faregister&success=1'
21-
);
22+
$this->handleOTP();
23+
24+
$this->handleTOTP();
2225
}
2326
}
27+
28+
private function handleOTP()
29+
{
30+
$user = $this->getUser();
31+
32+
//This is OTP password, saved to MySQL and send as email/sms
33+
$twoFactorAuth = ContainerFactory::getInstance()->getContainer()->get(TwoFactorAuthInterface::class);
34+
$OTPCode = $twoFactorAuth->generateOTPCode();
35+
36+
//Todo: create own field for OTP password
37+
$user->assign([
38+
'OXADDINFO' => $OTPCode,
39+
]);
40+
$user->save();
41+
42+
$mail = oxNew(\OxidEsales\Eshop\Core\Email::class);
43+
$mail->setUser($user);
44+
$mail->setRecipient($user->oxuser__oxusername->value);
45+
$mail->setBody('your code is ' . $OTPCode);
46+
$mail->send();
47+
48+
Registry::getUtils()->redirect(
49+
Registry::getConfig()->getShopHomeUrl() . 'cl=2fa'
50+
);
51+
}
52+
53+
private function handleTOTP()
54+
{
55+
//In case we have TOTP redirect to QR Code page
56+
Registry::getUtils()->redirect(
57+
Registry::getConfig()->getShopHomeUrl() . 'cl=2faregister&success=1'
58+
);
59+
}
2460
}

src/TwoFA/Controller/TwoFactorAuthController.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,67 @@
88
namespace OxidEsales\SecurityModule\TwoFA\Controller;
99

1010
use OxidEsales\Eshop\Application\Controller\FrontendController;
11-
use OxidEsales\Eshop\Core\Exception\UserException;
11+
use OxidEsales\Eshop\Application\Model\User;
1212
use OxidEsales\Eshop\Core\Registry;
1313
use OxidEsales\SecurityModule\TwoFA\Service\TwoFactorAuthInterface;
1414
use PragmaRX\Google2FA\Google2FA;
1515

1616
class TwoFactorAuthController extends FrontendController
1717
{
18-
protected $_sThisTemplate = '@oe_security_module/templates/2fa/two-factor-auth';
18+
protected $template = '@oe_security_module/templates/2fa/two-factor-auth';
1919

20-
public function render()
20+
public function render(): string
2121
{
2222
$template = parent::render();
2323

2424
$code = Registry::getRequest()->getRequestEscapedParameter('code');
2525
if ($code) {
26-
$secret = $this->getService(TwoFactorAuthInterface::class)->secretGenerate();
26+
$this->handleOTP($code);
2727

28-
$GA = new Google2FA();
29-
$valid = $GA->verify(
30-
$code,
31-
$secret,
32-
);
28+
$this->handleTOTP($code);
29+
}
30+
31+
return $template;
32+
}
3333

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);
3640

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();
4146
}
47+
}
4248

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+
);
4473
}
4574
}

src/TwoFA/Controller/TwoFactorAuthRegisterController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
namespace OxidEsales\SecurityModule\TwoFA\Controller;
99

1010
use OxidEsales\Eshop\Application\Controller\FrontendController;
11+
use OxidEsales\Eshop\Core\Registry;
1112
use OxidEsales\SecurityModule\TwoFA\Service\TwoFactorAuthInterface;
1213

1314
class TwoFactorAuthRegisterController extends FrontendController
1415
{
15-
protected $_sThisTemplate = '@oe_security_module/templates/2fa/two-factor-auth-registration';
16+
protected $template = '@oe_security_module/templates/2fa/two-factor-auth-registration';
1617

1718
public function render()
1819
{
1920
//Display QR Code after registration
2021
$template = parent::render();
2122

2223
$user = $this->getUser();
24+
if (!$user) {
25+
Registry::getUtils()->redirect(
26+
Registry::getConfig()->getShopHomeUrl()
27+
);
28+
}
2329

2430
$this->addTplParam(
2531
'qrcode',

src/TwoFA/Service/TwoFactorAuth.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function secretGenerate(): string
2525
return $secret;
2626
}
2727

28-
public function QrUrlGenerate(string $username): string
28+
public function QRUrlGenerate(string $username): string
2929
{
3030
$secret = $this->secretGenerate();
3131

@@ -38,11 +38,6 @@ public function QrUrlGenerate(string $username): string
3838
);
3939
}
4040

41-
public function codeVerify(): bool
42-
{
43-
return true;
44-
}
45-
4641
public function QRCodeGenerate(string $username): string
4742
{
4843
$writer = new Writer(
@@ -53,9 +48,14 @@ public function QRCodeGenerate(string $username): string
5348
);
5449

5550
return $writer->writeString(
56-
$this->QrUrlGenerate(
51+
$this->QRUrlGenerate(
5752
$username
5853
)
5954
);
6055
}
56+
57+
public function generateOTPCode(): int
58+
{
59+
return rand(100000, 999999);
60+
}
6161
}

src/TwoFA/Service/TwoFactorAuthInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
* See LICENSE file for license details.
66
*/
77

8-
98
namespace OxidEsales\SecurityModule\TwoFA\Service;
109

1110
interface TwoFactorAuthInterface
1211
{
13-
public function codeVerify(): bool;
14-
1512
public function QRCodeGenerate(string $username): string;
13+
14+
public function generateOTPCode(): int;
1615
}

0 commit comments

Comments
 (0)