1212
1313namespace chillerlan \OAuth \Core ;
1414
15- use chillerlan \HTTP \HTTPResponseInterface ;
15+ use chillerlan \HTTP \Psr7 ;
16+ use Psr \Http \Message \ResponseInterface ;
1617use DateTime ;
1718
1819abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
@@ -32,44 +33,40 @@ abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
3233 *
3334 * @return string
3435 */
35- public function getAuthURL (array $ params = null ):string {
36+ public function getAuthURL (array $ params = null ):string {
3637
3738 $ params = array_merge (
3839 $ params ?? [],
3940 ['oauth_token ' => $ this ->getRequestToken ()->requestToken ]
4041 );
4142
42- return $ this ->authURL .'? ' .$ this -> httpBuildQuery ($ params );
43+ return $ this ->authURL .'? ' .Psr7 \build_http_query ($ params );
4344 }
4445
4546 /**
4647 * @return \chillerlan\OAuth\Core\AccessToken
4748 */
48- public function getRequestToken ():AccessToken {
49- $ params = $ this ->getRequestTokenHeaderParams ();
49+ public function getRequestToken ():AccessToken {
50+ $ params = $ this ->getRequestTokenHeaderParams ();
51+ $ headers = array_merge ($ this ->authHeaders , [
52+ 'Authorization ' => 'OAuth ' .Psr7 \build_http_query ($ params , true , ', ' , '" ' )
53+ ]);
5054
5155 return $ this ->parseTokenResponse (
52- $ this ->httpPOST (
53- $ this ->requestTokenURL ,
54- [],
55- null ,
56- array_merge ($ this ->authHeaders , [
57- 'Authorization ' => 'OAuth ' .$ this ->httpBuildQuery ($ params , true , ', ' , '" ' )
58- ])
59- ),
56+ $ this ->http ->request ($ this ->requestTokenURL , 'POST ' , null , null , $ headers ),
6057 true
6158 );
6259 }
6360
6461 /**
65- * @param \chillerlan\HTTP\HTTPResponseInterface $response
66- * @param bool|null $checkCallbackConfirmed
62+ * @param \Psr\Http\Message\ResponseInterface $response
63+ * @param bool|null $checkCallbackConfirmed
6764 *
6865 * @return \chillerlan\OAuth\Core\AccessToken
6966 * @throws \chillerlan\OAuth\Core\ProviderException
7067 */
71- protected function parseTokenResponse (HTTPResponseInterface $ response , bool $ checkCallbackConfirmed = null ):AccessToken {
72- parse_str ($ response ->body , $ data );
68+ protected function parseTokenResponse (ResponseInterface $ response , bool $ checkCallbackConfirmed = null ):AccessToken {
69+ parse_str ($ response ->getBody ()-> getContents () , $ data );
7370
7471 if (!$ data || !is_array ($ data )){
7572 throw new ProviderException ('unable to parse token response ' );
@@ -110,7 +107,7 @@ protected function parseTokenResponse(HTTPResponseInterface $response, bool $che
110107 *
111108 * @return string
112109 */
113- protected function nonce ():string {
110+ protected function nonce ():string {
114111 $ nonce = random_bytes (32 );
115112
116113 // use the sodium extension if available
@@ -120,7 +117,7 @@ protected function nonce():string {
120117 /**
121118 * @return array
122119 */
123- protected function getRequestTokenHeaderParams ():array {
120+ protected function getRequestTokenHeaderParams ():array {
124121 $ params = [
125122 'oauth_callback ' => $ this ->options ->callbackURL ,
126123 'oauth_consumer_key ' => $ this ->options ->key ,
@@ -143,7 +140,7 @@ protected function getRequestTokenHeaderParams():array {
143140 * @return string
144141 * @throws \chillerlan\OAuth\Core\ProviderException
145142 */
146- public function getSignature (string $ url , array $ params , string $ method = null ):string {
143+ public function getSignature (string $ url , array $ params , string $ method = null ):string {
147144 $ parseURL = parse_url ($ url );
148145
149146 if (!isset ($ parseURL ['host ' ]) || !isset ($ parseURL ['scheme ' ]) || !in_array ($ parseURL ['scheme ' ], ['http ' , 'https ' ], true )){
@@ -158,7 +155,7 @@ public function getSignature(string $url, array $params, string $method = null):
158155 $ method ?? 'POST '
159156 );
160157
161- $ key = implode ('& ' , $ this -> rawurlencode ([$ this ->options ->secret , $ this ->tokenSecret ?? '' ]));
158+ $ key = implode ('& ' , Psr7 \raw_urlencode ([$ this ->options ->secret , $ this ->tokenSecret ?? '' ]));
162159
163160 return base64_encode (hash_hmac ('sha1 ' , $ data , $ key , true ));
164161 }
@@ -179,10 +176,10 @@ protected function getSignatureData(string $signatureURL, array $signatureParams
179176 $ data = [
180177 strtoupper ($ method ),
181178 $ signatureURL ,
182- $ this -> httpBuildQuery ($ signatureParams ),
179+ Psr7 \build_http_query ($ signatureParams ),
183180 ];
184181
185- return implode ('& ' , $ this -> rawurlencode ($ data ));
182+ return implode ('& ' , Psr7 \raw_urlencode ($ data ));
186183 }
187184
188185 /**
@@ -192,7 +189,7 @@ protected function getSignatureData(string $signatureURL, array $signatureParams
192189 *
193190 * @return \chillerlan\OAuth\Core\AccessToken
194191 */
195- public function getAccessToken (string $ token , string $ verifier , string $ tokenSecret = null ):AccessToken {
192+ public function getAccessToken (string $ token , string $ verifier , string $ tokenSecret = null ):AccessToken {
196193 $ this ->tokenSecret = $ tokenSecret ;
197194
198195 if (empty ($ this ->tokenSecret )){
@@ -202,7 +199,7 @@ public function getAccessToken(string $token, string $verifier, string $tokenSec
202199 $ body = ['oauth_verifier ' => $ verifier ];
203200
204201 return $ this ->parseTokenResponse (
205- $ this ->httpPOST ($ this ->accessTokenURL , [] , $ body , $ this ->getAccessTokenHeaders ($ body ))
202+ $ this ->http -> request ($ this ->accessTokenURL , ' POST ' , null , $ body , $ this ->getAccessTokenHeaders ($ body ))
206203 );
207204 }
208205
@@ -211,7 +208,7 @@ public function getAccessToken(string $token, string $verifier, string $tokenSec
211208 *
212209 * @return array
213210 */
214- protected function getAccessTokenHeaders (array $ body ):array {
211+ protected function getAccessTokenHeaders (array $ body ):array {
215212 return $ this ->requestHeaders ($ this ->storage ->getAccessToken ($ this ->serviceName ), $ this ->accessTokenURL , 'POST ' , $ body , []);
216213 }
217214
@@ -233,11 +230,10 @@ protected function requestHeaders(AccessToken $token, string $url, string $metho
233230
234231 if (isset ($ params ['oauth_session_handle ' ])){
235232 $ parameters ['oauth_session_handle ' ] = $ params ['oauth_session_handle ' ];
236- unset($ params ['oauth_session_handle ' ]);
237233 }
238234
239235 return array_merge ($ headers ?? [], $ this ->apiHeaders , [
240- 'Authorization ' => 'OAuth ' .$ this -> httpBuildQuery ($ parameters , true , ', ' , '" ' )
236+ 'Authorization ' => 'OAuth ' .Psr7 \build_http_query ($ parameters , true , ', ' , '" ' )
241237 ]);
242238 }
243239
@@ -247,7 +243,7 @@ protected function requestHeaders(AccessToken $token, string $url, string $metho
247243 * @return array
248244 * @throws \Exception
249245 */
250- protected function requestHeaderParams (AccessToken $ token ):array {
246+ protected function requestHeaderParams (AccessToken $ token ):array {
251247 return [
252248 'oauth_consumer_key ' => $ this ->options ->key ,
253249 'oauth_nonce ' => $ this ->nonce (),
@@ -265,9 +261,9 @@ protected function requestHeaderParams(AccessToken $token):array {
265261 * @param null $body
266262 * @param array $headers
267263 *
268- * @return \chillerlan\HTTP\HTTPResponseInterface
264+ * @return \Psr\Http\Message\ResponseInterface
269265 */
270- public function request (string $ path , array $ params = null , string $ method = null , $ body = null , array $ headers = null ):HTTPResponseInterface {
266+ public function request (string $ path , array $ params = null , string $ method = null , $ body = null , array $ headers = null ):ResponseInterface {
271267 $ method = $ method ?? 'GET ' ;
272268
273269 $ headers = $ this ->requestHeaders (
@@ -278,7 +274,7 @@ public function request(string $path, array $params = null, string $method = nul
278274 $ headers
279275 );
280276
281- return $ this ->httpRequest ($ this ->apiURL .$ path , $ params , $ method , $ body , $ headers );
277+ return $ this ->http -> request ($ this ->apiURL .$ path , $ method , $ params , $ body , $ headers );
282278 }
283279
284280}
0 commit comments