@@ -56,8 +56,7 @@ public function __construct(
56
56
ClientFactory $ clientFactory ,
57
57
NonceGeneratorInterface $ nonceGenerator ,
58
58
Utility $ utility
59
- )
60
- {
59
+ ) {
61
60
$ this ->urlProvider = $ urlProvider ;
62
61
$ this ->clientFactory = $ clientFactory ;
63
62
$ this ->_nonceGenerator = $ nonceGenerator ;
@@ -107,44 +106,21 @@ public function getRequestToken(): array
107
106
$ authParameters = ['oauth_consumer_key ' => $ this ->consumerKey ];
108
107
$ authParameters = $ this ->getBasicAuthorizationParams ($ authParameters );
109
108
$ 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
+ ];
111
116
112
117
$ responseBody = $ this ->fetchResponse ($ requestUrl , [], $ headers );
113
118
return $ this ->parseResponseBody ($ responseBody );
114
119
}
115
120
116
121
/**
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
+ *
148
124
* @param array $params
149
125
* @param string $consumerSecret
150
126
* @param string $requestUrl
@@ -158,8 +134,7 @@ public function buildAuthorizationHeaderToRequestToken(
158
134
string $ requestUrl ,
159
135
string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
160
136
string $ httpMethod = 'POST '
161
- ): string
162
- {
137
+ ): string {
163
138
$ params ['oauth_signature ' ] = $ this ->_httpUtility ->sign (
164
139
$ params ,
165
140
$ signatureMethod ,
@@ -173,6 +148,8 @@ public function buildAuthorizationHeaderToRequestToken(
173
148
}
174
149
175
150
/**
151
+ * Get access token
152
+ *
176
153
* @param array $token
177
154
* @param string $verifier
178
155
* @return array
@@ -201,6 +178,42 @@ public function getAccessToken(array $token, string $verifier): array
201
178
}
202
179
203
180
/**
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
+ *
204
217
* @param array $params
205
218
* @param string $consumerSecret
206
219
* @param string $requestUrl
@@ -210,16 +223,15 @@ public function getAccessToken(array $token, string $verifier): array
210
223
* @param string $httpMethod
211
224
* @return string
212
225
*/
213
- protected function buildAuthorizationHeaderForAPIRequest (
226
+ public function buildAuthorizationHeaderForAPIRequest (
214
227
array $ params ,
215
228
string $ consumerSecret ,
216
229
string $ requestUrl ,
217
230
array $ token ,
218
231
?array $ bodyParams = null ,
219
232
string $ signatureMethod = \Magento \Framework \Oauth \Oauth::SIGNATURE_SHA256 ,
220
233
string $ httpMethod = 'POST '
221
- ): string
222
- {
234
+ ): string {
223
235
224
236
if (isset ($ params ['oauth_callback ' ])) {
225
237
unset($ params ['oauth_callback ' ]);
@@ -241,46 +253,26 @@ protected function buildAuthorizationHeaderForAPIRequest(
241
253
}
242
254
243
255
/**
244
- * @param array $token
245
- * @param string $method
246
- * @return array
256
+ * Request token endpoint.
257
+ *
258
+ * @return string
259
+ * @throws \Exception
247
260
*/
248
- public function validateAccessToken ( array $ token , string $ method = ' GET ' ): array
261
+ public function getRequestTokenEndpoint ( ): string
249
262
{
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 ' );
273
264
}
274
265
275
266
/**
267
+ * Access token endpoint
268
+ *
276
269
* @return string
277
270
*/
278
271
public function getAccessTokenEndpoint (): string
279
272
{
280
273
return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/oauth/token/access ' );
281
274
}
282
-
283
-
275
+
284
276
/**
285
277
* Returns the TestModule1 Rest API endpoint.
286
278
*
@@ -293,6 +285,29 @@ public function getTestApiEndpoint(): string
293
285
return $ this ->urlProvider ->getRebuiltUrl (TESTS_BASE_URL . '/rest/ ' . $ defaultStoreCode . '/V1/testmodule1 ' );
294
286
}
295
287
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
+
296
311
/**
297
312
* Parse response body and return data in array.
298
313
*
0 commit comments