|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OAuth\OAuth1\Service; |
|
4
|
|
|
|
|
5
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
|
6
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
|
7
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
|
8
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
|
9
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
|
10
|
|
|
use OAuth\OAuth1\Service\AbstractService; |
|
11
|
|
|
use OAuth\OAuth1\Signature\SignatureInterface; |
|
12
|
|
|
use OAuth\OAuth1\Token\StdOAuth1Token; |
|
13
|
|
|
use OAuth\OAuth1\Token\TokenInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Magento2 extends AbstractService |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string|null */ |
|
18
|
|
|
protected $oauthVerifier = null; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
CredentialsInterface $credentials, |
|
22
|
|
|
ClientInterface $httpClient, |
|
23
|
|
|
TokenStorageInterface $storage, |
|
24
|
|
|
SignatureInterface $signature, |
|
25
|
|
|
UriInterface $baseApiUri = null |
|
26
|
|
|
) { |
|
27
|
|
|
parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getRequestTokenEndpoint() |
|
34
|
|
|
{ |
|
35
|
|
|
$uri = clone $this->baseApiUri; |
|
36
|
|
|
$uri->setPath('/oauth/token/request'); |
|
37
|
|
|
return $uri; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the authorization API endpoint. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \OAuth\Common\Exception\Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getAuthorizationEndpoint() |
|
46
|
|
|
{ |
|
47
|
|
|
throw new \OAuth\Common\Exception\Exception( |
|
48
|
|
|
'Magento REST API is 2-legged. Current operation is not available.' |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getAccessTokenEndpoint() |
|
56
|
|
|
{ |
|
57
|
|
|
$uri = clone $this->baseApiUri; |
|
58
|
|
|
$uri->setPath('/oauth/token/access'); |
|
59
|
|
|
return $uri; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parses the request token response and returns a TokenInterface. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $responseBody |
|
66
|
|
|
* @return TokenInterface |
|
67
|
|
|
* @throws TokenResponseException |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function parseRequestTokenResponse($responseBody) |
|
70
|
|
|
{ |
|
71
|
|
|
$data = $this->parseResponseBody($responseBody); |
|
72
|
|
|
if (isset($data['oauth_verifier'])) { |
|
73
|
|
|
$this->oauthVerifier = $data['oauth_verifier']; |
|
74
|
|
|
} |
|
75
|
|
|
return $this->parseToken($responseBody); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function parseAccessTokenResponse($responseBody) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->parseToken($responseBody); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Parse response body and create oAuth token object based on parameters provided. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $responseBody |
|
90
|
|
|
* @return StdOAuth1Token |
|
91
|
|
|
* @throws TokenResponseException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function parseToken($responseBody) |
|
94
|
|
|
{ |
|
95
|
|
|
$data = $this->parseResponseBody($responseBody); |
|
96
|
|
|
$token = new StdOAuth1Token(); |
|
97
|
|
|
$token->setRequestToken($data['oauth_token']); |
|
98
|
|
|
$token->setRequestTokenSecret($data['oauth_token_secret']); |
|
99
|
|
|
$token->setAccessToken($data['oauth_token']); |
|
100
|
|
|
$token->setAccessTokenSecret($data['oauth_token_secret']); |
|
101
|
|
|
$token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES); |
|
102
|
|
|
unset($data['oauth_token'], $data['oauth_token_secret']); |
|
103
|
|
|
$token->setExtraParams($data); |
|
104
|
|
|
return $token; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Parse response body and return data in array. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $responseBody |
|
111
|
|
|
* @return array |
|
112
|
|
|
* @throws TokenResponseException |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function parseResponseBody($responseBody) |
|
115
|
|
|
{ |
|
116
|
|
|
if (!is_string($responseBody)) { |
|
117
|
|
|
throw new TokenResponseException("Response body is expected to be a string."); |
|
118
|
|
|
} |
|
119
|
|
|
parse_str($responseBody, $data); |
|
120
|
|
|
if (null === $data || !is_array($data)) { |
|
121
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
|
122
|
|
|
} elseif (isset($data['error'])) { |
|
123
|
|
|
throw new TokenResponseException("Error occurred: '{$data['error']}'"); |
|
124
|
|
|
} |
|
125
|
|
|
return $data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Builds the authorization header for an authenticated API request |
|
130
|
|
|
* |
|
131
|
|
|
* This is changed from the parent to include $bodyParams in $authParameters. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $method |
|
134
|
|
|
* @param UriInterface $uri The uri the request is headed |
|
135
|
|
|
* @param TokenInterface $token |
|
136
|
|
|
* @param array $bodyParams Request body if applicable (key/value pairs) |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function buildAuthorizationHeaderForAPIRequest( |
|
141
|
|
|
$method, |
|
142
|
|
|
UriInterface $uri, |
|
143
|
|
|
TokenInterface $token, |
|
144
|
|
|
$bodyParams = null |
|
145
|
|
|
) { |
|
146
|
|
|
$this->signature->setTokenSecret($token->getAccessTokenSecret()); |
|
147
|
|
|
$authParameters = $this->getBasicAuthorizationHeaderInfo(); |
|
148
|
|
|
if (isset($authParameters['oauth_callback'])) { |
|
149
|
|
|
unset($authParameters['oauth_callback']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$authParameters = array_merge($authParameters, ['oauth_token' => $token->getAccessToken()]); |
|
153
|
|
|
if (is_array($bodyParams)) { |
|
154
|
|
|
$authParameters = array_merge($authParameters, $bodyParams); |
|
155
|
|
|
} |
|
156
|
|
|
$authParameters['oauth_signature'] = $this->signature->getSignature($uri, $authParameters, $method); |
|
157
|
|
|
|
|
158
|
|
|
$authorizationHeader = 'OAuth '; |
|
159
|
|
|
$delimiter = ''; |
|
160
|
|
|
|
|
161
|
|
|
foreach ($authParameters as $key => $value) { |
|
162
|
|
|
$authorizationHeader .= $delimiter . rawurlencode($key) . '="' . rawurlencode($value) . '"'; |
|
163
|
|
|
$delimiter = ', '; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $authorizationHeader; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|