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

Commit 3cd5c75

Browse files
committed
:octocat: chop up abstract providers into smaller bits
1 parent 9ac7dc5 commit 3cd5c75

File tree

7 files changed

+234
-263
lines changed

7 files changed

+234
-263
lines changed

src/Core/OAuth1Provider.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
use function array_merge, base64_encode, hash_hmac, implode, sprintf, strtoupper, time;
1818

1919
/**
20-
* Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
21-
22-
* @see https://datatracker.ietf.org/doc/html/rfc5849
20+
* Implements an abstract OAuth1 (1.0a) provider with all methods required by the OAuth1Interface.
21+
*
22+
* @see https://oauth.net/core/1.0a/
23+
* @see https://datatracker.ietf.org/doc/html/rfc5849
2324
*/
2425
abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
2526

@@ -32,15 +33,19 @@ abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
3233
* @inheritDoc
3334
*/
3435
public function getAuthURL(array|null $params = null, array|null $scopes = null):UriInterface{
35-
$params = array_merge(($params ?? []), ['oauth_token' => $this->getRequestToken()->accessToken]);
36+
$response = $this->sendRequestTokenRequest($this->requestTokenURL);
37+
$token = $this->parseTokenResponse($response, true);
38+
$params = array_merge(($params ?? []), ['oauth_token' => $token->accessToken]);
3639

3740
return $this->uriFactory->createUri(QueryUtil::merge($this->authURL, $params));
3841
}
3942

4043
/**
41-
* @inheritDoc
44+
* prepares the parameters for the request token request header
45+
*
46+
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-2.1
4247
*/
43-
public function getRequestToken():AccessToken{
48+
protected function getRequestTokenRequestParams():array{
4449

4550
$params = [
4651
'oauth_callback' => $this->options->callbackURL,
@@ -53,19 +58,22 @@ public function getRequestToken():AccessToken{
5358

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

56-
return $this->parseTokenResponse($this->sendRequestTokenRequest($params), true);
61+
return $params;
5762
}
5863

5964
/**
60-
* Sends a request to the request token endpoint with the given params
65+
* Sends a request to the request token endpoint
6166
*/
62-
protected function sendRequestTokenRequest(array $requestTokenRequestParams):ResponseInterface{
67+
protected function sendRequestTokenRequest(string $url):ResponseInterface{
68+
$params = $this->getRequestTokenRequestParams();
6369

6470
$request = $this->requestFactory
65-
->createRequest('POST', $this->requestTokenURL)
66-
->withHeader('Authorization', 'OAuth '.QueryUtil::build($requestTokenRequestParams, null, ', ', '"'))
67-
->withHeader('Accept-Encoding', 'identity') // try to avoid compression
68-
->withHeader('Content-Length', '0') // tumblr requires a content-length header set
71+
->createRequest('POST', $url)
72+
->withHeader('Authorization', 'OAuth '.QueryUtil::build($params, null, ', ', '"'))
73+
// try to avoid compression
74+
->withHeader('Accept-Encoding', 'identity')
75+
// tumblr requires a content-length header set
76+
->withHeader('Content-Length', '0')
6977
;
7078

7179
foreach($this::HEADERS_AUTH as $header => $value){
@@ -78,25 +86,29 @@ protected function sendRequestTokenRequest(array $requestTokenRequestParams):Res
7886
/**
7987
* Parses the response from a request to the token endpoint
8088
*
89+
* Note: "oauth_callback_confirmed" is only sent in request token response
90+
*
8191
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-2.1
8292
* @see https://datatracker.ietf.org/doc/html/rfc5849#section-2.3
8393
*
8494
* @throws \chillerlan\OAuth\Providers\ProviderException
8595
*/
86-
protected function parseTokenResponse(ResponseInterface $response, bool $checkCallback):AccessToken{
96+
protected function parseTokenResponse(ResponseInterface $response, bool $confirmCallback = false):AccessToken{
8797
$data = QueryUtil::parse(MessageUtil::decompress($response));
8898

8999
if(empty($data)){
90100
throw new ProviderException('unable to parse token response');
91101
}
92-
elseif(isset($data['error'])){
102+
103+
if(isset($data['error'])){
93104
throw new ProviderException(sprintf('error retrieving access token: "%s"', $data['error']));
94105
}
95-
elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
106+
107+
if(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
96108
throw new ProviderException('invalid token');
97109
}
98110

99-
if($checkCallback && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){
111+
if($confirmCallback && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){
100112
throw new ProviderException('oauth callback unconfirmed');
101113
}
102114

@@ -161,21 +173,25 @@ public function getAccessToken(string $requestToken, string $verifier):AccessTok
161173
throw new ProviderException('request token mismatch');
162174
}
163175

164-
return $this->parseTokenResponse($this->sendAccessTokenRequest($token, $verifier), false);
176+
$response = $this->sendAccessTokenRequest($verifier);
177+
178+
return $this->parseTokenResponse($response);
165179
}
166180

167181
/**
168182
* Sends the access token request
169183
*/
170-
protected function sendAccessTokenRequest(AccessToken $token, string $verifier):ResponseInterface{
184+
protected function sendAccessTokenRequest(string $verifier):ResponseInterface{
171185

172186
$request = $this->requestFactory
173187
->createRequest('POST', QueryUtil::merge($this->accessTokenURL, ['oauth_verifier' => $verifier]))
174188
->withHeader('Accept-Encoding', 'identity')
175189
->withHeader('Content-Length', '0')
176190
;
177191

178-
return $this->http->sendRequest($this->getRequestAuthorization($request, $token));
192+
$request = $this->getRequestAuthorization($request);
193+
194+
return $this->http->sendRequest($request);
179195
}
180196

181197
/**
@@ -205,7 +221,7 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken|n
205221
$params['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore
206222
}
207223

208-
return $request->withHeader('Authorization', 'OAuth '.QueryUtil::build($params, null, ', ', '"'));
224+
return $request->withHeader('Authorization', sprintf('OAuth %s', QueryUtil::build($params, null, ', ', '"')));
209225
}
210226

211227
}

0 commit comments

Comments
 (0)