Skip to content

Commit 7690b4b

Browse files
committed
Merge branch 'AC-520' of github.com:magento-cia/magento2ce into cia-2.4.4-develop-bugfixes-04112021
2 parents cf71a09 + 26cce21 commit 7690b4b

File tree

10 files changed

+522
-36
lines changed

10 files changed

+522
-36
lines changed

app/code/Magento/Integration/Model/Oauth/Token.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class Token extends \Magento\Framework\Model\AbstractModel
5959

6060
/**#@- */
6161

62-
/**#@- */
62+
/**
63+
* @var OauthHelper
64+
*/
6365
protected $_oauthHelper;
6466

6567
/**
@@ -136,9 +138,10 @@ public function __construct(
136138
$this->_consumerFactory = $consumerFactory;
137139
$this->_oauthData = $oauthData;
138140
$this->_oauthHelper = $oauthHelper;
139-
$this->reader = ObjectManager::getInstance()->get(UserTokenReaderInterface::class);
140-
$this->issuer = ObjectManager::getInstance()->get(UserTokenIssuerInterface::class);
141-
$this->tokenParamsFactory = ObjectManager::getInstance()->get(UserTokenParametersInterfaceFactory::class);
141+
$this->reader = $reader ?? ObjectManager::getInstance()->get(UserTokenReaderInterface::class);
142+
$this->issuer = $issuer ?? ObjectManager::getInstance()->get(UserTokenIssuerInterface::class);
143+
$this->tokenParamsFactory = $paramsFactory ??
144+
ObjectManager::getInstance()->get(UserTokenParametersInterfaceFactory::class);
142145
}
143146

144147
/**
@@ -360,6 +363,7 @@ public function loadByConsumerIdAndUserType($consumerId, $userType)
360363
{
361364
$tokenData = $this->getResource()->selectTokenByConsumerIdAndUserType($consumerId, $userType);
362365
$this->setData($tokenData ? $tokenData : []);
366+
$this->getResource()->afterLoad($this);
363367
return $this;
364368
}
365369

app/code/Magento/Integration/Model/Oauth/Token/Provider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
}
4545

4646
/**
47-
* {@inheritdoc}
47+
* @inheritdoc
4848
*/
4949
public function validateConsumer($consumer)
5050
{
@@ -58,7 +58,7 @@ public function validateConsumer($consumer)
5858
}
5959

6060
/**
61-
* {@inheritdoc}
61+
* @inheritdoc
6262
*/
6363
public function createRequestToken($consumer)
6464
{
@@ -73,7 +73,7 @@ public function createRequestToken($consumer)
7373
}
7474

7575
/**
76-
* {@inheritdoc}
76+
* @inheritdoc
7777
*/
7878
public function validateRequestToken($requestToken, $consumer, $oauthVerifier)
7979
{
@@ -99,7 +99,7 @@ public function validateRequestToken($requestToken, $consumer, $oauthVerifier)
9999
}
100100

101101
/**
102-
* {@inheritdoc}
102+
* @inheritdoc
103103
*/
104104
public function getAccessToken($consumer)
105105
{
@@ -118,7 +118,7 @@ public function getAccessToken($consumer)
118118
}
119119

120120
/**
121-
* {@inheritdoc}
121+
* @inheritdoc
122122
*/
123123
public function validateAccessTokenRequest($accessToken, $consumer)
124124
{
@@ -144,7 +144,7 @@ public function validateAccessTokenRequest($accessToken, $consumer)
144144
}
145145

146146
/**
147-
* {@inheritdoc}
147+
* @inheritdoc
148148
*/
149149
public function validateAccessToken($accessToken)
150150
{
@@ -168,15 +168,15 @@ public function validateAccessToken($accessToken)
168168
}
169169

170170
/**
171-
* {@inheritdoc}
171+
* @inheritdoc
172172
*/
173173
public function validateOauthToken($oauthToken)
174174
{
175175
return strlen($oauthToken) == \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN;
176176
}
177177

178178
/**
179-
* {@inheritdoc}
179+
* @inheritdoc
180180
*/
181181
public function getConsumerByKey($consumerKey)
182182
{

app/code/Magento/Integration/Model/ResourceModel/Oauth/Consumer.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@
55
*/
66
namespace Magento\Integration\Model\ResourceModel\Oauth;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Encryption\Encryptor;
10+
811
class Consumer extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
912
{
13+
14+
/**
15+
* @var Encryptor
16+
*/
17+
private $encryptor;
18+
1019
/**
1120
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
1221
* @param string $connectionName
22+
* @param Encryptor $encryptor
1323
*/
1424
public function __construct(
1525
\Magento\Framework\Model\ResourceModel\Db\Context $context,
16-
$connectionName = null
26+
$connectionName = null,
27+
Encryptor $encryptor = null
1728
) {
1829
parent::__construct($context, $connectionName);
30+
$this->encryptor = $encryptor ?? ObjectManager::getInstance()->get(Encryptor::class);
1931
}
2032

2133
/**
@@ -61,4 +73,40 @@ public function getTimeInSecondsSinceCreation($consumerId)
6173

6274
return $connection->fetchOne($select);
6375
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
81+
{
82+
if ($object->getSecret()) {
83+
$object->setSecret($this->encryptor->encrypt($object->getSecret()));
84+
}
85+
86+
return parent::_beforeSave($object);
87+
}
88+
89+
/**
90+
* @inheritdoc
91+
*/
92+
protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
93+
{
94+
if ($object->getSecret()) {
95+
$object->setSecret($this->encryptor->decrypt($object->getSecret()));
96+
}
97+
98+
return parent::_afterLoad($object);
99+
}
100+
101+
/**
102+
* @inheritdoc
103+
*/
104+
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
105+
{
106+
if ($object->getSecret()) {
107+
$object->setSecret($this->encryptor->decrypt($object->getSecret()));
108+
}
109+
110+
return parent::_afterSave($object);
111+
}
64112
}

app/code/Magento/Integration/Model/ResourceModel/Oauth/Token.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\Integration\Model\ResourceModel\Oauth;
77

88
use Magento\Authorization\Model\UserContextInterface;
9+
use Magento\Framework\Encryption\Encryptor;
10+
use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
11+
use Magento\Framework\App\ObjectManager;
912

1013
/**
1114
* OAuth token resource model
@@ -24,20 +27,28 @@ class Token extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
2427
*/
2528
protected $date;
2629

30+
/**
31+
* @var Encryptor
32+
*/
33+
private $encryptor;
34+
2735
/**
2836
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
2937
* @param \Magento\Framework\Stdlib\DateTime $dateTime
3038
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
3139
* @param string $connectionName
40+
* @param Encryptor $encryptor
3241
*/
3342
public function __construct(
3443
\Magento\Framework\Model\ResourceModel\Db\Context $context,
3544
\Magento\Framework\Stdlib\DateTime $dateTime,
3645
\Magento\Framework\Stdlib\DateTime\DateTime $date,
37-
$connectionName = null
46+
$connectionName = null,
47+
$encryptor = null
3848
) {
3949
$this->_dateTime = $dateTime;
4050
$this->date = $date;
51+
$this->encryptor = $encryptor ?? ObjectManager::getInstance()->get(Encryptor::class);
4152
parent::__construct($context, $connectionName);
4253
}
4354

@@ -195,4 +206,42 @@ public function selectTokenByCustomerId($customerId)
195206
->where('user_type = ?', UserContextInterface::USER_TYPE_CUSTOMER);
196207
return $connection->fetchRow($select);
197208
}
209+
210+
/**
211+
* @inheritdoc
212+
*/
213+
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
214+
{
215+
if ($object->getType() === \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS) {
216+
217+
if (!empty($object->getSecret())) {
218+
$object->setSecret($this->encryptor->encrypt($object->getSecret()));
219+
}
220+
}
221+
return parent::_beforeSave($object);
222+
}
223+
224+
/**
225+
* @inheritdoc
226+
*/
227+
protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
228+
{
229+
if ($object->getType() === \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS) {
230+
$object->setSecret($this->encryptor->decrypt($object->getSecret()));
231+
}
232+
233+
return parent::_afterLoad($object);
234+
}
235+
236+
/**
237+
* @inheritdoc
238+
*/
239+
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
240+
{
241+
if ($object->getType() === \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS) {
242+
$object->setSecret($this->encryptor->decrypt($object->getSecret()));
243+
}
244+
245+
return parent::_afterSave($object);
246+
}
198247
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Integration\Setup\Patch\Data;
8+
9+
use Magento\Framework\Encryption\Encryptor;
10+
use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
11+
use Magento\Framework\Setup\Patch\DataPatchInterface;
12+
use Magento\Framework\Setup\Patch\PatchVersionInterface;
13+
use Magento\Integration\Model\ResourceModel\Oauth\Consumer\Collection as ConsumerCollection;
14+
use Magento\Integration\Model\ResourceModel\Oauth\Consumer\CollectionFactory as ConsumerCollectionFactory;
15+
use Magento\Integration\Model\ResourceModel\Oauth\Consumer;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Upgrades Oauth Consumer Secret if not encrypted
20+
*/
21+
class UpgradeConsumerSecret implements DataPatchInterface, PatchVersionInterface
22+
{
23+
24+
/**
25+
* @var ConsumerCollection
26+
*/
27+
private $consumerCollection;
28+
29+
/**
30+
* @var Encryptor
31+
*/
32+
private $encryptor;
33+
34+
/**
35+
* @var Consumer
36+
*/
37+
private $consumerResourceModel;
38+
39+
/**
40+
* @var \Psr\Log\LoggerInterface
41+
*/
42+
private $logger;
43+
44+
/**#@+
45+
* Constant for batch size limit
46+
*/
47+
private const BATCH_SIZE = 100;
48+
/**#@-*/
49+
50+
/**
51+
* Constructor
52+
*
53+
* @param ConsumerCollectionFactory $consumerCollectionFactory
54+
* @param Encryptor $encryptor
55+
* @param Consumer $consumerResourceModel
56+
* @param LoggerInterface $logger
57+
*/
58+
public function __construct(
59+
ConsumerCollectionFactory $consumerCollectionFactory,
60+
Encryptor $encryptor,
61+
Consumer $consumerResourceModel,
62+
LoggerInterface $logger
63+
) {
64+
65+
$this->consumerCollection= $consumerCollectionFactory->create();
66+
$this->encryptor = $encryptor;
67+
$this->consumerResourceModel = $consumerResourceModel;
68+
$this->logger = $logger;
69+
}
70+
71+
/**
72+
* @inheritdoc
73+
*/
74+
public function apply()
75+
{
76+
$this->consumerCollection->addFieldToSelect('entity_id');
77+
$this->consumerCollection->addFieldToSelect('secret');
78+
$connection = $this->consumerResourceModel->getConnection();
79+
$this->consumerCollection->setPageSize(self::BATCH_SIZE);
80+
$pages = $this->consumerCollection->getLastPageNumber();
81+
$tableName = $this->consumerResourceModel->getMainTable();
82+
83+
for ($currentPage = 1; $currentPage <= $pages; $currentPage++) {
84+
$this->consumerCollection->setCurPage($currentPage);
85+
86+
/** @var $consumer Consumer */
87+
foreach ($this->consumerCollection as $consumer) {
88+
$existingSecret = $consumer->getSecret();
89+
$entityId = $consumer->getEntityId();
90+
91+
if ($entityId && $existingSecret) {
92+
if (strlen($existingSecret) <= OauthHelper::LENGTH_TOKEN_SECRET) {
93+
$data = ['secret' => $this->encryptor->encrypt($existingSecret)];
94+
$where = ['entity_id = ?' => $entityId];
95+
try {
96+
$connection->update($tableName, $data, $where);
97+
} catch (\Exception $exception) {
98+
$this->logger->critical($exception->getMessage());
99+
return $this;
100+
}
101+
}
102+
}
103+
}
104+
$this->consumerCollection->clear();
105+
}
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* @inheritdoc
112+
*/
113+
public static function getDependencies()
114+
{
115+
return [];
116+
}
117+
118+
/**
119+
* @inheritdoc
120+
*/
121+
public static function getVersion()
122+
{
123+
return '2.0.0';
124+
}
125+
126+
/**
127+
* @inheritdoc
128+
*/
129+
public function getAliases()
130+
{
131+
return [];
132+
}
133+
}

0 commit comments

Comments
 (0)