Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 4dae119

Browse files
committed
:octocat: OAuth1Provider: extract sendRequestTokenRequest() and sendAccessTokenRequest() to simplify mock tests
1 parent 0003498 commit 4dae119

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/Core/OAuth1Interface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public function getRequestToken():AccessToken;
3232
*
3333
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-2.3
3434
*/
35-
public function getAccessToken(string $token, string $verifier):AccessToken;
35+
public function getAccessToken(string $requestToken, string $verifier):AccessToken;
3636

3737
}

src/Core/OAuth1Provider.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ public function getRequestToken():AccessToken{
5353

5454
$params['oauth_signature'] = $this->getSignature($this->requestTokenURL, $params, 'POST');
5555

56+
return $this->parseTokenResponse($this->sendRequestTokenRequest($params), true);
57+
}
58+
59+
/**
60+
* Sends a request to the request token endpoint with the given params
61+
*/
62+
protected function sendRequestTokenRequest(array $requestTokenRequestParams):ResponseInterface{
63+
5664
$request = $this->requestFactory
5765
->createRequest('POST', $this->requestTokenURL)
58-
->withHeader('Authorization', 'OAuth '.QueryUtil::build($params, null, ', ', '"'))
66+
->withHeader('Authorization', 'OAuth '.QueryUtil::build($requestTokenRequestParams, null, ', ', '"'))
5967
->withHeader('Accept-Encoding', 'identity') // try to avoid compression
6068
->withHeader('Content-Length', '0') // tumblr requires a content-length header set
6169
;
@@ -64,7 +72,7 @@ public function getRequestToken():AccessToken{
6472
$request = $request->withHeader($header, $value);
6573
}
6674

67-
return $this->parseTokenResponse($this->http->sendRequest($request), true);
75+
return $this->http->sendRequest($request);
6876
}
6977

7078
/**
@@ -153,18 +161,30 @@ protected function getSignature(
153161

154162
/**
155163
* @inheritDoc
164+
* @throws \chillerlan\OAuth\Providers\ProviderException
165+
*/
166+
public function getAccessToken(string $requestToken, string $verifier):AccessToken{
167+
$token = $this->storage->getAccessToken($this->serviceName);
168+
169+
if($requestToken !== $token->accessToken){
170+
throw new ProviderException('request token mismatch');
171+
}
172+
173+
return $this->parseTokenResponse($this->sendAccessTokenRequest($token, $verifier), false);
174+
}
175+
176+
/**
177+
* Sends the access token request
156178
*/
157-
public function getAccessToken(string $token, string $verifier):AccessToken{
179+
protected function sendAccessTokenRequest(AccessToken $token, string $verifier):ResponseInterface{
158180

159181
$request = $this->requestFactory
160182
->createRequest('POST', QueryUtil::merge($this->accessTokenURL, ['oauth_verifier' => $verifier]))
161183
->withHeader('Accept-Encoding', 'identity')
162184
->withHeader('Content-Length', '0')
163185
;
164186

165-
$request = $this->getRequestAuthorization($request, $this->storage->getAccessToken($this->serviceName));
166-
167-
return $this->parseTokenResponse($this->http->sendRequest($request), false);
187+
return $this->http->sendRequest($this->getRequestAuthorization($request, $token));
168188
}
169189

170190
/**

0 commit comments

Comments
 (0)