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

Commit 9e397da

Browse files
committed
:octocat:
1 parent a606f08 commit 9e397da

10 files changed

+271
-363
lines changed

src/Core/AccessToken.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ class AccessToken extends SettingsContainerAbstract{
4646
*/
4747
public const EXPIRY_MAX = 86400 * 365;
4848

49-
/**
50-
* @var string
51-
*/
52-
protected $requestToken;
53-
54-
/**
55-
* @var string
56-
*/
57-
protected $requestTokenSecret;
58-
5949
/**
6050
* @var string
6151
*/
@@ -99,28 +89,6 @@ public function __construct(iterable $properties = null){
9989
$this->setExpiry($this->expires);
10090
}
10191

102-
/**
103-
* @return void
104-
*/
105-
public function __destruct(){
106-
107-
if(!function_exists('sodium_memzero')){
108-
return; // @codeCoverageIgnore
109-
}
110-
111-
foreach(array_keys(get_object_vars($this)) as $key){
112-
113-
if(is_scalar($this->{$key})){
114-
$this->{$key} = (string)$this->{$key};
115-
116-
sodium_memzero($this->{$key});
117-
}
118-
119-
unset($this->{$key});
120-
}
121-
122-
}
123-
12492
/**
12593
* AccessToken setter
12694
*

src/Core/OAuth1Interface.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,9 @@ public function getRequestToken():AccessToken;
2222
/**
2323
* @param string $token
2424
* @param string $verifier
25-
* @param string|null $tokenSecret
2625
*
2726
* @return \chillerlan\OAuth\Core\AccessToken
2827
*/
29-
public function getAccessToken(string $token, string $verifier, string $tokenSecret = null):AccessToken;
30-
31-
/**
32-
* @param string $url
33-
* @param array $params
34-
* @param string $method
35-
*
36-
* @return string
37-
*/
38-
public function getSignature(string $url, array $params, string $method = null):string;
28+
public function getAccessToken(string $token, string $verifier):AccessToken;
3929

4030
}

src/Core/OAuth1Provider.php

Lines changed: 56 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
namespace chillerlan\OAuth\Core;
1414

1515
use chillerlan\HTTP\Psr7;
16-
use Psr\Http\Message\ResponseInterface;
1716
use DateTime;
17+
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
1818

1919
abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
2020

@@ -23,39 +23,47 @@ abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
2323
*/
2424
protected $requestTokenURL;
2525

26-
/**
27-
* @var string
28-
*/
29-
protected $tokenSecret;
30-
3126
/**
3227
* @param array $params
3328
*
34-
* @return string
29+
* @return \Psr\Http\Message\UriInterface
3530
*/
36-
public function getAuthURL(array $params = null):string{
31+
public function getAuthURL(array $params = null):UriInterface{
3732

3833
$params = array_merge(
3934
$params ?? [],
40-
['oauth_token' => $this->getRequestToken()->requestToken]
35+
['oauth_token' => $this->getRequestToken()->accessToken]
4136
);
4237

43-
return $this->authURL.'?'.Psr7\build_http_query($params);
38+
return $this->uriFactory->createUri(Psr7\merge_query($this->authURL, $params));
4439
}
4540

4641
/**
4742
* @return \chillerlan\OAuth\Core\AccessToken
4843
*/
4944
public function getRequestToken():AccessToken{
50-
$params = $this->getRequestTokenHeaderParams();
51-
$headers = array_merge($this->authHeaders, [
52-
'Authorization' => 'OAuth '.Psr7\build_http_query($params, true, ', ', '"')
53-
]);
5445

55-
return $this->parseTokenResponse(
56-
$this->http->request($this->requestTokenURL, 'POST', null, null, $headers),
57-
true
58-
);
46+
$params = [
47+
'oauth_callback' => $this->options->callbackURL,
48+
'oauth_consumer_key' => $this->options->key,
49+
'oauth_nonce' => $this->nonce(),
50+
'oauth_signature_method' => 'HMAC-SHA1',
51+
'oauth_timestamp' => (new DateTime())->format('U'),
52+
'oauth_version' => '1.0',
53+
];
54+
55+
$params['oauth_signature'] = $this->getSignature($this->requestTokenURL, $params, 'POST');
56+
57+
$request = $this->requestFactory
58+
->createRequest('POST', $this->requestTokenURL)
59+
->withHeader('Authorization', 'OAuth '.Psr7\build_http_query($params, true, ', ', '"'));
60+
;
61+
62+
foreach($this->authHeaders as $header => $value){
63+
$request = $request->withAddedHeader($header, $value);
64+
}
65+
66+
return $this->parseTokenResponse($this->http->sendRequest($request), true);
5967
}
6068

6169
/**
@@ -78,19 +86,15 @@ protected function parseTokenResponse(ResponseInterface $response, bool $checkCa
7886
throw new ProviderException('token missing');
7987
}
8088

81-
if(($checkCallbackConfirmed ?? false)
82-
&& (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')
83-
){
89+
if($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){
8490
throw new ProviderException('oauth callback unconfirmed');
8591
}
8692

8793
$token = new AccessToken([
88-
'provider' => $this->serviceName,
89-
'requestToken' => $data['oauth_token'],
90-
'requestTokenSecret' => $data['oauth_token_secret'],
91-
'accessToken' => $data['oauth_token'],
92-
'accessTokenSecret' => $data['oauth_token_secret'],
93-
'expires' => AccessToken::EOL_NEVER_EXPIRES,
94+
'provider' => $this->serviceName,
95+
'accessToken' => $data['oauth_token'],
96+
'accessTokenSecret' => $data['oauth_token_secret'],
97+
'expires' => AccessToken::EOL_NEVER_EXPIRES,
9498
]);
9599

96100
unset($data['oauth_token'], $data['oauth_token_secret']);
@@ -111,36 +115,20 @@ protected function nonce():string{
111115
$nonce = random_bytes(32);
112116

113117
// use the sodium extension if available
118+
/** @noinspection PhpComposerExtensionStubsInspection */
114119
return function_exists('sodium_bin2hex') ? sodium_bin2hex($nonce) : bin2hex($nonce);
115120
}
116121

117-
/**
118-
* @return array
119-
*/
120-
protected function getRequestTokenHeaderParams():array{
121-
$params = [
122-
'oauth_callback' => $this->options->callbackURL,
123-
'oauth_consumer_key' => $this->options->key,
124-
'oauth_nonce' => $this->nonce(),
125-
'oauth_signature_method' => 'HMAC-SHA1',
126-
'oauth_timestamp' => (new DateTime())->format('U'),
127-
'oauth_version' => '1.0',
128-
];
129-
130-
$params['oauth_signature'] = $this->getSignature($this->requestTokenURL, $params);
131-
132-
return $params;
133-
}
134-
135122
/**
136123
* @param string $url
137124
* @param array $params
138125
* @param string $method
126+
* @param string $accessTokenSecret
139127
*
140128
* @return string
141129
* @throws \chillerlan\OAuth\Core\ProviderException
142130
*/
143-
public function getSignature(string $url, array $params, string $method = null):string{
131+
protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
144132
$parseURL = parse_url($url);
145133

146134
if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){
@@ -149,132 +137,60 @@ public function getSignature(string $url, array $params, string $method = null):
149137

150138
parse_str($parseURL['query'] ?? '', $query);
151139

152-
$data = $this->getSignatureData(
153-
$parseURL['scheme'].'://'.$parseURL['host'].($parseURL['path'] ?? ''),
154-
array_merge($query, $params),
155-
$method ?? 'POST'
156-
);
157-
158-
$key = implode('&', Psr7\raw_urlencode([$this->options->secret, $this->tokenSecret ?? '']));
159-
160-
return base64_encode(hash_hmac('sha1', $data, $key, true));
161-
}
162-
163-
/**
164-
* @param string $method
165-
* @param string $signatureURL
166-
* @param array $signatureParams
167-
*
168-
* @return string
169-
*/
170-
protected function getSignatureData(string $signatureURL, array $signatureParams, string $method){
140+
$signatureParams = array_merge($query, $params);
171141

172142
if(isset($signatureParams['oauth_signature'])){
173143
unset($signatureParams['oauth_signature']);
174144
}
175145

176-
$data = [
177-
strtoupper($method),
178-
$signatureURL,
146+
$key = implode('&', Psr7\raw_urlencode([$this->options->secret, $accessTokenSecret ?? '']));
147+
$data = Psr7\raw_urlencode([
148+
strtoupper($method ?? 'POST'),
149+
$parseURL['scheme'].'://'.$parseURL['host'].($parseURL['path'] ?? ''),
179150
Psr7\build_http_query($signatureParams),
180-
];
151+
]);
181152

182-
return implode('&', Psr7\raw_urlencode($data));
153+
return base64_encode(hash_hmac('sha1', implode('&', $data), $key, true));
183154
}
184155

185156
/**
186-
* @param string $token
187-
* @param string $verifier
188-
* @param string|null $tokenSecret
157+
* @param string $token
158+
* @param string $verifier
189159
*
190160
* @return \chillerlan\OAuth\Core\AccessToken
191161
*/
192-
public function getAccessToken(string $token, string $verifier, string $tokenSecret = null):AccessToken{
193-
$this->tokenSecret = $tokenSecret;
194-
195-
if(empty($this->tokenSecret)){
196-
$this->tokenSecret = $this->storage->getAccessToken($this->serviceName)->requestTokenSecret;
197-
}
162+
public function getAccessToken(string $token, string $verifier):AccessToken{
163+
$request = $this->requestFactory
164+
->createRequest('POST', Psr7\merge_query($this->accessTokenURL, ['oauth_verifier' => $verifier]));
198165

199-
$body = ['oauth_verifier' => $verifier];
166+
$request = $this->getRequestAuthorization($request, $this->storage->getAccessToken($this->serviceName));
200167

201-
return $this->parseTokenResponse(
202-
$this->http->request($this->accessTokenURL, 'POST', null, $body, $this->getAccessTokenHeaders($body))
203-
);
204-
}
205-
206-
/**
207-
* @param array $body
208-
*
209-
* @return array
210-
*/
211-
protected function getAccessTokenHeaders(array $body):array{
212-
return $this->requestHeaders($this->storage->getAccessToken($this->serviceName), $this->accessTokenURL, 'POST', $body, []);
168+
return $this->parseTokenResponse($this->http->sendRequest($request));
213169
}
214170

215171
/**
172+
* @param \Psr\Http\Message\RequestInterface $request
216173
* @param \chillerlan\OAuth\Core\AccessToken $token
217-
* @param string $url
218-
* @param string $method
219-
* @param array|string $params
220-
* @param array $headers
221174
*
222-
* @return array
223-
* @throws \Exception
175+
* @return \Psr\Http\Message\RequestInterface
224176
*/
225-
protected function requestHeaders(AccessToken $token, string $url, string $method, $params = null, array $headers = null):array{
226-
$this->tokenSecret = $token->accessTokenSecret;
227-
$parameters = $this->requestHeaderParams($token);
177+
public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
178+
$u = $request->getUri();
228179

229-
$parameters['oauth_signature'] = $this->getSignature($url, array_merge($params ?? [], $parameters), $method);
180+
parse_str($u->getQuery(), $p);
230181

231-
if(isset($params['oauth_session_handle'])){
232-
$parameters['oauth_session_handle'] = $params['oauth_session_handle'];
233-
}
234-
235-
return array_merge($headers ?? [], $this->apiHeaders, [
236-
'Authorization' => 'OAuth '.Psr7\build_http_query($parameters, true, ', ', '"')
237-
]);
238-
}
239-
240-
/**
241-
* @param \chillerlan\OAuth\Core\AccessToken $token
242-
*
243-
* @return array
244-
* @throws \Exception
245-
*/
246-
protected function requestHeaderParams(AccessToken $token):array{
247-
return [
182+
$parameters = [
248183
'oauth_consumer_key' => $this->options->key,
249184
'oauth_nonce' => $this->nonce(),
250185
'oauth_signature_method' => 'HMAC-SHA1',
251186
'oauth_timestamp' => (new DateTime)->format('U'),
252187
'oauth_token' => $token->accessToken,
253188
'oauth_version' => '1.0',
254189
];
255-
}
256190

257-
/**
258-
* @param string $path
259-
* @param array $params
260-
* @param string $method
261-
* @param null $body
262-
* @param array $headers
263-
*
264-
* @return \Psr\Http\Message\ResponseInterface
265-
*/
266-
public function request(string $path, array $params = null, string $method = null, $body = null, array $headers = null):ResponseInterface{
267-
$method = $method ?? 'GET';
268-
269-
$headers = $this->requestHeaders(
270-
$this->storage->getAccessToken($this->serviceName),
271-
$this->apiURL.$path,
272-
$method,
273-
$body ?? $params,
274-
$headers
275-
);
191+
$parameters['oauth_signature'] = $this->getSignature((string)$u->withQuery('')->withFragment(''), array_merge($p, $parameters), $request->getMethod(), $token->accessTokenSecret);
276192

277-
return $this->http->request($this->apiURL.$path, $method, $params, $body, $headers);
193+
return $request->withHeader('Authorization', 'OAuth '.Psr7\build_http_query($parameters, true, ', ', '"'));
278194
}
279195

280196
}

0 commit comments

Comments
 (0)