Skip to content

Commit c4b18a1

Browse files
committed
MAGETWO-65374: [GitHub][PR] Fix OAuth request helper to support Authorization header value parsing with non-leading OAuth key #8158
- Merge Pull Request #8158 from careys7/magento2:fix-oauth-header-value-processing
2 parents a005167 + 717899a commit c4b18a1

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

lib/internal/Magento/Framework/Oauth/Helper/Request.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ protected function _isProtocolParameter($attrName)
144144
*/
145145
protected function _processHeader($authHeaderValue, &$protocolParams)
146146
{
147-
if ($authHeaderValue && 'oauth' === strtolower(substr($authHeaderValue, 0, 5))) {
148-
$authHeaderValue = substr($authHeaderValue, 6);
149-
// ignore 'OAuth ' at the beginning
150-
147+
$oauthValuePosition = stripos(($authHeaderValue ? $authHeaderValue : ''), 'oauth ');
148+
if ($authHeaderValue && $oauthValuePosition !== false) {
149+
// Ignore anything before and including 'OAuth ' (trailing values validated later)
150+
$authHeaderValue = substr($authHeaderValue, $oauthValuePosition + 6);
151151
foreach (explode(',', $authHeaderValue) as $paramStr) {
152152
$nameAndValue = explode('=', trim($paramStr), 2);
153153

lib/internal/Magento/Framework/Oauth/Test/Unit/Helper/RequestTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace Magento\Framework\Oauth\Test\Unit\Helper;
99

10+
use Magento\Framework\App\Request\Http;
1011
use Magento\Framework\Phrase;
1112

1213
class RequestTest extends \PHPUnit_Framework_TestCase
@@ -120,4 +121,70 @@ public function hostsDataProvider()
120121
]
121122
];
122123
}
124+
125+
/**
126+
* Test that the OAuth parameters are correctly extracted from the Authorization header.
127+
*
128+
* @param $authHeaderValue
129+
* @param $expectedParams
130+
* @dataProvider dataProviderForTestPrepareRequestOAuthHeader
131+
*/
132+
public function testPrepareRequestOAuthHeader($authHeaderValue, $expectedParams)
133+
{
134+
$httpRequestMock = $this->getMockBuilder(Http::class)
135+
->disableOriginalConstructor()
136+
->getMock();
137+
138+
$httpRequestMock->expects($this->once())->method('getScheme')->willReturn('https');
139+
$httpRequestMock->expects($this->once())->method('getHttpHost')->willReturn('example.com');
140+
$httpRequestMock->expects($this->once())->method('getRequestUri')->willReturn('/');
141+
142+
$httpRequestMock->expects($this->any())
143+
->method('getHeader')
144+
->willReturnCallback(function ($header) use ($authHeaderValue) {
145+
switch ($header) {
146+
case 'Authorization':
147+
return $authHeaderValue;
148+
case \Zend_Http_Client::CONTENT_TYPE:
149+
return \Zend_Http_Client::ENC_URLENCODED;
150+
default:
151+
return null;
152+
}
153+
});
154+
155+
$this->assertEquals($expectedParams, $this->oauthRequestHelper->prepareRequest($httpRequestMock));
156+
}
157+
158+
/**
159+
* @return array
160+
*/
161+
public function dataProviderForTestPrepareRequestOAuthHeader()
162+
{
163+
return [
164+
[
165+
null,
166+
[]
167+
],
168+
[
169+
'',
170+
[]
171+
],
172+
[
173+
'OAuth oauth_consumer_key="x",oauth_token="x", Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=',
174+
['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
175+
],
176+
[
177+
'Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=, OAuth oauth_consumer_key="x",oauth_token="x"',
178+
['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
179+
],
180+
[
181+
'Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=, oauth oauth_consumer_key="x", oauth_token="x"',
182+
['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
183+
],
184+
[
185+
'oauth oauth_consumer_key="x", oauth_token="x", Basic d2luZHNvcm0yOldpTmRzb1JTbWlUSDAwMTQ=',
186+
['oauth_consumer_key' => 'x', 'oauth_token' => 'x']
187+
]
188+
];
189+
}
123190
}

0 commit comments

Comments
 (0)