6
6
7
7
namespace Magento \Framework \Oauth ;
8
8
9
- use GuzzleHttp \Subscriber \Oauth \Oauth1 ;
10
- use Laminas \OAuth \Http \Utility ;
11
- use GuzzleHttp \Psr7 \Request ;
12
- use Psr \Http \Message \RequestInterface ;
9
+ use Magento \Framework \Oauth \Helper \Utility ;
13
10
use Magento \Framework \Encryption \Helper \Security ;
14
11
use Magento \Framework \Phrase ;
15
12
use Magento \Framework \Oauth \Exception as AuthException ;
16
- use Magento \Framework \App \RequestInterface as AppRequest ;
17
13
18
14
/**
19
15
* Authorization service.
@@ -25,11 +21,6 @@ class Oauth implements OauthInterface
25
21
*/
26
22
protected $ _oauthHelper ;
27
23
28
- /**
29
- * @var Utility
30
- */
31
- protected $ _httpUtility ;
32
-
33
24
/**
34
25
* @var \Magento\Framework\Oauth\NonceGeneratorInterface
35
26
*/
@@ -41,35 +32,26 @@ class Oauth implements OauthInterface
41
32
protected $ _tokenProvider ;
42
33
43
34
/**
44
- * @var AppRequest
45
- */
46
- private AppRequest $ request ;
47
-
48
- /**
49
- * @var Oauth1|null
35
+ * @var Utility
50
36
*/
51
- private ? Oauth1 $ oauth1Helper = null ;
37
+ private Utility $ hmacSignatureHelper ;
52
38
53
39
/**
54
40
* @param Helper\Oauth $oauthHelper
55
41
* @param NonceGeneratorInterface $nonceGenerator
56
42
* @param TokenProviderInterface $tokenProvider
57
- * @param AppRequest $request
58
- * @param Utility|null $httpUtility
43
+ * @param Utility $utility
59
44
*/
60
45
public function __construct (
61
46
Helper \Oauth $ oauthHelper ,
62
47
NonceGeneratorInterface $ nonceGenerator ,
63
48
TokenProviderInterface $ tokenProvider ,
64
- AppRequest $ request ,
65
- Utility $ httpUtility = null
49
+ Utility $ utility
66
50
) {
67
51
$ this ->_oauthHelper = $ oauthHelper ;
68
52
$ this ->_nonceGenerator = $ nonceGenerator ;
69
53
$ this ->_tokenProvider = $ tokenProvider ;
70
- $ this ->request = $ request ;
71
- // null default to prevent ObjectManagerFactory from injecting, see MAGETWO-30809
72
- $ this ->_httpUtility = $ httpUtility ?: new Utility ();
54
+ $ this ->hmacSignatureHelper = $ utility ;
73
55
}
74
56
75
57
/**
@@ -172,18 +154,16 @@ public function buildAuthorizationHeader(
172
154
'oauth_version ' => '1.0 ' ,
173
155
];
174
156
$ headerParameters = array_merge ($ headerParameters , $ params );
175
- $ headerParameters ['oauth_signature ' ] = $ this ->_httpUtility ->sign (
157
+ $ headerParameters ['oauth_signature ' ] = $ this ->hmacSignatureHelper ->sign (
176
158
$ params ,
177
- $ signatureMethod ,
159
+ ' SHA256 ' ,
178
160
$ headerParameters ['oauth_consumer_secret ' ],
179
161
$ headerParameters ['oauth_token_secret ' ],
180
162
$ httpMethod ,
181
163
$ requestUrl
182
164
);
183
- $ authorizationHeader = $ this ->_httpUtility ->toAuthorizationHeader ($ headerParameters );
184
- // toAuthorizationHeader adds an optional realm="" which is not required for now.
185
- // http://tools.ietf.org/html/rfc2617#section-1.2
186
- return str_replace ('realm="", ' , '' , $ authorizationHeader );
165
+
166
+ return $ this ->hmacSignatureHelper ->toAuthorizationHeader ($ headerParameters );
187
167
}
188
168
189
169
/**
@@ -208,35 +188,16 @@ protected function _validateSignature($params, $consumerSecret, $httpMethod, $re
208
188
);
209
189
}
210
190
211
- $ allowedSignParams = $ params ;
212
- unset($ allowedSignParams ['oauth_signature ' ]);
213
-
214
- $ calculatedSign = $ this ->_httpUtility ->sign (
215
- $ allowedSignParams ,
216
- $ params ['oauth_signature_method ' ],
191
+ $ calculatedSign = $ this ->hmacSignatureHelper ->sign (
192
+ $ params ,
193
+ 'SHA256 ' ,
217
194
$ consumerSecret ,
218
195
$ tokenSecret ,
219
196
$ httpMethod ,
220
197
$ requestUrl
221
198
);
222
199
223
- $ calculatedSign2 = $ this ->getOauthHelper (
224
- [
225
- 'consumer_key ' => $ params ['oauth_consumer_key ' ],
226
- 'consumer_secret ' => $ consumerSecret ,
227
- 'token ' => $ params ['oauth_token ' ],
228
- 'token_secret ' => $ tokenSecret
229
- ]
230
- )->getSignature ($ this ->getRequestFromArray (
231
- [
232
- 'method ' => $ httpMethod ,
233
- 'uri ' => $ requestUrl ,
234
- 'headers ' => $ this ->request ->getHeaders ()->toArray (),
235
- 'body ' => $ httpMethod == 'GET ' ? null : json_encode ($ this ->request ->getParams ()) //this does not cover all cases request type
236
- ]
237
- ), $ params );
238
-
239
- if (!Security::compareStrings ($ calculatedSign2 , $ params ['oauth_signature ' ])) {
200
+ if (!Security::compareStrings ($ calculatedSign , $ params ['oauth_signature ' ])) {
240
201
throw new AuthException (new Phrase ('The signature is invalid. Verify and try again. ' ));
241
202
}
242
203
}
@@ -328,31 +289,4 @@ protected function _checkRequiredParams($protocolParams, $requiredParams)
328
289
throw $ exception ;
329
290
}
330
291
}
331
-
332
- /**
333
- * @param array $params
334
- * @return Oauth1
335
- */
336
- private function getOauthHelper (array $ params ): Oauth1
337
- {
338
- if (!$ this ->oauth1Helper ) {
339
- $ this ->oauth1Helper = new Oauth1 ($ params );
340
- }
341
-
342
- return $ this ->oauth1Helper ;
343
- }
344
-
345
- /**
346
- * @param array $data
347
- * @return RequestInterface
348
- */
349
- private function getRequestFromArray (array $ data ): RequestInterface
350
- {
351
- $ method = $ data ['method ' ] ?? 'GET ' ;
352
- $ uri = $ data ['uri ' ] ?? '' ;
353
- $ headers = $ data ['headers ' ] ?? [];
354
- $ body = $ data ['body ' ] ?? '' ;
355
-
356
- return new Request ($ method , $ uri , $ headers , $ body );
357
- }
358
292
}
0 commit comments