Skip to content

Commit 06f74fa

Browse files
committed
:octocat: OAuth1Provider: move verifier to access token request header (#4)
1 parent 6dc8fa4 commit 06f74fa

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

src/Core/OAuth1Provider.php

+38-5
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,58 @@ public function getAccessToken(string $requestToken, string $verifier):AccessTok
195195
throw new ProviderException('request token mismatch');
196196
}
197197

198-
$response = $this->sendAccessTokenRequest($verifier);
198+
$params = $this->getAccessTokenRequestHeaderParams($token, $verifier);
199+
$response = $this->sendAccessTokenRequest($params);
199200

200201
return $this->parseTokenResponse($response);
201202
}
202203

204+
/**
205+
* Prepares the header params for the access token request
206+
*/
207+
protected function getAccessTokenRequestHeaderParams(AccessToken $requestToken, string $verifier):array{
208+
209+
$params = [
210+
'oauth_consumer_key' => $this->options->key,
211+
'oauth_nonce' => $this->nonce(),
212+
'oauth_signature_method' => 'HMAC-SHA1',
213+
'oauth_timestamp' => time(),
214+
'oauth_token' => $requestToken->accessToken,
215+
'oauth_version' => '1.0',
216+
'oauth_verifier' => $verifier,
217+
];
218+
219+
$params['oauth_signature'] = $this->getSignature(
220+
$this->accessTokenURL,
221+
$params,
222+
'POST',
223+
$requestToken->accessTokenSecret,
224+
);
225+
226+
return $params;
227+
}
228+
229+
/**
230+
* Adds the "Authorization" header to the given `RequestInterface` using the given array or parameters
231+
*/
232+
protected function setAuthorizationHeader(RequestInterface $request, array $params):RequestInterface{
233+
return $request->withHeader('Authorization', sprintf('OAuth %s', QueryUtil::build($params, null, ', ', '"')));
234+
}
235+
203236
/**
204237
* Sends the access token request
205238
*
206239
* @see \chillerlan\OAuth\Core\OAuth1Provider::getAccessToken()
207240
*/
208-
protected function sendAccessTokenRequest(string $verifier):ResponseInterface{
241+
protected function sendAccessTokenRequest(array $headerParams):ResponseInterface{
209242

210243
$request = $this->requestFactory
211-
->createRequest('POST', QueryUtil::merge($this->accessTokenURL, ['oauth_verifier' => $verifier]))
244+
->createRequest('POST', $this->accessTokenURL)
212245
->withHeader('Accept-Encoding', 'identity')
213246
->withHeader('Content-Length', '0')
214247
;
215248

216-
$request = $this->getRequestAuthorization($request);
249+
$request = $this->setAuthorizationHeader($request, $headerParams);
217250

218251
return $this->http->sendRequest($request);
219252
}
@@ -245,7 +278,7 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken|n
245278
$token->accessTokenSecret,
246279
);
247280

248-
return $request->withHeader('Authorization', sprintf('OAuth %s', QueryUtil::build($params, null, ', ', '"')));
281+
return $this->setAuthorizationHeader($request, $params);
249282
}
250283

251284
}

tests/Providers/Unit/OAuth1ProviderUnitTestAbstract.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ public function testParseTokenResponseConfirmCallbackException():void{
155155
* access token
156156
*/
157157

158+
public function testGetAccessTokenRequestHeaderParams():void{
159+
160+
$testRequestToken = $this->getTestToken([
161+
'accessToken' => 'test_request_token',
162+
'accessTokenSecret' => 'test_request_token_secret',
163+
]);
164+
165+
$testVerifier = '*verifier*';
166+
167+
$headerParams = $this->invokeReflectionMethod('getAccessTokenRequestHeaderParams', [$testRequestToken, $testVerifier]);
168+
169+
$this::assertArrayHasKey('oauth_verifier', $headerParams);
170+
$this::assertSame($testVerifier, $headerParams['oauth_verifier']);
171+
172+
$this::assertArrayHasKey('oauth_token', $headerParams);
173+
$this::assertSame($testRequestToken->accessToken, $headerParams['oauth_token']);
174+
175+
$this::assertArrayHasKey('oauth_consumer_key', $headerParams);
176+
$this::assertArrayHasKey('oauth_nonce', $headerParams);
177+
$this::assertArrayHasKey('oauth_signature_method', $headerParams);
178+
$this::assertArrayHasKey('oauth_timestamp', $headerParams);
179+
$this::assertArrayHasKey('oauth_signature', $headerParams);
180+
}
181+
158182
public function testGetAccessToken():void{
159183
$this->setMockResponse($this->streamFactory->createStream($this::TEST_ACCESS_TOKEN));
160184

@@ -179,16 +203,12 @@ public function testSendAccessTokenRequest():void{
179203
'expires' => AccessToken::NEVER_EXPIRES,
180204
]);
181205

182-
183206
$this->provider->storeAccessToken($requestToken);
184207

185-
$response = $this->invokeReflectionMethod('sendAccessTokenRequest', ['*verifier*']);
208+
$response = $this->invokeReflectionMethod('sendAccessTokenRequest', [['foo' => 'bar']]);
186209
$json = MessageUtil::decodeJSON($response);
187210

188-
// check if the verifier is set
189-
$this::assertSame('*verifier*', $json->request->params->{'oauth_verifier'});
190-
191-
$this::assertTrue(str_starts_with($json->headers->{'Authorization'}, 'OAuth '));
211+
$this::assertSame('OAuth foo="bar"', $json->headers->{'Authorization'});
192212
$this::assertSame('identity', $json->headers->{'Accept-Encoding'});
193213
$this::assertSame('0', $json->headers->{'Content-Length'});
194214
$this::assertSame('POST', $json->request->method);

0 commit comments

Comments
 (0)