1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MercadoLibre Provider for OAuth 2.0 Client |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE file |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Lucas Banegas <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
11
|
|
|
* @link https://github.com/docta/oauth2-mercadolibre |
12
|
|
|
*/ |
13
|
|
|
namespace Docta\OAuth2\Client\Provider; |
14
|
|
|
|
15
|
|
|
use GuzzleHttp\Psr7\Uri; |
16
|
|
|
use GuzzleHttp\Psr7\UriResolver; |
17
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
18
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
19
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Represents a service provider (authorization server). |
24
|
|
|
* |
25
|
|
|
* @link http://tools.ietf.org/html/rfc6749#section-1.1 Roles (RFC 6749, §1.1) |
26
|
|
|
*/ |
27
|
|
|
class MercadoLibreProvider extends AbstractProvider |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string Key used in a token response to identify the resource owner. |
31
|
|
|
*/ |
32
|
|
|
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'user_id'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string Site |
36
|
|
|
*/ |
37
|
|
|
protected $site; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array authSites |
41
|
|
|
*/ |
42
|
|
|
protected $authSites = [ |
43
|
|
|
'MLA' => 'https://auth.mercadolibre.com.ar', |
44
|
|
|
'MLB' => 'https://auth.mercadolivre.com.br', |
45
|
|
|
'MCO' => 'https://auth.mercadolibre.com.co', |
46
|
|
|
'MCR' => 'https://auth.mercadolibre.com.cr', |
47
|
|
|
'MEC' => 'https://auth.mercadolibre.com.ec', |
48
|
|
|
'MLC' => 'https://auth.mercadolibre.cl', |
49
|
|
|
'MLM' => 'https://auth.mercadolibre.com.mx', |
50
|
|
|
'MLU' => 'https://auth.mercadolibre.com.uy', |
51
|
|
|
'MLV' => 'https://auth.mercadolibre.com.ve', |
52
|
|
|
'MPA' => 'https://auth.mercadolibre.com.pa', |
53
|
|
|
'MPE' => 'https://auth.mercadolibre.com.pe', |
54
|
|
|
'MPT' => 'https://auth.mercadolibre.com.pt', |
55
|
|
|
'MRD' => 'https://auth.mercadolibre.com.do' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string authSite |
60
|
|
|
*/ |
61
|
|
|
protected $authSite; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string apiUrl |
65
|
|
|
*/ |
66
|
|
|
protected $apiUrl = 'https://api.mercadolibre.com'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the base URL for authorizing a client. |
70
|
|
|
* |
71
|
|
|
* @return string Base authorization URL |
72
|
|
|
*/ |
73
|
|
|
public function getBaseAuthorizationUrl() |
74
|
|
|
{ |
75
|
|
|
return sprintf('%s/authorization', $this->authSites[$this->authSite]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the base URL for requesting an access token. |
80
|
|
|
* |
81
|
|
|
* @param array $params |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getBaseAccessTokenUrl(array $params = []) |
85
|
|
|
{ |
86
|
|
|
return sprintf('%s/oauth/token', $this->apiUrl); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Builds the authorization URL. |
91
|
|
|
* |
92
|
|
|
* @param array $options |
93
|
|
|
* @return string Authorization URL |
94
|
|
|
*/ |
95
|
|
|
public function getAuthorizationUrl(array $options = []) |
96
|
|
|
{ |
97
|
|
|
$options['approval_prompt'] = null; |
98
|
|
|
return parent::getAuthorizationUrl($options); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the default scopes used by this provider. |
103
|
|
|
* |
104
|
|
|
* @return null |
105
|
|
|
*/ |
106
|
|
|
protected function getDefaultScopes() |
107
|
|
|
{ |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the URL for requesting the resource owner's details. |
113
|
|
|
* |
114
|
|
|
* @param \League\OAuth2\Client\Token\AccessToken|null $token |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token = null) |
118
|
|
|
{ |
119
|
|
|
return sprintf('%s/users/me', $this->apiUrl); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the modified url for authenticated requests. |
124
|
|
|
* |
125
|
|
|
* @param string $url |
126
|
|
|
* @param \League\OAuth2\Client\Token\AccessToken $token |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getAuthenticatedRequestUrl($url, AccessToken $token) |
130
|
|
|
{ |
131
|
|
|
$url = UriResolver::resolve(new Uri($this->apiUrl), new Uri($url)); |
132
|
|
|
return (string) Uri::withQueryValue($url, 'access_token', $token->getToken()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns an authenticated request instance. |
137
|
|
|
* |
138
|
|
|
* @param string $method |
139
|
|
|
* @param string $url |
140
|
|
|
* @param \League\OAuth2\Client\Token\AccessToken|string $token |
141
|
|
|
* @param array $options |
142
|
|
|
* @return \GuzzleHttp\Psr7\Request |
143
|
|
|
*/ |
144
|
|
|
public function getAuthenticatedRequest($method, $url, AccessToken $token, array $options = []) |
145
|
|
|
{ |
146
|
|
|
$url = $this->getAuthenticatedRequestUrl($url, $token); |
147
|
|
|
return $this->createRequest($method, $url, $token, $options); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Checks a provider response for errors. |
152
|
|
|
* |
153
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
154
|
|
|
* @param array|string $data Parsed response data |
155
|
|
|
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
protected function checkResponse(ResponseInterface $response, $data) |
159
|
|
|
{ |
160
|
|
|
if (isset($data['error'])) { |
161
|
|
|
$message = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); |
162
|
|
|
$code = isset($data['status']) ? $data['status'] : $response->getStatusCode(); |
163
|
|
|
throw new IdentityProviderException($message, $code, $response); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Generates a resource owner object from a successful resource owner |
169
|
|
|
* details request. |
170
|
|
|
* |
171
|
|
|
* @param array $response |
172
|
|
|
* @param \League\OAuth2\Client\Token\AccessToken $token |
173
|
|
|
* @return \Docta\OAuth2\Client\Provider\MercadoLibreResourceOwner |
174
|
|
|
*/ |
175
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
176
|
|
|
{ |
177
|
|
|
return new MercadoLibreResourceOwner($response); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|