@@ -56,8 +56,7 @@ public function __construct(
5656 ClientFactory $ clientFactory ,
5757 NonceGeneratorInterface $ nonceGenerator ,
5858 Utility $ utility
59- )
60- {
59+ ) {
6160 $ this ->urlProvider = $ urlProvider ;
6261 $ this ->clientFactory = $ clientFactory ;
6362 $ this ->_nonceGenerator = $ nonceGenerator ;
@@ -107,44 +106,21 @@ public function getRequestToken(): array
107106 $ authParameters = ['oauth_consumer_key ' => $ this ->consumerKey ];
108107 $ authParameters = $ this ->getBasicAuthorizationParams ($ authParameters );
109108 $ requestUrl = $ this ->getRequestTokenEndpoint ();
110- $ headers = ['Authorization ' => $ this ->buildAuthorizationHeaderToRequestToken ($ authParameters , $ this ->consumerSecret , $ requestUrl )];
109+ $ headers = [
110+ 'Authorization ' => $ this ->buildAuthorizationHeaderToRequestToken (
111+ $ authParameters ,
112+ $ this ->consumerSecret ,
113+ $ requestUrl
114+ )
115+ ];
111116
112117 $ responseBody = $ this ->fetchResponse ($ requestUrl , [], $ headers );
113118 return $ this ->parseResponseBody ($ responseBody );
114119 }
115120
116121 /**
117- * Get request token endpoint.
118- * @return string
119- * @throws \Exception
120- */
121- public function getRequestTokenEndpoint (): string
122- {
123- return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/oauth/token/request ' );
124- }
125-
126- /**
127- * @param string $url
128- * @param array $requestBody
129- * @param array $headers
130- * @param string $method
131- * @return string
132- */
133- public function fetchResponse (string $ url , array $ requestBody , array $ headers , string $ method = 'POST ' ): string
134- {
135- $ httpClient = $ this ->clientFactory ->create ();
136- $ httpClient ->setHeaders ($ headers );
137- $ httpClient ->setOption (CURLOPT_FAILONERROR , true );
138- if ($ method === 'GET ' ) {
139- $ httpClient ->get ($ url );
140- } else {
141- $ httpClient ->post ($ url , $ requestBody );
142- }
143-
144- return $ httpClient ->getBody ();
145- }
146-
147- /**
122+ * Build header for request token
123+ *
148124 * @param array $params
149125 * @param string $consumerSecret
150126 * @param string $requestUrl
@@ -158,8 +134,7 @@ public function buildAuthorizationHeaderToRequestToken(
158134 string $ requestUrl ,
159135 string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
160136 string $ httpMethod = 'POST '
161- ): string
162- {
137+ ): string {
163138 $ params ['oauth_signature ' ] = $ this ->_httpUtility ->sign (
164139 $ params ,
165140 $ signatureMethod ,
@@ -173,6 +148,8 @@ public function buildAuthorizationHeaderToRequestToken(
173148 }
174149
175150 /**
151+ * Get access token
152+ *
176153 * @param array $token
177154 * @param string $verifier
178155 * @return array
@@ -201,6 +178,42 @@ public function getAccessToken(array $token, string $verifier): array
201178 }
202179
203180 /**
181+ * Validate access token
182+ *
183+ * @param array $token
184+ * @param string $method
185+ * @return array
186+ */
187+ public function validateAccessToken (array $ token , string $ method = 'GET ' ): array
188+ {
189+ $ authParameters = ['oauth_consumer_key ' => $ this ->consumerKey ];
190+ $ authParameters = $ this ->getBasicAuthorizationParams ($ authParameters );
191+
192+ //Need to add Accept header else Magento errors out with 503
193+ $ extraAuthenticationHeaders = ['Accept ' => 'application/json ' ];
194+
195+ $ authorizationHeader = [
196+ 'Authorization ' => $ this ->buildAuthorizationHeaderForAPIRequest (
197+ $ authParameters ,
198+ $ this ->consumerSecret ,
199+ $ this ->getTestApiEndpoint (),
200+ $ token ,
201+ [],
202+ \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
203+ $ method
204+ ),
205+ ];
206+
207+ $ headers = array_merge ($ authorizationHeader , $ extraAuthenticationHeaders );
208+
209+ $ responseBody = $ this ->fetchResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
210+
211+ return json_decode ($ responseBody );
212+ }
213+
214+ /**
215+ * Build header for api request
216+ *
204217 * @param array $params
205218 * @param string $consumerSecret
206219 * @param string $requestUrl
@@ -210,16 +223,15 @@ public function getAccessToken(array $token, string $verifier): array
210223 * @param string $httpMethod
211224 * @return string
212225 */
213- protected function buildAuthorizationHeaderForAPIRequest (
226+ public function buildAuthorizationHeaderForAPIRequest (
214227 array $ params ,
215228 string $ consumerSecret ,
216229 string $ requestUrl ,
217230 array $ token ,
218231 ?array $ bodyParams = null ,
219232 string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
220233 string $ httpMethod = 'POST '
221- ): string
222- {
234+ ): string {
223235
224236 if (isset ($ params ['oauth_callback ' ])) {
225237 unset($ params ['oauth_callback ' ]);
@@ -241,46 +253,26 @@ protected function buildAuthorizationHeaderForAPIRequest(
241253 }
242254
243255 /**
244- * @param array $token
245- * @param string $method
246- * @return array
256+ * Request token endpoint.
257+ *
258+ * @return string
259+ * @throws \Exception
247260 */
248- public function validateAccessToken ( array $ token , string $ method = ' GET ' ): array
261+ public function getRequestTokenEndpoint ( ): string
249262 {
250- $ authParameters = ['oauth_consumer_key ' => $ this ->consumerKey ];
251- $ authParameters = $ this ->getBasicAuthorizationParams ($ authParameters );
252-
253- //Need to add Accept header else Magento errors out with 503
254- $ extraAuthenticationHeaders = ['Accept ' => 'application/json ' ];
255-
256- $ authorizationHeader = [
257- 'Authorization ' => $ this ->buildAuthorizationHeaderForAPIRequest (
258- $ authParameters ,
259- $ this ->consumerSecret ,
260- $ this ->getTestApiEndpoint (),
261- $ token ,
262- [],
263- \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
264- $ method
265- ),
266- ];
267-
268- $ headers = array_merge ($ authorizationHeader , $ extraAuthenticationHeaders );
269-
270- $ responseBody = $ this ->fetchResponse ($ this ->getTestApiEndpoint (), [], $ headers , $ method );
271-
272- return json_decode ($ responseBody );
263+ return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/oauth/token/request ' );
273264 }
274265
275266 /**
267+ * Access token endpoint
268+ *
276269 * @return string
277270 */
278271 public function getAccessTokenEndpoint (): string
279272 {
280273 return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/oauth/token/access ' );
281274 }
282-
283-
275+
284276 /**
285277 * Returns the TestModule1 Rest API endpoint.
286278 *
@@ -293,6 +285,29 @@ public function getTestApiEndpoint(): string
293285 return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/rest/ ' . $ defaultStoreCode . '/V1/testmodule1 ' );
294286 }
295287
288+ /**
289+ * Fetch api response using curl client factory
290+ *
291+ * @param string $url
292+ * @param array $requestBody
293+ * @param array $headers
294+ * @param string $method
295+ * @return string
296+ */
297+ public function fetchResponse (string $ url , array $ requestBody , array $ headers , string $ method = 'POST ' ): string
298+ {
299+ $ httpClient = $ this ->clientFactory ->create ();
300+ $ httpClient ->setHeaders ($ headers );
301+ $ httpClient ->setOption (CURLOPT_FAILONERROR , true );
302+ if ($ method === 'GET ' ) {
303+ $ httpClient ->get ($ url );
304+ } else {
305+ $ httpClient ->post ($ url , $ requestBody );
306+ }
307+
308+ return $ httpClient ->getBody ();
309+ }
310+
296311 /**
297312 * Parse response body and return data in array.
298313 *
0 commit comments