Skip to content

Commit d64f03e

Browse files
committed
AC-1619: Integration access tokens do not work as Bearer tokens
1 parent a638ddf commit d64f03e

File tree

3 files changed

+84
-25
lines changed

3 files changed

+84
-25
lines changed

app/code/Magento/Webapi/Model/Authorization/SoapUserContext.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Authorization\Model\UserContextInterface;
1010
use Magento\Framework\App\ObjectManager;
11+
use Magento\Integration\Model\Config\AuthorizationConfig;
1112
use Magento\Integration\Model\Oauth\Token;
1213
use Magento\Integration\Model\Oauth\TokenFactory;
1314
use Magento\Integration\Api\IntegrationServiceInterface;
@@ -51,24 +52,30 @@ class SoapUserContext implements UserContextInterface
5152
*/
5253
private $integrationService;
5354

55+
/**
56+
* @var AuthorizationConfig
57+
*/
58+
private $authorizationConfig;
59+
5460
/**
5561
* Initialize dependencies.
5662
*
5763
* @param Request $request
5864
* @param TokenFactory $tokenFactory
5965
* @param IntegrationServiceInterface $integrationService
60-
* @param DateTime|null $dateTime
61-
* @param Date|null $date
62-
* @param OauthHelper|null $oauthHelper
66+
* @param AuthorizationConfig|null $authorizationConfig
6367
*/
6468
public function __construct(
6569
Request $request,
6670
TokenFactory $tokenFactory,
67-
IntegrationServiceInterface $integrationService
71+
IntegrationServiceInterface $integrationService,
72+
?AuthorizationConfig $authorizationConfig = null
6873
) {
6974
$this->request = $request;
7075
$this->tokenFactory = $tokenFactory;
7176
$this->integrationService = $integrationService;
77+
$this->authorizationConfig = $authorizationConfig ?? ObjectManager::getInstance()
78+
->get(AuthorizationConfig::class);
7279
}
7380

7481
/**
@@ -110,10 +117,11 @@ private function processRequest() //phpcs:ignore CopyPaste
110117
return;
111118
}
112119
$tokenType = strtolower($headerPieces[0]);
113-
if ($tokenType !== 'bearer') {
120+
if ($tokenType !== 'bearer' || !$this->authorizationConfig->isIntegrationAsBearerEnabled()) {
114121
$this->isRequestProcessed = true;
115122
return;
116123
}
124+
117125
$bearerToken = $headerPieces[1];
118126

119127
/** @var Token $token */

app/code/Magento/Webapi/etc/webapi_soap/di.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<type name="Magento\Webapi\Model\Authorization\OauthUserContext">
10-
<arguments>
11-
<argument name="request" xsi:type="object">Magento\Webapi\Controller\Soap\Request</argument>
12-
</arguments>
13-
</type>
149
<type name="Magento\Authorization\Model\CompositeUserContext">
1510
<arguments>
1611
<argument name="userContexts" xsi:type="array">
17-
<item name="soapUserContext" xsi:type="array">
18-
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\SoapUserContext</item>
19-
<item name="sortOrder" xsi:type="string">9</item>
12+
<item name="oauthUserContext" xsi:type="array">
13+
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\OauthUserContext</item>
14+
<item name="sortOrder" xsi:type="string">5</item>
2015
</item>
2116
<item name="tokenUserContext" xsi:type="array">
2217
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\TokenUserContext</item>
2318
<item name="sortOrder" xsi:type="string">10</item>
2419
</item>
20+
<item name="soapUserContext" xsi:type="array">
21+
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\SoapUserContext</item>
22+
<item name="sortOrder" xsi:type="string">15</item>
23+
</item>
2524
<item name="guestUserContext" xsi:type="array">
2625
<item name="type" xsi:type="object">Magento\Webapi\Model\Authorization\GuestUserContext</item>
2726
<item name="sortOrder" xsi:type="string">100</item>

dev/tests/api-functional/testsuite/Magento/Webapi/WsdlGenerationFromDataObjectTest.php

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Webapi;
88

9+
use Magento\TestFramework\Authentication\OauthHelper;
10+
use Magento\TestFramework\Authentication\Rest\OauthClient;
911
use Magento\TestFramework\Helper\Bootstrap;
1012

1113
/**
@@ -33,6 +35,39 @@ protected function setUp(): void
3335
parent::setUp();
3436
}
3537

38+
public function testDisabledIntegrationAsBearer()
39+
{
40+
$wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV1,testModule5AllSoapAndRestV2';
41+
$accessCredentials = \Magento\TestFramework\Authentication\OauthHelper::getApiAccessCredentials()['key'];
42+
$connection = curl_init($wsdlUrl);
43+
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
44+
curl_setopt($connection, CURLOPT_HTTPHEADER, ['header' => "Authorization: Bearer " . $accessCredentials]);
45+
$responseContent = curl_exec($connection);
46+
$this->assertEquals(curl_getinfo($connection, CURLINFO_HTTP_CODE), 401);
47+
$this->assertStringContainsString(
48+
"The consumer isn't authorized to access %resources.",
49+
htmlspecialchars_decode($responseContent, ENT_QUOTES)
50+
);
51+
}
52+
53+
public function testAuthenticationWithOAuth()
54+
{
55+
$wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV2';
56+
$this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}?services=testModule5AllSoapAndRestV2";
57+
$this->isSingleService = true;
58+
59+
$connection = curl_init($wsdlUrl);
60+
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
61+
curl_setopt($connection, CURLOPT_HTTPHEADER, ['header' => $this->getAuthHeader($wsdlUrl)]);
62+
$responseContent = curl_exec($connection);
63+
$this->assertEquals(curl_getinfo($connection, CURLINFO_HTTP_CODE), 200);
64+
$wsdlContent = $this->_convertXmlToString($responseContent);
65+
$this->checkAll($wsdlContent);
66+
}
67+
68+
/**
69+
* @magentoConfigFixture default_store oauth/consumer/enable_integration_as_bearer 1
70+
*/
3671
public function testMultiServiceWsdl()
3772
{
3873
$this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}"
@@ -41,27 +76,20 @@ public function testMultiServiceWsdl()
4176
$wsdlContent = $this->_convertXmlToString($this->_getWsdlContent($wsdlUrl));
4277
$this->isSingleService = false;
4378

44-
$this->_checkTypesDeclaration($wsdlContent);
45-
$this->_checkPortTypeDeclaration($wsdlContent);
46-
$this->_checkBindingDeclaration($wsdlContent);
47-
$this->_checkServiceDeclaration($wsdlContent);
48-
$this->_checkMessagesDeclaration($wsdlContent);
49-
$this->_checkFaultsDeclaration($wsdlContent);
79+
$this->checkAll($wsdlContent);
5080
}
5181

82+
/**
83+
* @magentoConfigFixture default_store oauth/consumer/enable_integration_as_bearer 1
84+
*/
5285
public function testSingleServiceWsdl()
5386
{
5487
$this->_soapUrl = "{$this->_baseUrl}/soap/{$this->_storeCode}?services=testModule5AllSoapAndRestV2";
5588
$wsdlUrl = $this->_getBaseWsdlUrl() . 'testModule5AllSoapAndRestV2';
5689
$wsdlContent = $this->_convertXmlToString($this->_getWsdlContent($wsdlUrl));
5790
$this->isSingleService = true;
5891

59-
$this->_checkTypesDeclaration($wsdlContent);
60-
$this->_checkPortTypeDeclaration($wsdlContent);
61-
$this->_checkBindingDeclaration($wsdlContent);
62-
$this->_checkServiceDeclaration($wsdlContent);
63-
$this->_checkMessagesDeclaration($wsdlContent);
64-
$this->_checkFaultsDeclaration($wsdlContent);
92+
$this->checkAll($wsdlContent);
6593
}
6694

6795
public function testNoAuthorizedServices()
@@ -983,4 +1011,28 @@ protected function _checkFaultsComplexTypeSection($wsdlContent)
9831011
'Details wrapped errors (array of wrapped errors) complex types declaration is invalid.'
9841012
);
9851013
}
1014+
1015+
private function getAuthHeader(string $url): string
1016+
{
1017+
$accessCredentials = OauthHelper::getApiAccessCredentials();
1018+
/** @var OauthClient $oAuthClient */
1019+
$oAuthClient = $accessCredentials['oauth_client'];
1020+
return $oAuthClient->buildOauthAuthorizationHeader(
1021+
$url,
1022+
$accessCredentials['key'],
1023+
$accessCredentials['secret'],
1024+
[],
1025+
'GET'
1026+
)[0];
1027+
}
1028+
1029+
private function checkAll(string $wsdlContent): void
1030+
{
1031+
$this->_checkTypesDeclaration($wsdlContent);
1032+
$this->_checkPortTypeDeclaration($wsdlContent);
1033+
$this->_checkBindingDeclaration($wsdlContent);
1034+
$this->_checkServiceDeclaration($wsdlContent);
1035+
$this->_checkMessagesDeclaration($wsdlContent);
1036+
$this->_checkFaultsDeclaration($wsdlContent);
1037+
}
9861038
}

0 commit comments

Comments
 (0)