Skip to content

Commit 0096042

Browse files
authored
Merge pull request #57 from magento-commerce/imported-anzin-security-package-314
[Imported] Updated endroid/qr-code to 4.3.5
2 parents 008f1db + 4a8b76d commit 0096042

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

TwoFactorAuth/Model/Provider/Engine/Google.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace Magento\TwoFactorAuth\Model\Provider\Engine;
99

10-
use Endroid\QrCode\ErrorCorrectionLevel;
11-
use Endroid\QrCode\Exception\ValidationException;
10+
use Base32\Base32;
11+
use Endroid\QrCode\Color\Color;
12+
use Endroid\QrCode\Encoding\Encoding;
13+
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
1214
use Endroid\QrCode\QrCode;
1315
use Endroid\QrCode\Writer\PngWriter;
1416
use Exception;
@@ -17,11 +19,10 @@
1719
use Magento\Framework\Encryption\EncryptorInterface;
1820
use Magento\Framework\Exception\NoSuchEntityException;
1921
use Magento\Store\Model\StoreManagerInterface;
22+
use Magento\TwoFactorAuth\Api\EngineInterface;
23+
use Magento\TwoFactorAuth\Api\UserConfigManagerInterface;
2024
use Magento\TwoFactorAuth\Model\Provider\Engine\Google\TotpFactory;
2125
use Magento\User\Api\Data\UserInterface;
22-
use Magento\TwoFactorAuth\Api\UserConfigManagerInterface;
23-
use Magento\TwoFactorAuth\Api\EngineInterface;
24-
use Base32\Base32;
2526
use OTPHP\TOTPInterface;
2627

2728
/**
@@ -206,27 +207,26 @@ public function verify(UserInterface $user, DataObject $request): bool
206207
* Render TFA QrCode
207208
*
208209
* @param UserInterface $user
210+
*
209211
* @return string
210-
* @throws NoSuchEntityException
211-
* @throws ValidationException
212+
* @throws Exception
212213
*/
213214
public function getQrCodeAsPng(UserInterface $user): string
214215
{
215216
// @codingStandardsIgnoreStart
216217
$qrCode = new QrCode($this->getProvisioningUrl($user));
217218
$qrCode->setSize(400);
218219
$qrCode->setMargin(0);
219-
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
220-
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
221-
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
222-
$qrCode->setLabelFontSize(16);
223-
$qrCode->setEncoding('UTF-8');
220+
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevelHigh());
221+
$qrCode->setForegroundColor(new Color(0, 0, 0, 0));
222+
$qrCode->setBackgroundColor(new Color(255, 255, 255, 0));
223+
$qrCode->setEncoding(new Encoding('UTF-8'));
224224

225225
$writer = new PngWriter();
226-
$pngData = $writer->writeString($qrCode);
226+
$pngData = $writer->write($qrCode);
227227
// @codingStandardsIgnoreEnd
228228

229-
return $pngData;
229+
return $pngData->getString();
230230
}
231231

232232
/**

TwoFactorAuth/Test/Unit/Model/Provider/Engine/GoogleTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\DataObject;
1313
use Magento\Framework\Encryption\EncryptorInterface;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\StoreManagerInterface;
1416
use Magento\TwoFactorAuth\Api\UserConfigManagerInterface;
1517
use Magento\TwoFactorAuth\Model\Provider\Engine\Google;
1618
use Magento\TwoFactorAuth\Model\Provider\Engine\Google\TotpFactory;
@@ -20,6 +22,9 @@
2022
use PHPUnit\Framework\TestCase;
2123
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2224

25+
/**
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
2328
class GoogleTest extends TestCase
2429
{
2530
/**
@@ -57,6 +62,11 @@ class GoogleTest extends TestCase
5762
*/
5863
private $encryptor;
5964

65+
/**
66+
* @var StoreManagerInterface|MockObject
67+
*/
68+
private $storeManager;
69+
6070
/**
6171
* @inheritDoc
6272
*/
@@ -74,13 +84,15 @@ protected function setUp(): void
7484
$this->user->method('getEmail')
7585
->willReturn('[email protected]');
7686
$this->configManager = $this->createMock(UserConfigManagerInterface::class);
87+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
7788
$this->model = $objectManager->getObject(
7889
Google::class,
7990
[
8091
'configManager' => $this->configManager,
8192
'totpFactory' => $this->totpFactory,
8293
'scopeConfig' => $this->scopeConfig,
83-
'encryptor' => $this->encryptor
94+
'encryptor' => $this->encryptor,
95+
'storeManager' => $this->storeManager
8496
]
8597
);
8698
}
@@ -196,4 +208,24 @@ public function testVerifyWindowFromUserConfigOverridesScopeConfig()
196208

197209
self::assertTrue($valid);
198210
}
211+
212+
/**
213+
* Check if method not return empty string and image is png.
214+
*
215+
* @return void
216+
* @throws \Exception
217+
*/
218+
public function testGetQrCodeAsPng(): void
219+
{
220+
$store = $this->createMock(Store::class);
221+
$store->method('getBaseUrl')
222+
->willReturn('http://www.example.com/');
223+
$this->storeManager->method('getStore')
224+
->willReturn($store);
225+
226+
$image = $this->model->getQrCodeAsPng($this->user);
227+
$this->assertNotEmpty($image);
228+
$imageData = getimagesizefromstring($image);
229+
$this->assertEquals('image/png', $imageData['mime']);
230+
}
199231
}

TwoFactorAuth/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"magento/module-integration": "*",
1515
"christian-riesen/base32": "^1.3",
1616
"spomky-labs/otphp": "^10.0",
17-
"endroid/qr-code": "^3.7",
17+
"endroid/qr-code": "^4.3.5",
1818
"2tvenom/cborencode": "^1.0"
1919
},
2020
"type": "magento2-module",

0 commit comments

Comments
 (0)