1212
1313namespace chillerlan \OAuth \Core ;
1414
15- use chillerlan \HTTP \Utils \Query ;
16- use DateTime ;
1715use Psr \Http \Message \{RequestInterface , ResponseInterface , UriInterface };
1816
19- use function array_merge , base64_encode , bin2hex , function_exists , hash_hmac ,
20- implode , in_array , is_array , parse_url , random_bytes , strtoupper ;
21- use function chillerlan \HTTP \Utils \{decompress_content , r_rawurlencode };
17+ use function array_map , array_merge , base64_encode , bin2hex , function_exists , hash_hmac ,
18+ implode , in_array , is_array , random_bytes , strtoupper , time ;
19+ use function chillerlan \HTTP \Utils \{decompress_content , parseUrl };
2220
2321/**
2422 * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
@@ -36,7 +34,7 @@ abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
3634 public function getAuthURL (array $ params = null ):UriInterface {
3735 $ params = array_merge ($ params ?? [], ['oauth_token ' => $ this ->getRequestToken ()->accessToken ]);
3836
39- return $ this ->uriFactory ->createUri (Query:: merge ($ this ->authURL , $ params ));
37+ return $ this ->uriFactory ->createUri ($ this -> mergeQuery ($ this ->authURL , $ params ));
4038 }
4139
4240 /**
@@ -49,15 +47,15 @@ public function getRequestToken():AccessToken{
4947 'oauth_consumer_key ' => $ this ->options ->key ,
5048 'oauth_nonce ' => $ this ->nonce (),
5149 'oauth_signature_method ' => 'HMAC-SHA1 ' ,
52- 'oauth_timestamp ' => ( new DateTime )-> format ( ' U ' ),
50+ 'oauth_timestamp ' => time ( ),
5351 'oauth_version ' => '1.0 ' ,
5452 ];
5553
5654 $ params ['oauth_signature ' ] = $ this ->getSignature ($ this ->requestTokenURL , $ params , 'POST ' );
5755
5856 $ request = $ this ->requestFactory
5957 ->createRequest ('POST ' , $ this ->requestTokenURL )
60- ->withHeader ('Authorization ' , 'OAuth ' .Query:: build ($ params , null , ', ' , '" ' ))
58+ ->withHeader ('Authorization ' , 'OAuth ' .$ this -> buildQuery ($ params , null , ', ' , '" ' ))
6159 ->withHeader ('Accept-Encoding ' , 'identity ' )
6260 ->withHeader ('Content-Length ' , '0 ' ) // tumblr requires a content-length header set
6361 ;
@@ -81,7 +79,7 @@ public function getRequestToken():AccessToken{
8179 * @throws \chillerlan\OAuth\Core\ProviderException
8280 */
8381 protected function parseTokenResponse (ResponseInterface $ response , bool $ checkCallbackConfirmed = null ):AccessToken {
84- $ data = Query:: parse (decompress_content ($ response ));
82+ $ data = $ this -> parseQuery (decompress_content ($ response ));
8583
8684 if (!$ data || !is_array ($ data )){
8785 throw new ProviderException ('unable to parse token response ' );
@@ -125,6 +123,7 @@ protected function nonce():string{
125123 $ nonce = random_bytes (32 );
126124
127125 // use the sodium extension if available
126+ /** @noinspection PhpFullyQualifiedNameUsageInspection */
128127 return function_exists ('sodium_bin2hex ' )
129128 ? \sodium_bin2hex ($ nonce )
130129 : bin2hex ($ nonce );
@@ -144,23 +143,23 @@ protected function nonce():string{
144143 * @throws \chillerlan\OAuth\Core\ProviderException
145144 */
146145 protected function getSignature (string $ url , array $ params , string $ method , string $ accessTokenSecret = null ):string {
147- $ parseURL = parse_url ($ url );
146+ $ parseURL = parseUrl ($ url );
148147
149148 if (!isset ($ parseURL ['host ' ]) || !isset ($ parseURL ['scheme ' ]) || !in_array ($ parseURL ['scheme ' ], ['http ' , 'https ' ], true )){
150149 throw new ProviderException ('getSignature: invalid url ' );
151150 }
152151
153- $ query = Query:: parse ($ parseURL ['query ' ] ?? '' );
152+ $ query = $ this -> parseQuery ($ parseURL ['query ' ] ?? '' );
154153
155154 $ signatureParams = array_merge ($ query , $ params );
156155
157156 unset($ signatureParams ['oauth_signature ' ]);
158157
159- $ key = implode ('& ' , r_rawurlencode ( [$ this ->options ->secret , $ accessTokenSecret ?? '' ]));
160- $ data = r_rawurlencode ( [
158+ $ key = implode ('& ' , array_map ( ' rawurlencode ' , [$ this ->options ->secret , $ accessTokenSecret ?? '' ]));
159+ $ data = array_map ( ' rawurlencode ' , [
161160 strtoupper ($ method ?? 'POST ' ),
162161 $ parseURL ['scheme ' ].':// ' .$ parseURL ['host ' ].($ parseURL ['path ' ] ?? '' ),
163- Query:: build ($ signatureParams ),
162+ $ this -> buildQuery ($ signatureParams ),
164163 ]);
165164
166165 return base64_encode (hash_hmac ('sha1 ' , implode ('& ' , $ data ), $ key , true ));
@@ -172,7 +171,7 @@ protected function getSignature(string $url, array $params, string $method, stri
172171 public function getAccessToken (string $ token , string $ verifier ):AccessToken {
173172
174173 $ request = $ this ->requestFactory
175- ->createRequest ('POST ' , Query:: merge ($ this ->accessTokenURL , ['oauth_verifier ' => $ verifier ]))
174+ ->createRequest ('POST ' , $ this -> mergeQuery ($ this ->accessTokenURL , ['oauth_verifier ' => $ verifier ]))
176175 ->withHeader ('Accept-Encoding ' , 'identity ' )
177176 ->withHeader ('Content-Length ' , '0 ' )
178177 ;
@@ -188,13 +187,13 @@ public function getAccessToken(string $token, string $verifier):AccessToken{
188187 public function getRequestAuthorization (RequestInterface $ request , AccessToken $ token ):RequestInterface {
189188 $ uri = $ request ->getUri ();
190189
191- $ query = Query:: parse ($ uri ->getQuery ());
190+ $ query = $ this -> parseQuery ($ uri ->getQuery ());
192191
193192 $ parameters = [
194193 'oauth_consumer_key ' => $ this ->options ->key ,
195194 'oauth_nonce ' => $ this ->nonce (),
196195 'oauth_signature_method ' => 'HMAC-SHA1 ' ,
197- 'oauth_timestamp ' => ( new DateTime )-> format ( ' U ' ),
196+ 'oauth_timestamp ' => time ( ),
198197 'oauth_token ' => $ token ->accessToken ,
199198 'oauth_version ' => '1.0 ' ,
200199 ];
@@ -210,7 +209,7 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken $
210209 $ parameters ['oauth_session_handle ' ] = $ query ['oauth_session_handle ' ]; // @codeCoverageIgnore
211210 }
212211
213- return $ request ->withHeader ('Authorization ' , 'OAuth ' .Query:: build ($ parameters , null , ', ' , '" ' ));
212+ return $ request ->withHeader ('Authorization ' , 'OAuth ' .$ this -> buildQuery ($ parameters , null , ', ' , '" ' ));
214213 }
215214
216215}
0 commit comments