1414use chillerlan \HTTP \Utils \{MessageUtil , QueryUtil };
1515use chillerlan \OAuth \Providers \ProviderException ;
1616use Psr \Http \Message \{RequestInterface , ResponseInterface , UriInterface };
17- use function array_merge , base64_encode , hash_hmac , implode , in_array , random_bytes , sodium_bin2hex , strtoupper , time ;
17+ use function array_merge , base64_encode , hash_hmac , implode , random_bytes , sodium_bin2hex , sprintf , strtoupper , time ;
1818
1919/**
2020 * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
@@ -61,7 +61,7 @@ public function getRequestToken():AccessToken{
6161 ;
6262
6363 foreach ($ this ::HEADERS_AUTH as $ header => $ value ){
64- $ request = $ request ->withAddedHeader ($ header , $ value );
64+ $ request = $ request ->withHeader ($ header , $ value );
6565 }
6666
6767 return $ this ->parseTokenResponse ($ this ->http ->sendRequest ($ request ), true );
@@ -75,35 +75,31 @@ public function getRequestToken():AccessToken{
7575 *
7676 * @throws \chillerlan\OAuth\Providers\ProviderException
7777 */
78- protected function parseTokenResponse (ResponseInterface $ response , bool $ checkCallbackConfirmed ):AccessToken {
78+ protected function parseTokenResponse (ResponseInterface $ response , bool $ checkCallback ):AccessToken {
7979 $ data = QueryUtil::parse (MessageUtil::decompress ($ response ));
8080
8181 if (empty ($ data )){
8282 throw new ProviderException ('unable to parse token response ' );
8383 }
8484 elseif (isset ($ data ['error ' ])){
85- throw new ProviderException ('error retrieving access token: ' . $ data ['error ' ]);
85+ throw new ProviderException (sprintf ( 'error retrieving access token: "%s" ' , $ data ['error ' ]) );
8686 }
8787 elseif (!isset ($ data ['oauth_token ' ]) || !isset ($ data ['oauth_token_secret ' ])){
8888 throw new ProviderException ('invalid token ' );
8989 }
9090
91- if (
92- $ checkCallbackConfirmed
93- && (!isset ($ data ['oauth_callback_confirmed ' ]) || $ data ['oauth_callback_confirmed ' ] !== 'true ' )
94- ){
91+ if ($ checkCallback && (!isset ($ data ['oauth_callback_confirmed ' ]) || $ data ['oauth_callback_confirmed ' ] !== 'true ' )){
9592 throw new ProviderException ('oauth callback unconfirmed ' );
9693 }
9794
98- $ token = $ this ->createAccessToken ();
99-
95+ $ token = $ this ->createAccessToken ();
10096 $ token ->accessToken = $ data ['oauth_token ' ];
10197 $ token ->accessTokenSecret = $ data ['oauth_token_secret ' ];
10298 $ token ->expires = AccessToken::EOL_NEVER_EXPIRES ;
10399
104100 unset($ data ['oauth_token ' ], $ data ['oauth_token_secret ' ]);
105101
106- $ token ->extraParams = $ data ;
102+ $ token ->extraParams = $ data ;
107103
108104 $ this ->storage ->storeAccessToken ($ token , $ this ->serviceName );
109105
@@ -126,20 +122,28 @@ protected function nonce():string{
126122 *
127123 * @throws \chillerlan\OAuth\Providers\ProviderException
128124 */
129- protected function getSignature (string $ url , array $ params , string $ method , string |null $ accessTokenSecret = null ):string {
130- $ parsed = $ this ->uriFactory ->createUri ($ url );
125+ protected function getSignature (
126+ UriInterface |string $ url ,
127+ array $ params ,
128+ string $ method ,
129+ string |null $ accessTokenSecret = null
130+ ):string {
131+
132+ if (!$ url instanceof UriInterface){
133+ $ url = $ this ->uriFactory ->createUri ($ url );
134+ }
131135
132- if ($ parsed ->getHost () == '' || $ parsed ->getScheme () === '' || ! in_array ( $ parsed -> getScheme (), [ ' http ' , ' https ']) ){
133- throw new ProviderException ('getSignature: invalid url ' );
136+ if ($ url ->getHost () === '' || $ url ->getScheme () !== ' https ' ){
137+ throw new ProviderException (sprintf ( 'getSignature: invalid url: "%s" ' , $ url ) );
134138 }
135139
136- $ signatureParams = array_merge (QueryUtil::parse ($ parsed ->getQuery ()), $ params );
137- $ url = ( string ) $ parsed ->withQuery ('' )->withFragment ('' );
140+ $ signatureParams = array_merge (QueryUtil::parse ($ url ->getQuery ()), $ params );
141+ $ url = $ url ->withQuery ('' )->withFragment ('' );
138142
139143 unset($ signatureParams ['oauth_signature ' ]);
140144
141145 // https://datatracker.ietf.org/doc/html/rfc5849#section-3.4.1.1
142- $ data = QueryUtil::recursiveRawurlencode ([strtoupper ($ method ), $ url , QueryUtil::build ($ signatureParams )]);
146+ $ data = QueryUtil::recursiveRawurlencode ([strtoupper ($ method ), ( string ) $ url , QueryUtil::build ($ signatureParams )]);
143147
144148 // https://datatracker.ietf.org/doc/html/rfc5849#section-3.4.2
145149 $ key = QueryUtil::recursiveRawurlencode ([$ this ->options ->secret , ($ accessTokenSecret ?? '' )]);
@@ -170,7 +174,7 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken $
170174 $ uri = $ request ->getUri ();
171175 $ query = QueryUtil::parse ($ uri ->getQuery ());
172176
173- $ parameters = [
177+ $ params = [
174178 'oauth_consumer_key ' => $ this ->options ->key ,
175179 'oauth_nonce ' => $ this ->nonce (),
176180 'oauth_signature_method ' => 'HMAC-SHA1 ' ,
@@ -179,18 +183,13 @@ public function getRequestAuthorization(RequestInterface $request, AccessToken $
179183 'oauth_version ' => '1.0 ' ,
180184 ];
181185
182- $ parameters ['oauth_signature ' ] = $ this ->getSignature (
183- (string )$ uri ->withQuery ('' )->withFragment ('' ),
184- array_merge ($ query , $ parameters ),
185- $ request ->getMethod (),
186- $ token ->accessTokenSecret
187- );
186+ $ params ['oauth_signature ' ] = $ this ->getSignature ($ uri , $ params , $ request ->getMethod (), $ token ->accessTokenSecret );
188187
189188 if (isset ($ query ['oauth_session_handle ' ])){
190- $ parameters ['oauth_session_handle ' ] = $ query ['oauth_session_handle ' ]; // @codeCoverageIgnore
189+ $ params ['oauth_session_handle ' ] = $ query ['oauth_session_handle ' ]; // @codeCoverageIgnore
191190 }
192191
193- return $ request ->withHeader ('Authorization ' , 'OAuth ' .QueryUtil::build ($ parameters , null , ', ' , '" ' ));
192+ return $ request ->withHeader ('Authorization ' , 'OAuth ' .QueryUtil::build ($ params , null , ', ' , '" ' ));
194193 }
195194
196195}
0 commit comments