@@ -43,127 +43,127 @@ |
||
43 | 43 | */ |
44 | 44 | class MicrosoftGraph extends OAuth2 |
45 | 45 | { |
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - protected $scope = 'openid user.read contacts.read'; |
|
50 | - |
|
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected $apiBaseUrl = 'https://graph.microsoft.com/v1.0/'; |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - protected $authorizeUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'; |
|
60 | - |
|
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - protected $accessTokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected $apiDocumentation = 'https://developer.microsoft.com/en-us/graph/docs/concepts/php'; |
|
70 | - |
|
71 | - /** |
|
72 | - * {@inheritdoc} |
|
73 | - */ |
|
74 | - protected function initialize() |
|
75 | - { |
|
76 | - parent::initialize(); |
|
77 | - |
|
78 | - $tenant = $this->config->get('tenant'); |
|
79 | - if (!empty($tenant)) { |
|
80 | - $adjustedEndpoints = [ |
|
81 | - 'authorize_url' => str_replace('/common/', '/' . $tenant . '/', $this->authorizeUrl), |
|
82 | - 'access_token_url' => str_replace('/common/', '/' . $tenant . '/', $this->accessTokenUrl), |
|
83 | - ]; |
|
84 | - |
|
85 | - $this->setApiEndpoints($adjustedEndpoints); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * {@inheritdoc} |
|
91 | - */ |
|
92 | - public function getUserProfile() |
|
93 | - { |
|
94 | - $response = $this->apiRequest('me'); |
|
95 | - |
|
96 | - $data = new Data\Collection($response); |
|
97 | - |
|
98 | - if (!$data->exists('id')) { |
|
99 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
100 | - } |
|
101 | - |
|
102 | - $userProfile = new User\Profile(); |
|
103 | - |
|
104 | - $userProfile->identifier = $data->get('id'); |
|
105 | - $userProfile->displayName = $data->get('displayName'); |
|
106 | - $userProfile->firstName = $data->get('givenName'); |
|
107 | - $userProfile->lastName = $data->get('surname'); |
|
108 | - $userProfile->language = $data->get('preferredLanguage'); |
|
109 | - |
|
110 | - $userProfile->phone = $data->get('mobilePhone'); |
|
111 | - if (empty($userProfile->phone)) { |
|
112 | - $businessPhones = $data->get('businessPhones'); |
|
113 | - if (isset($businessPhones[0])) { |
|
114 | - $userProfile->phone = $businessPhones[0]; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - $userProfile->email = $data->get('mail'); |
|
119 | - if (empty($userProfile->email)) { |
|
120 | - $email = $data->get('userPrincipalName'); |
|
121 | - if (strpos($email, '@') !== false) { |
|
122 | - $userProfile->email = $email; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - return $userProfile; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * {@inheritdoc} |
|
131 | - */ |
|
132 | - public function getUserContacts() |
|
133 | - { |
|
134 | - $apiUrl = 'me/contacts?$top=50'; |
|
135 | - $contacts = []; |
|
136 | - |
|
137 | - do { |
|
138 | - $response = $this->apiRequest($apiUrl); |
|
139 | - $data = new Data\Collection($response); |
|
140 | - if (!$data->exists('value')) { |
|
141 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
142 | - } |
|
143 | - foreach ($data->filter('value')->toArray() as $entry) { |
|
144 | - $entry = new Data\Collection($entry); |
|
145 | - $userContact = new User\Contact(); |
|
146 | - $userContact->identifier = $entry->get('id'); |
|
147 | - $userContact->displayName = $entry->get('displayName'); |
|
148 | - $emailAddresses = $entry->get('emailAddresses'); |
|
149 | - if (!empty($emailAddresses)) { |
|
150 | - $userContact->email = $emailAddresses[0]->address; |
|
151 | - } |
|
152 | - // only add to collection if we have usefull data |
|
153 | - if (!empty($userContact->displayName) || !empty($userContact->email)) { |
|
154 | - $contacts[] = $userContact; |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - if ($data->exists('@odata.nextLink')) { |
|
159 | - $apiUrl = $data->get('@odata.nextLink'); |
|
160 | - |
|
161 | - $pagedList = true; |
|
162 | - } else { |
|
163 | - $pagedList = false; |
|
164 | - } |
|
165 | - } while ($pagedList); |
|
166 | - |
|
167 | - return $contacts; |
|
168 | - } |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + protected $scope = 'openid user.read contacts.read'; |
|
50 | + |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected $apiBaseUrl = 'https://graph.microsoft.com/v1.0/'; |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + protected $authorizeUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'; |
|
60 | + |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + protected $accessTokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected $apiDocumentation = 'https://developer.microsoft.com/en-us/graph/docs/concepts/php'; |
|
70 | + |
|
71 | + /** |
|
72 | + * {@inheritdoc} |
|
73 | + */ |
|
74 | + protected function initialize() |
|
75 | + { |
|
76 | + parent::initialize(); |
|
77 | + |
|
78 | + $tenant = $this->config->get('tenant'); |
|
79 | + if (!empty($tenant)) { |
|
80 | + $adjustedEndpoints = [ |
|
81 | + 'authorize_url' => str_replace('/common/', '/' . $tenant . '/', $this->authorizeUrl), |
|
82 | + 'access_token_url' => str_replace('/common/', '/' . $tenant . '/', $this->accessTokenUrl), |
|
83 | + ]; |
|
84 | + |
|
85 | + $this->setApiEndpoints($adjustedEndpoints); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * {@inheritdoc} |
|
91 | + */ |
|
92 | + public function getUserProfile() |
|
93 | + { |
|
94 | + $response = $this->apiRequest('me'); |
|
95 | + |
|
96 | + $data = new Data\Collection($response); |
|
97 | + |
|
98 | + if (!$data->exists('id')) { |
|
99 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
100 | + } |
|
101 | + |
|
102 | + $userProfile = new User\Profile(); |
|
103 | + |
|
104 | + $userProfile->identifier = $data->get('id'); |
|
105 | + $userProfile->displayName = $data->get('displayName'); |
|
106 | + $userProfile->firstName = $data->get('givenName'); |
|
107 | + $userProfile->lastName = $data->get('surname'); |
|
108 | + $userProfile->language = $data->get('preferredLanguage'); |
|
109 | + |
|
110 | + $userProfile->phone = $data->get('mobilePhone'); |
|
111 | + if (empty($userProfile->phone)) { |
|
112 | + $businessPhones = $data->get('businessPhones'); |
|
113 | + if (isset($businessPhones[0])) { |
|
114 | + $userProfile->phone = $businessPhones[0]; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + $userProfile->email = $data->get('mail'); |
|
119 | + if (empty($userProfile->email)) { |
|
120 | + $email = $data->get('userPrincipalName'); |
|
121 | + if (strpos($email, '@') !== false) { |
|
122 | + $userProfile->email = $email; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + return $userProfile; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * {@inheritdoc} |
|
131 | + */ |
|
132 | + public function getUserContacts() |
|
133 | + { |
|
134 | + $apiUrl = 'me/contacts?$top=50'; |
|
135 | + $contacts = []; |
|
136 | + |
|
137 | + do { |
|
138 | + $response = $this->apiRequest($apiUrl); |
|
139 | + $data = new Data\Collection($response); |
|
140 | + if (!$data->exists('value')) { |
|
141 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
142 | + } |
|
143 | + foreach ($data->filter('value')->toArray() as $entry) { |
|
144 | + $entry = new Data\Collection($entry); |
|
145 | + $userContact = new User\Contact(); |
|
146 | + $userContact->identifier = $entry->get('id'); |
|
147 | + $userContact->displayName = $entry->get('displayName'); |
|
148 | + $emailAddresses = $entry->get('emailAddresses'); |
|
149 | + if (!empty($emailAddresses)) { |
|
150 | + $userContact->email = $emailAddresses[0]->address; |
|
151 | + } |
|
152 | + // only add to collection if we have usefull data |
|
153 | + if (!empty($userContact->displayName) || !empty($userContact->email)) { |
|
154 | + $contacts[] = $userContact; |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + if ($data->exists('@odata.nextLink')) { |
|
159 | + $apiUrl = $data->get('@odata.nextLink'); |
|
160 | + |
|
161 | + $pagedList = true; |
|
162 | + } else { |
|
163 | + $pagedList = false; |
|
164 | + } |
|
165 | + } while ($pagedList); |
|
166 | + |
|
167 | + return $contacts; |
|
168 | + } |
|
169 | 169 | } |
@@ -44,143 +44,143 @@ discard block |
||
44 | 44 | */ |
45 | 45 | class Telegram extends AbstractAdapter implements AdapterInterface |
46 | 46 | { |
47 | - protected $botId = ''; |
|
48 | - |
|
49 | - protected $botSecret = ''; |
|
50 | - |
|
51 | - protected $callbackUrl = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * IPD API Documentation |
|
55 | - * |
|
56 | - * OPTIONAL. |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $apiDocumentation = 'https://core.telegram.org/bots'; |
|
61 | - |
|
62 | - /** |
|
63 | - * {@inheritdoc} |
|
64 | - */ |
|
65 | - protected function configure() |
|
66 | - { |
|
67 | - $this->botId = $this->config->filter('keys')->get('id'); |
|
68 | - $this->botSecret = $this->config->filter('keys')->get('secret'); |
|
69 | - $this->callbackUrl = $this->config->get('callback'); |
|
70 | - |
|
71 | - if (!$this->botId || !$this->botSecret) { |
|
72 | - throw new InvalidApplicationCredentialsException( |
|
73 | - 'Your application id is required in order to connect to ' . $this->providerId |
|
74 | - ); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * {@inheritdoc} |
|
80 | - */ |
|
81 | - protected function initialize() |
|
82 | - { |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * {@inheritdoc} |
|
87 | - */ |
|
88 | - public function authenticate() |
|
89 | - { |
|
90 | - $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
91 | - if (!filter_input(INPUT_GET, 'hash')) { |
|
92 | - $this->authenticateBegin(); |
|
93 | - } else { |
|
94 | - $this->authenticateCheckError(); |
|
95 | - $this->authenticateFinish(); |
|
96 | - } |
|
97 | - return null; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * {@inheritdoc} |
|
102 | - */ |
|
103 | - public function isConnected() |
|
104 | - { |
|
105 | - $authData = $this->getStoredData('auth_data'); |
|
106 | - return !empty($authData); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * {@inheritdoc} |
|
111 | - */ |
|
112 | - public function getUserProfile() |
|
113 | - { |
|
114 | - $data = new Collection($this->getStoredData('auth_data')); |
|
115 | - |
|
116 | - if (!$data->exists('id')) { |
|
117 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
118 | - } |
|
119 | - |
|
120 | - $userProfile = new Profile(); |
|
121 | - |
|
122 | - $userProfile->identifier = $data->get('id'); |
|
123 | - $userProfile->firstName = $data->get('first_name'); |
|
124 | - $userProfile->lastName = $data->get('last_name'); |
|
125 | - $userProfile->displayName = $data->get('username'); |
|
126 | - $userProfile->photoURL = $data->get('photo_url'); |
|
127 | - $username = $data->get('username'); |
|
128 | - if (!empty($username)) { |
|
129 | - // Only some accounts have usernames. |
|
130 | - $userProfile->profileURL = "https://t.me/{$username}"; |
|
131 | - } |
|
132 | - |
|
133 | - return $userProfile; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * See: https://telegram.im/widget-login.php |
|
138 | - * See: https://gist.github.com/anonymous/6516521b1fb3b464534fbc30ea3573c2 |
|
139 | - */ |
|
140 | - protected function authenticateCheckError() |
|
141 | - { |
|
142 | - $auth_data = $this->parseAuthData(); |
|
143 | - |
|
144 | - $check_hash = $auth_data['hash']; |
|
145 | - unset($auth_data['hash']); |
|
146 | - $data_check_arr = []; |
|
147 | - |
|
148 | - foreach ($auth_data as $key => $value) { |
|
149 | - if (!empty($value)) { |
|
150 | - $data_check_arr[] = $key . '=' . $value; |
|
151 | - } |
|
152 | - } |
|
153 | - sort($data_check_arr); |
|
154 | - |
|
155 | - $data_check_string = implode("\n", $data_check_arr); |
|
156 | - $secret_key = hash('sha256', $this->botSecret, true); |
|
157 | - $hash = hash_hmac('sha256', $data_check_string, $secret_key); |
|
158 | - |
|
159 | - if (strcmp($hash, $check_hash) !== 0) { |
|
160 | - throw new InvalidAuthorizationCodeException( |
|
161 | - sprintf('Provider returned an error: %s', 'Data is NOT from Telegram') |
|
162 | - ); |
|
163 | - } |
|
164 | - |
|
165 | - if ((time() - $auth_data['auth_date']) > 86400) { |
|
166 | - throw new InvalidAuthorizationCodeException( |
|
167 | - sprintf('Provider returned an error: %s', 'Data is outdated') |
|
168 | - ); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * See: https://telegram.im/widget-login.php |
|
174 | - */ |
|
175 | - protected function authenticateBegin() |
|
176 | - { |
|
177 | - $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this))); |
|
178 | - |
|
179 | - $nonce = $this->config->get('nonce'); |
|
180 | - $nonce_code = empty($nonce) ? '' : "nonce=\"{$nonce}\""; |
|
181 | - |
|
182 | - exit( |
|
183 | - <<<HTML |
|
47 | + protected $botId = ''; |
|
48 | + |
|
49 | + protected $botSecret = ''; |
|
50 | + |
|
51 | + protected $callbackUrl = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * IPD API Documentation |
|
55 | + * |
|
56 | + * OPTIONAL. |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $apiDocumentation = 'https://core.telegram.org/bots'; |
|
61 | + |
|
62 | + /** |
|
63 | + * {@inheritdoc} |
|
64 | + */ |
|
65 | + protected function configure() |
|
66 | + { |
|
67 | + $this->botId = $this->config->filter('keys')->get('id'); |
|
68 | + $this->botSecret = $this->config->filter('keys')->get('secret'); |
|
69 | + $this->callbackUrl = $this->config->get('callback'); |
|
70 | + |
|
71 | + if (!$this->botId || !$this->botSecret) { |
|
72 | + throw new InvalidApplicationCredentialsException( |
|
73 | + 'Your application id is required in order to connect to ' . $this->providerId |
|
74 | + ); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * {@inheritdoc} |
|
80 | + */ |
|
81 | + protected function initialize() |
|
82 | + { |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * {@inheritdoc} |
|
87 | + */ |
|
88 | + public function authenticate() |
|
89 | + { |
|
90 | + $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
91 | + if (!filter_input(INPUT_GET, 'hash')) { |
|
92 | + $this->authenticateBegin(); |
|
93 | + } else { |
|
94 | + $this->authenticateCheckError(); |
|
95 | + $this->authenticateFinish(); |
|
96 | + } |
|
97 | + return null; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * {@inheritdoc} |
|
102 | + */ |
|
103 | + public function isConnected() |
|
104 | + { |
|
105 | + $authData = $this->getStoredData('auth_data'); |
|
106 | + return !empty($authData); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * {@inheritdoc} |
|
111 | + */ |
|
112 | + public function getUserProfile() |
|
113 | + { |
|
114 | + $data = new Collection($this->getStoredData('auth_data')); |
|
115 | + |
|
116 | + if (!$data->exists('id')) { |
|
117 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
118 | + } |
|
119 | + |
|
120 | + $userProfile = new Profile(); |
|
121 | + |
|
122 | + $userProfile->identifier = $data->get('id'); |
|
123 | + $userProfile->firstName = $data->get('first_name'); |
|
124 | + $userProfile->lastName = $data->get('last_name'); |
|
125 | + $userProfile->displayName = $data->get('username'); |
|
126 | + $userProfile->photoURL = $data->get('photo_url'); |
|
127 | + $username = $data->get('username'); |
|
128 | + if (!empty($username)) { |
|
129 | + // Only some accounts have usernames. |
|
130 | + $userProfile->profileURL = "https://t.me/{$username}"; |
|
131 | + } |
|
132 | + |
|
133 | + return $userProfile; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * See: https://telegram.im/widget-login.php |
|
138 | + * See: https://gist.github.com/anonymous/6516521b1fb3b464534fbc30ea3573c2 |
|
139 | + */ |
|
140 | + protected function authenticateCheckError() |
|
141 | + { |
|
142 | + $auth_data = $this->parseAuthData(); |
|
143 | + |
|
144 | + $check_hash = $auth_data['hash']; |
|
145 | + unset($auth_data['hash']); |
|
146 | + $data_check_arr = []; |
|
147 | + |
|
148 | + foreach ($auth_data as $key => $value) { |
|
149 | + if (!empty($value)) { |
|
150 | + $data_check_arr[] = $key . '=' . $value; |
|
151 | + } |
|
152 | + } |
|
153 | + sort($data_check_arr); |
|
154 | + |
|
155 | + $data_check_string = implode("\n", $data_check_arr); |
|
156 | + $secret_key = hash('sha256', $this->botSecret, true); |
|
157 | + $hash = hash_hmac('sha256', $data_check_string, $secret_key); |
|
158 | + |
|
159 | + if (strcmp($hash, $check_hash) !== 0) { |
|
160 | + throw new InvalidAuthorizationCodeException( |
|
161 | + sprintf('Provider returned an error: %s', 'Data is NOT from Telegram') |
|
162 | + ); |
|
163 | + } |
|
164 | + |
|
165 | + if ((time() - $auth_data['auth_date']) > 86400) { |
|
166 | + throw new InvalidAuthorizationCodeException( |
|
167 | + sprintf('Provider returned an error: %s', 'Data is outdated') |
|
168 | + ); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * See: https://telegram.im/widget-login.php |
|
174 | + */ |
|
175 | + protected function authenticateBegin() |
|
176 | + { |
|
177 | + $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this))); |
|
178 | + |
|
179 | + $nonce = $this->config->get('nonce'); |
|
180 | + $nonce_code = empty($nonce) ? '' : "nonce=\"{$nonce}\""; |
|
181 | + |
|
182 | + exit( |
|
183 | + <<<HTML |
|
184 | 184 | <center> |
185 | 185 | <script async src="https://telegram.org/js/telegram-widget.js?7" |
186 | 186 | {$nonce_code} |
@@ -191,31 +191,31 @@ discard block |
||
191 | 191 | </script> |
192 | 192 | </center> |
193 | 193 | HTML |
194 | - ); |
|
195 | - } |
|
196 | - |
|
197 | - protected function authenticateFinish() |
|
198 | - { |
|
199 | - $this->logger->debug( |
|
200 | - sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
201 | - [Util::getCurrentUrl(true)] |
|
202 | - ); |
|
203 | - |
|
204 | - $this->storeData('auth_data', $this->parseAuthData()); |
|
205 | - |
|
206 | - $this->initialize(); |
|
207 | - } |
|
208 | - |
|
209 | - protected function parseAuthData() |
|
210 | - { |
|
211 | - return [ |
|
212 | - 'id' => filter_input(INPUT_GET, 'id'), |
|
213 | - 'first_name' => filter_input(INPUT_GET, 'first_name'), |
|
214 | - 'last_name' => filter_input(INPUT_GET, 'last_name'), |
|
215 | - 'username' => filter_input(INPUT_GET, 'username'), |
|
216 | - 'photo_url' => filter_input(INPUT_GET, 'photo_url'), |
|
217 | - 'auth_date' => filter_input(INPUT_GET, 'auth_date'), |
|
218 | - 'hash' => filter_input(INPUT_GET, 'hash'), |
|
219 | - ]; |
|
220 | - } |
|
194 | + ); |
|
195 | + } |
|
196 | + |
|
197 | + protected function authenticateFinish() |
|
198 | + { |
|
199 | + $this->logger->debug( |
|
200 | + sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
201 | + [Util::getCurrentUrl(true)] |
|
202 | + ); |
|
203 | + |
|
204 | + $this->storeData('auth_data', $this->parseAuthData()); |
|
205 | + |
|
206 | + $this->initialize(); |
|
207 | + } |
|
208 | + |
|
209 | + protected function parseAuthData() |
|
210 | + { |
|
211 | + return [ |
|
212 | + 'id' => filter_input(INPUT_GET, 'id'), |
|
213 | + 'first_name' => filter_input(INPUT_GET, 'first_name'), |
|
214 | + 'last_name' => filter_input(INPUT_GET, 'last_name'), |
|
215 | + 'username' => filter_input(INPUT_GET, 'username'), |
|
216 | + 'photo_url' => filter_input(INPUT_GET, 'photo_url'), |
|
217 | + 'auth_date' => filter_input(INPUT_GET, 'auth_date'), |
|
218 | + 'hash' => filter_input(INPUT_GET, 'hash'), |
|
219 | + ]; |
|
220 | + } |
|
221 | 221 | } |
@@ -12,305 +12,305 @@ |
||
12 | 12 | */ |
13 | 13 | class Curl implements HttpClientInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * Default curl options |
|
17 | - * |
|
18 | - * These defaults options can be overwritten when sending requests. |
|
19 | - * |
|
20 | - * See setCurlOptions() |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $curlOptions = [ |
|
25 | - CURLOPT_TIMEOUT => 30, |
|
26 | - CURLOPT_CONNECTTIMEOUT => 30, |
|
27 | - CURLOPT_SSL_VERIFYPEER => false, |
|
28 | - CURLOPT_SSL_VERIFYHOST => false, |
|
29 | - CURLOPT_RETURNTRANSFER => true, |
|
30 | - CURLOPT_FOLLOWLOCATION => true, |
|
31 | - CURLOPT_MAXREDIRS => 5, |
|
32 | - CURLINFO_HEADER_OUT => true, |
|
33 | - CURLOPT_ENCODING => 'identity', |
|
34 | - // phpcs:ignore |
|
35 | - CURLOPT_USERAGENT => 'Hybridauth, PHP Social Authentication Library (https://github.com/hybridauth/hybridauth)', |
|
36 | - ]; |
|
37 | - |
|
38 | - /** |
|
39 | - * Method request() arguments |
|
40 | - * |
|
41 | - * This is used for debugging. |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $requestArguments = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * Default request headers |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $requestHeader = [ |
|
53 | - 'Accept' => '*/*', |
|
54 | - 'Cache-Control' => 'max-age=0', |
|
55 | - 'Connection' => 'keep-alive', |
|
56 | - 'Expect' => '', |
|
57 | - 'Pragma' => '', |
|
58 | - ]; |
|
59 | - |
|
60 | - /** |
|
61 | - * Raw response returned by server |
|
62 | - * |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - protected $responseBody = ''; |
|
66 | - |
|
67 | - /** |
|
68 | - * Headers returned in the response |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $responseHeader = []; |
|
73 | - |
|
74 | - /** |
|
75 | - * Response HTTP status code |
|
76 | - * |
|
77 | - * @var int |
|
78 | - */ |
|
79 | - protected $responseHttpCode = 0; |
|
80 | - |
|
81 | - /** |
|
82 | - * Last curl error number |
|
83 | - * |
|
84 | - * @var mixed |
|
85 | - */ |
|
86 | - protected $responseClientError = null; |
|
87 | - |
|
88 | - /** |
|
89 | - * Information about the last transfer |
|
90 | - * |
|
91 | - * @var mixed |
|
92 | - */ |
|
93 | - protected $responseClientInfo = []; |
|
94 | - |
|
95 | - /** |
|
96 | - * Hybridauth logger instance |
|
97 | - * |
|
98 | - * @var object |
|
99 | - */ |
|
100 | - protected $logger = null; |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritdoc} |
|
104 | - */ |
|
105 | - public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
106 | - { |
|
107 | - $this->requestHeader = array_replace($this->requestHeader, (array)$headers); |
|
108 | - |
|
109 | - $this->requestArguments = [ |
|
110 | - 'uri' => $uri, |
|
111 | - 'method' => $method, |
|
112 | - 'parameters' => $parameters, |
|
113 | - 'headers' => $this->requestHeader, |
|
114 | - ]; |
|
115 | - |
|
116 | - $curl = curl_init(); |
|
117 | - |
|
118 | - switch ($method) { |
|
119 | - case 'GET': |
|
120 | - case 'DELETE': |
|
121 | - unset($this->curlOptions[CURLOPT_POST]); |
|
122 | - unset($this->curlOptions[CURLOPT_POSTFIELDS]); |
|
123 | - |
|
124 | - $uri = $uri . (strpos($uri, '?') ? '&' : '?') . http_build_query($parameters); |
|
125 | - if ($method === 'DELETE') { |
|
126 | - $this->curlOptions[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
|
127 | - } |
|
128 | - break; |
|
129 | - case 'PUT': |
|
130 | - case 'POST': |
|
131 | - case 'PATCH': |
|
132 | - $body_content = $multipart ? $parameters : http_build_query($parameters); |
|
133 | - if (isset($this->requestHeader['Content-Type']) |
|
134 | - && $this->requestHeader['Content-Type'] == 'application/json' |
|
135 | - ) { |
|
136 | - $body_content = json_encode($parameters); |
|
137 | - } |
|
138 | - |
|
139 | - if ($method === 'POST') { |
|
140 | - $this->curlOptions[CURLOPT_POST] = true; |
|
141 | - } else { |
|
142 | - $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $method; |
|
143 | - } |
|
144 | - $this->curlOptions[CURLOPT_POSTFIELDS] = $body_content; |
|
145 | - break; |
|
146 | - } |
|
147 | - |
|
148 | - $this->curlOptions[CURLOPT_URL] = $uri; |
|
149 | - $this->curlOptions[CURLOPT_HTTPHEADER] = $this->prepareRequestHeaders(); |
|
150 | - $this->curlOptions[CURLOPT_HEADERFUNCTION] = [$this, 'fetchResponseHeader']; |
|
151 | - |
|
152 | - foreach ($this->curlOptions as $opt => $value) { |
|
153 | - curl_setopt($curl, $opt, $value); |
|
154 | - } |
|
155 | - |
|
156 | - $response = curl_exec($curl); |
|
157 | - |
|
158 | - $this->responseBody = $response; |
|
159 | - $this->responseHttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
160 | - $this->responseClientError = curl_error($curl); |
|
161 | - $this->responseClientInfo = curl_getinfo($curl); |
|
162 | - |
|
163 | - if ($this->logger) { |
|
164 | - // phpcs:ignore |
|
165 | - $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
166 | - |
|
167 | - if (false === $response) { |
|
168 | - // phpcs:ignore |
|
169 | - $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - curl_close($curl); |
|
174 | - |
|
175 | - return $this->responseBody; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Get response details |
|
180 | - * |
|
181 | - * @return array Map structure of details |
|
182 | - */ |
|
183 | - public function getResponse() |
|
184 | - { |
|
185 | - $curlOptions = $this->curlOptions; |
|
186 | - |
|
187 | - $curlOptions[CURLOPT_HEADERFUNCTION] = '*omitted'; |
|
188 | - |
|
189 | - return [ |
|
190 | - 'request' => $this->getRequestArguments(), |
|
191 | - 'response' => [ |
|
192 | - 'code' => $this->getResponseHttpCode(), |
|
193 | - 'headers' => $this->getResponseHeader(), |
|
194 | - 'body' => $this->getResponseBody(), |
|
195 | - ], |
|
196 | - 'client' => [ |
|
197 | - 'error' => $this->getResponseClientError(), |
|
198 | - 'info' => $this->getResponseClientInfo(), |
|
199 | - 'opts' => $curlOptions, |
|
200 | - ], |
|
201 | - ]; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Reset curl options |
|
206 | - * |
|
207 | - * @param array $curlOptions |
|
208 | - */ |
|
209 | - public function setCurlOptions($curlOptions) |
|
210 | - { |
|
211 | - foreach ($curlOptions as $opt => $value) { |
|
212 | - $this->curlOptions[$opt] = $value; |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Set logger instance |
|
218 | - * |
|
219 | - * @param object $logger |
|
220 | - */ |
|
221 | - public function setLogger($logger) |
|
222 | - { |
|
223 | - $this->logger = $logger; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * {@inheritdoc} |
|
228 | - */ |
|
229 | - public function getResponseBody() |
|
230 | - { |
|
231 | - return $this->responseBody; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * {@inheritdoc} |
|
236 | - */ |
|
237 | - public function getResponseHeader() |
|
238 | - { |
|
239 | - return $this->responseHeader; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * {@inheritdoc} |
|
244 | - */ |
|
245 | - public function getResponseHttpCode() |
|
246 | - { |
|
247 | - return $this->responseHttpCode; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * {@inheritdoc} |
|
252 | - */ |
|
253 | - public function getResponseClientError() |
|
254 | - { |
|
255 | - return $this->responseClientError; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * @return array |
|
260 | - */ |
|
261 | - protected function getResponseClientInfo() |
|
262 | - { |
|
263 | - return $this->responseClientInfo; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Returns method request() arguments |
|
268 | - * |
|
269 | - * This is used for debugging. |
|
270 | - * |
|
271 | - * @return array |
|
272 | - */ |
|
273 | - protected function getRequestArguments() |
|
274 | - { |
|
275 | - return $this->requestArguments; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Fetch server response headers |
|
280 | - * |
|
281 | - * @param mixed $curl |
|
282 | - * @param string $header |
|
283 | - * |
|
284 | - * @return int |
|
285 | - */ |
|
286 | - protected function fetchResponseHeader($curl, $header) |
|
287 | - { |
|
288 | - $pos = strpos($header, ':'); |
|
289 | - |
|
290 | - if (!empty($pos)) { |
|
291 | - $key = str_replace('-', '_', strtolower(substr($header, 0, $pos))); |
|
292 | - |
|
293 | - $value = trim(substr($header, $pos + 2)); |
|
294 | - |
|
295 | - $this->responseHeader[$key] = $value; |
|
296 | - } |
|
297 | - |
|
298 | - return strlen($header); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Convert request headers to the expect curl format |
|
303 | - * |
|
304 | - * @return array |
|
305 | - */ |
|
306 | - protected function prepareRequestHeaders() |
|
307 | - { |
|
308 | - $headers = []; |
|
309 | - |
|
310 | - foreach ($this->requestHeader as $header => $value) { |
|
311 | - $headers[] = trim($header) . ': ' . trim($value); |
|
312 | - } |
|
313 | - |
|
314 | - return $headers; |
|
315 | - } |
|
15 | + /** |
|
16 | + * Default curl options |
|
17 | + * |
|
18 | + * These defaults options can be overwritten when sending requests. |
|
19 | + * |
|
20 | + * See setCurlOptions() |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $curlOptions = [ |
|
25 | + CURLOPT_TIMEOUT => 30, |
|
26 | + CURLOPT_CONNECTTIMEOUT => 30, |
|
27 | + CURLOPT_SSL_VERIFYPEER => false, |
|
28 | + CURLOPT_SSL_VERIFYHOST => false, |
|
29 | + CURLOPT_RETURNTRANSFER => true, |
|
30 | + CURLOPT_FOLLOWLOCATION => true, |
|
31 | + CURLOPT_MAXREDIRS => 5, |
|
32 | + CURLINFO_HEADER_OUT => true, |
|
33 | + CURLOPT_ENCODING => 'identity', |
|
34 | + // phpcs:ignore |
|
35 | + CURLOPT_USERAGENT => 'Hybridauth, PHP Social Authentication Library (https://github.com/hybridauth/hybridauth)', |
|
36 | + ]; |
|
37 | + |
|
38 | + /** |
|
39 | + * Method request() arguments |
|
40 | + * |
|
41 | + * This is used for debugging. |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $requestArguments = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * Default request headers |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $requestHeader = [ |
|
53 | + 'Accept' => '*/*', |
|
54 | + 'Cache-Control' => 'max-age=0', |
|
55 | + 'Connection' => 'keep-alive', |
|
56 | + 'Expect' => '', |
|
57 | + 'Pragma' => '', |
|
58 | + ]; |
|
59 | + |
|
60 | + /** |
|
61 | + * Raw response returned by server |
|
62 | + * |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + protected $responseBody = ''; |
|
66 | + |
|
67 | + /** |
|
68 | + * Headers returned in the response |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $responseHeader = []; |
|
73 | + |
|
74 | + /** |
|
75 | + * Response HTTP status code |
|
76 | + * |
|
77 | + * @var int |
|
78 | + */ |
|
79 | + protected $responseHttpCode = 0; |
|
80 | + |
|
81 | + /** |
|
82 | + * Last curl error number |
|
83 | + * |
|
84 | + * @var mixed |
|
85 | + */ |
|
86 | + protected $responseClientError = null; |
|
87 | + |
|
88 | + /** |
|
89 | + * Information about the last transfer |
|
90 | + * |
|
91 | + * @var mixed |
|
92 | + */ |
|
93 | + protected $responseClientInfo = []; |
|
94 | + |
|
95 | + /** |
|
96 | + * Hybridauth logger instance |
|
97 | + * |
|
98 | + * @var object |
|
99 | + */ |
|
100 | + protected $logger = null; |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritdoc} |
|
104 | + */ |
|
105 | + public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
106 | + { |
|
107 | + $this->requestHeader = array_replace($this->requestHeader, (array)$headers); |
|
108 | + |
|
109 | + $this->requestArguments = [ |
|
110 | + 'uri' => $uri, |
|
111 | + 'method' => $method, |
|
112 | + 'parameters' => $parameters, |
|
113 | + 'headers' => $this->requestHeader, |
|
114 | + ]; |
|
115 | + |
|
116 | + $curl = curl_init(); |
|
117 | + |
|
118 | + switch ($method) { |
|
119 | + case 'GET': |
|
120 | + case 'DELETE': |
|
121 | + unset($this->curlOptions[CURLOPT_POST]); |
|
122 | + unset($this->curlOptions[CURLOPT_POSTFIELDS]); |
|
123 | + |
|
124 | + $uri = $uri . (strpos($uri, '?') ? '&' : '?') . http_build_query($parameters); |
|
125 | + if ($method === 'DELETE') { |
|
126 | + $this->curlOptions[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
|
127 | + } |
|
128 | + break; |
|
129 | + case 'PUT': |
|
130 | + case 'POST': |
|
131 | + case 'PATCH': |
|
132 | + $body_content = $multipart ? $parameters : http_build_query($parameters); |
|
133 | + if (isset($this->requestHeader['Content-Type']) |
|
134 | + && $this->requestHeader['Content-Type'] == 'application/json' |
|
135 | + ) { |
|
136 | + $body_content = json_encode($parameters); |
|
137 | + } |
|
138 | + |
|
139 | + if ($method === 'POST') { |
|
140 | + $this->curlOptions[CURLOPT_POST] = true; |
|
141 | + } else { |
|
142 | + $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $method; |
|
143 | + } |
|
144 | + $this->curlOptions[CURLOPT_POSTFIELDS] = $body_content; |
|
145 | + break; |
|
146 | + } |
|
147 | + |
|
148 | + $this->curlOptions[CURLOPT_URL] = $uri; |
|
149 | + $this->curlOptions[CURLOPT_HTTPHEADER] = $this->prepareRequestHeaders(); |
|
150 | + $this->curlOptions[CURLOPT_HEADERFUNCTION] = [$this, 'fetchResponseHeader']; |
|
151 | + |
|
152 | + foreach ($this->curlOptions as $opt => $value) { |
|
153 | + curl_setopt($curl, $opt, $value); |
|
154 | + } |
|
155 | + |
|
156 | + $response = curl_exec($curl); |
|
157 | + |
|
158 | + $this->responseBody = $response; |
|
159 | + $this->responseHttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
160 | + $this->responseClientError = curl_error($curl); |
|
161 | + $this->responseClientInfo = curl_getinfo($curl); |
|
162 | + |
|
163 | + if ($this->logger) { |
|
164 | + // phpcs:ignore |
|
165 | + $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
166 | + |
|
167 | + if (false === $response) { |
|
168 | + // phpcs:ignore |
|
169 | + $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + curl_close($curl); |
|
174 | + |
|
175 | + return $this->responseBody; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Get response details |
|
180 | + * |
|
181 | + * @return array Map structure of details |
|
182 | + */ |
|
183 | + public function getResponse() |
|
184 | + { |
|
185 | + $curlOptions = $this->curlOptions; |
|
186 | + |
|
187 | + $curlOptions[CURLOPT_HEADERFUNCTION] = '*omitted'; |
|
188 | + |
|
189 | + return [ |
|
190 | + 'request' => $this->getRequestArguments(), |
|
191 | + 'response' => [ |
|
192 | + 'code' => $this->getResponseHttpCode(), |
|
193 | + 'headers' => $this->getResponseHeader(), |
|
194 | + 'body' => $this->getResponseBody(), |
|
195 | + ], |
|
196 | + 'client' => [ |
|
197 | + 'error' => $this->getResponseClientError(), |
|
198 | + 'info' => $this->getResponseClientInfo(), |
|
199 | + 'opts' => $curlOptions, |
|
200 | + ], |
|
201 | + ]; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Reset curl options |
|
206 | + * |
|
207 | + * @param array $curlOptions |
|
208 | + */ |
|
209 | + public function setCurlOptions($curlOptions) |
|
210 | + { |
|
211 | + foreach ($curlOptions as $opt => $value) { |
|
212 | + $this->curlOptions[$opt] = $value; |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Set logger instance |
|
218 | + * |
|
219 | + * @param object $logger |
|
220 | + */ |
|
221 | + public function setLogger($logger) |
|
222 | + { |
|
223 | + $this->logger = $logger; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * {@inheritdoc} |
|
228 | + */ |
|
229 | + public function getResponseBody() |
|
230 | + { |
|
231 | + return $this->responseBody; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * {@inheritdoc} |
|
236 | + */ |
|
237 | + public function getResponseHeader() |
|
238 | + { |
|
239 | + return $this->responseHeader; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * {@inheritdoc} |
|
244 | + */ |
|
245 | + public function getResponseHttpCode() |
|
246 | + { |
|
247 | + return $this->responseHttpCode; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * {@inheritdoc} |
|
252 | + */ |
|
253 | + public function getResponseClientError() |
|
254 | + { |
|
255 | + return $this->responseClientError; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * @return array |
|
260 | + */ |
|
261 | + protected function getResponseClientInfo() |
|
262 | + { |
|
263 | + return $this->responseClientInfo; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Returns method request() arguments |
|
268 | + * |
|
269 | + * This is used for debugging. |
|
270 | + * |
|
271 | + * @return array |
|
272 | + */ |
|
273 | + protected function getRequestArguments() |
|
274 | + { |
|
275 | + return $this->requestArguments; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Fetch server response headers |
|
280 | + * |
|
281 | + * @param mixed $curl |
|
282 | + * @param string $header |
|
283 | + * |
|
284 | + * @return int |
|
285 | + */ |
|
286 | + protected function fetchResponseHeader($curl, $header) |
|
287 | + { |
|
288 | + $pos = strpos($header, ':'); |
|
289 | + |
|
290 | + if (!empty($pos)) { |
|
291 | + $key = str_replace('-', '_', strtolower(substr($header, 0, $pos))); |
|
292 | + |
|
293 | + $value = trim(substr($header, $pos + 2)); |
|
294 | + |
|
295 | + $this->responseHeader[$key] = $value; |
|
296 | + } |
|
297 | + |
|
298 | + return strlen($header); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Convert request headers to the expect curl format |
|
303 | + * |
|
304 | + * @return array |
|
305 | + */ |
|
306 | + protected function prepareRequestHeaders() |
|
307 | + { |
|
308 | + $headers = []; |
|
309 | + |
|
310 | + foreach ($this->requestHeader as $header => $value) { |
|
311 | + $headers[] = trim($header) . ': ' . trim($value); |
|
312 | + } |
|
313 | + |
|
314 | + return $headers; |
|
315 | + } |
|
316 | 316 | } |
@@ -27,590 +27,590 @@ |
||
27 | 27 | */ |
28 | 28 | abstract class OAuth1 extends AbstractAdapter implements AdapterInterface |
29 | 29 | { |
30 | - /** |
|
31 | - * Base URL to provider API |
|
32 | - * |
|
33 | - * This var will be used to build urls when sending signed requests |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $apiBaseUrl = ''; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $authorizeUrl = ''; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - protected $requestTokenUrl = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $accessTokenUrl = ''; |
|
53 | - |
|
54 | - /** |
|
55 | - * IPD API Documentation |
|
56 | - * |
|
57 | - * OPTIONAL. |
|
58 | - * |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - protected $apiDocumentation = ''; |
|
62 | - |
|
63 | - /** |
|
64 | - * OAuth Version |
|
65 | - * |
|
66 | - * '1.0' OAuth Core 1.0 |
|
67 | - * '1.0a' OAuth Core 1.0 Revision A |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - protected $oauth1Version = '1.0a'; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - protected $consumerKey = null; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var string |
|
80 | - */ |
|
81 | - protected $consumerSecret = null; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var object |
|
85 | - */ |
|
86 | - protected $OAuthConsumer = null; |
|
87 | - |
|
88 | - /** |
|
89 | - * @var object |
|
90 | - */ |
|
91 | - protected $sha1Method = null; |
|
92 | - |
|
93 | - /** |
|
94 | - * @var object |
|
95 | - */ |
|
96 | - protected $consumerToken = null; |
|
97 | - |
|
98 | - /** |
|
99 | - * Authorization Url Parameters |
|
100 | - * |
|
101 | - * @var bool |
|
102 | - */ |
|
103 | - protected $AuthorizeUrlParameters = []; |
|
104 | - |
|
105 | - /** |
|
106 | - * @var string |
|
107 | - */ |
|
108 | - protected $requestTokenMethod = 'POST'; |
|
109 | - |
|
110 | - /** |
|
111 | - * @var array |
|
112 | - */ |
|
113 | - protected $requestTokenParameters = []; |
|
114 | - |
|
115 | - /** |
|
116 | - * @var array |
|
117 | - */ |
|
118 | - protected $requestTokenHeaders = []; |
|
119 | - |
|
120 | - /** |
|
121 | - * @var string |
|
122 | - */ |
|
123 | - protected $tokenExchangeMethod = 'POST'; |
|
124 | - |
|
125 | - /** |
|
126 | - * @var array |
|
127 | - */ |
|
128 | - protected $tokenExchangeParameters = []; |
|
129 | - |
|
130 | - /** |
|
131 | - * @var array |
|
132 | - */ |
|
133 | - protected $tokenExchangeHeaders = []; |
|
134 | - |
|
135 | - /** |
|
136 | - * @var array |
|
137 | - */ |
|
138 | - protected $apiRequestParameters = []; |
|
139 | - |
|
140 | - /** |
|
141 | - * @var array |
|
142 | - */ |
|
143 | - protected $apiRequestHeaders = []; |
|
144 | - |
|
145 | - /** |
|
146 | - * {@inheritdoc} |
|
147 | - */ |
|
148 | - protected function configure() |
|
149 | - { |
|
150 | - $this->consumerKey = $this->config->filter('keys')->get('id') ?: $this->config->filter('keys')->get('key'); |
|
151 | - $this->consumerSecret = $this->config->filter('keys')->get('secret'); |
|
152 | - |
|
153 | - if (!$this->consumerKey || !$this->consumerSecret) { |
|
154 | - throw new InvalidApplicationCredentialsException( |
|
155 | - 'Your application id is required in order to connect to ' . $this->providerId |
|
156 | - ); |
|
157 | - } |
|
158 | - |
|
159 | - if ($this->config->exists('tokens')) { |
|
160 | - $this->setAccessToken($this->config->get('tokens')); |
|
161 | - } |
|
162 | - |
|
163 | - $this->setCallback($this->config->get('callback')); |
|
164 | - $this->setApiEndpoints($this->config->get('endpoints')); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * {@inheritdoc} |
|
169 | - */ |
|
170 | - protected function initialize() |
|
171 | - { |
|
172 | - /** |
|
173 | - * Set up OAuth Signature and Consumer |
|
174 | - * |
|
175 | - * OAuth Core: All Token requests and Protected Resources requests MUST be signed |
|
176 | - * by the Consumer and verified by the Service Provider. |
|
177 | - * |
|
178 | - * The protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT.. |
|
179 | - * |
|
180 | - * The Consumer declares a signature method in the oauth_signature_method parameter.. |
|
181 | - * |
|
182 | - * http://oauth.net/core/1.0a/#signing_process |
|
183 | - */ |
|
184 | - $this->sha1Method = new OAuthSignatureMethodHMACSHA1(); |
|
185 | - |
|
186 | - $this->OAuthConsumer = new OAuthConsumer( |
|
187 | - $this->consumerKey, |
|
188 | - $this->consumerSecret |
|
189 | - ); |
|
190 | - |
|
191 | - if ($this->getStoredData('request_token')) { |
|
192 | - $this->consumerToken = new OAuthConsumer( |
|
193 | - $this->getStoredData('request_token'), |
|
194 | - $this->getStoredData('request_token_secret') |
|
195 | - ); |
|
196 | - } |
|
197 | - |
|
198 | - if ($this->getStoredData('access_token')) { |
|
199 | - $this->consumerToken = new OAuthConsumer( |
|
200 | - $this->getStoredData('access_token'), |
|
201 | - $this->getStoredData('access_token_secret') |
|
202 | - ); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * {@inheritdoc} |
|
208 | - */ |
|
209 | - public function authenticate() |
|
210 | - { |
|
211 | - $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
212 | - |
|
213 | - if ($this->isConnected()) { |
|
214 | - return true; |
|
215 | - } |
|
216 | - |
|
217 | - try { |
|
218 | - if (!$this->getStoredData('request_token')) { |
|
219 | - // Start a new flow. |
|
220 | - $this->authenticateBegin(); |
|
221 | - } elseif (empty($_GET['oauth_token']) && empty($_GET['denied'])) { |
|
222 | - // A previous authentication was not finished, and this request is not finishing it. |
|
223 | - $this->authenticateBegin(); |
|
224 | - } else { |
|
225 | - // Finish a flow. |
|
226 | - $this->authenticateFinish(); |
|
227 | - } |
|
228 | - } catch (Exception $exception) { |
|
229 | - $this->clearStoredData(); |
|
230 | - |
|
231 | - throw $exception; |
|
232 | - } |
|
233 | - |
|
234 | - return null; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * {@inheritdoc} |
|
239 | - */ |
|
240 | - public function isConnected() |
|
241 | - { |
|
242 | - return (bool)$this->getStoredData('access_token'); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Initiate the authorization protocol |
|
247 | - * |
|
248 | - * 1. Obtaining an Unauthorized Request Token |
|
249 | - * 2. Build Authorization URL for Authorization Request and redirect the user-agent to the |
|
250 | - * Authorization Server. |
|
251 | - */ |
|
252 | - protected function authenticateBegin() |
|
253 | - { |
|
254 | - $response = $this->requestAuthToken(); |
|
255 | - |
|
256 | - $this->validateAuthTokenRequest($response); |
|
257 | - |
|
258 | - $authUrl = $this->getAuthorizeUrl(); |
|
259 | - |
|
260 | - $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this)), [$authUrl]); |
|
261 | - |
|
262 | - HttpClient\Util::redirect($authUrl); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Finalize the authorization process |
|
267 | - * |
|
268 | - * @throws AuthorizationDeniedException |
|
269 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
270 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
271 | - * @throws InvalidAccessTokenException |
|
272 | - * @throws InvalidOauthTokenException |
|
273 | - */ |
|
274 | - protected function authenticateFinish() |
|
275 | - { |
|
276 | - $this->logger->debug( |
|
277 | - sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
278 | - [HttpClient\Util::getCurrentUrl(true)] |
|
279 | - ); |
|
280 | - |
|
281 | - $denied = filter_input(INPUT_GET, 'denied'); |
|
282 | - $oauth_problem = filter_input(INPUT_GET, 'oauth_problem'); |
|
283 | - $oauth_token = filter_input(INPUT_GET, 'oauth_token'); |
|
284 | - $oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier'); |
|
285 | - |
|
286 | - if ($denied) { |
|
287 | - throw new AuthorizationDeniedException( |
|
288 | - 'User denied access request. Provider returned a denied token: ' . htmlentities($denied) |
|
289 | - ); |
|
290 | - } |
|
291 | - |
|
292 | - if ($oauth_problem) { |
|
293 | - throw new InvalidOauthTokenException( |
|
294 | - 'Provider returned an error. oauth_problem: ' . htmlentities($oauth_problem) |
|
295 | - ); |
|
296 | - } |
|
297 | - |
|
298 | - if (!$oauth_token) { |
|
299 | - throw new InvalidOauthTokenException( |
|
300 | - 'Expecting a non-null oauth_token to continue the authorization flow.' |
|
301 | - ); |
|
302 | - } |
|
303 | - |
|
304 | - $response = $this->exchangeAuthTokenForAccessToken($oauth_token, $oauth_verifier); |
|
305 | - |
|
306 | - $this->validateAccessTokenExchange($response); |
|
307 | - |
|
308 | - $this->initialize(); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Build Authorization URL for Authorization Request |
|
313 | - * |
|
314 | - * @param array $parameters |
|
315 | - * |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - protected function getAuthorizeUrl($parameters = []) |
|
319 | - { |
|
320 | - $this->AuthorizeUrlParameters = !empty($parameters) |
|
321 | - ? $parameters |
|
322 | - : array_replace( |
|
323 | - (array)$this->AuthorizeUrlParameters, |
|
324 | - (array)$this->config->get('authorize_url_parameters') |
|
325 | - ); |
|
326 | - |
|
327 | - $this->AuthorizeUrlParameters['oauth_token'] = $this->getStoredData('request_token'); |
|
328 | - |
|
329 | - return $this->authorizeUrl . '?' . http_build_query($this->AuthorizeUrlParameters, '', '&'); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Unauthorized Request Token |
|
334 | - * |
|
335 | - * OAuth Core: The Consumer obtains an unauthorized Request Token by asking the Service Provider |
|
336 | - * to issue a Token. The Request Token's sole purpose is to receive User approval and can only |
|
337 | - * be used to obtain an Access Token. |
|
338 | - * |
|
339 | - * http://oauth.net/core/1.0/#auth_step1 |
|
340 | - * 6.1.1. Consumer Obtains a Request Token |
|
341 | - * |
|
342 | - * @return string Raw Provider API response |
|
343 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
344 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
345 | - */ |
|
346 | - protected function requestAuthToken() |
|
347 | - { |
|
348 | - /** |
|
349 | - * OAuth Core 1.0 Revision A: oauth_callback: An absolute URL to which the Service Provider will redirect |
|
350 | - * the User back when the Obtaining User Authorization step is completed. |
|
351 | - * |
|
352 | - * http://oauth.net/core/1.0a/#auth_step1 |
|
353 | - */ |
|
354 | - if ('1.0a' == $this->oauth1Version) { |
|
355 | - $this->requestTokenParameters['oauth_callback'] = $this->callback; |
|
356 | - } |
|
357 | - |
|
358 | - $response = $this->oauthRequest( |
|
359 | - $this->requestTokenUrl, |
|
360 | - $this->requestTokenMethod, |
|
361 | - $this->requestTokenParameters, |
|
362 | - $this->requestTokenHeaders |
|
363 | - ); |
|
364 | - |
|
365 | - return $response; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Validate Unauthorized Request Token Response |
|
370 | - * |
|
371 | - * OAuth Core: The Service Provider verifies the signature and Consumer Key. If successful, |
|
372 | - * it generates a Request Token and Token Secret and returns them to the Consumer in the HTTP |
|
373 | - * response body. |
|
374 | - * |
|
375 | - * http://oauth.net/core/1.0/#auth_step1 |
|
376 | - * 6.1.2. Service Provider Issues an Unauthorized Request Token |
|
377 | - * |
|
378 | - * @param string $response |
|
379 | - * |
|
380 | - * @return \Hybridauth\Data\Collection |
|
381 | - * @throws InvalidOauthTokenException |
|
382 | - */ |
|
383 | - protected function validateAuthTokenRequest($response) |
|
384 | - { |
|
385 | - /** |
|
386 | - * The response contains the following parameters: |
|
387 | - * |
|
388 | - * - oauth_token The Request Token. |
|
389 | - * - oauth_token_secret The Token Secret. |
|
390 | - * - oauth_callback_confirmed MUST be present and set to true. |
|
391 | - * |
|
392 | - * http://oauth.net/core/1.0/#auth_step1 |
|
393 | - * 6.1.2. Service Provider Issues an Unauthorized Request Token |
|
394 | - * |
|
395 | - * Example of a successful response: |
|
396 | - * |
|
397 | - * HTTP/1.1 200 OK |
|
398 | - * Content-Type: text/html; charset=utf-8 |
|
399 | - * Cache-Control: no-store |
|
400 | - * Pragma: no-cache |
|
401 | - * |
|
402 | - * oauth_token=80359084-clg1DEtxQF3wstTcyUdHF3wsdHM&oauth_token_secret=OIF07hPmJB:P |
|
403 | - * 6qiHTi1znz6qiH3tTcyUdHnz6qiH3tTcyUdH3xW3wsDvV08e&example_parameter=example_value |
|
404 | - * |
|
405 | - * OAuthUtil::parse_parameters will attempt to decode the raw response into an array. |
|
406 | - */ |
|
407 | - $tokens = OAuthUtil::parse_parameters($response); |
|
408 | - |
|
409 | - $collection = new Data\Collection($tokens); |
|
410 | - |
|
411 | - if (!$collection->exists('oauth_token')) { |
|
412 | - throw new InvalidOauthTokenException( |
|
413 | - 'Provider returned no oauth_token: ' . htmlentities($response) |
|
414 | - ); |
|
415 | - } |
|
416 | - |
|
417 | - $this->consumerToken = new OAuthConsumer( |
|
418 | - $tokens['oauth_token'], |
|
419 | - $tokens['oauth_token_secret'] |
|
420 | - ); |
|
421 | - |
|
422 | - $this->storeData('request_token', $tokens['oauth_token']); |
|
423 | - $this->storeData('request_token_secret', $tokens['oauth_token_secret']); |
|
424 | - |
|
425 | - return $collection; |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * Requests an Access Token |
|
430 | - * |
|
431 | - * OAuth Core: The Request Token and Token Secret MUST be exchanged for an Access Token and Token Secret. |
|
432 | - * |
|
433 | - * http://oauth.net/core/1.0a/#auth_step3 |
|
434 | - * 6.3.1. Consumer Requests an Access Token |
|
435 | - * |
|
436 | - * @param string $oauth_token |
|
437 | - * @param string $oauth_verifier |
|
438 | - * |
|
439 | - * @return string Raw Provider API response |
|
440 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
441 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
442 | - */ |
|
443 | - protected function exchangeAuthTokenForAccessToken($oauth_token, $oauth_verifier = '') |
|
444 | - { |
|
445 | - $this->tokenExchangeParameters['oauth_token'] = $oauth_token; |
|
446 | - |
|
447 | - /** |
|
448 | - * OAuth Core 1.0 Revision A: oauth_verifier: The verification code received from the Service Provider |
|
449 | - * in the "Service Provider Directs the User Back to the Consumer" step. |
|
450 | - * |
|
451 | - * http://oauth.net/core/1.0a/#auth_step3 |
|
452 | - */ |
|
453 | - if ('1.0a' == $this->oauth1Version) { |
|
454 | - $this->tokenExchangeParameters['oauth_verifier'] = $oauth_verifier; |
|
455 | - } |
|
456 | - |
|
457 | - $response = $this->oauthRequest( |
|
458 | - $this->accessTokenUrl, |
|
459 | - $this->tokenExchangeMethod, |
|
460 | - $this->tokenExchangeParameters, |
|
461 | - $this->tokenExchangeHeaders |
|
462 | - ); |
|
463 | - |
|
464 | - return $response; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Validate Access Token Response |
|
469 | - * |
|
470 | - * OAuth Core: If successful, the Service Provider generates an Access Token and Token Secret and returns |
|
471 | - * them in the HTTP response body. |
|
472 | - * |
|
473 | - * The Access Token and Token Secret are stored by the Consumer and used when signing Protected Resources requests. |
|
474 | - * |
|
475 | - * http://oauth.net/core/1.0a/#auth_step3 |
|
476 | - * 6.3.2. Service Provider Grants an Access Token |
|
477 | - * |
|
478 | - * @param string $response |
|
479 | - * |
|
480 | - * @return \Hybridauth\Data\Collection |
|
481 | - * @throws InvalidAccessTokenException |
|
482 | - */ |
|
483 | - protected function validateAccessTokenExchange($response) |
|
484 | - { |
|
485 | - /** |
|
486 | - * The response contains the following parameters: |
|
487 | - * |
|
488 | - * - oauth_token The Access Token. |
|
489 | - * - oauth_token_secret The Token Secret. |
|
490 | - * |
|
491 | - * http://oauth.net/core/1.0/#auth_step3 |
|
492 | - * 6.3.2. Service Provider Grants an Access Token |
|
493 | - * |
|
494 | - * Example of a successful response: |
|
495 | - * |
|
496 | - * HTTP/1.1 200 OK |
|
497 | - * Content-Type: text/html; charset=utf-8 |
|
498 | - * Cache-Control: no-store |
|
499 | - * Pragma: no-cache |
|
500 | - * |
|
501 | - * oauth_token=sHeLU7Far428zj8PzlWR75&oauth_token_secret=fXb30rzoG&oauth_callback_confirmed=true |
|
502 | - * |
|
503 | - * OAuthUtil::parse_parameters will attempt to decode the raw response into an array. |
|
504 | - */ |
|
505 | - $tokens = OAuthUtil::parse_parameters($response); |
|
506 | - |
|
507 | - $collection = new Data\Collection($tokens); |
|
508 | - |
|
509 | - if (!$collection->exists('oauth_token')) { |
|
510 | - throw new InvalidAccessTokenException( |
|
511 | - 'Provider returned no access_token: ' . htmlentities($response) |
|
512 | - ); |
|
513 | - } |
|
514 | - |
|
515 | - $this->consumerToken = new OAuthConsumer( |
|
516 | - $collection->get('oauth_token'), |
|
517 | - $collection->get('oauth_token_secret') |
|
518 | - ); |
|
519 | - |
|
520 | - $this->storeData('access_token', $collection->get('oauth_token')); |
|
521 | - $this->storeData('access_token_secret', $collection->get('oauth_token_secret')); |
|
522 | - |
|
523 | - $this->deleteStoredData('request_token'); |
|
524 | - $this->deleteStoredData('request_token_secret'); |
|
525 | - |
|
526 | - return $collection; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Send a signed request to provider API |
|
531 | - * |
|
532 | - * Note: Since the specifics of error responses is beyond the scope of RFC6749 and OAuth specifications, |
|
533 | - * Hybridauth will consider any HTTP status code that is different than '200 OK' as an ERROR. |
|
534 | - * |
|
535 | - * @param string $url |
|
536 | - * @param string $method |
|
537 | - * @param array $parameters |
|
538 | - * @param array $headers |
|
539 | - * @param bool $multipart |
|
540 | - * |
|
541 | - * @return mixed |
|
542 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
543 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
544 | - */ |
|
545 | - public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
546 | - { |
|
547 | - // refresh tokens if needed |
|
548 | - $this->maintainToken(); |
|
549 | - |
|
550 | - if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) { |
|
551 | - $url = rtrim($this->apiBaseUrl, '/') . '/' . ltrim($url, '/'); |
|
552 | - } |
|
553 | - |
|
554 | - $parameters = array_replace($this->apiRequestParameters, (array)$parameters); |
|
555 | - |
|
556 | - $headers = array_replace($this->apiRequestHeaders, (array)$headers); |
|
557 | - |
|
558 | - $response = $this->oauthRequest($url, $method, $parameters, $headers, $multipart); |
|
559 | - |
|
560 | - $response = (new Data\Parser())->parse($response); |
|
561 | - |
|
562 | - return $response; |
|
563 | - } |
|
564 | - |
|
565 | - /** |
|
566 | - * Setup and Send a Signed Oauth Request |
|
567 | - * |
|
568 | - * This method uses OAuth Library. |
|
569 | - * |
|
570 | - * @param string $uri |
|
571 | - * @param string $method |
|
572 | - * @param array $parameters |
|
573 | - * @param array $headers |
|
574 | - * @param bool $multipart |
|
575 | - * |
|
576 | - * @return string Raw Provider API response |
|
577 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
578 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
579 | - */ |
|
580 | - protected function oauthRequest($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
581 | - { |
|
582 | - $signing_parameters = $parameters; |
|
583 | - if ($multipart) { |
|
584 | - $signing_parameters = []; |
|
585 | - } |
|
586 | - |
|
587 | - $request = OAuthRequest::from_consumer_and_token( |
|
588 | - $this->OAuthConsumer, |
|
589 | - $this->consumerToken, |
|
590 | - $method, |
|
591 | - $uri, |
|
592 | - $signing_parameters |
|
593 | - ); |
|
594 | - |
|
595 | - $request->sign_request( |
|
596 | - $this->sha1Method, |
|
597 | - $this->OAuthConsumer, |
|
598 | - $this->consumerToken |
|
599 | - ); |
|
600 | - |
|
601 | - $uri = $request->get_normalized_http_url(); |
|
602 | - $headers = array_replace($request->to_header(), (array)$headers); |
|
603 | - |
|
604 | - $response = $this->httpClient->request( |
|
605 | - $uri, |
|
606 | - $method, |
|
607 | - $parameters, |
|
608 | - $headers, |
|
609 | - $multipart |
|
610 | - ); |
|
611 | - |
|
612 | - $this->validateApiResponse('Signed API request to ' . $uri . ' has returned an error'); |
|
613 | - |
|
614 | - return $response; |
|
615 | - } |
|
30 | + /** |
|
31 | + * Base URL to provider API |
|
32 | + * |
|
33 | + * This var will be used to build urls when sending signed requests |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $apiBaseUrl = ''; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $authorizeUrl = ''; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + protected $requestTokenUrl = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $accessTokenUrl = ''; |
|
53 | + |
|
54 | + /** |
|
55 | + * IPD API Documentation |
|
56 | + * |
|
57 | + * OPTIONAL. |
|
58 | + * |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + protected $apiDocumentation = ''; |
|
62 | + |
|
63 | + /** |
|
64 | + * OAuth Version |
|
65 | + * |
|
66 | + * '1.0' OAuth Core 1.0 |
|
67 | + * '1.0a' OAuth Core 1.0 Revision A |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + protected $oauth1Version = '1.0a'; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + protected $consumerKey = null; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var string |
|
80 | + */ |
|
81 | + protected $consumerSecret = null; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var object |
|
85 | + */ |
|
86 | + protected $OAuthConsumer = null; |
|
87 | + |
|
88 | + /** |
|
89 | + * @var object |
|
90 | + */ |
|
91 | + protected $sha1Method = null; |
|
92 | + |
|
93 | + /** |
|
94 | + * @var object |
|
95 | + */ |
|
96 | + protected $consumerToken = null; |
|
97 | + |
|
98 | + /** |
|
99 | + * Authorization Url Parameters |
|
100 | + * |
|
101 | + * @var bool |
|
102 | + */ |
|
103 | + protected $AuthorizeUrlParameters = []; |
|
104 | + |
|
105 | + /** |
|
106 | + * @var string |
|
107 | + */ |
|
108 | + protected $requestTokenMethod = 'POST'; |
|
109 | + |
|
110 | + /** |
|
111 | + * @var array |
|
112 | + */ |
|
113 | + protected $requestTokenParameters = []; |
|
114 | + |
|
115 | + /** |
|
116 | + * @var array |
|
117 | + */ |
|
118 | + protected $requestTokenHeaders = []; |
|
119 | + |
|
120 | + /** |
|
121 | + * @var string |
|
122 | + */ |
|
123 | + protected $tokenExchangeMethod = 'POST'; |
|
124 | + |
|
125 | + /** |
|
126 | + * @var array |
|
127 | + */ |
|
128 | + protected $tokenExchangeParameters = []; |
|
129 | + |
|
130 | + /** |
|
131 | + * @var array |
|
132 | + */ |
|
133 | + protected $tokenExchangeHeaders = []; |
|
134 | + |
|
135 | + /** |
|
136 | + * @var array |
|
137 | + */ |
|
138 | + protected $apiRequestParameters = []; |
|
139 | + |
|
140 | + /** |
|
141 | + * @var array |
|
142 | + */ |
|
143 | + protected $apiRequestHeaders = []; |
|
144 | + |
|
145 | + /** |
|
146 | + * {@inheritdoc} |
|
147 | + */ |
|
148 | + protected function configure() |
|
149 | + { |
|
150 | + $this->consumerKey = $this->config->filter('keys')->get('id') ?: $this->config->filter('keys')->get('key'); |
|
151 | + $this->consumerSecret = $this->config->filter('keys')->get('secret'); |
|
152 | + |
|
153 | + if (!$this->consumerKey || !$this->consumerSecret) { |
|
154 | + throw new InvalidApplicationCredentialsException( |
|
155 | + 'Your application id is required in order to connect to ' . $this->providerId |
|
156 | + ); |
|
157 | + } |
|
158 | + |
|
159 | + if ($this->config->exists('tokens')) { |
|
160 | + $this->setAccessToken($this->config->get('tokens')); |
|
161 | + } |
|
162 | + |
|
163 | + $this->setCallback($this->config->get('callback')); |
|
164 | + $this->setApiEndpoints($this->config->get('endpoints')); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * {@inheritdoc} |
|
169 | + */ |
|
170 | + protected function initialize() |
|
171 | + { |
|
172 | + /** |
|
173 | + * Set up OAuth Signature and Consumer |
|
174 | + * |
|
175 | + * OAuth Core: All Token requests and Protected Resources requests MUST be signed |
|
176 | + * by the Consumer and verified by the Service Provider. |
|
177 | + * |
|
178 | + * The protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT.. |
|
179 | + * |
|
180 | + * The Consumer declares a signature method in the oauth_signature_method parameter.. |
|
181 | + * |
|
182 | + * http://oauth.net/core/1.0a/#signing_process |
|
183 | + */ |
|
184 | + $this->sha1Method = new OAuthSignatureMethodHMACSHA1(); |
|
185 | + |
|
186 | + $this->OAuthConsumer = new OAuthConsumer( |
|
187 | + $this->consumerKey, |
|
188 | + $this->consumerSecret |
|
189 | + ); |
|
190 | + |
|
191 | + if ($this->getStoredData('request_token')) { |
|
192 | + $this->consumerToken = new OAuthConsumer( |
|
193 | + $this->getStoredData('request_token'), |
|
194 | + $this->getStoredData('request_token_secret') |
|
195 | + ); |
|
196 | + } |
|
197 | + |
|
198 | + if ($this->getStoredData('access_token')) { |
|
199 | + $this->consumerToken = new OAuthConsumer( |
|
200 | + $this->getStoredData('access_token'), |
|
201 | + $this->getStoredData('access_token_secret') |
|
202 | + ); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * {@inheritdoc} |
|
208 | + */ |
|
209 | + public function authenticate() |
|
210 | + { |
|
211 | + $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
212 | + |
|
213 | + if ($this->isConnected()) { |
|
214 | + return true; |
|
215 | + } |
|
216 | + |
|
217 | + try { |
|
218 | + if (!$this->getStoredData('request_token')) { |
|
219 | + // Start a new flow. |
|
220 | + $this->authenticateBegin(); |
|
221 | + } elseif (empty($_GET['oauth_token']) && empty($_GET['denied'])) { |
|
222 | + // A previous authentication was not finished, and this request is not finishing it. |
|
223 | + $this->authenticateBegin(); |
|
224 | + } else { |
|
225 | + // Finish a flow. |
|
226 | + $this->authenticateFinish(); |
|
227 | + } |
|
228 | + } catch (Exception $exception) { |
|
229 | + $this->clearStoredData(); |
|
230 | + |
|
231 | + throw $exception; |
|
232 | + } |
|
233 | + |
|
234 | + return null; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * {@inheritdoc} |
|
239 | + */ |
|
240 | + public function isConnected() |
|
241 | + { |
|
242 | + return (bool)$this->getStoredData('access_token'); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Initiate the authorization protocol |
|
247 | + * |
|
248 | + * 1. Obtaining an Unauthorized Request Token |
|
249 | + * 2. Build Authorization URL for Authorization Request and redirect the user-agent to the |
|
250 | + * Authorization Server. |
|
251 | + */ |
|
252 | + protected function authenticateBegin() |
|
253 | + { |
|
254 | + $response = $this->requestAuthToken(); |
|
255 | + |
|
256 | + $this->validateAuthTokenRequest($response); |
|
257 | + |
|
258 | + $authUrl = $this->getAuthorizeUrl(); |
|
259 | + |
|
260 | + $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this)), [$authUrl]); |
|
261 | + |
|
262 | + HttpClient\Util::redirect($authUrl); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Finalize the authorization process |
|
267 | + * |
|
268 | + * @throws AuthorizationDeniedException |
|
269 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
270 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
271 | + * @throws InvalidAccessTokenException |
|
272 | + * @throws InvalidOauthTokenException |
|
273 | + */ |
|
274 | + protected function authenticateFinish() |
|
275 | + { |
|
276 | + $this->logger->debug( |
|
277 | + sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
278 | + [HttpClient\Util::getCurrentUrl(true)] |
|
279 | + ); |
|
280 | + |
|
281 | + $denied = filter_input(INPUT_GET, 'denied'); |
|
282 | + $oauth_problem = filter_input(INPUT_GET, 'oauth_problem'); |
|
283 | + $oauth_token = filter_input(INPUT_GET, 'oauth_token'); |
|
284 | + $oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier'); |
|
285 | + |
|
286 | + if ($denied) { |
|
287 | + throw new AuthorizationDeniedException( |
|
288 | + 'User denied access request. Provider returned a denied token: ' . htmlentities($denied) |
|
289 | + ); |
|
290 | + } |
|
291 | + |
|
292 | + if ($oauth_problem) { |
|
293 | + throw new InvalidOauthTokenException( |
|
294 | + 'Provider returned an error. oauth_problem: ' . htmlentities($oauth_problem) |
|
295 | + ); |
|
296 | + } |
|
297 | + |
|
298 | + if (!$oauth_token) { |
|
299 | + throw new InvalidOauthTokenException( |
|
300 | + 'Expecting a non-null oauth_token to continue the authorization flow.' |
|
301 | + ); |
|
302 | + } |
|
303 | + |
|
304 | + $response = $this->exchangeAuthTokenForAccessToken($oauth_token, $oauth_verifier); |
|
305 | + |
|
306 | + $this->validateAccessTokenExchange($response); |
|
307 | + |
|
308 | + $this->initialize(); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Build Authorization URL for Authorization Request |
|
313 | + * |
|
314 | + * @param array $parameters |
|
315 | + * |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + protected function getAuthorizeUrl($parameters = []) |
|
319 | + { |
|
320 | + $this->AuthorizeUrlParameters = !empty($parameters) |
|
321 | + ? $parameters |
|
322 | + : array_replace( |
|
323 | + (array)$this->AuthorizeUrlParameters, |
|
324 | + (array)$this->config->get('authorize_url_parameters') |
|
325 | + ); |
|
326 | + |
|
327 | + $this->AuthorizeUrlParameters['oauth_token'] = $this->getStoredData('request_token'); |
|
328 | + |
|
329 | + return $this->authorizeUrl . '?' . http_build_query($this->AuthorizeUrlParameters, '', '&'); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Unauthorized Request Token |
|
334 | + * |
|
335 | + * OAuth Core: The Consumer obtains an unauthorized Request Token by asking the Service Provider |
|
336 | + * to issue a Token. The Request Token's sole purpose is to receive User approval and can only |
|
337 | + * be used to obtain an Access Token. |
|
338 | + * |
|
339 | + * http://oauth.net/core/1.0/#auth_step1 |
|
340 | + * 6.1.1. Consumer Obtains a Request Token |
|
341 | + * |
|
342 | + * @return string Raw Provider API response |
|
343 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
344 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
345 | + */ |
|
346 | + protected function requestAuthToken() |
|
347 | + { |
|
348 | + /** |
|
349 | + * OAuth Core 1.0 Revision A: oauth_callback: An absolute URL to which the Service Provider will redirect |
|
350 | + * the User back when the Obtaining User Authorization step is completed. |
|
351 | + * |
|
352 | + * http://oauth.net/core/1.0a/#auth_step1 |
|
353 | + */ |
|
354 | + if ('1.0a' == $this->oauth1Version) { |
|
355 | + $this->requestTokenParameters['oauth_callback'] = $this->callback; |
|
356 | + } |
|
357 | + |
|
358 | + $response = $this->oauthRequest( |
|
359 | + $this->requestTokenUrl, |
|
360 | + $this->requestTokenMethod, |
|
361 | + $this->requestTokenParameters, |
|
362 | + $this->requestTokenHeaders |
|
363 | + ); |
|
364 | + |
|
365 | + return $response; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Validate Unauthorized Request Token Response |
|
370 | + * |
|
371 | + * OAuth Core: The Service Provider verifies the signature and Consumer Key. If successful, |
|
372 | + * it generates a Request Token and Token Secret and returns them to the Consumer in the HTTP |
|
373 | + * response body. |
|
374 | + * |
|
375 | + * http://oauth.net/core/1.0/#auth_step1 |
|
376 | + * 6.1.2. Service Provider Issues an Unauthorized Request Token |
|
377 | + * |
|
378 | + * @param string $response |
|
379 | + * |
|
380 | + * @return \Hybridauth\Data\Collection |
|
381 | + * @throws InvalidOauthTokenException |
|
382 | + */ |
|
383 | + protected function validateAuthTokenRequest($response) |
|
384 | + { |
|
385 | + /** |
|
386 | + * The response contains the following parameters: |
|
387 | + * |
|
388 | + * - oauth_token The Request Token. |
|
389 | + * - oauth_token_secret The Token Secret. |
|
390 | + * - oauth_callback_confirmed MUST be present and set to true. |
|
391 | + * |
|
392 | + * http://oauth.net/core/1.0/#auth_step1 |
|
393 | + * 6.1.2. Service Provider Issues an Unauthorized Request Token |
|
394 | + * |
|
395 | + * Example of a successful response: |
|
396 | + * |
|
397 | + * HTTP/1.1 200 OK |
|
398 | + * Content-Type: text/html; charset=utf-8 |
|
399 | + * Cache-Control: no-store |
|
400 | + * Pragma: no-cache |
|
401 | + * |
|
402 | + * oauth_token=80359084-clg1DEtxQF3wstTcyUdHF3wsdHM&oauth_token_secret=OIF07hPmJB:P |
|
403 | + * 6qiHTi1znz6qiH3tTcyUdHnz6qiH3tTcyUdH3xW3wsDvV08e&example_parameter=example_value |
|
404 | + * |
|
405 | + * OAuthUtil::parse_parameters will attempt to decode the raw response into an array. |
|
406 | + */ |
|
407 | + $tokens = OAuthUtil::parse_parameters($response); |
|
408 | + |
|
409 | + $collection = new Data\Collection($tokens); |
|
410 | + |
|
411 | + if (!$collection->exists('oauth_token')) { |
|
412 | + throw new InvalidOauthTokenException( |
|
413 | + 'Provider returned no oauth_token: ' . htmlentities($response) |
|
414 | + ); |
|
415 | + } |
|
416 | + |
|
417 | + $this->consumerToken = new OAuthConsumer( |
|
418 | + $tokens['oauth_token'], |
|
419 | + $tokens['oauth_token_secret'] |
|
420 | + ); |
|
421 | + |
|
422 | + $this->storeData('request_token', $tokens['oauth_token']); |
|
423 | + $this->storeData('request_token_secret', $tokens['oauth_token_secret']); |
|
424 | + |
|
425 | + return $collection; |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * Requests an Access Token |
|
430 | + * |
|
431 | + * OAuth Core: The Request Token and Token Secret MUST be exchanged for an Access Token and Token Secret. |
|
432 | + * |
|
433 | + * http://oauth.net/core/1.0a/#auth_step3 |
|
434 | + * 6.3.1. Consumer Requests an Access Token |
|
435 | + * |
|
436 | + * @param string $oauth_token |
|
437 | + * @param string $oauth_verifier |
|
438 | + * |
|
439 | + * @return string Raw Provider API response |
|
440 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
441 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
442 | + */ |
|
443 | + protected function exchangeAuthTokenForAccessToken($oauth_token, $oauth_verifier = '') |
|
444 | + { |
|
445 | + $this->tokenExchangeParameters['oauth_token'] = $oauth_token; |
|
446 | + |
|
447 | + /** |
|
448 | + * OAuth Core 1.0 Revision A: oauth_verifier: The verification code received from the Service Provider |
|
449 | + * in the "Service Provider Directs the User Back to the Consumer" step. |
|
450 | + * |
|
451 | + * http://oauth.net/core/1.0a/#auth_step3 |
|
452 | + */ |
|
453 | + if ('1.0a' == $this->oauth1Version) { |
|
454 | + $this->tokenExchangeParameters['oauth_verifier'] = $oauth_verifier; |
|
455 | + } |
|
456 | + |
|
457 | + $response = $this->oauthRequest( |
|
458 | + $this->accessTokenUrl, |
|
459 | + $this->tokenExchangeMethod, |
|
460 | + $this->tokenExchangeParameters, |
|
461 | + $this->tokenExchangeHeaders |
|
462 | + ); |
|
463 | + |
|
464 | + return $response; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Validate Access Token Response |
|
469 | + * |
|
470 | + * OAuth Core: If successful, the Service Provider generates an Access Token and Token Secret and returns |
|
471 | + * them in the HTTP response body. |
|
472 | + * |
|
473 | + * The Access Token and Token Secret are stored by the Consumer and used when signing Protected Resources requests. |
|
474 | + * |
|
475 | + * http://oauth.net/core/1.0a/#auth_step3 |
|
476 | + * 6.3.2. Service Provider Grants an Access Token |
|
477 | + * |
|
478 | + * @param string $response |
|
479 | + * |
|
480 | + * @return \Hybridauth\Data\Collection |
|
481 | + * @throws InvalidAccessTokenException |
|
482 | + */ |
|
483 | + protected function validateAccessTokenExchange($response) |
|
484 | + { |
|
485 | + /** |
|
486 | + * The response contains the following parameters: |
|
487 | + * |
|
488 | + * - oauth_token The Access Token. |
|
489 | + * - oauth_token_secret The Token Secret. |
|
490 | + * |
|
491 | + * http://oauth.net/core/1.0/#auth_step3 |
|
492 | + * 6.3.2. Service Provider Grants an Access Token |
|
493 | + * |
|
494 | + * Example of a successful response: |
|
495 | + * |
|
496 | + * HTTP/1.1 200 OK |
|
497 | + * Content-Type: text/html; charset=utf-8 |
|
498 | + * Cache-Control: no-store |
|
499 | + * Pragma: no-cache |
|
500 | + * |
|
501 | + * oauth_token=sHeLU7Far428zj8PzlWR75&oauth_token_secret=fXb30rzoG&oauth_callback_confirmed=true |
|
502 | + * |
|
503 | + * OAuthUtil::parse_parameters will attempt to decode the raw response into an array. |
|
504 | + */ |
|
505 | + $tokens = OAuthUtil::parse_parameters($response); |
|
506 | + |
|
507 | + $collection = new Data\Collection($tokens); |
|
508 | + |
|
509 | + if (!$collection->exists('oauth_token')) { |
|
510 | + throw new InvalidAccessTokenException( |
|
511 | + 'Provider returned no access_token: ' . htmlentities($response) |
|
512 | + ); |
|
513 | + } |
|
514 | + |
|
515 | + $this->consumerToken = new OAuthConsumer( |
|
516 | + $collection->get('oauth_token'), |
|
517 | + $collection->get('oauth_token_secret') |
|
518 | + ); |
|
519 | + |
|
520 | + $this->storeData('access_token', $collection->get('oauth_token')); |
|
521 | + $this->storeData('access_token_secret', $collection->get('oauth_token_secret')); |
|
522 | + |
|
523 | + $this->deleteStoredData('request_token'); |
|
524 | + $this->deleteStoredData('request_token_secret'); |
|
525 | + |
|
526 | + return $collection; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Send a signed request to provider API |
|
531 | + * |
|
532 | + * Note: Since the specifics of error responses is beyond the scope of RFC6749 and OAuth specifications, |
|
533 | + * Hybridauth will consider any HTTP status code that is different than '200 OK' as an ERROR. |
|
534 | + * |
|
535 | + * @param string $url |
|
536 | + * @param string $method |
|
537 | + * @param array $parameters |
|
538 | + * @param array $headers |
|
539 | + * @param bool $multipart |
|
540 | + * |
|
541 | + * @return mixed |
|
542 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
543 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
544 | + */ |
|
545 | + public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
546 | + { |
|
547 | + // refresh tokens if needed |
|
548 | + $this->maintainToken(); |
|
549 | + |
|
550 | + if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) { |
|
551 | + $url = rtrim($this->apiBaseUrl, '/') . '/' . ltrim($url, '/'); |
|
552 | + } |
|
553 | + |
|
554 | + $parameters = array_replace($this->apiRequestParameters, (array)$parameters); |
|
555 | + |
|
556 | + $headers = array_replace($this->apiRequestHeaders, (array)$headers); |
|
557 | + |
|
558 | + $response = $this->oauthRequest($url, $method, $parameters, $headers, $multipart); |
|
559 | + |
|
560 | + $response = (new Data\Parser())->parse($response); |
|
561 | + |
|
562 | + return $response; |
|
563 | + } |
|
564 | + |
|
565 | + /** |
|
566 | + * Setup and Send a Signed Oauth Request |
|
567 | + * |
|
568 | + * This method uses OAuth Library. |
|
569 | + * |
|
570 | + * @param string $uri |
|
571 | + * @param string $method |
|
572 | + * @param array $parameters |
|
573 | + * @param array $headers |
|
574 | + * @param bool $multipart |
|
575 | + * |
|
576 | + * @return string Raw Provider API response |
|
577 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
578 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
579 | + */ |
|
580 | + protected function oauthRequest($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
581 | + { |
|
582 | + $signing_parameters = $parameters; |
|
583 | + if ($multipart) { |
|
584 | + $signing_parameters = []; |
|
585 | + } |
|
586 | + |
|
587 | + $request = OAuthRequest::from_consumer_and_token( |
|
588 | + $this->OAuthConsumer, |
|
589 | + $this->consumerToken, |
|
590 | + $method, |
|
591 | + $uri, |
|
592 | + $signing_parameters |
|
593 | + ); |
|
594 | + |
|
595 | + $request->sign_request( |
|
596 | + $this->sha1Method, |
|
597 | + $this->OAuthConsumer, |
|
598 | + $this->consumerToken |
|
599 | + ); |
|
600 | + |
|
601 | + $uri = $request->get_normalized_http_url(); |
|
602 | + $headers = array_replace($request->to_header(), (array)$headers); |
|
603 | + |
|
604 | + $response = $this->httpClient->request( |
|
605 | + $uri, |
|
606 | + $method, |
|
607 | + $parameters, |
|
608 | + $headers, |
|
609 | + $multipart |
|
610 | + ); |
|
611 | + |
|
612 | + $this->validateApiResponse('Signed API request to ' . $uri . ' has returned an error'); |
|
613 | + |
|
614 | + return $response; |
|
615 | + } |
|
616 | 616 | } |
@@ -18,177 +18,177 @@ |
||
18 | 18 | */ |
19 | 19 | class Patreon extends OAuth2 |
20 | 20 | { |
21 | - /** |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - protected $scope = 'identity identity[email]'; |
|
25 | - |
|
26 | - /** |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - protected $apiBaseUrl = 'https://www.patreon.com/api'; |
|
30 | - |
|
31 | - /** |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - protected $authorizeUrl = 'https://www.patreon.com/oauth2/authorize'; |
|
35 | - |
|
36 | - /** |
|
37 | - * {@inheritdoc} |
|
38 | - */ |
|
39 | - protected $accessTokenUrl = 'https://www.patreon.com/api/oauth2/token'; |
|
40 | - |
|
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - */ |
|
44 | - protected $apiDocumentation = 'https://docs.patreon.com/#oauth'; |
|
45 | - |
|
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - protected function initialize() |
|
50 | - { |
|
51 | - parent::initialize(); |
|
52 | - |
|
53 | - if ($this->isRefreshTokenAvailable()) { |
|
54 | - $this->tokenRefreshParameters += [ |
|
55 | - 'client_id' => $this->clientId, |
|
56 | - 'client_secret' => $this->clientSecret, |
|
57 | - ]; |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - public function getUserProfile() |
|
65 | - { |
|
66 | - $response = $this->apiRequest('oauth2/v2/identity', 'GET', [ |
|
67 | - 'fields[user]' => 'created,first_name,last_name,email,full_name,is_email_verified,thumb_url,url', |
|
68 | - ]); |
|
69 | - |
|
70 | - $collection = new Collection($response); |
|
71 | - if (!$collection->exists('data')) { |
|
72 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
73 | - } |
|
74 | - |
|
75 | - $userProfile = new Profile(); |
|
76 | - |
|
77 | - $data = $collection->filter('data'); |
|
78 | - $attributes = $data->filter('attributes'); |
|
79 | - |
|
80 | - $userProfile->identifier = $data->get('id'); |
|
81 | - $userProfile->email = $attributes->get('email'); |
|
82 | - $userProfile->firstName = $attributes->get('first_name'); |
|
83 | - $userProfile->lastName = $attributes->get('last_name'); |
|
84 | - $userProfile->displayName = $attributes->get('full_name') ?: $data->get('id'); |
|
85 | - $userProfile->photoURL = $attributes->get('thumb_url'); |
|
86 | - $userProfile->profileURL = $attributes->get('url'); |
|
87 | - |
|
88 | - $userProfile->emailVerified = $attributes->get('is_email_verified') ? $userProfile->email : ''; |
|
89 | - |
|
90 | - return $userProfile; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Contacts are defined as Patrons here |
|
95 | - */ |
|
96 | - public function getUserContacts() |
|
97 | - { |
|
98 | - $campaignId = $this->config->get('campaign_id') ?: null; |
|
99 | - $tierFilter = $this->config->get('tier_filter') ?: null; |
|
100 | - |
|
101 | - $campaigns = []; |
|
102 | - if ($campaignId === null) { |
|
103 | - $campaignsUrl = 'oauth2/v2/campaigns'; |
|
104 | - do { |
|
105 | - $response = $this->apiRequest($campaignsUrl); |
|
106 | - $data = new Collection($response); |
|
107 | - |
|
108 | - if (!$data->exists('data')) { |
|
109 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
110 | - } |
|
111 | - |
|
112 | - foreach ($data->filter('data')->toArray() as $item) { |
|
113 | - $campaign = new Collection($item); |
|
114 | - $campaigns[] = $campaign->get('id'); |
|
115 | - } |
|
116 | - |
|
117 | - if ($data->filter('links')->exists('next')) { |
|
118 | - $campaignsUrl = $data->filter('links')->get('next'); |
|
119 | - |
|
120 | - $pagedList = true; |
|
121 | - } else { |
|
122 | - $pagedList = false; |
|
123 | - } |
|
124 | - } while ($pagedList); |
|
125 | - } else { |
|
126 | - $campaigns[] = $campaignId; |
|
127 | - } |
|
128 | - |
|
129 | - $contacts = []; |
|
130 | - |
|
131 | - foreach ($campaigns as $campaignId) { |
|
132 | - $params = [ |
|
133 | - 'include' => 'currently_entitled_tiers', |
|
134 | - 'fields[member]' => 'full_name,patron_status,email', |
|
135 | - 'fields[tier]' => 'title', |
|
136 | - ]; |
|
137 | - $membersUrl = 'oauth2/v2/campaigns/' . $campaignId . '/members?' . http_build_query($params); |
|
138 | - |
|
139 | - do { |
|
140 | - $response = $this->apiRequest($membersUrl); |
|
141 | - |
|
142 | - $data = new Collection($response); |
|
143 | - |
|
144 | - if (!$data->exists('data')) { |
|
145 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
146 | - } |
|
147 | - |
|
148 | - $tierTitles = []; |
|
149 | - |
|
150 | - foreach ($data->filter('included')->toArray() as $item) { |
|
151 | - $includedItem = new Collection($item); |
|
152 | - if ($includedItem->get('type') == 'tier') { |
|
153 | - $tierTitles[$includedItem->get('id')] = $includedItem->filter('attributes')->get('title'); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - foreach ($data->filter('data')->toArray() as $item) { |
|
158 | - $member = new Collection($item); |
|
159 | - |
|
160 | - if ($member->filter('attributes')->get('patron_status') == 'active_patron') { |
|
161 | - $tiers = []; |
|
162 | - $tierObs = $member->filter('relationships')->filter('currently_entitled_tiers')->get('data'); |
|
163 | - foreach ($tierObs as $item) { |
|
164 | - $tier = new Collection($item); |
|
165 | - $tierId = $tier->get('id'); |
|
166 | - $tiers[] = $tierTitles[$tierId]; |
|
167 | - } |
|
168 | - |
|
169 | - if (($tierFilter === null) || (in_array($tierFilter, $tiers))) { |
|
170 | - $userContact = new User\Contact(); |
|
171 | - |
|
172 | - $userContact->identifier = $member->get('id'); |
|
173 | - $userContact->email = $member->filter('attributes')->get('email'); |
|
174 | - $userContact->displayName = $member->filter('attributes')->get('full_name'); |
|
175 | - $userContact->description = json_encode($tiers); |
|
176 | - |
|
177 | - $contacts[] = $userContact; |
|
178 | - } |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - if ($data->filter('links')->exists('next')) { |
|
183 | - $membersUrl = $data->filter('links')->get('next'); |
|
184 | - |
|
185 | - $pagedList = true; |
|
186 | - } else { |
|
187 | - $pagedList = false; |
|
188 | - } |
|
189 | - } while ($pagedList); |
|
190 | - } |
|
191 | - |
|
192 | - return $contacts; |
|
193 | - } |
|
21 | + /** |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + protected $scope = 'identity identity[email]'; |
|
25 | + |
|
26 | + /** |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + protected $apiBaseUrl = 'https://www.patreon.com/api'; |
|
30 | + |
|
31 | + /** |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + protected $authorizeUrl = 'https://www.patreon.com/oauth2/authorize'; |
|
35 | + |
|
36 | + /** |
|
37 | + * {@inheritdoc} |
|
38 | + */ |
|
39 | + protected $accessTokenUrl = 'https://www.patreon.com/api/oauth2/token'; |
|
40 | + |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + */ |
|
44 | + protected $apiDocumentation = 'https://docs.patreon.com/#oauth'; |
|
45 | + |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + protected function initialize() |
|
50 | + { |
|
51 | + parent::initialize(); |
|
52 | + |
|
53 | + if ($this->isRefreshTokenAvailable()) { |
|
54 | + $this->tokenRefreshParameters += [ |
|
55 | + 'client_id' => $this->clientId, |
|
56 | + 'client_secret' => $this->clientSecret, |
|
57 | + ]; |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + public function getUserProfile() |
|
65 | + { |
|
66 | + $response = $this->apiRequest('oauth2/v2/identity', 'GET', [ |
|
67 | + 'fields[user]' => 'created,first_name,last_name,email,full_name,is_email_verified,thumb_url,url', |
|
68 | + ]); |
|
69 | + |
|
70 | + $collection = new Collection($response); |
|
71 | + if (!$collection->exists('data')) { |
|
72 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
73 | + } |
|
74 | + |
|
75 | + $userProfile = new Profile(); |
|
76 | + |
|
77 | + $data = $collection->filter('data'); |
|
78 | + $attributes = $data->filter('attributes'); |
|
79 | + |
|
80 | + $userProfile->identifier = $data->get('id'); |
|
81 | + $userProfile->email = $attributes->get('email'); |
|
82 | + $userProfile->firstName = $attributes->get('first_name'); |
|
83 | + $userProfile->lastName = $attributes->get('last_name'); |
|
84 | + $userProfile->displayName = $attributes->get('full_name') ?: $data->get('id'); |
|
85 | + $userProfile->photoURL = $attributes->get('thumb_url'); |
|
86 | + $userProfile->profileURL = $attributes->get('url'); |
|
87 | + |
|
88 | + $userProfile->emailVerified = $attributes->get('is_email_verified') ? $userProfile->email : ''; |
|
89 | + |
|
90 | + return $userProfile; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Contacts are defined as Patrons here |
|
95 | + */ |
|
96 | + public function getUserContacts() |
|
97 | + { |
|
98 | + $campaignId = $this->config->get('campaign_id') ?: null; |
|
99 | + $tierFilter = $this->config->get('tier_filter') ?: null; |
|
100 | + |
|
101 | + $campaigns = []; |
|
102 | + if ($campaignId === null) { |
|
103 | + $campaignsUrl = 'oauth2/v2/campaigns'; |
|
104 | + do { |
|
105 | + $response = $this->apiRequest($campaignsUrl); |
|
106 | + $data = new Collection($response); |
|
107 | + |
|
108 | + if (!$data->exists('data')) { |
|
109 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
110 | + } |
|
111 | + |
|
112 | + foreach ($data->filter('data')->toArray() as $item) { |
|
113 | + $campaign = new Collection($item); |
|
114 | + $campaigns[] = $campaign->get('id'); |
|
115 | + } |
|
116 | + |
|
117 | + if ($data->filter('links')->exists('next')) { |
|
118 | + $campaignsUrl = $data->filter('links')->get('next'); |
|
119 | + |
|
120 | + $pagedList = true; |
|
121 | + } else { |
|
122 | + $pagedList = false; |
|
123 | + } |
|
124 | + } while ($pagedList); |
|
125 | + } else { |
|
126 | + $campaigns[] = $campaignId; |
|
127 | + } |
|
128 | + |
|
129 | + $contacts = []; |
|
130 | + |
|
131 | + foreach ($campaigns as $campaignId) { |
|
132 | + $params = [ |
|
133 | + 'include' => 'currently_entitled_tiers', |
|
134 | + 'fields[member]' => 'full_name,patron_status,email', |
|
135 | + 'fields[tier]' => 'title', |
|
136 | + ]; |
|
137 | + $membersUrl = 'oauth2/v2/campaigns/' . $campaignId . '/members?' . http_build_query($params); |
|
138 | + |
|
139 | + do { |
|
140 | + $response = $this->apiRequest($membersUrl); |
|
141 | + |
|
142 | + $data = new Collection($response); |
|
143 | + |
|
144 | + if (!$data->exists('data')) { |
|
145 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
146 | + } |
|
147 | + |
|
148 | + $tierTitles = []; |
|
149 | + |
|
150 | + foreach ($data->filter('included')->toArray() as $item) { |
|
151 | + $includedItem = new Collection($item); |
|
152 | + if ($includedItem->get('type') == 'tier') { |
|
153 | + $tierTitles[$includedItem->get('id')] = $includedItem->filter('attributes')->get('title'); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + foreach ($data->filter('data')->toArray() as $item) { |
|
158 | + $member = new Collection($item); |
|
159 | + |
|
160 | + if ($member->filter('attributes')->get('patron_status') == 'active_patron') { |
|
161 | + $tiers = []; |
|
162 | + $tierObs = $member->filter('relationships')->filter('currently_entitled_tiers')->get('data'); |
|
163 | + foreach ($tierObs as $item) { |
|
164 | + $tier = new Collection($item); |
|
165 | + $tierId = $tier->get('id'); |
|
166 | + $tiers[] = $tierTitles[$tierId]; |
|
167 | + } |
|
168 | + |
|
169 | + if (($tierFilter === null) || (in_array($tierFilter, $tiers))) { |
|
170 | + $userContact = new User\Contact(); |
|
171 | + |
|
172 | + $userContact->identifier = $member->get('id'); |
|
173 | + $userContact->email = $member->filter('attributes')->get('email'); |
|
174 | + $userContact->displayName = $member->filter('attributes')->get('full_name'); |
|
175 | + $userContact->description = json_encode($tiers); |
|
176 | + |
|
177 | + $contacts[] = $userContact; |
|
178 | + } |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + if ($data->filter('links')->exists('next')) { |
|
183 | + $membersUrl = $data->filter('links')->get('next'); |
|
184 | + |
|
185 | + $pagedList = true; |
|
186 | + } else { |
|
187 | + $pagedList = false; |
|
188 | + } |
|
189 | + } while ($pagedList); |
|
190 | + } |
|
191 | + |
|
192 | + return $contacts; |
|
193 | + } |
|
194 | 194 | } |
@@ -17,240 +17,240 @@ |
||
17 | 17 | */ |
18 | 18 | class Instagram extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'user_profile,user_media'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://graph.instagram.com'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl = 'https://api.instagram.com/oauth/authorize'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://api.instagram.com/oauth/access_token'; |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - protected $apiDocumentation = 'https://developers.facebook.com/docs/instagram-basic-display-api'; |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - protected function initialize() |
|
49 | - { |
|
50 | - parent::initialize(); |
|
51 | - |
|
52 | - // The Instagram API requires an access_token from authenticated users |
|
53 | - // for each endpoint. |
|
54 | - $accessToken = $this->getStoredData($this->accessTokenName); |
|
55 | - $this->apiRequestParameters[$this->accessTokenName] = $accessToken; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - protected function validateAccessTokenExchange($response) |
|
62 | - { |
|
63 | - $collection = parent::validateAccessTokenExchange($response); |
|
64 | - |
|
65 | - if (!$collection->exists('expires_in')) { |
|
66 | - // Instagram tokens always expire in an hour, but this is implicit not explicit |
|
67 | - |
|
68 | - $expires_in = 60 * 60; |
|
69 | - |
|
70 | - $expires_at = time() + $expires_in; |
|
71 | - |
|
72 | - $this->storeData('expires_in', $expires_in); |
|
73 | - $this->storeData('expires_at', $expires_at); |
|
74 | - } |
|
75 | - |
|
76 | - return $collection; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * {@inheritdoc} |
|
81 | - */ |
|
82 | - public function maintainToken() |
|
83 | - { |
|
84 | - if (!$this->isConnected()) { |
|
85 | - return; |
|
86 | - } |
|
87 | - |
|
88 | - // Handle token exchange prior to the standard handler for an API request |
|
89 | - $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
90 | - if ($exchange_by_expiry_days !== null) { |
|
91 | - $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
92 | - if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
93 | - $this->exchangeAccessToken(); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Exchange the Access Token with one that expires further in the future. |
|
100 | - * |
|
101 | - * @return string Raw Provider API response |
|
102 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
103 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
104 | - * @throws InvalidAccessTokenException |
|
105 | - */ |
|
106 | - public function exchangeAccessToken() |
|
107 | - { |
|
108 | - if ($this->getStoredData('expires_in') >= 5000000) { |
|
109 | - /* |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'user_profile,user_media'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://graph.instagram.com'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl = 'https://api.instagram.com/oauth/authorize'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://api.instagram.com/oauth/access_token'; |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + protected $apiDocumentation = 'https://developers.facebook.com/docs/instagram-basic-display-api'; |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + protected function initialize() |
|
49 | + { |
|
50 | + parent::initialize(); |
|
51 | + |
|
52 | + // The Instagram API requires an access_token from authenticated users |
|
53 | + // for each endpoint. |
|
54 | + $accessToken = $this->getStoredData($this->accessTokenName); |
|
55 | + $this->apiRequestParameters[$this->accessTokenName] = $accessToken; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + protected function validateAccessTokenExchange($response) |
|
62 | + { |
|
63 | + $collection = parent::validateAccessTokenExchange($response); |
|
64 | + |
|
65 | + if (!$collection->exists('expires_in')) { |
|
66 | + // Instagram tokens always expire in an hour, but this is implicit not explicit |
|
67 | + |
|
68 | + $expires_in = 60 * 60; |
|
69 | + |
|
70 | + $expires_at = time() + $expires_in; |
|
71 | + |
|
72 | + $this->storeData('expires_in', $expires_in); |
|
73 | + $this->storeData('expires_at', $expires_at); |
|
74 | + } |
|
75 | + |
|
76 | + return $collection; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * {@inheritdoc} |
|
81 | + */ |
|
82 | + public function maintainToken() |
|
83 | + { |
|
84 | + if (!$this->isConnected()) { |
|
85 | + return; |
|
86 | + } |
|
87 | + |
|
88 | + // Handle token exchange prior to the standard handler for an API request |
|
89 | + $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
90 | + if ($exchange_by_expiry_days !== null) { |
|
91 | + $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
92 | + if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
93 | + $this->exchangeAccessToken(); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Exchange the Access Token with one that expires further in the future. |
|
100 | + * |
|
101 | + * @return string Raw Provider API response |
|
102 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
103 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
104 | + * @throws InvalidAccessTokenException |
|
105 | + */ |
|
106 | + public function exchangeAccessToken() |
|
107 | + { |
|
108 | + if ($this->getStoredData('expires_in') >= 5000000) { |
|
109 | + /* |
|
110 | 110 | Refresh a long-lived token (needed on Instagram, but not Facebook). |
111 | 111 | It's not an oAuth style refresh using a refresh token. |
112 | 112 | Actually it's really just another exchange, and invalidates the old token. |
113 | 113 | Facebook/Instagram documentation is not very helpful at explaining that! |
114 | 114 | */ |
115 | - $exchangeTokenParameters = [ |
|
116 | - 'grant_type' => 'ig_refresh_token', |
|
117 | - 'client_secret' => $this->clientSecret, |
|
118 | - 'access_token' => $this->getStoredData('access_token'), |
|
119 | - ]; |
|
120 | - $url = 'https://graph.instagram.com/refresh_access_token'; |
|
121 | - } else { |
|
122 | - // Exchange short-lived to long-lived |
|
123 | - $exchangeTokenParameters = [ |
|
124 | - 'grant_type' => 'ig_exchange_token', |
|
125 | - 'client_secret' => $this->clientSecret, |
|
126 | - 'access_token' => $this->getStoredData('access_token'), |
|
127 | - ]; |
|
128 | - $url = 'https://graph.instagram.com/access_token'; |
|
129 | - } |
|
130 | - |
|
131 | - $response = $this->httpClient->request( |
|
132 | - $url, |
|
133 | - 'GET', |
|
134 | - $exchangeTokenParameters |
|
135 | - ); |
|
136 | - |
|
137 | - $this->validateApiResponse('Unable to exchange the access token'); |
|
138 | - |
|
139 | - $this->validateAccessTokenExchange($response); |
|
140 | - |
|
141 | - return $response; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - public function getUserProfile() |
|
148 | - { |
|
149 | - $response = $this->apiRequest('me', 'GET', [ |
|
150 | - 'fields' => 'id,username,account_type,media_count', |
|
151 | - ]); |
|
152 | - |
|
153 | - $data = new Collection($response); |
|
154 | - if (!$data->exists('id')) { |
|
155 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
156 | - } |
|
157 | - |
|
158 | - $userProfile = new User\Profile(); |
|
159 | - $userProfile->identifier = $data->get('id'); |
|
160 | - $userProfile->displayName = $data->get('username'); |
|
161 | - $userProfile->profileURL = "https://instagram.com/{$userProfile->displayName}"; |
|
162 | - $userProfile->data = [ |
|
163 | - 'account_type' => $data->get('account_type'), |
|
164 | - 'media_count' => $data->get('media_count'), |
|
165 | - ]; |
|
166 | - |
|
167 | - return $userProfile; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Fetch user medias. |
|
172 | - * |
|
173 | - * @param int $limit Number of elements per page. |
|
174 | - * @param string $pageId Current pager ID. |
|
175 | - * @param array|null $fields Fields to fetch per media. |
|
176 | - * |
|
177 | - * @return \Hybridauth\Data\Collection |
|
178 | - * |
|
179 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
180 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
181 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
182 | - * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
183 | - */ |
|
184 | - public function getUserMedia($limit = 12, $pageId = null, array $fields = null) |
|
185 | - { |
|
186 | - if (empty($fields)) { |
|
187 | - $fields = [ |
|
188 | - 'id', |
|
189 | - 'caption', |
|
190 | - 'media_type', |
|
191 | - 'media_url', |
|
192 | - 'thumbnail_url', |
|
193 | - 'permalink', |
|
194 | - 'timestamp', |
|
195 | - 'username', |
|
196 | - ]; |
|
197 | - } |
|
198 | - |
|
199 | - $params = [ |
|
200 | - 'fields' => implode(',', $fields), |
|
201 | - 'limit' => $limit, |
|
202 | - ]; |
|
203 | - if ($pageId !== null) { |
|
204 | - $params['after'] = $pageId; |
|
205 | - } |
|
206 | - |
|
207 | - $response = $this->apiRequest('me/media', 'GET', $params); |
|
208 | - |
|
209 | - $data = new Collection($response); |
|
210 | - if (!$data->exists('data')) { |
|
211 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
212 | - } |
|
213 | - |
|
214 | - return $data; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Fetches a single user's media. |
|
219 | - * |
|
220 | - * @param string $mediaId Media ID. |
|
221 | - * @param array|null $fields Fields to fetch per media. |
|
222 | - * |
|
223 | - * @return \Hybridauth\Data\Collection |
|
224 | - * |
|
225 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
226 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
227 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
228 | - * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
229 | - */ |
|
230 | - public function getMedia($mediaId, array $fields = null) |
|
231 | - { |
|
232 | - if (empty($fields)) { |
|
233 | - $fields = [ |
|
234 | - 'id', |
|
235 | - 'caption', |
|
236 | - 'media_type', |
|
237 | - 'media_url', |
|
238 | - 'thumbnail_url', |
|
239 | - 'permalink', |
|
240 | - 'timestamp', |
|
241 | - 'username', |
|
242 | - ]; |
|
243 | - } |
|
244 | - |
|
245 | - $response = $this->apiRequest($mediaId, 'GET', [ |
|
246 | - 'fields' => implode(',', $fields), |
|
247 | - ]); |
|
248 | - |
|
249 | - $data = new Collection($response); |
|
250 | - if (!$data->exists('id')) { |
|
251 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
252 | - } |
|
253 | - |
|
254 | - return $data; |
|
255 | - } |
|
115 | + $exchangeTokenParameters = [ |
|
116 | + 'grant_type' => 'ig_refresh_token', |
|
117 | + 'client_secret' => $this->clientSecret, |
|
118 | + 'access_token' => $this->getStoredData('access_token'), |
|
119 | + ]; |
|
120 | + $url = 'https://graph.instagram.com/refresh_access_token'; |
|
121 | + } else { |
|
122 | + // Exchange short-lived to long-lived |
|
123 | + $exchangeTokenParameters = [ |
|
124 | + 'grant_type' => 'ig_exchange_token', |
|
125 | + 'client_secret' => $this->clientSecret, |
|
126 | + 'access_token' => $this->getStoredData('access_token'), |
|
127 | + ]; |
|
128 | + $url = 'https://graph.instagram.com/access_token'; |
|
129 | + } |
|
130 | + |
|
131 | + $response = $this->httpClient->request( |
|
132 | + $url, |
|
133 | + 'GET', |
|
134 | + $exchangeTokenParameters |
|
135 | + ); |
|
136 | + |
|
137 | + $this->validateApiResponse('Unable to exchange the access token'); |
|
138 | + |
|
139 | + $this->validateAccessTokenExchange($response); |
|
140 | + |
|
141 | + return $response; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + public function getUserProfile() |
|
148 | + { |
|
149 | + $response = $this->apiRequest('me', 'GET', [ |
|
150 | + 'fields' => 'id,username,account_type,media_count', |
|
151 | + ]); |
|
152 | + |
|
153 | + $data = new Collection($response); |
|
154 | + if (!$data->exists('id')) { |
|
155 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
156 | + } |
|
157 | + |
|
158 | + $userProfile = new User\Profile(); |
|
159 | + $userProfile->identifier = $data->get('id'); |
|
160 | + $userProfile->displayName = $data->get('username'); |
|
161 | + $userProfile->profileURL = "https://instagram.com/{$userProfile->displayName}"; |
|
162 | + $userProfile->data = [ |
|
163 | + 'account_type' => $data->get('account_type'), |
|
164 | + 'media_count' => $data->get('media_count'), |
|
165 | + ]; |
|
166 | + |
|
167 | + return $userProfile; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Fetch user medias. |
|
172 | + * |
|
173 | + * @param int $limit Number of elements per page. |
|
174 | + * @param string $pageId Current pager ID. |
|
175 | + * @param array|null $fields Fields to fetch per media. |
|
176 | + * |
|
177 | + * @return \Hybridauth\Data\Collection |
|
178 | + * |
|
179 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
180 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
181 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
182 | + * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
183 | + */ |
|
184 | + public function getUserMedia($limit = 12, $pageId = null, array $fields = null) |
|
185 | + { |
|
186 | + if (empty($fields)) { |
|
187 | + $fields = [ |
|
188 | + 'id', |
|
189 | + 'caption', |
|
190 | + 'media_type', |
|
191 | + 'media_url', |
|
192 | + 'thumbnail_url', |
|
193 | + 'permalink', |
|
194 | + 'timestamp', |
|
195 | + 'username', |
|
196 | + ]; |
|
197 | + } |
|
198 | + |
|
199 | + $params = [ |
|
200 | + 'fields' => implode(',', $fields), |
|
201 | + 'limit' => $limit, |
|
202 | + ]; |
|
203 | + if ($pageId !== null) { |
|
204 | + $params['after'] = $pageId; |
|
205 | + } |
|
206 | + |
|
207 | + $response = $this->apiRequest('me/media', 'GET', $params); |
|
208 | + |
|
209 | + $data = new Collection($response); |
|
210 | + if (!$data->exists('data')) { |
|
211 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
212 | + } |
|
213 | + |
|
214 | + return $data; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Fetches a single user's media. |
|
219 | + * |
|
220 | + * @param string $mediaId Media ID. |
|
221 | + * @param array|null $fields Fields to fetch per media. |
|
222 | + * |
|
223 | + * @return \Hybridauth\Data\Collection |
|
224 | + * |
|
225 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
226 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
227 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
228 | + * @throws \Hybridauth\Exception\UnexpectedApiResponseException |
|
229 | + */ |
|
230 | + public function getMedia($mediaId, array $fields = null) |
|
231 | + { |
|
232 | + if (empty($fields)) { |
|
233 | + $fields = [ |
|
234 | + 'id', |
|
235 | + 'caption', |
|
236 | + 'media_type', |
|
237 | + 'media_url', |
|
238 | + 'thumbnail_url', |
|
239 | + 'permalink', |
|
240 | + 'timestamp', |
|
241 | + 'username', |
|
242 | + ]; |
|
243 | + } |
|
244 | + |
|
245 | + $response = $this->apiRequest($mediaId, 'GET', [ |
|
246 | + 'fields' => implode(',', $fields), |
|
247 | + ]); |
|
248 | + |
|
249 | + $data = new Collection($response); |
|
250 | + if (!$data->exists('id')) { |
|
251 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
252 | + } |
|
253 | + |
|
254 | + return $data; |
|
255 | + } |
|
256 | 256 | } |
@@ -17,80 +17,80 @@ |
||
17 | 17 | */ |
18 | 18 | class AutoDesk extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'data:read'; |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'data:read'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://developer.api.autodesk.com/'; |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://developer.api.autodesk.com/'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl |
|
34 | - = 'https://developer.api.autodesk.com/authentication/v1/authorize'; |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl |
|
34 | + = 'https://developer.api.autodesk.com/authentication/v1/authorize'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * {@inheritdoc} |
|
38 | - */ |
|
39 | - protected $accessTokenUrl |
|
40 | - = 'https://developer.api.autodesk.com/authentication/v1/gettoken'; |
|
36 | + /** |
|
37 | + * {@inheritdoc} |
|
38 | + */ |
|
39 | + protected $accessTokenUrl |
|
40 | + = 'https://developer.api.autodesk.com/authentication/v1/gettoken'; |
|
41 | 41 | |
42 | - /** |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - protected $refreshTokenUrl |
|
46 | - = 'https://developer.api.autodesk.com/authentication/v1/refreshtoken'; |
|
42 | + /** |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + protected $refreshTokenUrl |
|
46 | + = 'https://developer.api.autodesk.com/authentication/v1/refreshtoken'; |
|
47 | 47 | |
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - protected $apiDocumentation |
|
52 | - = 'https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/overview/'; |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + protected $apiDocumentation |
|
52 | + = 'https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/overview/'; |
|
53 | 53 | |
54 | - /** |
|
55 | - * {@inheritdoc} |
|
56 | - */ |
|
57 | - protected function initialize() |
|
58 | - { |
|
59 | - parent::initialize(); |
|
54 | + /** |
|
55 | + * {@inheritdoc} |
|
56 | + */ |
|
57 | + protected function initialize() |
|
58 | + { |
|
59 | + parent::initialize(); |
|
60 | 60 | |
61 | - if ($this->isRefreshTokenAvailable()) { |
|
62 | - $this->tokenRefreshParameters += [ |
|
63 | - 'client_id' => $this->clientId, |
|
64 | - 'client_secret' => $this->clientSecret, |
|
65 | - 'grant_type' => 'refresh_token', |
|
66 | - ]; |
|
67 | - } |
|
68 | - } |
|
61 | + if ($this->isRefreshTokenAvailable()) { |
|
62 | + $this->tokenRefreshParameters += [ |
|
63 | + 'client_id' => $this->clientId, |
|
64 | + 'client_secret' => $this->clientSecret, |
|
65 | + 'grant_type' => 'refresh_token', |
|
66 | + ]; |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - * |
|
73 | - * See: https://forge.autodesk.com/en/docs/oauth/v2/reference/http/users-@me-GET/ |
|
74 | - */ |
|
75 | - public function getUserProfile() |
|
76 | - { |
|
77 | - $response = $this->apiRequest('userprofile/v1/users/@me'); |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + * |
|
73 | + * See: https://forge.autodesk.com/en/docs/oauth/v2/reference/http/users-@me-GET/ |
|
74 | + */ |
|
75 | + public function getUserProfile() |
|
76 | + { |
|
77 | + $response = $this->apiRequest('userprofile/v1/users/@me'); |
|
78 | 78 | |
79 | - $collection = new Data\Collection($response); |
|
79 | + $collection = new Data\Collection($response); |
|
80 | 80 | |
81 | - $userProfile = new User\Profile(); |
|
81 | + $userProfile = new User\Profile(); |
|
82 | 82 | |
83 | - $userProfile->identifier = $collection->get('userId'); |
|
84 | - $userProfile->displayName |
|
85 | - = $collection->get('firstName') .' '. $collection->get('lastName'); |
|
86 | - $userProfile->firstName = $collection->get('firstName'); |
|
87 | - $userProfile->lastName = $collection->get('lastName'); |
|
88 | - $userProfile->email = $collection->get('emailId'); |
|
89 | - $userProfile->language = $collection->get('language'); |
|
90 | - $userProfile->webSiteURL = $collection->get('websiteUrl'); |
|
91 | - $userProfile->photoURL |
|
92 | - = $collection->filter('profileImages')->get('sizeX360'); |
|
83 | + $userProfile->identifier = $collection->get('userId'); |
|
84 | + $userProfile->displayName |
|
85 | + = $collection->get('firstName') .' '. $collection->get('lastName'); |
|
86 | + $userProfile->firstName = $collection->get('firstName'); |
|
87 | + $userProfile->lastName = $collection->get('lastName'); |
|
88 | + $userProfile->email = $collection->get('emailId'); |
|
89 | + $userProfile->language = $collection->get('language'); |
|
90 | + $userProfile->webSiteURL = $collection->get('websiteUrl'); |
|
91 | + $userProfile->photoURL |
|
92 | + = $collection->filter('profileImages')->get('sizeX360'); |
|
93 | 93 | |
94 | - return $userProfile; |
|
95 | - } |
|
94 | + return $userProfile; |
|
95 | + } |
|
96 | 96 | } |
@@ -17,121 +17,121 @@ |
||
17 | 17 | */ |
18 | 18 | class WeChat extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'snsapi_login,snsapi_userinfo,scope.userInfo'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://api.wechat.com/sns/'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl = 'https://open.weixin.qq.com/connect/qrconnect'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://api.wechat.com/sns/oauth2/access_token'; |
|
39 | - |
|
40 | - /** |
|
41 | - * Refresh Token Endpoint |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - protected $tokenRefreshUrl = 'https://api.wechat.com/sns/oauth2/refresh_token'; |
|
45 | - |
|
46 | - /** |
|
47 | - * {@ịnheritdoc} |
|
48 | - */ |
|
49 | - protected $accessTokenInfoUrl = 'https://api.wechat.com/sns/auth'; |
|
50 | - |
|
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected $tokenExchangeMethod = 'GET'; |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - protected $tokenRefreshMethod = 'GET'; |
|
60 | - |
|
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - protected $apiDocumentation = ''; // Not available |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function initialize() |
|
70 | - { |
|
71 | - parent::initialize(); |
|
72 | - |
|
73 | - $this->AuthorizeUrlParameters += [ |
|
74 | - 'appid' => $this->clientId |
|
75 | - ]; |
|
76 | - unset($this->AuthorizeUrlParameters['client_id']); |
|
77 | - |
|
78 | - $this->tokenExchangeParameters += [ |
|
79 | - 'appid' => $this->clientId, |
|
80 | - 'secret' => $this->clientSecret |
|
81 | - ]; |
|
82 | - unset($this->tokenExchangeParameters['client_id']); |
|
83 | - unset($this->tokenExchangeParameters['client_secret']); |
|
84 | - |
|
85 | - if ($this->isRefreshTokenAvailable()) { |
|
86 | - $this->tokenRefreshParameters += [ |
|
87 | - 'appid' => $this->clientId, |
|
88 | - ]; |
|
89 | - } |
|
90 | - |
|
91 | - $this->apiRequestParameters = [ |
|
92 | - 'appid' => $this->clientId, |
|
93 | - 'secret' => $this->clientSecret |
|
94 | - ]; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * {@inheritdoc} |
|
99 | - */ |
|
100 | - protected function validateAccessTokenExchange($response) |
|
101 | - { |
|
102 | - $collection = parent::validateAccessTokenExchange($response); |
|
103 | - |
|
104 | - $this->storeData('openid', $collection->get('openid')); |
|
105 | - $this->storeData('access_token', $collection->get('access_token')); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * {@inheritdoc} |
|
110 | - */ |
|
111 | - public function getUserProfile() |
|
112 | - { |
|
113 | - $openid = $this->getStoredData('openid'); |
|
114 | - $access_token = $this->getStoredData('access_token'); |
|
115 | - |
|
116 | - $response = $this->apiRequest('userinfo', 'GET', ['openid' => $openid, 'access_token' => $access_token]); |
|
117 | - |
|
118 | - $data = new Data\Collection($response); |
|
119 | - |
|
120 | - if (!$data->exists('openid')) { |
|
121 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
122 | - } |
|
123 | - |
|
124 | - $userProfile = new User\Profile(); |
|
125 | - |
|
126 | - $userProfile->identifier = $data->get('openid'); |
|
127 | - $userProfile->displayName = $data->get('nickname'); |
|
128 | - $userProfile->photoURL = $data->get('headimgurl'); |
|
129 | - $userProfile->city = $data->get('city'); |
|
130 | - $userProfile->region = $data->get('province'); |
|
131 | - $userProfile->country = $data->get('country'); |
|
132 | - $genders = ['', 'male', 'female']; |
|
133 | - $userProfile->gender = $genders[(int)$data->get('sex')]; |
|
134 | - |
|
135 | - return $userProfile; |
|
136 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'snsapi_login,snsapi_userinfo,scope.userInfo'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://api.wechat.com/sns/'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl = 'https://open.weixin.qq.com/connect/qrconnect'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://api.wechat.com/sns/oauth2/access_token'; |
|
39 | + |
|
40 | + /** |
|
41 | + * Refresh Token Endpoint |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + protected $tokenRefreshUrl = 'https://api.wechat.com/sns/oauth2/refresh_token'; |
|
45 | + |
|
46 | + /** |
|
47 | + * {@ịnheritdoc} |
|
48 | + */ |
|
49 | + protected $accessTokenInfoUrl = 'https://api.wechat.com/sns/auth'; |
|
50 | + |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected $tokenExchangeMethod = 'GET'; |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + protected $tokenRefreshMethod = 'GET'; |
|
60 | + |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + protected $apiDocumentation = ''; // Not available |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function initialize() |
|
70 | + { |
|
71 | + parent::initialize(); |
|
72 | + |
|
73 | + $this->AuthorizeUrlParameters += [ |
|
74 | + 'appid' => $this->clientId |
|
75 | + ]; |
|
76 | + unset($this->AuthorizeUrlParameters['client_id']); |
|
77 | + |
|
78 | + $this->tokenExchangeParameters += [ |
|
79 | + 'appid' => $this->clientId, |
|
80 | + 'secret' => $this->clientSecret |
|
81 | + ]; |
|
82 | + unset($this->tokenExchangeParameters['client_id']); |
|
83 | + unset($this->tokenExchangeParameters['client_secret']); |
|
84 | + |
|
85 | + if ($this->isRefreshTokenAvailable()) { |
|
86 | + $this->tokenRefreshParameters += [ |
|
87 | + 'appid' => $this->clientId, |
|
88 | + ]; |
|
89 | + } |
|
90 | + |
|
91 | + $this->apiRequestParameters = [ |
|
92 | + 'appid' => $this->clientId, |
|
93 | + 'secret' => $this->clientSecret |
|
94 | + ]; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * {@inheritdoc} |
|
99 | + */ |
|
100 | + protected function validateAccessTokenExchange($response) |
|
101 | + { |
|
102 | + $collection = parent::validateAccessTokenExchange($response); |
|
103 | + |
|
104 | + $this->storeData('openid', $collection->get('openid')); |
|
105 | + $this->storeData('access_token', $collection->get('access_token')); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * {@inheritdoc} |
|
110 | + */ |
|
111 | + public function getUserProfile() |
|
112 | + { |
|
113 | + $openid = $this->getStoredData('openid'); |
|
114 | + $access_token = $this->getStoredData('access_token'); |
|
115 | + |
|
116 | + $response = $this->apiRequest('userinfo', 'GET', ['openid' => $openid, 'access_token' => $access_token]); |
|
117 | + |
|
118 | + $data = new Data\Collection($response); |
|
119 | + |
|
120 | + if (!$data->exists('openid')) { |
|
121 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
122 | + } |
|
123 | + |
|
124 | + $userProfile = new User\Profile(); |
|
125 | + |
|
126 | + $userProfile->identifier = $data->get('openid'); |
|
127 | + $userProfile->displayName = $data->get('nickname'); |
|
128 | + $userProfile->photoURL = $data->get('headimgurl'); |
|
129 | + $userProfile->city = $data->get('city'); |
|
130 | + $userProfile->region = $data->get('province'); |
|
131 | + $userProfile->country = $data->get('country'); |
|
132 | + $genders = ['', 'male', 'female']; |
|
133 | + $userProfile->gender = $genders[(int)$data->get('sex')]; |
|
134 | + |
|
135 | + return $userProfile; |
|
136 | + } |
|
137 | 137 | } |
@@ -40,225 +40,225 @@ |
||
40 | 40 | */ |
41 | 41 | class Twitter extends OAuth1 |
42 | 42 | { |
43 | - /** |
|
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - protected $apiBaseUrl = 'https://api.twitter.com/1.1/'; |
|
47 | - |
|
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - protected $authorizeUrl = 'https://api.twitter.com/oauth/authenticate'; |
|
52 | - |
|
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - protected $requestTokenUrl = 'https://api.twitter.com/oauth/request_token'; |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - protected $accessTokenUrl = 'https://api.twitter.com/oauth/access_token'; |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - protected $apiDocumentation = 'https://dev.twitter.com/web/sign-in/implementing'; |
|
67 | - |
|
68 | - /** |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected function getAuthorizeUrl($parameters = []) |
|
72 | - { |
|
73 | - if ($this->config->get('authorize') === true) { |
|
74 | - $this->authorizeUrl = 'https://api.twitter.com/oauth/authorize'; |
|
75 | - } |
|
76 | - |
|
77 | - return parent::getAuthorizeUrl($parameters); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritdoc} |
|
82 | - */ |
|
83 | - public function getUserProfile() |
|
84 | - { |
|
85 | - $response = $this->apiRequest('account/verify_credentials.json', 'GET', [ |
|
86 | - 'include_email' => $this->config->get('include_email') === false ? 'false' : 'true', |
|
87 | - ]); |
|
88 | - |
|
89 | - $data = new Data\Collection($response); |
|
90 | - |
|
91 | - if (!$data->exists('id_str')) { |
|
92 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
93 | - } |
|
94 | - |
|
95 | - $userProfile = new User\Profile(); |
|
96 | - |
|
97 | - $userProfile->identifier = $data->get('id_str'); |
|
98 | - $userProfile->displayName = $data->get('screen_name'); |
|
99 | - $userProfile->description = $data->get('description'); |
|
100 | - $userProfile->firstName = $data->get('name'); |
|
101 | - $userProfile->email = $data->get('email'); |
|
102 | - $userProfile->emailVerified = $data->get('email'); |
|
103 | - $userProfile->webSiteURL = $data->get('url'); |
|
104 | - $userProfile->region = $data->get('location'); |
|
105 | - |
|
106 | - $userProfile->profileURL = $data->exists('screen_name') |
|
107 | - ? ('https://twitter.com/' . $data->get('screen_name')) |
|
108 | - : ''; |
|
109 | - |
|
110 | - $photoSize = $this->config->get('photo_size') ?: 'original'; |
|
111 | - $photoSize = $photoSize === 'original' ? '' : "_{$photoSize}"; |
|
112 | - $userProfile->photoURL = $data->exists('profile_image_url_https') |
|
113 | - ? str_replace('_normal', $photoSize, $data->get('profile_image_url_https')) |
|
114 | - : ''; |
|
115 | - |
|
116 | - $userProfile->data = [ |
|
117 | - 'followed_by' => $data->get('followers_count'), |
|
118 | - 'follows' => $data->get('friends_count'), |
|
119 | - ]; |
|
120 | - |
|
121 | - return $userProfile; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * {@inheritdoc} |
|
126 | - */ |
|
127 | - public function getUserContacts($parameters = []) |
|
128 | - { |
|
129 | - $parameters = ['cursor' => '-1'] + $parameters; |
|
130 | - |
|
131 | - $response = $this->apiRequest('friends/ids.json', 'GET', $parameters); |
|
132 | - |
|
133 | - $data = new Data\Collection($response); |
|
134 | - |
|
135 | - if (!$data->exists('ids')) { |
|
136 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
137 | - } |
|
138 | - |
|
139 | - if ($data->filter('ids')->isEmpty()) { |
|
140 | - return []; |
|
141 | - } |
|
142 | - |
|
143 | - $contacts = []; |
|
144 | - |
|
145 | - // 75 id per time should be okey |
|
146 | - $contactsIds = array_chunk((array)$data->get('ids'), 75); |
|
147 | - |
|
148 | - foreach ($contactsIds as $chunk) { |
|
149 | - $parameters = ['user_id' => implode(',', $chunk)]; |
|
150 | - |
|
151 | - try { |
|
152 | - $response = $this->apiRequest('users/lookup.json', 'GET', $parameters); |
|
153 | - |
|
154 | - if ($response && count($response)) { |
|
155 | - foreach ($response as $item) { |
|
156 | - $contacts[] = $this->fetchUserContact($item); |
|
157 | - } |
|
158 | - } |
|
159 | - } catch (\Exception $e) { |
|
160 | - continue; |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - return $contacts; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @param $item |
|
169 | - * |
|
170 | - * @return User\Contact |
|
171 | - */ |
|
172 | - protected function fetchUserContact($item) |
|
173 | - { |
|
174 | - $item = new Data\Collection($item); |
|
175 | - |
|
176 | - $userContact = new User\Contact(); |
|
177 | - |
|
178 | - $userContact->identifier = $item->get('id_str'); |
|
179 | - $userContact->displayName = $item->get('name'); |
|
180 | - $userContact->photoURL = $item->get('profile_image_url'); |
|
181 | - $userContact->description = $item->get('description'); |
|
182 | - |
|
183 | - $userContact->profileURL = $item->exists('screen_name') |
|
184 | - ? ('https://twitter.com/' . $item->get('screen_name')) |
|
185 | - : ''; |
|
186 | - |
|
187 | - return $userContact; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * {@inheritdoc} |
|
192 | - */ |
|
193 | - public function setUserStatus($status) |
|
194 | - { |
|
195 | - if (is_string($status)) { |
|
196 | - $status = ['status' => $status]; |
|
197 | - } |
|
198 | - |
|
199 | - // Prepare request parameters. |
|
200 | - $params = []; |
|
201 | - if (isset($status['status'])) { |
|
202 | - $params['status'] = $status['status']; |
|
203 | - } |
|
204 | - if (isset($status['picture'])) { |
|
205 | - $media = $this->apiRequest('https://upload.twitter.com/1.1/media/upload.json', 'POST', [ |
|
206 | - 'media' => base64_encode(file_get_contents($status['picture'])), |
|
207 | - ]); |
|
208 | - $params['media_ids'] = $media->media_id; |
|
209 | - } |
|
210 | - |
|
211 | - $response = $this->apiRequest('statuses/update.json', 'POST', $params); |
|
212 | - |
|
213 | - return $response; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * {@inheritdoc} |
|
218 | - */ |
|
219 | - public function getUserActivity($stream = 'me') |
|
220 | - { |
|
221 | - $apiUrl = ($stream == 'me') |
|
222 | - ? 'statuses/user_timeline.json' |
|
223 | - : 'statuses/home_timeline.json'; |
|
224 | - |
|
225 | - $response = $this->apiRequest($apiUrl); |
|
226 | - |
|
227 | - if (!$response) { |
|
228 | - return []; |
|
229 | - } |
|
230 | - |
|
231 | - $activities = []; |
|
232 | - |
|
233 | - foreach ($response as $item) { |
|
234 | - $activities[] = $this->fetchUserActivity($item); |
|
235 | - } |
|
236 | - |
|
237 | - return $activities; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param $item |
|
242 | - * @return User\Activity |
|
243 | - */ |
|
244 | - protected function fetchUserActivity($item) |
|
245 | - { |
|
246 | - $item = new Data\Collection($item); |
|
247 | - |
|
248 | - $userActivity = new User\Activity(); |
|
249 | - |
|
250 | - $userActivity->id = $item->get('id_str'); |
|
251 | - $userActivity->date = $item->get('created_at'); |
|
252 | - $userActivity->text = $item->get('text'); |
|
253 | - |
|
254 | - $userActivity->user->identifier = $item->filter('user')->get('id_str'); |
|
255 | - $userActivity->user->displayName = $item->filter('user')->get('name'); |
|
256 | - $userActivity->user->photoURL = $item->filter('user')->get('profile_image_url'); |
|
257 | - |
|
258 | - $userActivity->user->profileURL = $item->filter('user')->get('screen_name') |
|
259 | - ? ('https://twitter.com/' . $item->filter('user')->get('screen_name')) |
|
260 | - : ''; |
|
261 | - |
|
262 | - return $userActivity; |
|
263 | - } |
|
43 | + /** |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + protected $apiBaseUrl = 'https://api.twitter.com/1.1/'; |
|
47 | + |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + protected $authorizeUrl = 'https://api.twitter.com/oauth/authenticate'; |
|
52 | + |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + protected $requestTokenUrl = 'https://api.twitter.com/oauth/request_token'; |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + protected $accessTokenUrl = 'https://api.twitter.com/oauth/access_token'; |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + protected $apiDocumentation = 'https://dev.twitter.com/web/sign-in/implementing'; |
|
67 | + |
|
68 | + /** |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected function getAuthorizeUrl($parameters = []) |
|
72 | + { |
|
73 | + if ($this->config->get('authorize') === true) { |
|
74 | + $this->authorizeUrl = 'https://api.twitter.com/oauth/authorize'; |
|
75 | + } |
|
76 | + |
|
77 | + return parent::getAuthorizeUrl($parameters); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritdoc} |
|
82 | + */ |
|
83 | + public function getUserProfile() |
|
84 | + { |
|
85 | + $response = $this->apiRequest('account/verify_credentials.json', 'GET', [ |
|
86 | + 'include_email' => $this->config->get('include_email') === false ? 'false' : 'true', |
|
87 | + ]); |
|
88 | + |
|
89 | + $data = new Data\Collection($response); |
|
90 | + |
|
91 | + if (!$data->exists('id_str')) { |
|
92 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
93 | + } |
|
94 | + |
|
95 | + $userProfile = new User\Profile(); |
|
96 | + |
|
97 | + $userProfile->identifier = $data->get('id_str'); |
|
98 | + $userProfile->displayName = $data->get('screen_name'); |
|
99 | + $userProfile->description = $data->get('description'); |
|
100 | + $userProfile->firstName = $data->get('name'); |
|
101 | + $userProfile->email = $data->get('email'); |
|
102 | + $userProfile->emailVerified = $data->get('email'); |
|
103 | + $userProfile->webSiteURL = $data->get('url'); |
|
104 | + $userProfile->region = $data->get('location'); |
|
105 | + |
|
106 | + $userProfile->profileURL = $data->exists('screen_name') |
|
107 | + ? ('https://twitter.com/' . $data->get('screen_name')) |
|
108 | + : ''; |
|
109 | + |
|
110 | + $photoSize = $this->config->get('photo_size') ?: 'original'; |
|
111 | + $photoSize = $photoSize === 'original' ? '' : "_{$photoSize}"; |
|
112 | + $userProfile->photoURL = $data->exists('profile_image_url_https') |
|
113 | + ? str_replace('_normal', $photoSize, $data->get('profile_image_url_https')) |
|
114 | + : ''; |
|
115 | + |
|
116 | + $userProfile->data = [ |
|
117 | + 'followed_by' => $data->get('followers_count'), |
|
118 | + 'follows' => $data->get('friends_count'), |
|
119 | + ]; |
|
120 | + |
|
121 | + return $userProfile; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * {@inheritdoc} |
|
126 | + */ |
|
127 | + public function getUserContacts($parameters = []) |
|
128 | + { |
|
129 | + $parameters = ['cursor' => '-1'] + $parameters; |
|
130 | + |
|
131 | + $response = $this->apiRequest('friends/ids.json', 'GET', $parameters); |
|
132 | + |
|
133 | + $data = new Data\Collection($response); |
|
134 | + |
|
135 | + if (!$data->exists('ids')) { |
|
136 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
137 | + } |
|
138 | + |
|
139 | + if ($data->filter('ids')->isEmpty()) { |
|
140 | + return []; |
|
141 | + } |
|
142 | + |
|
143 | + $contacts = []; |
|
144 | + |
|
145 | + // 75 id per time should be okey |
|
146 | + $contactsIds = array_chunk((array)$data->get('ids'), 75); |
|
147 | + |
|
148 | + foreach ($contactsIds as $chunk) { |
|
149 | + $parameters = ['user_id' => implode(',', $chunk)]; |
|
150 | + |
|
151 | + try { |
|
152 | + $response = $this->apiRequest('users/lookup.json', 'GET', $parameters); |
|
153 | + |
|
154 | + if ($response && count($response)) { |
|
155 | + foreach ($response as $item) { |
|
156 | + $contacts[] = $this->fetchUserContact($item); |
|
157 | + } |
|
158 | + } |
|
159 | + } catch (\Exception $e) { |
|
160 | + continue; |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + return $contacts; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @param $item |
|
169 | + * |
|
170 | + * @return User\Contact |
|
171 | + */ |
|
172 | + protected function fetchUserContact($item) |
|
173 | + { |
|
174 | + $item = new Data\Collection($item); |
|
175 | + |
|
176 | + $userContact = new User\Contact(); |
|
177 | + |
|
178 | + $userContact->identifier = $item->get('id_str'); |
|
179 | + $userContact->displayName = $item->get('name'); |
|
180 | + $userContact->photoURL = $item->get('profile_image_url'); |
|
181 | + $userContact->description = $item->get('description'); |
|
182 | + |
|
183 | + $userContact->profileURL = $item->exists('screen_name') |
|
184 | + ? ('https://twitter.com/' . $item->get('screen_name')) |
|
185 | + : ''; |
|
186 | + |
|
187 | + return $userContact; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * {@inheritdoc} |
|
192 | + */ |
|
193 | + public function setUserStatus($status) |
|
194 | + { |
|
195 | + if (is_string($status)) { |
|
196 | + $status = ['status' => $status]; |
|
197 | + } |
|
198 | + |
|
199 | + // Prepare request parameters. |
|
200 | + $params = []; |
|
201 | + if (isset($status['status'])) { |
|
202 | + $params['status'] = $status['status']; |
|
203 | + } |
|
204 | + if (isset($status['picture'])) { |
|
205 | + $media = $this->apiRequest('https://upload.twitter.com/1.1/media/upload.json', 'POST', [ |
|
206 | + 'media' => base64_encode(file_get_contents($status['picture'])), |
|
207 | + ]); |
|
208 | + $params['media_ids'] = $media->media_id; |
|
209 | + } |
|
210 | + |
|
211 | + $response = $this->apiRequest('statuses/update.json', 'POST', $params); |
|
212 | + |
|
213 | + return $response; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * {@inheritdoc} |
|
218 | + */ |
|
219 | + public function getUserActivity($stream = 'me') |
|
220 | + { |
|
221 | + $apiUrl = ($stream == 'me') |
|
222 | + ? 'statuses/user_timeline.json' |
|
223 | + : 'statuses/home_timeline.json'; |
|
224 | + |
|
225 | + $response = $this->apiRequest($apiUrl); |
|
226 | + |
|
227 | + if (!$response) { |
|
228 | + return []; |
|
229 | + } |
|
230 | + |
|
231 | + $activities = []; |
|
232 | + |
|
233 | + foreach ($response as $item) { |
|
234 | + $activities[] = $this->fetchUserActivity($item); |
|
235 | + } |
|
236 | + |
|
237 | + return $activities; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param $item |
|
242 | + * @return User\Activity |
|
243 | + */ |
|
244 | + protected function fetchUserActivity($item) |
|
245 | + { |
|
246 | + $item = new Data\Collection($item); |
|
247 | + |
|
248 | + $userActivity = new User\Activity(); |
|
249 | + |
|
250 | + $userActivity->id = $item->get('id_str'); |
|
251 | + $userActivity->date = $item->get('created_at'); |
|
252 | + $userActivity->text = $item->get('text'); |
|
253 | + |
|
254 | + $userActivity->user->identifier = $item->filter('user')->get('id_str'); |
|
255 | + $userActivity->user->displayName = $item->filter('user')->get('name'); |
|
256 | + $userActivity->user->photoURL = $item->filter('user')->get('profile_image_url'); |
|
257 | + |
|
258 | + $userActivity->user->profileURL = $item->filter('user')->get('screen_name') |
|
259 | + ? ('https://twitter.com/' . $item->filter('user')->get('screen_name')) |
|
260 | + : ''; |
|
261 | + |
|
262 | + return $userActivity; |
|
263 | + } |
|
264 | 264 | } |