@@ -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 | } |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | parent::initialize(); |
77 | 77 | |
78 | 78 | $tenant = $this->config->get('tenant'); |
79 | - if (!empty($tenant)) { |
|
79 | + if ( ! empty($tenant)) { |
|
80 | 80 | $adjustedEndpoints = [ |
81 | - 'authorize_url' => str_replace('/common/', '/' . $tenant . '/', $this->authorizeUrl), |
|
82 | - 'access_token_url' => str_replace('/common/', '/' . $tenant . '/', $this->accessTokenUrl), |
|
81 | + 'authorize_url' => str_replace('/common/', '/'.$tenant.'/', $this->authorizeUrl), |
|
82 | + 'access_token_url' => str_replace('/common/', '/'.$tenant.'/', $this->accessTokenUrl), |
|
83 | 83 | ]; |
84 | 84 | |
85 | 85 | $this->setApiEndpoints($adjustedEndpoints); |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | |
96 | 96 | $data = new Data\Collection($response); |
97 | 97 | |
98 | - if (!$data->exists('id')) { |
|
98 | + if ( ! $data->exists('id')) { |
|
99 | 99 | throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
100 | 100 | } |
101 | 101 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | do { |
138 | 138 | $response = $this->apiRequest($apiUrl); |
139 | 139 | $data = new Data\Collection($response); |
140 | - if (!$data->exists('value')) { |
|
140 | + if ( ! $data->exists('value')) { |
|
141 | 141 | throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
142 | 142 | } |
143 | 143 | foreach ($data->filter('value')->toArray() as $entry) { |
@@ -146,11 +146,11 @@ discard block |
||
146 | 146 | $userContact->identifier = $entry->get('id'); |
147 | 147 | $userContact->displayName = $entry->get('displayName'); |
148 | 148 | $emailAddresses = $entry->get('emailAddresses'); |
149 | - if (!empty($emailAddresses)) { |
|
149 | + if ( ! empty($emailAddresses)) { |
|
150 | 150 | $userContact->email = $emailAddresses[0]->address; |
151 | 151 | } |
152 | 152 | // only add to collection if we have usefull data |
153 | - if (!empty($userContact->displayName) || !empty($userContact->email)) { |
|
153 | + if ( ! empty($userContact->displayName) || ! empty($userContact->email)) { |
|
154 | 154 | $contacts[] = $userContact; |
155 | 155 | } |
156 | 156 | } |
@@ -60,229 +60,229 @@ |
||
60 | 60 | */ |
61 | 61 | class Apple extends OAuth2 |
62 | 62 | { |
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - protected $scope = 'name email'; |
|
67 | - |
|
68 | - /** |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected $apiBaseUrl = 'https://appleid.apple.com/auth/'; |
|
72 | - |
|
73 | - /** |
|
74 | - * {@inheritdoc} |
|
75 | - */ |
|
76 | - protected $authorizeUrl = 'https://appleid.apple.com/auth/authorize'; |
|
77 | - |
|
78 | - /** |
|
79 | - * {@inheritdoc} |
|
80 | - */ |
|
81 | - protected $accessTokenUrl = 'https://appleid.apple.com/auth/token'; |
|
82 | - |
|
83 | - /** |
|
84 | - * {@inheritdoc} |
|
85 | - */ |
|
86 | - protected $apiDocumentation = 'https://developer.apple.com/documentation/sign_in_with_apple'; |
|
87 | - |
|
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - * The Sign in with Apple servers require percent encoding (or URL encoding) |
|
91 | - * for its query parameters. If you are using the Sign in with Apple REST API, |
|
92 | - * you must provide values with encoded spaces (`%20`) instead of plus (`+`) signs. |
|
93 | - */ |
|
94 | - protected $AuthorizeUrlParametersEncType = PHP_QUERY_RFC3986; |
|
95 | - |
|
96 | - /** |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - protected function initialize() |
|
100 | - { |
|
101 | - parent::initialize(); |
|
102 | - $this->AuthorizeUrlParameters['response_mode'] = 'form_post'; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * {@inheritdoc} |
|
107 | - */ |
|
108 | - protected function configure() |
|
109 | - { |
|
110 | - $keys = $this->config->get('keys'); |
|
111 | - $keys['secret'] = $this->getSecret(); |
|
112 | - $this->config->set('keys', $keys); |
|
113 | - return parent::configure(); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * {@inheritdoc} |
|
118 | - * |
|
119 | - * include id_token $tokenNames |
|
120 | - */ |
|
121 | - public function getAccessToken() |
|
122 | - { |
|
123 | - $tokenNames = [ |
|
124 | - 'access_token', |
|
125 | - 'id_token', |
|
126 | - 'access_token_secret', |
|
127 | - 'token_type', |
|
128 | - 'refresh_token', |
|
129 | - 'expires_in', |
|
130 | - 'expires_at', |
|
131 | - ]; |
|
132 | - |
|
133 | - $tokens = []; |
|
134 | - |
|
135 | - foreach ($tokenNames as $name) { |
|
136 | - if ($this->getStoredData($name)) { |
|
137 | - $tokens[$name] = $this->getStoredData($name); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - return $tokens; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - protected function validateAccessTokenExchange($response) |
|
148 | - { |
|
149 | - $collection = parent::validateAccessTokenExchange($response); |
|
150 | - |
|
151 | - $this->storeData('id_token', $collection->get('id_token')); |
|
152 | - |
|
153 | - return $collection; |
|
154 | - } |
|
155 | - |
|
156 | - public function getUserProfile() |
|
157 | - { |
|
158 | - $id_token = $this->getStoredData('id_token'); |
|
159 | - |
|
160 | - $verifyTokenSignature = |
|
161 | - ($this->config->exists('verifyTokenSignature')) ? $this->config->get('verifyTokenSignature') : true; |
|
162 | - |
|
163 | - if (!$verifyTokenSignature) { |
|
164 | - // payload extraction by https://github.com/omidborjian |
|
165 | - // https://github.com/hybridauth/hybridauth/issues/1095#issuecomment-626479263 |
|
166 | - // JWT splits the string to 3 components 1) first is header 2) is payload 3) is signature |
|
167 | - $payload = explode('.', $id_token)[1]; |
|
168 | - $payload = json_decode(base64_decode($payload)); |
|
169 | - } else { |
|
170 | - // validate the token signature and get the payload |
|
171 | - $publicKeys = $this->apiRequest('keys'); |
|
172 | - |
|
173 | - \Firebase\JWT\JWT::$leeway = 120; |
|
174 | - |
|
175 | - $error = false; |
|
176 | - $payload = null; |
|
177 | - |
|
178 | - foreach ($publicKeys->keys as $publicKey) { |
|
179 | - try { |
|
180 | - $rsa = new RSA(); |
|
181 | - $jwk = (array)$publicKey; |
|
182 | - |
|
183 | - $rsa->loadKey( |
|
184 | - [ |
|
185 | - 'e' => new BigInteger(base64_decode($jwk['e']), 256), |
|
186 | - 'n' => new BigInteger(base64_decode(strtr($jwk['n'], '-_', '+/'), true), 256) |
|
187 | - ] |
|
188 | - ); |
|
189 | - $pem = $rsa->getPublicKey(); |
|
190 | - |
|
191 | - $payload = JWT::decode($id_token, $pem, ['RS256']); |
|
192 | - break; |
|
193 | - } catch (\Exception $e) { |
|
194 | - $error = $e->getMessage(); |
|
195 | - if ($e instanceof \Firebase\JWT\ExpiredException) { |
|
196 | - break; |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - if ($error) { |
|
201 | - throw new \Exception($error); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - $data = new Data\Collection($payload); |
|
206 | - |
|
207 | - if (!$data->exists('sub')) { |
|
208 | - throw new UnexpectedValueException('Missing token payload.'); |
|
209 | - } |
|
210 | - |
|
211 | - $userProfile = new User\Profile(); |
|
212 | - $userProfile->identifier = $data->get('sub'); |
|
213 | - $userProfile->email = $data->get('email'); |
|
214 | - $this->storeData('expires_at', $data->get('exp')); |
|
215 | - |
|
216 | - if (!empty($_REQUEST['user'])) { |
|
217 | - $objUser = json_decode($_REQUEST['user']); |
|
218 | - $user = new Data\Collection($objUser); |
|
219 | - if (!$user->isEmpty()) { |
|
220 | - $name = $user->get('name'); |
|
221 | - $userProfile->firstName = $name->firstName; |
|
222 | - $userProfile->lastName = $name->lastName; |
|
223 | - $userProfile->displayName = join(' ', [$userProfile->firstName, $userProfile->lastName]); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - return $userProfile; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * @return string secret token |
|
232 | - */ |
|
233 | - private function getSecret() |
|
234 | - { |
|
235 | - // Your 10-character Team ID |
|
236 | - if (!$team_id = $this->config->filter('keys')->get('team_id')) { |
|
237 | - throw new InvalidApplicationCredentialsException( |
|
238 | - 'Missing parameter team_id: your team id is required to generate the JWS token.' |
|
239 | - ); |
|
240 | - } |
|
241 | - |
|
242 | - // Your Services ID, e.g. com.aaronparecki.services |
|
243 | - if (!$client_id = $this->config->filter('keys')->get('id') ?: $this->config->filter('keys')->get('key')) { |
|
244 | - throw new InvalidApplicationCredentialsException( |
|
245 | - 'Missing parameter id: your client id is required to generate the JWS token.' |
|
246 | - ); |
|
247 | - } |
|
248 | - |
|
249 | - // Find the 10-char Key ID value from the portal |
|
250 | - if (!$key_id = $this->config->filter('keys')->get('key_id')) { |
|
251 | - throw new InvalidApplicationCredentialsException( |
|
252 | - 'Missing parameter key_id: your key id is required to generate the JWS token.' |
|
253 | - ); |
|
254 | - } |
|
255 | - |
|
256 | - // Find the 10-char Key ID value from the portal |
|
257 | - $key_content = $this->config->filter('keys')->get('key_content'); |
|
258 | - |
|
259 | - // Save your private key from Apple in a file called `key.txt` |
|
260 | - if (!$key_content) { |
|
261 | - if (!$key_file = $this->config->filter('keys')->get('key_file')) { |
|
262 | - throw new InvalidApplicationCredentialsException( |
|
263 | - 'Missing parameter key_content or key_file: your key is required to generate the JWS token.' |
|
264 | - ); |
|
265 | - } |
|
266 | - |
|
267 | - if (!file_exists($key_file)) { |
|
268 | - throw new InvalidApplicationCredentialsException( |
|
269 | - "Your key file $key_file does not exist." |
|
270 | - ); |
|
271 | - } |
|
272 | - |
|
273 | - $key_content = file_get_contents($key_file); |
|
274 | - } |
|
275 | - |
|
276 | - $data = [ |
|
277 | - 'iat' => time(), |
|
278 | - 'exp' => time() + 86400 * 180, |
|
279 | - 'iss' => $team_id, |
|
280 | - 'aud' => 'https://appleid.apple.com', |
|
281 | - 'sub' => $client_id |
|
282 | - ]; |
|
283 | - |
|
284 | - $secret = JWT::encode($data, $key_content, 'ES256', $key_id); |
|
285 | - |
|
286 | - return $secret; |
|
287 | - } |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + protected $scope = 'name email'; |
|
67 | + |
|
68 | + /** |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected $apiBaseUrl = 'https://appleid.apple.com/auth/'; |
|
72 | + |
|
73 | + /** |
|
74 | + * {@inheritdoc} |
|
75 | + */ |
|
76 | + protected $authorizeUrl = 'https://appleid.apple.com/auth/authorize'; |
|
77 | + |
|
78 | + /** |
|
79 | + * {@inheritdoc} |
|
80 | + */ |
|
81 | + protected $accessTokenUrl = 'https://appleid.apple.com/auth/token'; |
|
82 | + |
|
83 | + /** |
|
84 | + * {@inheritdoc} |
|
85 | + */ |
|
86 | + protected $apiDocumentation = 'https://developer.apple.com/documentation/sign_in_with_apple'; |
|
87 | + |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + * The Sign in with Apple servers require percent encoding (or URL encoding) |
|
91 | + * for its query parameters. If you are using the Sign in with Apple REST API, |
|
92 | + * you must provide values with encoded spaces (`%20`) instead of plus (`+`) signs. |
|
93 | + */ |
|
94 | + protected $AuthorizeUrlParametersEncType = PHP_QUERY_RFC3986; |
|
95 | + |
|
96 | + /** |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + protected function initialize() |
|
100 | + { |
|
101 | + parent::initialize(); |
|
102 | + $this->AuthorizeUrlParameters['response_mode'] = 'form_post'; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * {@inheritdoc} |
|
107 | + */ |
|
108 | + protected function configure() |
|
109 | + { |
|
110 | + $keys = $this->config->get('keys'); |
|
111 | + $keys['secret'] = $this->getSecret(); |
|
112 | + $this->config->set('keys', $keys); |
|
113 | + return parent::configure(); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * {@inheritdoc} |
|
118 | + * |
|
119 | + * include id_token $tokenNames |
|
120 | + */ |
|
121 | + public function getAccessToken() |
|
122 | + { |
|
123 | + $tokenNames = [ |
|
124 | + 'access_token', |
|
125 | + 'id_token', |
|
126 | + 'access_token_secret', |
|
127 | + 'token_type', |
|
128 | + 'refresh_token', |
|
129 | + 'expires_in', |
|
130 | + 'expires_at', |
|
131 | + ]; |
|
132 | + |
|
133 | + $tokens = []; |
|
134 | + |
|
135 | + foreach ($tokenNames as $name) { |
|
136 | + if ($this->getStoredData($name)) { |
|
137 | + $tokens[$name] = $this->getStoredData($name); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + return $tokens; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + protected function validateAccessTokenExchange($response) |
|
148 | + { |
|
149 | + $collection = parent::validateAccessTokenExchange($response); |
|
150 | + |
|
151 | + $this->storeData('id_token', $collection->get('id_token')); |
|
152 | + |
|
153 | + return $collection; |
|
154 | + } |
|
155 | + |
|
156 | + public function getUserProfile() |
|
157 | + { |
|
158 | + $id_token = $this->getStoredData('id_token'); |
|
159 | + |
|
160 | + $verifyTokenSignature = |
|
161 | + ($this->config->exists('verifyTokenSignature')) ? $this->config->get('verifyTokenSignature') : true; |
|
162 | + |
|
163 | + if (!$verifyTokenSignature) { |
|
164 | + // payload extraction by https://github.com/omidborjian |
|
165 | + // https://github.com/hybridauth/hybridauth/issues/1095#issuecomment-626479263 |
|
166 | + // JWT splits the string to 3 components 1) first is header 2) is payload 3) is signature |
|
167 | + $payload = explode('.', $id_token)[1]; |
|
168 | + $payload = json_decode(base64_decode($payload)); |
|
169 | + } else { |
|
170 | + // validate the token signature and get the payload |
|
171 | + $publicKeys = $this->apiRequest('keys'); |
|
172 | + |
|
173 | + \Firebase\JWT\JWT::$leeway = 120; |
|
174 | + |
|
175 | + $error = false; |
|
176 | + $payload = null; |
|
177 | + |
|
178 | + foreach ($publicKeys->keys as $publicKey) { |
|
179 | + try { |
|
180 | + $rsa = new RSA(); |
|
181 | + $jwk = (array)$publicKey; |
|
182 | + |
|
183 | + $rsa->loadKey( |
|
184 | + [ |
|
185 | + 'e' => new BigInteger(base64_decode($jwk['e']), 256), |
|
186 | + 'n' => new BigInteger(base64_decode(strtr($jwk['n'], '-_', '+/'), true), 256) |
|
187 | + ] |
|
188 | + ); |
|
189 | + $pem = $rsa->getPublicKey(); |
|
190 | + |
|
191 | + $payload = JWT::decode($id_token, $pem, ['RS256']); |
|
192 | + break; |
|
193 | + } catch (\Exception $e) { |
|
194 | + $error = $e->getMessage(); |
|
195 | + if ($e instanceof \Firebase\JWT\ExpiredException) { |
|
196 | + break; |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + if ($error) { |
|
201 | + throw new \Exception($error); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + $data = new Data\Collection($payload); |
|
206 | + |
|
207 | + if (!$data->exists('sub')) { |
|
208 | + throw new UnexpectedValueException('Missing token payload.'); |
|
209 | + } |
|
210 | + |
|
211 | + $userProfile = new User\Profile(); |
|
212 | + $userProfile->identifier = $data->get('sub'); |
|
213 | + $userProfile->email = $data->get('email'); |
|
214 | + $this->storeData('expires_at', $data->get('exp')); |
|
215 | + |
|
216 | + if (!empty($_REQUEST['user'])) { |
|
217 | + $objUser = json_decode($_REQUEST['user']); |
|
218 | + $user = new Data\Collection($objUser); |
|
219 | + if (!$user->isEmpty()) { |
|
220 | + $name = $user->get('name'); |
|
221 | + $userProfile->firstName = $name->firstName; |
|
222 | + $userProfile->lastName = $name->lastName; |
|
223 | + $userProfile->displayName = join(' ', [$userProfile->firstName, $userProfile->lastName]); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + return $userProfile; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * @return string secret token |
|
232 | + */ |
|
233 | + private function getSecret() |
|
234 | + { |
|
235 | + // Your 10-character Team ID |
|
236 | + if (!$team_id = $this->config->filter('keys')->get('team_id')) { |
|
237 | + throw new InvalidApplicationCredentialsException( |
|
238 | + 'Missing parameter team_id: your team id is required to generate the JWS token.' |
|
239 | + ); |
|
240 | + } |
|
241 | + |
|
242 | + // Your Services ID, e.g. com.aaronparecki.services |
|
243 | + if (!$client_id = $this->config->filter('keys')->get('id') ?: $this->config->filter('keys')->get('key')) { |
|
244 | + throw new InvalidApplicationCredentialsException( |
|
245 | + 'Missing parameter id: your client id is required to generate the JWS token.' |
|
246 | + ); |
|
247 | + } |
|
248 | + |
|
249 | + // Find the 10-char Key ID value from the portal |
|
250 | + if (!$key_id = $this->config->filter('keys')->get('key_id')) { |
|
251 | + throw new InvalidApplicationCredentialsException( |
|
252 | + 'Missing parameter key_id: your key id is required to generate the JWS token.' |
|
253 | + ); |
|
254 | + } |
|
255 | + |
|
256 | + // Find the 10-char Key ID value from the portal |
|
257 | + $key_content = $this->config->filter('keys')->get('key_content'); |
|
258 | + |
|
259 | + // Save your private key from Apple in a file called `key.txt` |
|
260 | + if (!$key_content) { |
|
261 | + if (!$key_file = $this->config->filter('keys')->get('key_file')) { |
|
262 | + throw new InvalidApplicationCredentialsException( |
|
263 | + 'Missing parameter key_content or key_file: your key is required to generate the JWS token.' |
|
264 | + ); |
|
265 | + } |
|
266 | + |
|
267 | + if (!file_exists($key_file)) { |
|
268 | + throw new InvalidApplicationCredentialsException( |
|
269 | + "Your key file $key_file does not exist." |
|
270 | + ); |
|
271 | + } |
|
272 | + |
|
273 | + $key_content = file_get_contents($key_file); |
|
274 | + } |
|
275 | + |
|
276 | + $data = [ |
|
277 | + 'iat' => time(), |
|
278 | + 'exp' => time() + 86400 * 180, |
|
279 | + 'iss' => $team_id, |
|
280 | + 'aud' => 'https://appleid.apple.com', |
|
281 | + 'sub' => $client_id |
|
282 | + ]; |
|
283 | + |
|
284 | + $secret = JWT::encode($data, $key_content, 'ES256', $key_id); |
|
285 | + |
|
286 | + return $secret; |
|
287 | + } |
|
288 | 288 | } |
@@ -45,405 +45,405 @@ |
||
45 | 45 | */ |
46 | 46 | class Facebook extends OAuth2 |
47 | 47 | { |
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - protected $scope = 'email, public_profile'; |
|
52 | - |
|
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - protected $apiBaseUrl = 'https://graph.facebook.com/v8.0/'; |
|
57 | - |
|
58 | - /** |
|
59 | - * {@inheritdoc} |
|
60 | - */ |
|
61 | - protected $authorizeUrl = 'https://www.facebook.com/dialog/oauth'; |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - protected $accessTokenUrl = 'https://graph.facebook.com/oauth/access_token'; |
|
67 | - |
|
68 | - /** |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected $apiDocumentation = 'https://developers.facebook.com/docs/facebook-login/overview'; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var string Profile URL template as the fallback when no `link` returned from the API. |
|
75 | - */ |
|
76 | - protected $profileUrlTemplate = 'https://www.facebook.com/%s'; |
|
77 | - |
|
78 | - /** |
|
79 | - * {@inheritdoc} |
|
80 | - */ |
|
81 | - protected function initialize() |
|
82 | - { |
|
83 | - parent::initialize(); |
|
84 | - |
|
85 | - // Require proof on all Facebook api calls |
|
86 | - // https://developers.facebook.com/docs/graph-api/securing-requests#appsecret_proof |
|
87 | - if ($accessToken = $this->getStoredData('access_token')) { |
|
88 | - $this->apiRequestParameters['appsecret_proof'] = hash_hmac('sha256', $accessToken, $this->clientSecret); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * {@inheritdoc} |
|
94 | - */ |
|
95 | - public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
96 | - { |
|
97 | - // Handle token exchange prior to the standard handler for an API request |
|
98 | - $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
99 | - if ($exchange_by_expiry_days !== null) { |
|
100 | - $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
101 | - if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
102 | - $this->exchangeAccessToken(); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - return parent::apiRequest($url, $method, $parameters, $headers, $multipart); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Exchange the Access Token with one that expires further in the future. |
|
111 | - * |
|
112 | - * @return string Raw Provider API response |
|
113 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
114 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
115 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
116 | - */ |
|
117 | - public function exchangeAccessToken() |
|
118 | - { |
|
119 | - $exchangeTokenParameters = [ |
|
120 | - 'grant_type' => 'fb_exchange_token', |
|
121 | - 'client_id' => $this->clientId, |
|
122 | - 'client_secret' => $this->clientSecret, |
|
123 | - 'fb_exchange_token' => $this->getStoredData('access_token'), |
|
124 | - ]; |
|
125 | - |
|
126 | - $response = $this->httpClient->request( |
|
127 | - $this->accessTokenUrl, |
|
128 | - 'GET', |
|
129 | - $exchangeTokenParameters |
|
130 | - ); |
|
131 | - |
|
132 | - $this->validateApiResponse('Unable to exchange the access token'); |
|
133 | - |
|
134 | - $this->validateAccessTokenExchange($response); |
|
135 | - |
|
136 | - return $response; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * {@inheritdoc} |
|
141 | - */ |
|
142 | - public function getUserProfile() |
|
143 | - { |
|
144 | - $fields = [ |
|
145 | - 'id', |
|
146 | - 'name', |
|
147 | - 'first_name', |
|
148 | - 'last_name', |
|
149 | - 'website', |
|
150 | - 'locale', |
|
151 | - 'about', |
|
152 | - 'email', |
|
153 | - 'hometown', |
|
154 | - 'birthday', |
|
155 | - ]; |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + protected $scope = 'email, public_profile'; |
|
52 | + |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + protected $apiBaseUrl = 'https://graph.facebook.com/v8.0/'; |
|
57 | + |
|
58 | + /** |
|
59 | + * {@inheritdoc} |
|
60 | + */ |
|
61 | + protected $authorizeUrl = 'https://www.facebook.com/dialog/oauth'; |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + protected $accessTokenUrl = 'https://graph.facebook.com/oauth/access_token'; |
|
67 | + |
|
68 | + /** |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected $apiDocumentation = 'https://developers.facebook.com/docs/facebook-login/overview'; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var string Profile URL template as the fallback when no `link` returned from the API. |
|
75 | + */ |
|
76 | + protected $profileUrlTemplate = 'https://www.facebook.com/%s'; |
|
77 | + |
|
78 | + /** |
|
79 | + * {@inheritdoc} |
|
80 | + */ |
|
81 | + protected function initialize() |
|
82 | + { |
|
83 | + parent::initialize(); |
|
84 | + |
|
85 | + // Require proof on all Facebook api calls |
|
86 | + // https://developers.facebook.com/docs/graph-api/securing-requests#appsecret_proof |
|
87 | + if ($accessToken = $this->getStoredData('access_token')) { |
|
88 | + $this->apiRequestParameters['appsecret_proof'] = hash_hmac('sha256', $accessToken, $this->clientSecret); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * {@inheritdoc} |
|
94 | + */ |
|
95 | + public function apiRequest($url, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
96 | + { |
|
97 | + // Handle token exchange prior to the standard handler for an API request |
|
98 | + $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
99 | + if ($exchange_by_expiry_days !== null) { |
|
100 | + $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
101 | + if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
102 | + $this->exchangeAccessToken(); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + return parent::apiRequest($url, $method, $parameters, $headers, $multipart); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Exchange the Access Token with one that expires further in the future. |
|
111 | + * |
|
112 | + * @return string Raw Provider API response |
|
113 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
114 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
115 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
116 | + */ |
|
117 | + public function exchangeAccessToken() |
|
118 | + { |
|
119 | + $exchangeTokenParameters = [ |
|
120 | + 'grant_type' => 'fb_exchange_token', |
|
121 | + 'client_id' => $this->clientId, |
|
122 | + 'client_secret' => $this->clientSecret, |
|
123 | + 'fb_exchange_token' => $this->getStoredData('access_token'), |
|
124 | + ]; |
|
125 | + |
|
126 | + $response = $this->httpClient->request( |
|
127 | + $this->accessTokenUrl, |
|
128 | + 'GET', |
|
129 | + $exchangeTokenParameters |
|
130 | + ); |
|
131 | + |
|
132 | + $this->validateApiResponse('Unable to exchange the access token'); |
|
133 | + |
|
134 | + $this->validateAccessTokenExchange($response); |
|
135 | + |
|
136 | + return $response; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * {@inheritdoc} |
|
141 | + */ |
|
142 | + public function getUserProfile() |
|
143 | + { |
|
144 | + $fields = [ |
|
145 | + 'id', |
|
146 | + 'name', |
|
147 | + 'first_name', |
|
148 | + 'last_name', |
|
149 | + 'website', |
|
150 | + 'locale', |
|
151 | + 'about', |
|
152 | + 'email', |
|
153 | + 'hometown', |
|
154 | + 'birthday', |
|
155 | + ]; |
|
156 | 156 | |
157 | - if (strpos($this->scope, 'user_link') !== false) { |
|
158 | - $fields[] = 'link'; |
|
159 | - } |
|
160 | - |
|
161 | - if (strpos($this->scope, 'user_gender') !== false) { |
|
162 | - $fields[] = 'gender'; |
|
163 | - } |
|
164 | - |
|
165 | - // Note that en_US is needed for gender fields to match convention. |
|
166 | - $locale = $this->config->get('locale') ?: 'en_US'; |
|
167 | - $response = $this->apiRequest('me', 'GET', [ |
|
168 | - 'fields' => implode(',', $fields), |
|
169 | - 'locale' => $locale, |
|
170 | - ]); |
|
171 | - |
|
172 | - $data = new Data\Collection($response); |
|
173 | - |
|
174 | - if (!$data->exists('id')) { |
|
175 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
176 | - } |
|
177 | - |
|
178 | - $userProfile = new User\Profile(); |
|
179 | - |
|
180 | - $userProfile->identifier = $data->get('id'); |
|
181 | - $userProfile->displayName = $data->get('name'); |
|
182 | - $userProfile->firstName = $data->get('first_name'); |
|
183 | - $userProfile->lastName = $data->get('last_name'); |
|
184 | - $userProfile->profileURL = $data->get('link'); |
|
185 | - $userProfile->webSiteURL = $data->get('website'); |
|
186 | - $userProfile->gender = $data->get('gender'); |
|
187 | - $userProfile->language = $data->get('locale'); |
|
188 | - $userProfile->description = $data->get('about'); |
|
189 | - $userProfile->email = $data->get('email'); |
|
190 | - |
|
191 | - // Fallback for profile URL in case Facebook does not provide "pretty" link with username (if user set it). |
|
192 | - if (empty($userProfile->profileURL)) { |
|
193 | - $userProfile->profileURL = $this->getProfileUrl($userProfile->identifier); |
|
194 | - } |
|
195 | - |
|
196 | - $userProfile->region = $data->filter('hometown')->get('name'); |
|
197 | - |
|
198 | - $photoSize = $this->config->get('photo_size') ?: '150'; |
|
199 | - |
|
200 | - $userProfile->photoURL = $this->apiBaseUrl . $userProfile->identifier; |
|
201 | - $userProfile->photoURL .= '/picture?width=' . $photoSize . '&height=' . $photoSize; |
|
202 | - |
|
203 | - $userProfile->emailVerified = $userProfile->email; |
|
204 | - |
|
205 | - $userProfile = $this->fetchUserRegion($userProfile); |
|
206 | - |
|
207 | - $userProfile = $this->fetchBirthday($userProfile, $data->get('birthday')); |
|
208 | - |
|
209 | - return $userProfile; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Retrieve the user region. |
|
214 | - * |
|
215 | - * @param User\Profile $userProfile |
|
216 | - * |
|
217 | - * @return \Hybridauth\User\Profile |
|
218 | - */ |
|
219 | - protected function fetchUserRegion(User\Profile $userProfile) |
|
220 | - { |
|
221 | - if (!empty($userProfile->region)) { |
|
222 | - $regionArr = explode(',', $userProfile->region); |
|
223 | - |
|
224 | - if (count($regionArr) > 1) { |
|
225 | - $userProfile->city = trim($regionArr[0]); |
|
226 | - $userProfile->country = trim($regionArr[1]); |
|
227 | - } |
|
228 | - } |
|
229 | - |
|
230 | - return $userProfile; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Retrieve the user birthday. |
|
235 | - * |
|
236 | - * @param User\Profile $userProfile |
|
237 | - * @param string $birthday |
|
238 | - * |
|
239 | - * @return \Hybridauth\User\Profile |
|
240 | - */ |
|
241 | - protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
242 | - { |
|
243 | - $result = (new Data\Parser())->parseBirthday($birthday, '/'); |
|
244 | - |
|
245 | - $userProfile->birthYear = (int)$result[0]; |
|
246 | - $userProfile->birthMonth = (int)$result[1]; |
|
247 | - $userProfile->birthDay = (int)$result[2]; |
|
248 | - |
|
249 | - return $userProfile; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * /v2.0/me/friends only returns the user's friends who also use the app. |
|
254 | - * In the cases where you want to let people tag their friends in stories published by your app, |
|
255 | - * you can use the Taggable Friends API. |
|
256 | - * |
|
257 | - * https://developers.facebook.com/docs/apps/faq#unable_full_friend_list |
|
258 | - */ |
|
259 | - public function getUserContacts() |
|
260 | - { |
|
261 | - $contacts = []; |
|
262 | - |
|
263 | - $apiUrl = 'me/friends?fields=link,name'; |
|
264 | - |
|
265 | - do { |
|
266 | - $response = $this->apiRequest($apiUrl); |
|
267 | - |
|
268 | - $data = new Data\Collection($response); |
|
269 | - |
|
270 | - if (!$data->exists('data')) { |
|
271 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
272 | - } |
|
273 | - |
|
274 | - if (!$data->filter('data')->isEmpty()) { |
|
275 | - foreach ($data->filter('data')->toArray() as $item) { |
|
276 | - $contacts[] = $this->fetchUserContact($item); |
|
277 | - } |
|
278 | - } |
|
279 | - |
|
280 | - if ($data->filter('paging')->exists('next')) { |
|
281 | - $apiUrl = $data->filter('paging')->get('next'); |
|
282 | - |
|
283 | - $pagedList = true; |
|
284 | - } else { |
|
285 | - $pagedList = false; |
|
286 | - } |
|
287 | - } while ($pagedList); |
|
288 | - |
|
289 | - return $contacts; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Parse the user contact. |
|
294 | - * |
|
295 | - * @param array $item |
|
296 | - * |
|
297 | - * @return \Hybridauth\User\Contact |
|
298 | - */ |
|
299 | - protected function fetchUserContact($item) |
|
300 | - { |
|
301 | - $userContact = new User\Contact(); |
|
302 | - |
|
303 | - $item = new Data\Collection($item); |
|
304 | - |
|
305 | - $userContact->identifier = $item->get('id'); |
|
306 | - $userContact->displayName = $item->get('name'); |
|
307 | - |
|
308 | - $userContact->profileURL = $item->exists('link') |
|
309 | - ?: $this->getProfileUrl($userContact->identifier); |
|
310 | - |
|
311 | - $userContact->photoURL = $this->apiBaseUrl . $userContact->identifier . '/picture?width=150&height=150'; |
|
312 | - |
|
313 | - return $userContact; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * {@inheritdoc} |
|
318 | - */ |
|
319 | - public function setPageStatus($status, $pageId) |
|
320 | - { |
|
321 | - $status = is_string($status) ? ['message' => $status] : $status; |
|
322 | - |
|
323 | - // Post on user wall. |
|
324 | - if ($pageId === 'me') { |
|
325 | - return $this->setUserStatus($status); |
|
326 | - } |
|
327 | - |
|
328 | - // Retrieve writable user pages and filter by given one. |
|
329 | - $pages = $this->getUserPages(true); |
|
330 | - $pages = array_filter($pages, function ($page) use ($pageId) { |
|
331 | - return $page->id == $pageId; |
|
332 | - }); |
|
333 | - |
|
334 | - if (!$pages) { |
|
335 | - throw new InvalidArgumentException('Could not find a page with given id.'); |
|
336 | - } |
|
337 | - |
|
338 | - $page = reset($pages); |
|
339 | - |
|
340 | - // Use page access token instead of user access token. |
|
341 | - $headers = [ |
|
342 | - 'Authorization' => 'Bearer ' . $page->access_token, |
|
343 | - ]; |
|
344 | - |
|
345 | - // Refresh proof for API call. |
|
346 | - $parameters = $status + [ |
|
347 | - 'appsecret_proof' => hash_hmac('sha256', $page->access_token, $this->clientSecret), |
|
348 | - ]; |
|
349 | - |
|
350 | - $response = $this->apiRequest("{$pageId}/feed", 'POST', $parameters, $headers); |
|
351 | - |
|
352 | - return $response; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * {@inheritdoc} |
|
357 | - */ |
|
358 | - public function getUserPages($writable = false) |
|
359 | - { |
|
360 | - $pages = $this->apiRequest('me/accounts'); |
|
361 | - |
|
362 | - if (!$writable) { |
|
363 | - return $pages->data; |
|
364 | - } |
|
365 | - |
|
366 | - // Filter user pages by CREATE_CONTENT permission. |
|
367 | - return array_filter($pages->data, function ($page) { |
|
368 | - return in_array('CREATE_CONTENT', $page->tasks); |
|
369 | - }); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * {@inheritdoc} |
|
374 | - */ |
|
375 | - public function getUserActivity($stream = 'me') |
|
376 | - { |
|
377 | - $apiUrl = $stream == 'me' ? 'me/feed' : 'me/home'; |
|
378 | - |
|
379 | - $response = $this->apiRequest($apiUrl); |
|
380 | - |
|
381 | - $data = new Data\Collection($response); |
|
382 | - |
|
383 | - if (!$data->exists('data')) { |
|
384 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
385 | - } |
|
386 | - |
|
387 | - $activities = []; |
|
388 | - |
|
389 | - foreach ($data->filter('data')->toArray() as $item) { |
|
390 | - $activities[] = $this->fetchUserActivity($item); |
|
391 | - } |
|
392 | - |
|
393 | - return $activities; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * @param $item |
|
398 | - * |
|
399 | - * @return User\Activity |
|
400 | - */ |
|
401 | - protected function fetchUserActivity($item) |
|
402 | - { |
|
403 | - $userActivity = new User\Activity(); |
|
404 | - |
|
405 | - $item = new Data\Collection($item); |
|
406 | - |
|
407 | - $userActivity->id = $item->get('id'); |
|
408 | - $userActivity->date = $item->get('created_time'); |
|
409 | - |
|
410 | - if ('video' == $item->get('type') || 'link' == $item->get('type')) { |
|
411 | - $userActivity->text = $item->get('link'); |
|
412 | - } |
|
413 | - |
|
414 | - if (empty($userActivity->text) && $item->exists('story')) { |
|
415 | - $userActivity->text = $item->get('link'); |
|
416 | - } |
|
417 | - |
|
418 | - if (empty($userActivity->text) && $item->exists('message')) { |
|
419 | - $userActivity->text = $item->get('message'); |
|
420 | - } |
|
421 | - |
|
422 | - if (!empty($userActivity->text) && $item->exists('from')) { |
|
423 | - $userActivity->user->identifier = $item->filter('from')->get('id'); |
|
424 | - $userActivity->user->displayName = $item->filter('from')->get('name'); |
|
425 | - |
|
426 | - $userActivity->user->profileURL = $this->getProfileUrl($userActivity->user->identifier); |
|
427 | - |
|
428 | - $userActivity->user->photoURL = $this->apiBaseUrl . $userActivity->user->identifier; |
|
429 | - $userActivity->user->photoURL .= '/picture?width=150&height=150'; |
|
430 | - } |
|
431 | - |
|
432 | - return $userActivity; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Get profile URL. |
|
437 | - * |
|
438 | - * @param int $identity User ID. |
|
439 | - * @return string|null NULL when identity is not provided. |
|
440 | - */ |
|
441 | - protected function getProfileUrl($identity) |
|
442 | - { |
|
443 | - if (!is_numeric($identity)) { |
|
444 | - return null; |
|
445 | - } |
|
446 | - |
|
447 | - return sprintf($this->profileUrlTemplate, $identity); |
|
448 | - } |
|
157 | + if (strpos($this->scope, 'user_link') !== false) { |
|
158 | + $fields[] = 'link'; |
|
159 | + } |
|
160 | + |
|
161 | + if (strpos($this->scope, 'user_gender') !== false) { |
|
162 | + $fields[] = 'gender'; |
|
163 | + } |
|
164 | + |
|
165 | + // Note that en_US is needed for gender fields to match convention. |
|
166 | + $locale = $this->config->get('locale') ?: 'en_US'; |
|
167 | + $response = $this->apiRequest('me', 'GET', [ |
|
168 | + 'fields' => implode(',', $fields), |
|
169 | + 'locale' => $locale, |
|
170 | + ]); |
|
171 | + |
|
172 | + $data = new Data\Collection($response); |
|
173 | + |
|
174 | + if (!$data->exists('id')) { |
|
175 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
176 | + } |
|
177 | + |
|
178 | + $userProfile = new User\Profile(); |
|
179 | + |
|
180 | + $userProfile->identifier = $data->get('id'); |
|
181 | + $userProfile->displayName = $data->get('name'); |
|
182 | + $userProfile->firstName = $data->get('first_name'); |
|
183 | + $userProfile->lastName = $data->get('last_name'); |
|
184 | + $userProfile->profileURL = $data->get('link'); |
|
185 | + $userProfile->webSiteURL = $data->get('website'); |
|
186 | + $userProfile->gender = $data->get('gender'); |
|
187 | + $userProfile->language = $data->get('locale'); |
|
188 | + $userProfile->description = $data->get('about'); |
|
189 | + $userProfile->email = $data->get('email'); |
|
190 | + |
|
191 | + // Fallback for profile URL in case Facebook does not provide "pretty" link with username (if user set it). |
|
192 | + if (empty($userProfile->profileURL)) { |
|
193 | + $userProfile->profileURL = $this->getProfileUrl($userProfile->identifier); |
|
194 | + } |
|
195 | + |
|
196 | + $userProfile->region = $data->filter('hometown')->get('name'); |
|
197 | + |
|
198 | + $photoSize = $this->config->get('photo_size') ?: '150'; |
|
199 | + |
|
200 | + $userProfile->photoURL = $this->apiBaseUrl . $userProfile->identifier; |
|
201 | + $userProfile->photoURL .= '/picture?width=' . $photoSize . '&height=' . $photoSize; |
|
202 | + |
|
203 | + $userProfile->emailVerified = $userProfile->email; |
|
204 | + |
|
205 | + $userProfile = $this->fetchUserRegion($userProfile); |
|
206 | + |
|
207 | + $userProfile = $this->fetchBirthday($userProfile, $data->get('birthday')); |
|
208 | + |
|
209 | + return $userProfile; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Retrieve the user region. |
|
214 | + * |
|
215 | + * @param User\Profile $userProfile |
|
216 | + * |
|
217 | + * @return \Hybridauth\User\Profile |
|
218 | + */ |
|
219 | + protected function fetchUserRegion(User\Profile $userProfile) |
|
220 | + { |
|
221 | + if (!empty($userProfile->region)) { |
|
222 | + $regionArr = explode(',', $userProfile->region); |
|
223 | + |
|
224 | + if (count($regionArr) > 1) { |
|
225 | + $userProfile->city = trim($regionArr[0]); |
|
226 | + $userProfile->country = trim($regionArr[1]); |
|
227 | + } |
|
228 | + } |
|
229 | + |
|
230 | + return $userProfile; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Retrieve the user birthday. |
|
235 | + * |
|
236 | + * @param User\Profile $userProfile |
|
237 | + * @param string $birthday |
|
238 | + * |
|
239 | + * @return \Hybridauth\User\Profile |
|
240 | + */ |
|
241 | + protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
242 | + { |
|
243 | + $result = (new Data\Parser())->parseBirthday($birthday, '/'); |
|
244 | + |
|
245 | + $userProfile->birthYear = (int)$result[0]; |
|
246 | + $userProfile->birthMonth = (int)$result[1]; |
|
247 | + $userProfile->birthDay = (int)$result[2]; |
|
248 | + |
|
249 | + return $userProfile; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * /v2.0/me/friends only returns the user's friends who also use the app. |
|
254 | + * In the cases where you want to let people tag their friends in stories published by your app, |
|
255 | + * you can use the Taggable Friends API. |
|
256 | + * |
|
257 | + * https://developers.facebook.com/docs/apps/faq#unable_full_friend_list |
|
258 | + */ |
|
259 | + public function getUserContacts() |
|
260 | + { |
|
261 | + $contacts = []; |
|
262 | + |
|
263 | + $apiUrl = 'me/friends?fields=link,name'; |
|
264 | + |
|
265 | + do { |
|
266 | + $response = $this->apiRequest($apiUrl); |
|
267 | + |
|
268 | + $data = new Data\Collection($response); |
|
269 | + |
|
270 | + if (!$data->exists('data')) { |
|
271 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
272 | + } |
|
273 | + |
|
274 | + if (!$data->filter('data')->isEmpty()) { |
|
275 | + foreach ($data->filter('data')->toArray() as $item) { |
|
276 | + $contacts[] = $this->fetchUserContact($item); |
|
277 | + } |
|
278 | + } |
|
279 | + |
|
280 | + if ($data->filter('paging')->exists('next')) { |
|
281 | + $apiUrl = $data->filter('paging')->get('next'); |
|
282 | + |
|
283 | + $pagedList = true; |
|
284 | + } else { |
|
285 | + $pagedList = false; |
|
286 | + } |
|
287 | + } while ($pagedList); |
|
288 | + |
|
289 | + return $contacts; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Parse the user contact. |
|
294 | + * |
|
295 | + * @param array $item |
|
296 | + * |
|
297 | + * @return \Hybridauth\User\Contact |
|
298 | + */ |
|
299 | + protected function fetchUserContact($item) |
|
300 | + { |
|
301 | + $userContact = new User\Contact(); |
|
302 | + |
|
303 | + $item = new Data\Collection($item); |
|
304 | + |
|
305 | + $userContact->identifier = $item->get('id'); |
|
306 | + $userContact->displayName = $item->get('name'); |
|
307 | + |
|
308 | + $userContact->profileURL = $item->exists('link') |
|
309 | + ?: $this->getProfileUrl($userContact->identifier); |
|
310 | + |
|
311 | + $userContact->photoURL = $this->apiBaseUrl . $userContact->identifier . '/picture?width=150&height=150'; |
|
312 | + |
|
313 | + return $userContact; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * {@inheritdoc} |
|
318 | + */ |
|
319 | + public function setPageStatus($status, $pageId) |
|
320 | + { |
|
321 | + $status = is_string($status) ? ['message' => $status] : $status; |
|
322 | + |
|
323 | + // Post on user wall. |
|
324 | + if ($pageId === 'me') { |
|
325 | + return $this->setUserStatus($status); |
|
326 | + } |
|
327 | + |
|
328 | + // Retrieve writable user pages and filter by given one. |
|
329 | + $pages = $this->getUserPages(true); |
|
330 | + $pages = array_filter($pages, function ($page) use ($pageId) { |
|
331 | + return $page->id == $pageId; |
|
332 | + }); |
|
333 | + |
|
334 | + if (!$pages) { |
|
335 | + throw new InvalidArgumentException('Could not find a page with given id.'); |
|
336 | + } |
|
337 | + |
|
338 | + $page = reset($pages); |
|
339 | + |
|
340 | + // Use page access token instead of user access token. |
|
341 | + $headers = [ |
|
342 | + 'Authorization' => 'Bearer ' . $page->access_token, |
|
343 | + ]; |
|
344 | + |
|
345 | + // Refresh proof for API call. |
|
346 | + $parameters = $status + [ |
|
347 | + 'appsecret_proof' => hash_hmac('sha256', $page->access_token, $this->clientSecret), |
|
348 | + ]; |
|
349 | + |
|
350 | + $response = $this->apiRequest("{$pageId}/feed", 'POST', $parameters, $headers); |
|
351 | + |
|
352 | + return $response; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * {@inheritdoc} |
|
357 | + */ |
|
358 | + public function getUserPages($writable = false) |
|
359 | + { |
|
360 | + $pages = $this->apiRequest('me/accounts'); |
|
361 | + |
|
362 | + if (!$writable) { |
|
363 | + return $pages->data; |
|
364 | + } |
|
365 | + |
|
366 | + // Filter user pages by CREATE_CONTENT permission. |
|
367 | + return array_filter($pages->data, function ($page) { |
|
368 | + return in_array('CREATE_CONTENT', $page->tasks); |
|
369 | + }); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * {@inheritdoc} |
|
374 | + */ |
|
375 | + public function getUserActivity($stream = 'me') |
|
376 | + { |
|
377 | + $apiUrl = $stream == 'me' ? 'me/feed' : 'me/home'; |
|
378 | + |
|
379 | + $response = $this->apiRequest($apiUrl); |
|
380 | + |
|
381 | + $data = new Data\Collection($response); |
|
382 | + |
|
383 | + if (!$data->exists('data')) { |
|
384 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
385 | + } |
|
386 | + |
|
387 | + $activities = []; |
|
388 | + |
|
389 | + foreach ($data->filter('data')->toArray() as $item) { |
|
390 | + $activities[] = $this->fetchUserActivity($item); |
|
391 | + } |
|
392 | + |
|
393 | + return $activities; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * @param $item |
|
398 | + * |
|
399 | + * @return User\Activity |
|
400 | + */ |
|
401 | + protected function fetchUserActivity($item) |
|
402 | + { |
|
403 | + $userActivity = new User\Activity(); |
|
404 | + |
|
405 | + $item = new Data\Collection($item); |
|
406 | + |
|
407 | + $userActivity->id = $item->get('id'); |
|
408 | + $userActivity->date = $item->get('created_time'); |
|
409 | + |
|
410 | + if ('video' == $item->get('type') || 'link' == $item->get('type')) { |
|
411 | + $userActivity->text = $item->get('link'); |
|
412 | + } |
|
413 | + |
|
414 | + if (empty($userActivity->text) && $item->exists('story')) { |
|
415 | + $userActivity->text = $item->get('link'); |
|
416 | + } |
|
417 | + |
|
418 | + if (empty($userActivity->text) && $item->exists('message')) { |
|
419 | + $userActivity->text = $item->get('message'); |
|
420 | + } |
|
421 | + |
|
422 | + if (!empty($userActivity->text) && $item->exists('from')) { |
|
423 | + $userActivity->user->identifier = $item->filter('from')->get('id'); |
|
424 | + $userActivity->user->displayName = $item->filter('from')->get('name'); |
|
425 | + |
|
426 | + $userActivity->user->profileURL = $this->getProfileUrl($userActivity->user->identifier); |
|
427 | + |
|
428 | + $userActivity->user->photoURL = $this->apiBaseUrl . $userActivity->user->identifier; |
|
429 | + $userActivity->user->photoURL .= '/picture?width=150&height=150'; |
|
430 | + } |
|
431 | + |
|
432 | + return $userActivity; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Get profile URL. |
|
437 | + * |
|
438 | + * @param int $identity User ID. |
|
439 | + * @return string|null NULL when identity is not provided. |
|
440 | + */ |
|
441 | + protected function getProfileUrl($identity) |
|
442 | + { |
|
443 | + if (!is_numeric($identity)) { |
|
444 | + return null; |
|
445 | + } |
|
446 | + |
|
447 | + return sprintf($this->profileUrlTemplate, $identity); |
|
448 | + } |
|
449 | 449 | } |
@@ -17,119 +17,119 @@ |
||
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 | - $this->tokenRefreshParameters += [ |
|
86 | - 'appid' => $this->clientId |
|
87 | - ]; |
|
88 | - |
|
89 | - $this->apiRequestParameters = [ |
|
90 | - 'appid' => $this->clientId, |
|
91 | - 'secret' => $this->clientSecret |
|
92 | - ]; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * {@inheritdoc} |
|
97 | - */ |
|
98 | - protected function validateAccessTokenExchange($response) |
|
99 | - { |
|
100 | - $collection = parent::validateAccessTokenExchange($response); |
|
101 | - |
|
102 | - $this->storeData('openid', $collection->get('openid')); |
|
103 | - $this->storeData('access_token', $collection->get('access_token')); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * {@inheritdoc} |
|
108 | - */ |
|
109 | - public function getUserProfile() |
|
110 | - { |
|
111 | - $openid = $this->getStoredData('openid'); |
|
112 | - $access_token = $this->getStoredData('access_token'); |
|
113 | - |
|
114 | - $response = $this->apiRequest('userinfo', 'GET', ['openid' => $openid, 'access_token' => $access_token]); |
|
115 | - |
|
116 | - $data = new Data\Collection($response); |
|
117 | - |
|
118 | - if (!$data->exists('openid')) { |
|
119 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
120 | - } |
|
121 | - |
|
122 | - $userProfile = new User\Profile(); |
|
123 | - |
|
124 | - $userProfile->identifier = $data->get('openid'); |
|
125 | - $userProfile->displayName = $data->get('nickname'); |
|
126 | - $userProfile->photoURL = $data->get('headimgurl'); |
|
127 | - $userProfile->city = $data->get('city'); |
|
128 | - $userProfile->region = $data->get('province'); |
|
129 | - $userProfile->country = $data->get('country'); |
|
130 | - $genders = ['', 'male', 'female']; |
|
131 | - $userProfile->gender = $genders[(int)$data->get('sex')]; |
|
132 | - |
|
133 | - return $userProfile; |
|
134 | - } |
|
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 | + $this->tokenRefreshParameters += [ |
|
86 | + 'appid' => $this->clientId |
|
87 | + ]; |
|
88 | + |
|
89 | + $this->apiRequestParameters = [ |
|
90 | + 'appid' => $this->clientId, |
|
91 | + 'secret' => $this->clientSecret |
|
92 | + ]; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * {@inheritdoc} |
|
97 | + */ |
|
98 | + protected function validateAccessTokenExchange($response) |
|
99 | + { |
|
100 | + $collection = parent::validateAccessTokenExchange($response); |
|
101 | + |
|
102 | + $this->storeData('openid', $collection->get('openid')); |
|
103 | + $this->storeData('access_token', $collection->get('access_token')); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * {@inheritdoc} |
|
108 | + */ |
|
109 | + public function getUserProfile() |
|
110 | + { |
|
111 | + $openid = $this->getStoredData('openid'); |
|
112 | + $access_token = $this->getStoredData('access_token'); |
|
113 | + |
|
114 | + $response = $this->apiRequest('userinfo', 'GET', ['openid' => $openid, 'access_token' => $access_token]); |
|
115 | + |
|
116 | + $data = new Data\Collection($response); |
|
117 | + |
|
118 | + if (!$data->exists('openid')) { |
|
119 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
120 | + } |
|
121 | + |
|
122 | + $userProfile = new User\Profile(); |
|
123 | + |
|
124 | + $userProfile->identifier = $data->get('openid'); |
|
125 | + $userProfile->displayName = $data->get('nickname'); |
|
126 | + $userProfile->photoURL = $data->get('headimgurl'); |
|
127 | + $userProfile->city = $data->get('city'); |
|
128 | + $userProfile->region = $data->get('province'); |
|
129 | + $userProfile->country = $data->get('country'); |
|
130 | + $genders = ['', 'male', 'female']; |
|
131 | + $userProfile->gender = $genders[(int)$data->get('sex')]; |
|
132 | + |
|
133 | + return $userProfile; |
|
134 | + } |
|
135 | 135 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | |
116 | 116 | $data = new Data\Collection($response); |
117 | 117 | |
118 | - if (!$data->exists('openid')) { |
|
118 | + if ( ! $data->exists('openid')) { |
|
119 | 119 | throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
120 | 120 | } |
121 | 121 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | $userProfile->region = $data->get('province'); |
129 | 129 | $userProfile->country = $data->get('country'); |
130 | 130 | $genders = ['', 'male', 'female']; |
131 | - $userProfile->gender = $genders[(int)$data->get('sex')]; |
|
131 | + $userProfile->gender = $genders[(int) $data->get('sex')]; |
|
132 | 132 | |
133 | 133 | return $userProfile; |
134 | 134 | } |
@@ -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 | } |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | $this->botSecret = $this->config->filter('keys')->get('secret'); |
69 | 69 | $this->callbackUrl = $this->config->get('callback'); |
70 | 70 | |
71 | - if (!$this->botId || !$this->botSecret) { |
|
71 | + if ( ! $this->botId || ! $this->botSecret) { |
|
72 | 72 | throw new InvalidApplicationCredentialsException( |
73 | - 'Your application id is required in order to connect to ' . $this->providerId |
|
73 | + 'Your application id is required in order to connect to '.$this->providerId |
|
74 | 74 | ); |
75 | 75 | } |
76 | 76 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | public function authenticate() |
89 | 89 | { |
90 | 90 | $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
91 | - if (!filter_input(INPUT_GET, 'hash')) { |
|
91 | + if ( ! filter_input(INPUT_GET, 'hash')) { |
|
92 | 92 | $this->authenticateBegin(); |
93 | 93 | } else { |
94 | 94 | $this->authenticateCheckError(); |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | public function isConnected() |
104 | 104 | { |
105 | 105 | $authData = $this->getStoredData('auth_data'); |
106 | - return !empty($authData); |
|
106 | + return ! empty($authData); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | /** |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | { |
114 | 114 | $data = new Collection($this->getStoredData('auth_data')); |
115 | 115 | |
116 | - if (!$data->exists('id')) { |
|
116 | + if ( ! $data->exists('id')) { |
|
117 | 117 | throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
118 | 118 | } |
119 | 119 | |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | $userProfile->displayName = $data->get('username'); |
126 | 126 | $userProfile->photoURL = $data->get('photo_url'); |
127 | 127 | $username = $data->get('username'); |
128 | - if (!empty($username)) { |
|
128 | + if ( ! empty($username)) { |
|
129 | 129 | // Only some accounts have usernames. |
130 | 130 | $userProfile->profileURL = "https://t.me/{$username}"; |
131 | 131 | } |
@@ -146,8 +146,8 @@ discard block |
||
146 | 146 | $data_check_arr = []; |
147 | 147 | |
148 | 148 | foreach ($auth_data as $key => $value) { |
149 | - if (!empty($value)) { |
|
150 | - $data_check_arr[] = $key . '=' . $value; |
|
149 | + if ( ! empty($value)) { |
|
150 | + $data_check_arr[] = $key.'='.$value; |
|
151 | 151 | } |
152 | 152 | } |
153 | 153 | sort($data_check_arr); |
@@ -23,261 +23,261 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class OpenID extends AbstractAdapter implements AdapterInterface |
25 | 25 | { |
26 | - /** |
|
27 | - * LightOpenID instance |
|
28 | - * |
|
29 | - * @var object |
|
30 | - */ |
|
31 | - protected $openIdClient = null; |
|
32 | - |
|
33 | - /** |
|
34 | - * Openid provider identifier |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $openidIdentifier = ''; |
|
39 | - |
|
40 | - /** |
|
41 | - * IPD API Documentation |
|
42 | - * |
|
43 | - * OPTIONAL. |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - protected $apiDocumentation = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - protected function configure() |
|
53 | - { |
|
54 | - if ($this->config->exists('openid_identifier')) { |
|
55 | - $this->openidIdentifier = $this->config->get('openid_identifier'); |
|
56 | - } |
|
57 | - |
|
58 | - if (empty($this->openidIdentifier)) { |
|
59 | - throw new InvalidOpenidIdentifierException('OpenID adapter requires an openid_identifier.', 4); |
|
60 | - } |
|
61 | - |
|
62 | - $this->setCallback($this->config->get('callback')); |
|
63 | - $this->setApiEndpoints($this->config->get('endpoints')); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function initialize() |
|
70 | - { |
|
71 | - $hostPort = parse_url($this->callback, PHP_URL_PORT); |
|
72 | - $hostUrl = parse_url($this->callback, PHP_URL_HOST); |
|
73 | - |
|
74 | - if ($hostPort) { |
|
75 | - $hostUrl .= ':' . $hostPort; |
|
76 | - } |
|
77 | - |
|
78 | - // @fixme: add proxy |
|
79 | - $this->openIdClient = new LightOpenID($hostUrl, null); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * {@inheritdoc} |
|
84 | - */ |
|
85 | - public function authenticate() |
|
86 | - { |
|
87 | - $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
88 | - |
|
89 | - if ($this->isConnected()) { |
|
90 | - return true; |
|
91 | - } |
|
92 | - |
|
93 | - if (empty($_REQUEST['openid_mode'])) { |
|
94 | - $this->authenticateBegin(); |
|
95 | - } else { |
|
96 | - return $this->authenticateFinish(); |
|
97 | - } |
|
98 | - |
|
99 | - return null; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritdoc} |
|
104 | - */ |
|
105 | - public function isConnected() |
|
106 | - { |
|
107 | - return (bool)$this->storage->get($this->providerId . '.user'); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * {@inheritdoc} |
|
112 | - */ |
|
113 | - public function disconnect() |
|
114 | - { |
|
115 | - $this->storage->delete($this->providerId . '.user'); |
|
116 | - |
|
117 | - return true; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Initiate the authorization protocol |
|
122 | - * |
|
123 | - * Include and instantiate LightOpenID |
|
124 | - */ |
|
125 | - protected function authenticateBegin() |
|
126 | - { |
|
127 | - $this->openIdClient->identity = $this->openidIdentifier; |
|
128 | - $this->openIdClient->returnUrl = $this->callback; |
|
129 | - $this->openIdClient->required = [ |
|
130 | - 'namePerson/first', |
|
131 | - 'namePerson/last', |
|
132 | - 'namePerson/friendly', |
|
133 | - 'namePerson', |
|
134 | - 'contact/email', |
|
135 | - 'birthDate', |
|
136 | - 'birthDate/birthDay', |
|
137 | - 'birthDate/birthMonth', |
|
138 | - 'birthDate/birthYear', |
|
139 | - 'person/gender', |
|
140 | - 'pref/language', |
|
141 | - 'contact/postalCode/home', |
|
142 | - 'contact/city/home', |
|
143 | - 'contact/country/home', |
|
144 | - |
|
145 | - 'media/image/default', |
|
146 | - ]; |
|
147 | - |
|
148 | - $authUrl = $this->openIdClient->authUrl(); |
|
149 | - |
|
150 | - $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this)), [$authUrl]); |
|
151 | - |
|
152 | - HttpClient\Util::redirect($authUrl); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Finalize the authorization process. |
|
157 | - * |
|
158 | - * @throws AuthorizationDeniedException |
|
159 | - * @throws UnexpectedApiResponseException |
|
160 | - */ |
|
161 | - protected function authenticateFinish() |
|
162 | - { |
|
163 | - $this->logger->debug( |
|
164 | - sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
165 | - [HttpClient\Util::getCurrentUrl(true)] |
|
166 | - ); |
|
167 | - |
|
168 | - if ($this->openIdClient->mode == 'cancel') { |
|
169 | - throw new AuthorizationDeniedException('User has cancelled the authentication.'); |
|
170 | - } |
|
171 | - |
|
172 | - if (!$this->openIdClient->validate()) { |
|
173 | - throw new UnexpectedApiResponseException('Invalid response received.'); |
|
174 | - } |
|
175 | - |
|
176 | - $openidAttributes = $this->openIdClient->getAttributes(); |
|
177 | - |
|
178 | - if (!$this->openIdClient->identity) { |
|
179 | - throw new UnexpectedApiResponseException('Provider returned an unexpected response.'); |
|
180 | - } |
|
181 | - |
|
182 | - $userProfile = $this->fetchUserProfile($openidAttributes); |
|
183 | - |
|
184 | - /* with openid providers we only get user profiles once, so we store it */ |
|
185 | - $this->storage->set($this->providerId . '.user', $userProfile); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Fetch user profile from received openid attributes |
|
190 | - * |
|
191 | - * @param array $openidAttributes |
|
192 | - * |
|
193 | - * @return User\Profile |
|
194 | - */ |
|
195 | - protected function fetchUserProfile($openidAttributes) |
|
196 | - { |
|
197 | - $data = new Data\Collection($openidAttributes); |
|
198 | - |
|
199 | - $userProfile = new User\Profile(); |
|
200 | - |
|
201 | - $userProfile->identifier = $this->openIdClient->identity; |
|
202 | - |
|
203 | - $userProfile->firstName = $data->get('namePerson/first'); |
|
204 | - $userProfile->lastName = $data->get('namePerson/last'); |
|
205 | - $userProfile->email = $data->get('contact/email'); |
|
206 | - $userProfile->language = $data->get('pref/language'); |
|
207 | - $userProfile->country = $data->get('contact/country/home'); |
|
208 | - $userProfile->zip = $data->get('contact/postalCode/home'); |
|
209 | - $userProfile->gender = $data->get('person/gender'); |
|
210 | - $userProfile->photoURL = $data->get('media/image/default'); |
|
211 | - $userProfile->birthDay = $data->get('birthDate/birthDay'); |
|
212 | - $userProfile->birthMonth = $data->get('birthDate/birthMonth'); |
|
213 | - $userProfile->birthYear = $data->get('birthDate/birthDate'); |
|
214 | - |
|
215 | - $userProfile = $this->fetchUserGender($userProfile, $data->get('person/gender')); |
|
216 | - |
|
217 | - $userProfile = $this->fetchUserDisplayName($userProfile, $data); |
|
218 | - |
|
219 | - return $userProfile; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Extract users display names |
|
224 | - * |
|
225 | - * @param User\Profile $userProfile |
|
226 | - * @param Data\Collection $data |
|
227 | - * |
|
228 | - * @return User\Profile |
|
229 | - */ |
|
230 | - protected function fetchUserDisplayName(User\Profile $userProfile, Data\Collection $data) |
|
231 | - { |
|
232 | - $userProfile->displayName = $data->get('namePerson'); |
|
233 | - |
|
234 | - $userProfile->displayName = $userProfile->displayName |
|
235 | - ? $userProfile->displayName |
|
236 | - : $data->get('namePerson/friendly'); |
|
237 | - |
|
238 | - $userProfile->displayName = $userProfile->displayName |
|
239 | - ? $userProfile->displayName |
|
240 | - : trim($userProfile->firstName . ' ' . $userProfile->lastName); |
|
241 | - |
|
242 | - return $userProfile; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Extract users gender |
|
247 | - * |
|
248 | - * @param User\Profile $userProfile |
|
249 | - * @param string $gender |
|
250 | - * |
|
251 | - * @return User\Profile |
|
252 | - */ |
|
253 | - protected function fetchUserGender(User\Profile $userProfile, $gender) |
|
254 | - { |
|
255 | - $gender = strtolower($gender); |
|
256 | - |
|
257 | - if ('f' == $gender) { |
|
258 | - $gender = 'female'; |
|
259 | - } |
|
260 | - |
|
261 | - if ('m' == $gender) { |
|
262 | - $gender = 'male'; |
|
263 | - } |
|
264 | - |
|
265 | - $userProfile->gender = $gender; |
|
266 | - |
|
267 | - return $userProfile; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * OpenID only provide the user profile one. This method will attempt to retrieve the profile from storage. |
|
272 | - */ |
|
273 | - public function getUserProfile() |
|
274 | - { |
|
275 | - $userProfile = $this->storage->get($this->providerId . '.user'); |
|
276 | - |
|
277 | - if (!is_object($userProfile)) { |
|
278 | - throw new UnexpectedApiResponseException('Provider returned an unexpected response.'); |
|
279 | - } |
|
280 | - |
|
281 | - return $userProfile; |
|
282 | - } |
|
26 | + /** |
|
27 | + * LightOpenID instance |
|
28 | + * |
|
29 | + * @var object |
|
30 | + */ |
|
31 | + protected $openIdClient = null; |
|
32 | + |
|
33 | + /** |
|
34 | + * Openid provider identifier |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $openidIdentifier = ''; |
|
39 | + |
|
40 | + /** |
|
41 | + * IPD API Documentation |
|
42 | + * |
|
43 | + * OPTIONAL. |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + protected $apiDocumentation = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + protected function configure() |
|
53 | + { |
|
54 | + if ($this->config->exists('openid_identifier')) { |
|
55 | + $this->openidIdentifier = $this->config->get('openid_identifier'); |
|
56 | + } |
|
57 | + |
|
58 | + if (empty($this->openidIdentifier)) { |
|
59 | + throw new InvalidOpenidIdentifierException('OpenID adapter requires an openid_identifier.', 4); |
|
60 | + } |
|
61 | + |
|
62 | + $this->setCallback($this->config->get('callback')); |
|
63 | + $this->setApiEndpoints($this->config->get('endpoints')); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function initialize() |
|
70 | + { |
|
71 | + $hostPort = parse_url($this->callback, PHP_URL_PORT); |
|
72 | + $hostUrl = parse_url($this->callback, PHP_URL_HOST); |
|
73 | + |
|
74 | + if ($hostPort) { |
|
75 | + $hostUrl .= ':' . $hostPort; |
|
76 | + } |
|
77 | + |
|
78 | + // @fixme: add proxy |
|
79 | + $this->openIdClient = new LightOpenID($hostUrl, null); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * {@inheritdoc} |
|
84 | + */ |
|
85 | + public function authenticate() |
|
86 | + { |
|
87 | + $this->logger->info(sprintf('%s::authenticate()', get_class($this))); |
|
88 | + |
|
89 | + if ($this->isConnected()) { |
|
90 | + return true; |
|
91 | + } |
|
92 | + |
|
93 | + if (empty($_REQUEST['openid_mode'])) { |
|
94 | + $this->authenticateBegin(); |
|
95 | + } else { |
|
96 | + return $this->authenticateFinish(); |
|
97 | + } |
|
98 | + |
|
99 | + return null; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritdoc} |
|
104 | + */ |
|
105 | + public function isConnected() |
|
106 | + { |
|
107 | + return (bool)$this->storage->get($this->providerId . '.user'); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * {@inheritdoc} |
|
112 | + */ |
|
113 | + public function disconnect() |
|
114 | + { |
|
115 | + $this->storage->delete($this->providerId . '.user'); |
|
116 | + |
|
117 | + return true; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Initiate the authorization protocol |
|
122 | + * |
|
123 | + * Include and instantiate LightOpenID |
|
124 | + */ |
|
125 | + protected function authenticateBegin() |
|
126 | + { |
|
127 | + $this->openIdClient->identity = $this->openidIdentifier; |
|
128 | + $this->openIdClient->returnUrl = $this->callback; |
|
129 | + $this->openIdClient->required = [ |
|
130 | + 'namePerson/first', |
|
131 | + 'namePerson/last', |
|
132 | + 'namePerson/friendly', |
|
133 | + 'namePerson', |
|
134 | + 'contact/email', |
|
135 | + 'birthDate', |
|
136 | + 'birthDate/birthDay', |
|
137 | + 'birthDate/birthMonth', |
|
138 | + 'birthDate/birthYear', |
|
139 | + 'person/gender', |
|
140 | + 'pref/language', |
|
141 | + 'contact/postalCode/home', |
|
142 | + 'contact/city/home', |
|
143 | + 'contact/country/home', |
|
144 | + |
|
145 | + 'media/image/default', |
|
146 | + ]; |
|
147 | + |
|
148 | + $authUrl = $this->openIdClient->authUrl(); |
|
149 | + |
|
150 | + $this->logger->debug(sprintf('%s::authenticateBegin(), redirecting user to:', get_class($this)), [$authUrl]); |
|
151 | + |
|
152 | + HttpClient\Util::redirect($authUrl); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Finalize the authorization process. |
|
157 | + * |
|
158 | + * @throws AuthorizationDeniedException |
|
159 | + * @throws UnexpectedApiResponseException |
|
160 | + */ |
|
161 | + protected function authenticateFinish() |
|
162 | + { |
|
163 | + $this->logger->debug( |
|
164 | + sprintf('%s::authenticateFinish(), callback url:', get_class($this)), |
|
165 | + [HttpClient\Util::getCurrentUrl(true)] |
|
166 | + ); |
|
167 | + |
|
168 | + if ($this->openIdClient->mode == 'cancel') { |
|
169 | + throw new AuthorizationDeniedException('User has cancelled the authentication.'); |
|
170 | + } |
|
171 | + |
|
172 | + if (!$this->openIdClient->validate()) { |
|
173 | + throw new UnexpectedApiResponseException('Invalid response received.'); |
|
174 | + } |
|
175 | + |
|
176 | + $openidAttributes = $this->openIdClient->getAttributes(); |
|
177 | + |
|
178 | + if (!$this->openIdClient->identity) { |
|
179 | + throw new UnexpectedApiResponseException('Provider returned an unexpected response.'); |
|
180 | + } |
|
181 | + |
|
182 | + $userProfile = $this->fetchUserProfile($openidAttributes); |
|
183 | + |
|
184 | + /* with openid providers we only get user profiles once, so we store it */ |
|
185 | + $this->storage->set($this->providerId . '.user', $userProfile); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Fetch user profile from received openid attributes |
|
190 | + * |
|
191 | + * @param array $openidAttributes |
|
192 | + * |
|
193 | + * @return User\Profile |
|
194 | + */ |
|
195 | + protected function fetchUserProfile($openidAttributes) |
|
196 | + { |
|
197 | + $data = new Data\Collection($openidAttributes); |
|
198 | + |
|
199 | + $userProfile = new User\Profile(); |
|
200 | + |
|
201 | + $userProfile->identifier = $this->openIdClient->identity; |
|
202 | + |
|
203 | + $userProfile->firstName = $data->get('namePerson/first'); |
|
204 | + $userProfile->lastName = $data->get('namePerson/last'); |
|
205 | + $userProfile->email = $data->get('contact/email'); |
|
206 | + $userProfile->language = $data->get('pref/language'); |
|
207 | + $userProfile->country = $data->get('contact/country/home'); |
|
208 | + $userProfile->zip = $data->get('contact/postalCode/home'); |
|
209 | + $userProfile->gender = $data->get('person/gender'); |
|
210 | + $userProfile->photoURL = $data->get('media/image/default'); |
|
211 | + $userProfile->birthDay = $data->get('birthDate/birthDay'); |
|
212 | + $userProfile->birthMonth = $data->get('birthDate/birthMonth'); |
|
213 | + $userProfile->birthYear = $data->get('birthDate/birthDate'); |
|
214 | + |
|
215 | + $userProfile = $this->fetchUserGender($userProfile, $data->get('person/gender')); |
|
216 | + |
|
217 | + $userProfile = $this->fetchUserDisplayName($userProfile, $data); |
|
218 | + |
|
219 | + return $userProfile; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Extract users display names |
|
224 | + * |
|
225 | + * @param User\Profile $userProfile |
|
226 | + * @param Data\Collection $data |
|
227 | + * |
|
228 | + * @return User\Profile |
|
229 | + */ |
|
230 | + protected function fetchUserDisplayName(User\Profile $userProfile, Data\Collection $data) |
|
231 | + { |
|
232 | + $userProfile->displayName = $data->get('namePerson'); |
|
233 | + |
|
234 | + $userProfile->displayName = $userProfile->displayName |
|
235 | + ? $userProfile->displayName |
|
236 | + : $data->get('namePerson/friendly'); |
|
237 | + |
|
238 | + $userProfile->displayName = $userProfile->displayName |
|
239 | + ? $userProfile->displayName |
|
240 | + : trim($userProfile->firstName . ' ' . $userProfile->lastName); |
|
241 | + |
|
242 | + return $userProfile; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Extract users gender |
|
247 | + * |
|
248 | + * @param User\Profile $userProfile |
|
249 | + * @param string $gender |
|
250 | + * |
|
251 | + * @return User\Profile |
|
252 | + */ |
|
253 | + protected function fetchUserGender(User\Profile $userProfile, $gender) |
|
254 | + { |
|
255 | + $gender = strtolower($gender); |
|
256 | + |
|
257 | + if ('f' == $gender) { |
|
258 | + $gender = 'female'; |
|
259 | + } |
|
260 | + |
|
261 | + if ('m' == $gender) { |
|
262 | + $gender = 'male'; |
|
263 | + } |
|
264 | + |
|
265 | + $userProfile->gender = $gender; |
|
266 | + |
|
267 | + return $userProfile; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * OpenID only provide the user profile one. This method will attempt to retrieve the profile from storage. |
|
272 | + */ |
|
273 | + public function getUserProfile() |
|
274 | + { |
|
275 | + $userProfile = $this->storage->get($this->providerId . '.user'); |
|
276 | + |
|
277 | + if (!is_object($userProfile)) { |
|
278 | + throw new UnexpectedApiResponseException('Provider returned an unexpected response.'); |
|
279 | + } |
|
280 | + |
|
281 | + return $userProfile; |
|
282 | + } |
|
283 | 283 | } |
@@ -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 | } |
@@ -33,243 +33,243 @@ |
||
33 | 33 | */ |
34 | 34 | class Guzzle implements HttpClientInterface |
35 | 35 | { |
36 | - /** |
|
37 | - * Method request() arguments |
|
38 | - * |
|
39 | - * This is used for debugging. |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $requestArguments = []; |
|
44 | - |
|
45 | - /** |
|
46 | - * Default request headers |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $requestHeader = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * Raw response returned by server |
|
54 | - * |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - protected $responseBody = ''; |
|
58 | - |
|
59 | - /** |
|
60 | - * Headers returned in the response |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - protected $responseHeader = []; |
|
65 | - |
|
66 | - /** |
|
67 | - * Response HTTP status code |
|
68 | - * |
|
69 | - * @var int |
|
70 | - */ |
|
71 | - protected $responseHttpCode = 0; |
|
72 | - |
|
73 | - /** |
|
74 | - * Last curl error number |
|
75 | - * |
|
76 | - * @var mixed |
|
77 | - */ |
|
78 | - protected $responseClientError = null; |
|
79 | - |
|
80 | - /** |
|
81 | - * Information about the last transfer |
|
82 | - * |
|
83 | - * @var mixed |
|
84 | - */ |
|
85 | - protected $responseClientInfo = []; |
|
86 | - |
|
87 | - /** |
|
88 | - * Hybridauth logger instance |
|
89 | - * |
|
90 | - * @var object |
|
91 | - */ |
|
92 | - protected $logger = null; |
|
93 | - |
|
94 | - /** |
|
95 | - * GuzzleHttp client |
|
96 | - * |
|
97 | - * @var \GuzzleHttp\Client |
|
98 | - */ |
|
99 | - protected $client = null; |
|
100 | - |
|
101 | - /** |
|
102 | - * .. |
|
103 | - * @param null $client |
|
104 | - * @param array $config |
|
105 | - */ |
|
106 | - public function __construct($client = null, $config = []) |
|
107 | - { |
|
108 | - $this->client = $client ? $client : new Client($config); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * {@inheritdoc} |
|
113 | - */ |
|
114 | - public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
115 | - { |
|
116 | - $this->requestHeader = array_replace($this->requestHeader, (array)$headers); |
|
117 | - |
|
118 | - $this->requestArguments = [ |
|
119 | - 'uri' => $uri, |
|
120 | - 'method' => $method, |
|
121 | - 'parameters' => $parameters, |
|
122 | - 'headers' => $this->requestHeader, |
|
123 | - ]; |
|
124 | - |
|
125 | - $response = null; |
|
126 | - |
|
127 | - try { |
|
128 | - switch ($method) { |
|
129 | - case 'GET': |
|
130 | - case 'DELETE': |
|
131 | - $response = $this->client->request($method, $uri, [ |
|
132 | - 'query' => $parameters, |
|
133 | - 'headers' => $this->requestHeader, |
|
134 | - ]); |
|
135 | - break; |
|
136 | - case 'PUT': |
|
137 | - case 'POST': |
|
138 | - $body_type = $multipart ? 'multipart' : 'form_params'; |
|
139 | - |
|
140 | - if (isset($this->requestHeader['Content-Type']) |
|
141 | - && $this->requestHeader['Content-Type'] === 'application/json' |
|
142 | - ) { |
|
143 | - $body_type = 'json'; |
|
144 | - } |
|
145 | - |
|
146 | - $body_content = $parameters; |
|
147 | - if ($multipart) { |
|
148 | - $body_content = []; |
|
149 | - foreach ($parameters as $key => $val) { |
|
150 | - if ($val instanceof \CURLFile) { |
|
151 | - $val = fopen($val->getFilename(), 'r'); |
|
152 | - } |
|
153 | - |
|
154 | - $body_content[] = [ |
|
155 | - 'name' => $key, |
|
156 | - 'contents' => $val, |
|
157 | - ]; |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - $response = $this->client->request($method, $uri, [ |
|
162 | - $body_type => $body_content, |
|
163 | - 'headers' => $this->requestHeader, |
|
164 | - ]); |
|
165 | - break; |
|
166 | - } |
|
167 | - } catch (\Exception $e) { |
|
168 | - $response = $e->getResponse(); |
|
169 | - |
|
170 | - $this->responseClientError = $e->getMessage(); |
|
171 | - } |
|
172 | - |
|
173 | - if (!$this->responseClientError) { |
|
174 | - $this->responseBody = $response->getBody(); |
|
175 | - $this->responseHttpCode = $response->getStatusCode(); |
|
176 | - $this->responseHeader = $response->getHeaders(); |
|
177 | - } |
|
178 | - |
|
179 | - if ($this->logger) { |
|
180 | - // phpcs:ignore |
|
181 | - $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
182 | - |
|
183 | - if ($this->responseClientError) { |
|
184 | - // phpcs:ignore |
|
185 | - $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - return $this->responseBody; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Get response details |
|
194 | - * |
|
195 | - * @return array Map structure of details |
|
196 | - */ |
|
197 | - public function getResponse() |
|
198 | - { |
|
199 | - return [ |
|
200 | - 'request' => $this->getRequestArguments(), |
|
201 | - 'response' => [ |
|
202 | - 'code' => $this->getResponseHttpCode(), |
|
203 | - 'headers' => $this->getResponseHeader(), |
|
204 | - 'body' => $this->getResponseBody(), |
|
205 | - ], |
|
206 | - 'client' => [ |
|
207 | - 'error' => $this->getResponseClientError(), |
|
208 | - 'info' => $this->getResponseClientInfo(), |
|
209 | - 'opts' => null, |
|
210 | - ], |
|
211 | - ]; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Set logger instance |
|
216 | - * |
|
217 | - * @param object $logger |
|
218 | - */ |
|
219 | - public function setLogger($logger) |
|
220 | - { |
|
221 | - $this->logger = $logger; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * {@inheritdoc} |
|
226 | - */ |
|
227 | - public function getResponseBody() |
|
228 | - { |
|
229 | - return $this->responseBody; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * {@inheritdoc} |
|
234 | - */ |
|
235 | - public function getResponseHeader() |
|
236 | - { |
|
237 | - return $this->responseHeader; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * {@inheritdoc} |
|
242 | - */ |
|
243 | - public function getResponseHttpCode() |
|
244 | - { |
|
245 | - return $this->responseHttpCode; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * {@inheritdoc} |
|
250 | - */ |
|
251 | - public function getResponseClientError() |
|
252 | - { |
|
253 | - return $this->responseClientError; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - protected function getResponseClientInfo() |
|
260 | - { |
|
261 | - return $this->responseClientInfo; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Returns method request() arguments |
|
266 | - * |
|
267 | - * This is used for debugging. |
|
268 | - * |
|
269 | - * @return array |
|
270 | - */ |
|
271 | - protected function getRequestArguments() |
|
272 | - { |
|
273 | - return $this->requestArguments; |
|
274 | - } |
|
36 | + /** |
|
37 | + * Method request() arguments |
|
38 | + * |
|
39 | + * This is used for debugging. |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $requestArguments = []; |
|
44 | + |
|
45 | + /** |
|
46 | + * Default request headers |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $requestHeader = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * Raw response returned by server |
|
54 | + * |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + protected $responseBody = ''; |
|
58 | + |
|
59 | + /** |
|
60 | + * Headers returned in the response |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + protected $responseHeader = []; |
|
65 | + |
|
66 | + /** |
|
67 | + * Response HTTP status code |
|
68 | + * |
|
69 | + * @var int |
|
70 | + */ |
|
71 | + protected $responseHttpCode = 0; |
|
72 | + |
|
73 | + /** |
|
74 | + * Last curl error number |
|
75 | + * |
|
76 | + * @var mixed |
|
77 | + */ |
|
78 | + protected $responseClientError = null; |
|
79 | + |
|
80 | + /** |
|
81 | + * Information about the last transfer |
|
82 | + * |
|
83 | + * @var mixed |
|
84 | + */ |
|
85 | + protected $responseClientInfo = []; |
|
86 | + |
|
87 | + /** |
|
88 | + * Hybridauth logger instance |
|
89 | + * |
|
90 | + * @var object |
|
91 | + */ |
|
92 | + protected $logger = null; |
|
93 | + |
|
94 | + /** |
|
95 | + * GuzzleHttp client |
|
96 | + * |
|
97 | + * @var \GuzzleHttp\Client |
|
98 | + */ |
|
99 | + protected $client = null; |
|
100 | + |
|
101 | + /** |
|
102 | + * .. |
|
103 | + * @param null $client |
|
104 | + * @param array $config |
|
105 | + */ |
|
106 | + public function __construct($client = null, $config = []) |
|
107 | + { |
|
108 | + $this->client = $client ? $client : new Client($config); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * {@inheritdoc} |
|
113 | + */ |
|
114 | + public function request($uri, $method = 'GET', $parameters = [], $headers = [], $multipart = false) |
|
115 | + { |
|
116 | + $this->requestHeader = array_replace($this->requestHeader, (array)$headers); |
|
117 | + |
|
118 | + $this->requestArguments = [ |
|
119 | + 'uri' => $uri, |
|
120 | + 'method' => $method, |
|
121 | + 'parameters' => $parameters, |
|
122 | + 'headers' => $this->requestHeader, |
|
123 | + ]; |
|
124 | + |
|
125 | + $response = null; |
|
126 | + |
|
127 | + try { |
|
128 | + switch ($method) { |
|
129 | + case 'GET': |
|
130 | + case 'DELETE': |
|
131 | + $response = $this->client->request($method, $uri, [ |
|
132 | + 'query' => $parameters, |
|
133 | + 'headers' => $this->requestHeader, |
|
134 | + ]); |
|
135 | + break; |
|
136 | + case 'PUT': |
|
137 | + case 'POST': |
|
138 | + $body_type = $multipart ? 'multipart' : 'form_params'; |
|
139 | + |
|
140 | + if (isset($this->requestHeader['Content-Type']) |
|
141 | + && $this->requestHeader['Content-Type'] === 'application/json' |
|
142 | + ) { |
|
143 | + $body_type = 'json'; |
|
144 | + } |
|
145 | + |
|
146 | + $body_content = $parameters; |
|
147 | + if ($multipart) { |
|
148 | + $body_content = []; |
|
149 | + foreach ($parameters as $key => $val) { |
|
150 | + if ($val instanceof \CURLFile) { |
|
151 | + $val = fopen($val->getFilename(), 'r'); |
|
152 | + } |
|
153 | + |
|
154 | + $body_content[] = [ |
|
155 | + 'name' => $key, |
|
156 | + 'contents' => $val, |
|
157 | + ]; |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + $response = $this->client->request($method, $uri, [ |
|
162 | + $body_type => $body_content, |
|
163 | + 'headers' => $this->requestHeader, |
|
164 | + ]); |
|
165 | + break; |
|
166 | + } |
|
167 | + } catch (\Exception $e) { |
|
168 | + $response = $e->getResponse(); |
|
169 | + |
|
170 | + $this->responseClientError = $e->getMessage(); |
|
171 | + } |
|
172 | + |
|
173 | + if (!$this->responseClientError) { |
|
174 | + $this->responseBody = $response->getBody(); |
|
175 | + $this->responseHttpCode = $response->getStatusCode(); |
|
176 | + $this->responseHeader = $response->getHeaders(); |
|
177 | + } |
|
178 | + |
|
179 | + if ($this->logger) { |
|
180 | + // phpcs:ignore |
|
181 | + $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
182 | + |
|
183 | + if ($this->responseClientError) { |
|
184 | + // phpcs:ignore |
|
185 | + $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + return $this->responseBody; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Get response details |
|
194 | + * |
|
195 | + * @return array Map structure of details |
|
196 | + */ |
|
197 | + public function getResponse() |
|
198 | + { |
|
199 | + return [ |
|
200 | + 'request' => $this->getRequestArguments(), |
|
201 | + 'response' => [ |
|
202 | + 'code' => $this->getResponseHttpCode(), |
|
203 | + 'headers' => $this->getResponseHeader(), |
|
204 | + 'body' => $this->getResponseBody(), |
|
205 | + ], |
|
206 | + 'client' => [ |
|
207 | + 'error' => $this->getResponseClientError(), |
|
208 | + 'info' => $this->getResponseClientInfo(), |
|
209 | + 'opts' => null, |
|
210 | + ], |
|
211 | + ]; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Set logger instance |
|
216 | + * |
|
217 | + * @param object $logger |
|
218 | + */ |
|
219 | + public function setLogger($logger) |
|
220 | + { |
|
221 | + $this->logger = $logger; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * {@inheritdoc} |
|
226 | + */ |
|
227 | + public function getResponseBody() |
|
228 | + { |
|
229 | + return $this->responseBody; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * {@inheritdoc} |
|
234 | + */ |
|
235 | + public function getResponseHeader() |
|
236 | + { |
|
237 | + return $this->responseHeader; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * {@inheritdoc} |
|
242 | + */ |
|
243 | + public function getResponseHttpCode() |
|
244 | + { |
|
245 | + return $this->responseHttpCode; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * {@inheritdoc} |
|
250 | + */ |
|
251 | + public function getResponseClientError() |
|
252 | + { |
|
253 | + return $this->responseClientError; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + protected function getResponseClientInfo() |
|
260 | + { |
|
261 | + return $this->responseClientInfo; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Returns method request() arguments |
|
266 | + * |
|
267 | + * This is used for debugging. |
|
268 | + * |
|
269 | + * @return array |
|
270 | + */ |
|
271 | + protected function getRequestArguments() |
|
272 | + { |
|
273 | + return $this->requestArguments; |
|
274 | + } |
|
275 | 275 | } |
@@ -15,105 +15,105 @@ |
||
15 | 15 | */ |
16 | 16 | final class Parser |
17 | 17 | { |
18 | - /** |
|
19 | - * Decodes a string into an object. |
|
20 | - * |
|
21 | - * This method will first attempt to parse data as a JSON string (since most providers use this format) |
|
22 | - * then XML and parse_str. |
|
23 | - * |
|
24 | - * @param string $raw |
|
25 | - * |
|
26 | - * @return mixed |
|
27 | - */ |
|
28 | - public function parse($raw = null) |
|
29 | - { |
|
30 | - $data = $this->parseJson($raw); |
|
31 | - |
|
32 | - if (!$data) { |
|
33 | - $data = $this->parseXml($raw); |
|
34 | - |
|
35 | - if (!$data) { |
|
36 | - $data = $this->parseQueryString($raw); |
|
37 | - } |
|
38 | - } |
|
39 | - |
|
40 | - return $data; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Decodes a JSON string |
|
45 | - * |
|
46 | - * @param $result |
|
47 | - * |
|
48 | - * @return mixed |
|
49 | - */ |
|
50 | - public function parseJson($result) |
|
51 | - { |
|
52 | - return json_decode($result); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Decodes a XML string |
|
57 | - * |
|
58 | - * @param $result |
|
59 | - * |
|
60 | - * @return mixed |
|
61 | - */ |
|
62 | - public function parseXml($result) |
|
63 | - { |
|
64 | - libxml_use_internal_errors(true); |
|
65 | - |
|
66 | - $result = preg_replace('/([<\/])([a-z0-9-]+):/i', '$1', $result); |
|
67 | - $xml = simplexml_load_string($result); |
|
68 | - |
|
69 | - libxml_use_internal_errors(false); |
|
70 | - |
|
71 | - if (!$xml) { |
|
72 | - return []; |
|
73 | - } |
|
74 | - |
|
75 | - $arr = json_decode(json_encode((array)$xml), true); |
|
76 | - $arr = array($xml->getName() => $arr); |
|
77 | - |
|
78 | - return $arr; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Parses a string into variables |
|
83 | - * |
|
84 | - * @param $result |
|
85 | - * |
|
86 | - * @return \StdClass |
|
87 | - */ |
|
88 | - public function parseQueryString($result) |
|
89 | - { |
|
90 | - parse_str($result, $output); |
|
91 | - |
|
92 | - if (!is_array($output)) { |
|
93 | - return $result; |
|
94 | - } |
|
95 | - |
|
96 | - $result = new \StdClass(); |
|
97 | - |
|
98 | - foreach ($output as $k => $v) { |
|
99 | - $result->$k = $v; |
|
100 | - } |
|
101 | - |
|
102 | - return $result; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * needs to be improved |
|
107 | - * |
|
108 | - * @param $birthday |
|
109 | - * @param $seperator |
|
110 | - * |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - public function parseBirthday($birthday, $seperator) |
|
114 | - { |
|
115 | - $birthday = date_parse($birthday); |
|
116 | - |
|
117 | - return [$birthday['year'], $birthday['month'], $birthday['day']]; |
|
118 | - } |
|
18 | + /** |
|
19 | + * Decodes a string into an object. |
|
20 | + * |
|
21 | + * This method will first attempt to parse data as a JSON string (since most providers use this format) |
|
22 | + * then XML and parse_str. |
|
23 | + * |
|
24 | + * @param string $raw |
|
25 | + * |
|
26 | + * @return mixed |
|
27 | + */ |
|
28 | + public function parse($raw = null) |
|
29 | + { |
|
30 | + $data = $this->parseJson($raw); |
|
31 | + |
|
32 | + if (!$data) { |
|
33 | + $data = $this->parseXml($raw); |
|
34 | + |
|
35 | + if (!$data) { |
|
36 | + $data = $this->parseQueryString($raw); |
|
37 | + } |
|
38 | + } |
|
39 | + |
|
40 | + return $data; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Decodes a JSON string |
|
45 | + * |
|
46 | + * @param $result |
|
47 | + * |
|
48 | + * @return mixed |
|
49 | + */ |
|
50 | + public function parseJson($result) |
|
51 | + { |
|
52 | + return json_decode($result); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Decodes a XML string |
|
57 | + * |
|
58 | + * @param $result |
|
59 | + * |
|
60 | + * @return mixed |
|
61 | + */ |
|
62 | + public function parseXml($result) |
|
63 | + { |
|
64 | + libxml_use_internal_errors(true); |
|
65 | + |
|
66 | + $result = preg_replace('/([<\/])([a-z0-9-]+):/i', '$1', $result); |
|
67 | + $xml = simplexml_load_string($result); |
|
68 | + |
|
69 | + libxml_use_internal_errors(false); |
|
70 | + |
|
71 | + if (!$xml) { |
|
72 | + return []; |
|
73 | + } |
|
74 | + |
|
75 | + $arr = json_decode(json_encode((array)$xml), true); |
|
76 | + $arr = array($xml->getName() => $arr); |
|
77 | + |
|
78 | + return $arr; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Parses a string into variables |
|
83 | + * |
|
84 | + * @param $result |
|
85 | + * |
|
86 | + * @return \StdClass |
|
87 | + */ |
|
88 | + public function parseQueryString($result) |
|
89 | + { |
|
90 | + parse_str($result, $output); |
|
91 | + |
|
92 | + if (!is_array($output)) { |
|
93 | + return $result; |
|
94 | + } |
|
95 | + |
|
96 | + $result = new \StdClass(); |
|
97 | + |
|
98 | + foreach ($output as $k => $v) { |
|
99 | + $result->$k = $v; |
|
100 | + } |
|
101 | + |
|
102 | + return $result; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * needs to be improved |
|
107 | + * |
|
108 | + * @param $birthday |
|
109 | + * @param $seperator |
|
110 | + * |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + public function parseBirthday($birthday, $seperator) |
|
114 | + { |
|
115 | + $birthday = date_parse($birthday); |
|
116 | + |
|
117 | + return [$birthday['year'], $birthday['month'], $birthday['day']]; |
|
118 | + } |
|
119 | 119 | } |