@@ -12,143 +12,143 @@ |
||
12 | 12 | */ |
13 | 13 | final class Collection |
14 | 14 | { |
15 | - /** |
|
16 | - * Data collection |
|
17 | - * |
|
18 | - * @var mixed |
|
19 | - */ |
|
20 | - protected $collection = null; |
|
21 | - |
|
22 | - /** |
|
23 | - * @param mixed $data |
|
24 | - */ |
|
25 | - public function __construct($data = null) |
|
26 | - { |
|
27 | - $this->collection = (object)$data; |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Retrieves the whole collection as array |
|
32 | - * |
|
33 | - * @return mixed |
|
34 | - */ |
|
35 | - public function toArray() |
|
36 | - { |
|
37 | - return (array)$this->collection; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Retrieves an item |
|
42 | - * |
|
43 | - * @param $property |
|
44 | - * |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function get($property) |
|
48 | - { |
|
49 | - if ($this->exists($property)) { |
|
50 | - return $this->collection->$property; |
|
51 | - } |
|
52 | - |
|
53 | - return null; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Add or update an item |
|
58 | - * |
|
59 | - * @param $property |
|
60 | - * @param mixed $value |
|
61 | - */ |
|
62 | - public function set($property, $value) |
|
63 | - { |
|
64 | - if ($property) { |
|
65 | - $this->collection->$property = $value; |
|
66 | - } |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * .. until I come with a better name.. |
|
71 | - * |
|
72 | - * @param $property |
|
73 | - * |
|
74 | - * @return Collection |
|
75 | - */ |
|
76 | - public function filter($property) |
|
77 | - { |
|
78 | - if ($this->exists($property)) { |
|
79 | - $data = $this->get($property); |
|
80 | - |
|
81 | - if (!is_a($data, 'Collection')) { |
|
82 | - $data = new Collection($data); |
|
83 | - } |
|
84 | - |
|
85 | - return $data; |
|
86 | - } |
|
87 | - |
|
88 | - return new Collection([]); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Checks whether an item within the collection |
|
93 | - * |
|
94 | - * @param $property |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public function exists($property) |
|
99 | - { |
|
100 | - return property_exists($this->collection, $property); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Finds whether the collection is empty |
|
105 | - * |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public function isEmpty() |
|
109 | - { |
|
110 | - return !(bool)$this->count(); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Count all items in collection |
|
115 | - * |
|
116 | - * @return int |
|
117 | - */ |
|
118 | - public function count() |
|
119 | - { |
|
120 | - return count($this->properties()); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns all items properties names |
|
125 | - * |
|
126 | - * @return array |
|
127 | - */ |
|
128 | - public function properties() |
|
129 | - { |
|
130 | - $properties = []; |
|
131 | - |
|
132 | - foreach ($this->collection as $key => $value) { |
|
133 | - $properties[] = $key; |
|
134 | - } |
|
135 | - |
|
136 | - return $properties; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Returns all items values |
|
141 | - * |
|
142 | - * @return array |
|
143 | - */ |
|
144 | - public function values() |
|
145 | - { |
|
146 | - $values = []; |
|
147 | - |
|
148 | - foreach ($this->collection as $value) { |
|
149 | - $values[] = $value; |
|
150 | - } |
|
151 | - |
|
152 | - return $values; |
|
153 | - } |
|
15 | + /** |
|
16 | + * Data collection |
|
17 | + * |
|
18 | + * @var mixed |
|
19 | + */ |
|
20 | + protected $collection = null; |
|
21 | + |
|
22 | + /** |
|
23 | + * @param mixed $data |
|
24 | + */ |
|
25 | + public function __construct($data = null) |
|
26 | + { |
|
27 | + $this->collection = (object)$data; |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Retrieves the whole collection as array |
|
32 | + * |
|
33 | + * @return mixed |
|
34 | + */ |
|
35 | + public function toArray() |
|
36 | + { |
|
37 | + return (array)$this->collection; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Retrieves an item |
|
42 | + * |
|
43 | + * @param $property |
|
44 | + * |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function get($property) |
|
48 | + { |
|
49 | + if ($this->exists($property)) { |
|
50 | + return $this->collection->$property; |
|
51 | + } |
|
52 | + |
|
53 | + return null; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Add or update an item |
|
58 | + * |
|
59 | + * @param $property |
|
60 | + * @param mixed $value |
|
61 | + */ |
|
62 | + public function set($property, $value) |
|
63 | + { |
|
64 | + if ($property) { |
|
65 | + $this->collection->$property = $value; |
|
66 | + } |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * .. until I come with a better name.. |
|
71 | + * |
|
72 | + * @param $property |
|
73 | + * |
|
74 | + * @return Collection |
|
75 | + */ |
|
76 | + public function filter($property) |
|
77 | + { |
|
78 | + if ($this->exists($property)) { |
|
79 | + $data = $this->get($property); |
|
80 | + |
|
81 | + if (!is_a($data, 'Collection')) { |
|
82 | + $data = new Collection($data); |
|
83 | + } |
|
84 | + |
|
85 | + return $data; |
|
86 | + } |
|
87 | + |
|
88 | + return new Collection([]); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Checks whether an item within the collection |
|
93 | + * |
|
94 | + * @param $property |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public function exists($property) |
|
99 | + { |
|
100 | + return property_exists($this->collection, $property); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Finds whether the collection is empty |
|
105 | + * |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public function isEmpty() |
|
109 | + { |
|
110 | + return !(bool)$this->count(); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Count all items in collection |
|
115 | + * |
|
116 | + * @return int |
|
117 | + */ |
|
118 | + public function count() |
|
119 | + { |
|
120 | + return count($this->properties()); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns all items properties names |
|
125 | + * |
|
126 | + * @return array |
|
127 | + */ |
|
128 | + public function properties() |
|
129 | + { |
|
130 | + $properties = []; |
|
131 | + |
|
132 | + foreach ($this->collection as $key => $value) { |
|
133 | + $properties[] = $key; |
|
134 | + } |
|
135 | + |
|
136 | + return $properties; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Returns all items values |
|
141 | + * |
|
142 | + * @return array |
|
143 | + */ |
|
144 | + public function values() |
|
145 | + { |
|
146 | + $values = []; |
|
147 | + |
|
148 | + foreach ($this->collection as $value) { |
|
149 | + $values[] = $value; |
|
150 | + } |
|
151 | + |
|
152 | + return $values; |
|
153 | + } |
|
154 | 154 | } |
@@ -33,244 +33,244 @@ |
||
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 'PATCH': |
|
138 | - case 'POST': |
|
139 | - $body_type = $multipart ? 'multipart' : 'form_params'; |
|
140 | - |
|
141 | - if (isset($this->requestHeader['Content-Type']) |
|
142 | - && $this->requestHeader['Content-Type'] === 'application/json' |
|
143 | - ) { |
|
144 | - $body_type = 'json'; |
|
145 | - } |
|
146 | - |
|
147 | - $body_content = $parameters; |
|
148 | - if ($multipart) { |
|
149 | - $body_content = []; |
|
150 | - foreach ($parameters as $key => $val) { |
|
151 | - if ($val instanceof \CURLFile) { |
|
152 | - $val = fopen($val->getFilename(), 'r'); |
|
153 | - } |
|
154 | - |
|
155 | - $body_content[] = [ |
|
156 | - 'name' => $key, |
|
157 | - 'contents' => $val, |
|
158 | - ]; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - $response = $this->client->request($method, $uri, [ |
|
163 | - $body_type => $body_content, |
|
164 | - 'headers' => $this->requestHeader, |
|
165 | - ]); |
|
166 | - break; |
|
167 | - } |
|
168 | - } catch (\Exception $e) { |
|
169 | - $response = $e->getResponse(); |
|
170 | - |
|
171 | - $this->responseClientError = $e->getMessage(); |
|
172 | - } |
|
173 | - |
|
174 | - if (!$this->responseClientError) { |
|
175 | - $this->responseBody = $response->getBody(); |
|
176 | - $this->responseHttpCode = $response->getStatusCode(); |
|
177 | - $this->responseHeader = $response->getHeaders(); |
|
178 | - } |
|
179 | - |
|
180 | - if ($this->logger) { |
|
181 | - // phpcs:ignore |
|
182 | - $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
183 | - |
|
184 | - if ($this->responseClientError) { |
|
185 | - // phpcs:ignore |
|
186 | - $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - return $this->responseBody; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Get response details |
|
195 | - * |
|
196 | - * @return array Map structure of details |
|
197 | - */ |
|
198 | - public function getResponse() |
|
199 | - { |
|
200 | - return [ |
|
201 | - 'request' => $this->getRequestArguments(), |
|
202 | - 'response' => [ |
|
203 | - 'code' => $this->getResponseHttpCode(), |
|
204 | - 'headers' => $this->getResponseHeader(), |
|
205 | - 'body' => $this->getResponseBody(), |
|
206 | - ], |
|
207 | - 'client' => [ |
|
208 | - 'error' => $this->getResponseClientError(), |
|
209 | - 'info' => $this->getResponseClientInfo(), |
|
210 | - 'opts' => null, |
|
211 | - ], |
|
212 | - ]; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Set logger instance |
|
217 | - * |
|
218 | - * @param object $logger |
|
219 | - */ |
|
220 | - public function setLogger($logger) |
|
221 | - { |
|
222 | - $this->logger = $logger; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * {@inheritdoc} |
|
227 | - */ |
|
228 | - public function getResponseBody() |
|
229 | - { |
|
230 | - return $this->responseBody; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * {@inheritdoc} |
|
235 | - */ |
|
236 | - public function getResponseHeader() |
|
237 | - { |
|
238 | - return $this->responseHeader; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * {@inheritdoc} |
|
243 | - */ |
|
244 | - public function getResponseHttpCode() |
|
245 | - { |
|
246 | - return $this->responseHttpCode; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * {@inheritdoc} |
|
251 | - */ |
|
252 | - public function getResponseClientError() |
|
253 | - { |
|
254 | - return $this->responseClientError; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @return array |
|
259 | - */ |
|
260 | - protected function getResponseClientInfo() |
|
261 | - { |
|
262 | - return $this->responseClientInfo; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Returns method request() arguments |
|
267 | - * |
|
268 | - * This is used for debugging. |
|
269 | - * |
|
270 | - * @return array |
|
271 | - */ |
|
272 | - protected function getRequestArguments() |
|
273 | - { |
|
274 | - return $this->requestArguments; |
|
275 | - } |
|
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 'PATCH': |
|
138 | + case 'POST': |
|
139 | + $body_type = $multipart ? 'multipart' : 'form_params'; |
|
140 | + |
|
141 | + if (isset($this->requestHeader['Content-Type']) |
|
142 | + && $this->requestHeader['Content-Type'] === 'application/json' |
|
143 | + ) { |
|
144 | + $body_type = 'json'; |
|
145 | + } |
|
146 | + |
|
147 | + $body_content = $parameters; |
|
148 | + if ($multipart) { |
|
149 | + $body_content = []; |
|
150 | + foreach ($parameters as $key => $val) { |
|
151 | + if ($val instanceof \CURLFile) { |
|
152 | + $val = fopen($val->getFilename(), 'r'); |
|
153 | + } |
|
154 | + |
|
155 | + $body_content[] = [ |
|
156 | + 'name' => $key, |
|
157 | + 'contents' => $val, |
|
158 | + ]; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + $response = $this->client->request($method, $uri, [ |
|
163 | + $body_type => $body_content, |
|
164 | + 'headers' => $this->requestHeader, |
|
165 | + ]); |
|
166 | + break; |
|
167 | + } |
|
168 | + } catch (\Exception $e) { |
|
169 | + $response = $e->getResponse(); |
|
170 | + |
|
171 | + $this->responseClientError = $e->getMessage(); |
|
172 | + } |
|
173 | + |
|
174 | + if (!$this->responseClientError) { |
|
175 | + $this->responseBody = $response->getBody(); |
|
176 | + $this->responseHttpCode = $response->getStatusCode(); |
|
177 | + $this->responseHeader = $response->getHeaders(); |
|
178 | + } |
|
179 | + |
|
180 | + if ($this->logger) { |
|
181 | + // phpcs:ignore |
|
182 | + $this->logger->debug(sprintf('%s::request( %s, %s ), response:', get_class($this), $uri, $method), $this->getResponse()); |
|
183 | + |
|
184 | + if ($this->responseClientError) { |
|
185 | + // phpcs:ignore |
|
186 | + $this->logger->error(sprintf('%s::request( %s, %s ), error:', get_class($this), $uri, $method), [$this->responseClientError]); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + return $this->responseBody; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Get response details |
|
195 | + * |
|
196 | + * @return array Map structure of details |
|
197 | + */ |
|
198 | + public function getResponse() |
|
199 | + { |
|
200 | + return [ |
|
201 | + 'request' => $this->getRequestArguments(), |
|
202 | + 'response' => [ |
|
203 | + 'code' => $this->getResponseHttpCode(), |
|
204 | + 'headers' => $this->getResponseHeader(), |
|
205 | + 'body' => $this->getResponseBody(), |
|
206 | + ], |
|
207 | + 'client' => [ |
|
208 | + 'error' => $this->getResponseClientError(), |
|
209 | + 'info' => $this->getResponseClientInfo(), |
|
210 | + 'opts' => null, |
|
211 | + ], |
|
212 | + ]; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Set logger instance |
|
217 | + * |
|
218 | + * @param object $logger |
|
219 | + */ |
|
220 | + public function setLogger($logger) |
|
221 | + { |
|
222 | + $this->logger = $logger; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * {@inheritdoc} |
|
227 | + */ |
|
228 | + public function getResponseBody() |
|
229 | + { |
|
230 | + return $this->responseBody; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * {@inheritdoc} |
|
235 | + */ |
|
236 | + public function getResponseHeader() |
|
237 | + { |
|
238 | + return $this->responseHeader; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * {@inheritdoc} |
|
243 | + */ |
|
244 | + public function getResponseHttpCode() |
|
245 | + { |
|
246 | + return $this->responseHttpCode; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * {@inheritdoc} |
|
251 | + */ |
|
252 | + public function getResponseClientError() |
|
253 | + { |
|
254 | + return $this->responseClientError; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @return array |
|
259 | + */ |
|
260 | + protected function getResponseClientInfo() |
|
261 | + { |
|
262 | + return $this->responseClientInfo; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Returns method request() arguments |
|
267 | + * |
|
268 | + * This is used for debugging. |
|
269 | + * |
|
270 | + * @return array |
|
271 | + */ |
|
272 | + protected function getRequestArguments() |
|
273 | + { |
|
274 | + return $this->requestArguments; |
|
275 | + } |
|
276 | 276 | } |
@@ -46,154 +46,154 @@ |
||
46 | 46 | */ |
47 | 47 | class Google extends OAuth2 |
48 | 48 | { |
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - // phpcs:ignore |
|
53 | - protected $scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - protected $apiBaseUrl = 'https://www.googleapis.com/'; |
|
59 | - |
|
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected $authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; |
|
64 | - |
|
65 | - /** |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - protected $accessTokenUrl = 'https://oauth2.googleapis.com/token'; |
|
69 | - |
|
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - protected $apiDocumentation = 'https://developers.google.com/identity/protocols/OAuth2'; |
|
74 | - |
|
75 | - /** |
|
76 | - * {@inheritdoc} |
|
77 | - */ |
|
78 | - protected function initialize() |
|
79 | - { |
|
80 | - parent::initialize(); |
|
81 | - |
|
82 | - $this->AuthorizeUrlParameters += [ |
|
83 | - 'access_type' => 'offline' |
|
84 | - ]; |
|
85 | - |
|
86 | - if ($this->isRefreshTokenAvailable()) { |
|
87 | - $this->tokenRefreshParameters += [ |
|
88 | - 'client_id' => $this->clientId, |
|
89 | - 'client_secret' => $this->clientSecret |
|
90 | - ]; |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * {@inheritdoc} |
|
96 | - * |
|
97 | - * See: https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo |
|
98 | - */ |
|
99 | - public function getUserProfile() |
|
100 | - { |
|
101 | - $response = $this->apiRequest('oauth2/v3/userinfo'); |
|
102 | - |
|
103 | - $data = new Data\Collection($response); |
|
104 | - |
|
105 | - if (!$data->exists('sub')) { |
|
106 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
107 | - } |
|
108 | - |
|
109 | - $userProfile = new User\Profile(); |
|
110 | - |
|
111 | - $userProfile->identifier = $data->get('sub'); |
|
112 | - $userProfile->firstName = $data->get('given_name'); |
|
113 | - $userProfile->lastName = $data->get('family_name'); |
|
114 | - $userProfile->displayName = $data->get('name'); |
|
115 | - $userProfile->photoURL = $data->get('picture'); |
|
116 | - $userProfile->profileURL = $data->get('profile'); |
|
117 | - $userProfile->gender = $data->get('gender'); |
|
118 | - $userProfile->language = $data->get('locale'); |
|
119 | - $userProfile->email = $data->get('email'); |
|
120 | - |
|
121 | - $userProfile->emailVerified = $data->get('email_verified') ? $userProfile->email : ''; |
|
122 | - |
|
123 | - if ($this->config->get('photo_size')) { |
|
124 | - $userProfile->photoURL .= '?sz=' . $this->config->get('photo_size'); |
|
125 | - } |
|
126 | - |
|
127 | - return $userProfile; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * {@inheritdoc} |
|
132 | - */ |
|
133 | - public function getUserContacts($parameters = []) |
|
134 | - { |
|
135 | - $parameters = ['max-results' => 500] + $parameters; |
|
136 | - |
|
137 | - // Google Gmail and Android contacts |
|
138 | - if (false !== strpos($this->scope, '/m8/feeds/') || false !== strpos($this->scope, '/auth/contacts.readonly')) { |
|
139 | - return $this->getGmailContacts($parameters); |
|
140 | - } |
|
141 | - |
|
142 | - return []; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Retrieve Gmail contacts |
|
147 | - * |
|
148 | - * @param array $parameters |
|
149 | - * |
|
150 | - * @return array |
|
151 | - * |
|
152 | - * @throws \Exception |
|
153 | - */ |
|
154 | - protected function getGmailContacts($parameters = []) |
|
155 | - { |
|
156 | - $url = 'https://www.google.com/m8/feeds/contacts/default/full?' |
|
157 | - . http_build_query(array_replace(['alt' => 'json', 'v' => '3.0'], (array)$parameters)); |
|
158 | - |
|
159 | - $response = $this->apiRequest($url); |
|
160 | - |
|
161 | - if (!$response) { |
|
162 | - return []; |
|
163 | - } |
|
164 | - |
|
165 | - $contacts = []; |
|
166 | - |
|
167 | - if (isset($response->feed->entry)) { |
|
168 | - foreach ($response->feed->entry as $idx => $entry) { |
|
169 | - $uc = new User\Contact(); |
|
170 | - |
|
171 | - $uc->email = isset($entry->{'gd$email'}[0]->address) |
|
172 | - ? (string)$entry->{'gd$email'}[0]->address |
|
173 | - : ''; |
|
174 | - |
|
175 | - $uc->displayName = isset($entry->title->{'$t'}) ? (string)$entry->title->{'$t'} : ''; |
|
176 | - $uc->identifier = ($uc->email != '') ? $uc->email : ''; |
|
177 | - $uc->description = ''; |
|
178 | - |
|
179 | - if (property_exists($response, 'website')) { |
|
180 | - if (is_array($response->website)) { |
|
181 | - foreach ($response->website as $w) { |
|
182 | - if ($w->primary == true) { |
|
183 | - $uc->webSiteURL = $w->value; |
|
184 | - } |
|
185 | - } |
|
186 | - } else { |
|
187 | - $uc->webSiteURL = $response->website->value; |
|
188 | - } |
|
189 | - } else { |
|
190 | - $uc->webSiteURL = ''; |
|
191 | - } |
|
192 | - |
|
193 | - $contacts[] = $uc; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - return $contacts; |
|
198 | - } |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + // phpcs:ignore |
|
53 | + protected $scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + protected $apiBaseUrl = 'https://www.googleapis.com/'; |
|
59 | + |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected $authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; |
|
64 | + |
|
65 | + /** |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + protected $accessTokenUrl = 'https://oauth2.googleapis.com/token'; |
|
69 | + |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + protected $apiDocumentation = 'https://developers.google.com/identity/protocols/OAuth2'; |
|
74 | + |
|
75 | + /** |
|
76 | + * {@inheritdoc} |
|
77 | + */ |
|
78 | + protected function initialize() |
|
79 | + { |
|
80 | + parent::initialize(); |
|
81 | + |
|
82 | + $this->AuthorizeUrlParameters += [ |
|
83 | + 'access_type' => 'offline' |
|
84 | + ]; |
|
85 | + |
|
86 | + if ($this->isRefreshTokenAvailable()) { |
|
87 | + $this->tokenRefreshParameters += [ |
|
88 | + 'client_id' => $this->clientId, |
|
89 | + 'client_secret' => $this->clientSecret |
|
90 | + ]; |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * {@inheritdoc} |
|
96 | + * |
|
97 | + * See: https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo |
|
98 | + */ |
|
99 | + public function getUserProfile() |
|
100 | + { |
|
101 | + $response = $this->apiRequest('oauth2/v3/userinfo'); |
|
102 | + |
|
103 | + $data = new Data\Collection($response); |
|
104 | + |
|
105 | + if (!$data->exists('sub')) { |
|
106 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
107 | + } |
|
108 | + |
|
109 | + $userProfile = new User\Profile(); |
|
110 | + |
|
111 | + $userProfile->identifier = $data->get('sub'); |
|
112 | + $userProfile->firstName = $data->get('given_name'); |
|
113 | + $userProfile->lastName = $data->get('family_name'); |
|
114 | + $userProfile->displayName = $data->get('name'); |
|
115 | + $userProfile->photoURL = $data->get('picture'); |
|
116 | + $userProfile->profileURL = $data->get('profile'); |
|
117 | + $userProfile->gender = $data->get('gender'); |
|
118 | + $userProfile->language = $data->get('locale'); |
|
119 | + $userProfile->email = $data->get('email'); |
|
120 | + |
|
121 | + $userProfile->emailVerified = $data->get('email_verified') ? $userProfile->email : ''; |
|
122 | + |
|
123 | + if ($this->config->get('photo_size')) { |
|
124 | + $userProfile->photoURL .= '?sz=' . $this->config->get('photo_size'); |
|
125 | + } |
|
126 | + |
|
127 | + return $userProfile; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * {@inheritdoc} |
|
132 | + */ |
|
133 | + public function getUserContacts($parameters = []) |
|
134 | + { |
|
135 | + $parameters = ['max-results' => 500] + $parameters; |
|
136 | + |
|
137 | + // Google Gmail and Android contacts |
|
138 | + if (false !== strpos($this->scope, '/m8/feeds/') || false !== strpos($this->scope, '/auth/contacts.readonly')) { |
|
139 | + return $this->getGmailContacts($parameters); |
|
140 | + } |
|
141 | + |
|
142 | + return []; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Retrieve Gmail contacts |
|
147 | + * |
|
148 | + * @param array $parameters |
|
149 | + * |
|
150 | + * @return array |
|
151 | + * |
|
152 | + * @throws \Exception |
|
153 | + */ |
|
154 | + protected function getGmailContacts($parameters = []) |
|
155 | + { |
|
156 | + $url = 'https://www.google.com/m8/feeds/contacts/default/full?' |
|
157 | + . http_build_query(array_replace(['alt' => 'json', 'v' => '3.0'], (array)$parameters)); |
|
158 | + |
|
159 | + $response = $this->apiRequest($url); |
|
160 | + |
|
161 | + if (!$response) { |
|
162 | + return []; |
|
163 | + } |
|
164 | + |
|
165 | + $contacts = []; |
|
166 | + |
|
167 | + if (isset($response->feed->entry)) { |
|
168 | + foreach ($response->feed->entry as $idx => $entry) { |
|
169 | + $uc = new User\Contact(); |
|
170 | + |
|
171 | + $uc->email = isset($entry->{'gd$email'}[0]->address) |
|
172 | + ? (string)$entry->{'gd$email'}[0]->address |
|
173 | + : ''; |
|
174 | + |
|
175 | + $uc->displayName = isset($entry->title->{'$t'}) ? (string)$entry->title->{'$t'} : ''; |
|
176 | + $uc->identifier = ($uc->email != '') ? $uc->email : ''; |
|
177 | + $uc->description = ''; |
|
178 | + |
|
179 | + if (property_exists($response, 'website')) { |
|
180 | + if (is_array($response->website)) { |
|
181 | + foreach ($response->website as $w) { |
|
182 | + if ($w->primary == true) { |
|
183 | + $uc->webSiteURL = $w->value; |
|
184 | + } |
|
185 | + } |
|
186 | + } else { |
|
187 | + $uc->webSiteURL = $response->website->value; |
|
188 | + } |
|
189 | + } else { |
|
190 | + $uc->webSiteURL = ''; |
|
191 | + } |
|
192 | + |
|
193 | + $contacts[] = $uc; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + return $contacts; |
|
198 | + } |
|
199 | 199 | } |
@@ -17,65 +17,65 @@ |
||
17 | 17 | */ |
18 | 18 | class Amazon extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'profile'; |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'profile'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://api.amazon.com/'; |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://api.amazon.com/'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl = 'https://www.amazon.com/ap/oa'; |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl = 'https://www.amazon.com/ap/oa'; |
|
34 | 34 | |
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://api.amazon.com/auth/o2/token'; |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://api.amazon.com/auth/o2/token'; |
|
39 | 39 | |
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - protected $apiDocumentation = 'https://developer.amazon.com/docs/login-with-amazon/documentation-overview.html'; |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + protected $apiDocumentation = 'https://developer.amazon.com/docs/login-with-amazon/documentation-overview.html'; |
|
44 | 44 | |
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - protected function initialize() |
|
49 | - { |
|
50 | - parent::initialize(); |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + protected function initialize() |
|
49 | + { |
|
50 | + parent::initialize(); |
|
51 | 51 | |
52 | - if ($this->isRefreshTokenAvailable()) { |
|
53 | - $this->tokenRefreshParameters += [ |
|
54 | - 'client_id' => $this->clientId, |
|
55 | - 'client_secret' => $this->clientSecret, |
|
56 | - ]; |
|
57 | - } |
|
58 | - } |
|
52 | + if ($this->isRefreshTokenAvailable()) { |
|
53 | + $this->tokenRefreshParameters += [ |
|
54 | + 'client_id' => $this->clientId, |
|
55 | + 'client_secret' => $this->clientSecret, |
|
56 | + ]; |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - public function getUserProfile() |
|
64 | - { |
|
65 | - $response = $this->apiRequest('user/profile'); |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + public function getUserProfile() |
|
64 | + { |
|
65 | + $response = $this->apiRequest('user/profile'); |
|
66 | 66 | |
67 | - $data = new Data\Collection($response); |
|
67 | + $data = new Data\Collection($response); |
|
68 | 68 | |
69 | - if (!$data->exists('user_id')) { |
|
70 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
71 | - } |
|
69 | + if (!$data->exists('user_id')) { |
|
70 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
71 | + } |
|
72 | 72 | |
73 | - $userProfile = new User\Profile(); |
|
73 | + $userProfile = new User\Profile(); |
|
74 | 74 | |
75 | - $userProfile->identifier = $data->get('user_id'); |
|
76 | - $userProfile->displayName = $data->get('name'); |
|
77 | - $userProfile->email = $data->get('email'); |
|
75 | + $userProfile->identifier = $data->get('user_id'); |
|
76 | + $userProfile->displayName = $data->get('name'); |
|
77 | + $userProfile->email = $data->get('email'); |
|
78 | 78 | |
79 | - return $userProfile; |
|
80 | - } |
|
79 | + return $userProfile; |
|
80 | + } |
|
81 | 81 | } |
@@ -45,407 +45,407 @@ |
||
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 maintainToken() |
|
96 | - { |
|
97 | - if (!$this->isConnected()) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - // Handle token exchange prior to the standard handler for an API request |
|
102 | - $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
103 | - if ($exchange_by_expiry_days !== null) { |
|
104 | - $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
105 | - if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
106 | - $this->exchangeAccessToken(); |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Exchange the Access Token with one that expires further in the future. |
|
113 | - * |
|
114 | - * @return string Raw Provider API response |
|
115 | - * @throws \Hybridauth\Exception\HttpClientFailureException |
|
116 | - * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
117 | - * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
118 | - */ |
|
119 | - public function exchangeAccessToken() |
|
120 | - { |
|
121 | - $exchangeTokenParameters = [ |
|
122 | - 'grant_type' => 'fb_exchange_token', |
|
123 | - 'client_id' => $this->clientId, |
|
124 | - 'client_secret' => $this->clientSecret, |
|
125 | - 'fb_exchange_token' => $this->getStoredData('access_token'), |
|
126 | - ]; |
|
127 | - |
|
128 | - $response = $this->httpClient->request( |
|
129 | - $this->accessTokenUrl, |
|
130 | - 'GET', |
|
131 | - $exchangeTokenParameters |
|
132 | - ); |
|
133 | - |
|
134 | - $this->validateApiResponse('Unable to exchange the access token'); |
|
135 | - |
|
136 | - $this->validateAccessTokenExchange($response); |
|
137 | - |
|
138 | - return $response; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * {@inheritdoc} |
|
143 | - */ |
|
144 | - public function getUserProfile() |
|
145 | - { |
|
146 | - $fields = [ |
|
147 | - 'id', |
|
148 | - 'name', |
|
149 | - 'first_name', |
|
150 | - 'last_name', |
|
151 | - 'website', |
|
152 | - 'locale', |
|
153 | - 'about', |
|
154 | - 'email', |
|
155 | - 'hometown', |
|
156 | - 'birthday', |
|
157 | - ]; |
|
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 maintainToken() |
|
96 | + { |
|
97 | + if (!$this->isConnected()) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + // Handle token exchange prior to the standard handler for an API request |
|
102 | + $exchange_by_expiry_days = $this->config->get('exchange_by_expiry_days') ?: 45; |
|
103 | + if ($exchange_by_expiry_days !== null) { |
|
104 | + $projected_timestamp = time() + 60 * 60 * 24 * $exchange_by_expiry_days; |
|
105 | + if (!$this->hasAccessTokenExpired() && $this->hasAccessTokenExpired($projected_timestamp)) { |
|
106 | + $this->exchangeAccessToken(); |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Exchange the Access Token with one that expires further in the future. |
|
113 | + * |
|
114 | + * @return string Raw Provider API response |
|
115 | + * @throws \Hybridauth\Exception\HttpClientFailureException |
|
116 | + * @throws \Hybridauth\Exception\HttpRequestFailedException |
|
117 | + * @throws \Hybridauth\Exception\InvalidAccessTokenException |
|
118 | + */ |
|
119 | + public function exchangeAccessToken() |
|
120 | + { |
|
121 | + $exchangeTokenParameters = [ |
|
122 | + 'grant_type' => 'fb_exchange_token', |
|
123 | + 'client_id' => $this->clientId, |
|
124 | + 'client_secret' => $this->clientSecret, |
|
125 | + 'fb_exchange_token' => $this->getStoredData('access_token'), |
|
126 | + ]; |
|
127 | + |
|
128 | + $response = $this->httpClient->request( |
|
129 | + $this->accessTokenUrl, |
|
130 | + 'GET', |
|
131 | + $exchangeTokenParameters |
|
132 | + ); |
|
133 | + |
|
134 | + $this->validateApiResponse('Unable to exchange the access token'); |
|
135 | + |
|
136 | + $this->validateAccessTokenExchange($response); |
|
137 | + |
|
138 | + return $response; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * {@inheritdoc} |
|
143 | + */ |
|
144 | + public function getUserProfile() |
|
145 | + { |
|
146 | + $fields = [ |
|
147 | + 'id', |
|
148 | + 'name', |
|
149 | + 'first_name', |
|
150 | + 'last_name', |
|
151 | + 'website', |
|
152 | + 'locale', |
|
153 | + 'about', |
|
154 | + 'email', |
|
155 | + 'hometown', |
|
156 | + 'birthday', |
|
157 | + ]; |
|
158 | 158 | |
159 | - if (strpos($this->scope, 'user_link') !== false) { |
|
160 | - $fields[] = 'link'; |
|
161 | - } |
|
162 | - |
|
163 | - if (strpos($this->scope, 'user_gender') !== false) { |
|
164 | - $fields[] = 'gender'; |
|
165 | - } |
|
166 | - |
|
167 | - // Note that en_US is needed for gender fields to match convention. |
|
168 | - $locale = $this->config->get('locale') ?: 'en_US'; |
|
169 | - $response = $this->apiRequest('me', 'GET', [ |
|
170 | - 'fields' => implode(',', $fields), |
|
171 | - 'locale' => $locale, |
|
172 | - ]); |
|
173 | - |
|
174 | - $data = new Data\Collection($response); |
|
175 | - |
|
176 | - if (!$data->exists('id')) { |
|
177 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
178 | - } |
|
179 | - |
|
180 | - $userProfile = new User\Profile(); |
|
181 | - |
|
182 | - $userProfile->identifier = $data->get('id'); |
|
183 | - $userProfile->displayName = $data->get('name'); |
|
184 | - $userProfile->firstName = $data->get('first_name'); |
|
185 | - $userProfile->lastName = $data->get('last_name'); |
|
186 | - $userProfile->profileURL = $data->get('link'); |
|
187 | - $userProfile->webSiteURL = $data->get('website'); |
|
188 | - $userProfile->gender = $data->get('gender'); |
|
189 | - $userProfile->language = $data->get('locale'); |
|
190 | - $userProfile->description = $data->get('about'); |
|
191 | - $userProfile->email = $data->get('email'); |
|
192 | - |
|
193 | - // Fallback for profile URL in case Facebook does not provide "pretty" link with username (if user set it). |
|
194 | - if (empty($userProfile->profileURL)) { |
|
195 | - $userProfile->profileURL = $this->getProfileUrl($userProfile->identifier); |
|
196 | - } |
|
197 | - |
|
198 | - $userProfile->region = $data->filter('hometown')->get('name'); |
|
199 | - |
|
200 | - $photoSize = $this->config->get('photo_size') ?: '150'; |
|
201 | - |
|
202 | - $userProfile->photoURL = $this->apiBaseUrl . $userProfile->identifier; |
|
203 | - $userProfile->photoURL .= '/picture?width=' . $photoSize . '&height=' . $photoSize; |
|
204 | - |
|
205 | - $userProfile->emailVerified = $userProfile->email; |
|
206 | - |
|
207 | - $userProfile = $this->fetchUserRegion($userProfile); |
|
208 | - |
|
209 | - $userProfile = $this->fetchBirthday($userProfile, $data->get('birthday')); |
|
210 | - |
|
211 | - return $userProfile; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Retrieve the user region. |
|
216 | - * |
|
217 | - * @param User\Profile $userProfile |
|
218 | - * |
|
219 | - * @return \Hybridauth\User\Profile |
|
220 | - */ |
|
221 | - protected function fetchUserRegion(User\Profile $userProfile) |
|
222 | - { |
|
223 | - if (!empty($userProfile->region)) { |
|
224 | - $regionArr = explode(',', $userProfile->region); |
|
225 | - |
|
226 | - if (count($regionArr) > 1) { |
|
227 | - $userProfile->city = trim($regionArr[0]); |
|
228 | - $userProfile->country = trim($regionArr[1]); |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - return $userProfile; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Retrieve the user birthday. |
|
237 | - * |
|
238 | - * @param User\Profile $userProfile |
|
239 | - * @param string $birthday |
|
240 | - * |
|
241 | - * @return \Hybridauth\User\Profile |
|
242 | - */ |
|
243 | - protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
244 | - { |
|
245 | - $result = (new Data\Parser())->parseBirthday($birthday); |
|
246 | - |
|
247 | - $userProfile->birthYear = (int)$result[0]; |
|
248 | - $userProfile->birthMonth = (int)$result[1]; |
|
249 | - $userProfile->birthDay = (int)$result[2]; |
|
250 | - |
|
251 | - return $userProfile; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * /v2.0/me/friends only returns the user's friends who also use the app. |
|
256 | - * In the cases where you want to let people tag their friends in stories published by your app, |
|
257 | - * you can use the Taggable Friends API. |
|
258 | - * |
|
259 | - * https://developers.facebook.com/docs/apps/faq#unable_full_friend_list |
|
260 | - */ |
|
261 | - public function getUserContacts() |
|
262 | - { |
|
263 | - $contacts = []; |
|
264 | - |
|
265 | - $apiUrl = 'me/friends?fields=link,name'; |
|
266 | - |
|
267 | - do { |
|
268 | - $response = $this->apiRequest($apiUrl); |
|
269 | - |
|
270 | - $data = new Data\Collection($response); |
|
271 | - |
|
272 | - if (!$data->exists('data')) { |
|
273 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
274 | - } |
|
275 | - |
|
276 | - if (!$data->filter('data')->isEmpty()) { |
|
277 | - foreach ($data->filter('data')->toArray() as $item) { |
|
278 | - $contacts[] = $this->fetchUserContact($item); |
|
279 | - } |
|
280 | - } |
|
281 | - |
|
282 | - if ($data->filter('paging')->exists('next')) { |
|
283 | - $apiUrl = $data->filter('paging')->get('next'); |
|
284 | - |
|
285 | - $pagedList = true; |
|
286 | - } else { |
|
287 | - $pagedList = false; |
|
288 | - } |
|
289 | - } while ($pagedList); |
|
290 | - |
|
291 | - return $contacts; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Parse the user contact. |
|
296 | - * |
|
297 | - * @param array $item |
|
298 | - * |
|
299 | - * @return \Hybridauth\User\Contact |
|
300 | - */ |
|
301 | - protected function fetchUserContact($item) |
|
302 | - { |
|
303 | - $userContact = new User\Contact(); |
|
304 | - |
|
305 | - $item = new Data\Collection($item); |
|
306 | - |
|
307 | - $userContact->identifier = $item->get('id'); |
|
308 | - $userContact->displayName = $item->get('name'); |
|
309 | - |
|
310 | - $userContact->profileURL = $item->exists('link') |
|
311 | - ?: $this->getProfileUrl($userContact->identifier); |
|
312 | - |
|
313 | - $userContact->photoURL = $this->apiBaseUrl . $userContact->identifier . '/picture?width=150&height=150'; |
|
314 | - |
|
315 | - return $userContact; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * {@inheritdoc} |
|
320 | - */ |
|
321 | - public function setPageStatus($status, $pageId) |
|
322 | - { |
|
323 | - $status = is_string($status) ? ['message' => $status] : $status; |
|
324 | - |
|
325 | - // Post on user wall. |
|
326 | - if ($pageId === 'me') { |
|
327 | - return $this->setUserStatus($status); |
|
328 | - } |
|
329 | - |
|
330 | - // Retrieve writable user pages and filter by given one. |
|
331 | - $pages = $this->getUserPages(true); |
|
332 | - $pages = array_filter($pages, function ($page) use ($pageId) { |
|
333 | - return $page->id == $pageId; |
|
334 | - }); |
|
335 | - |
|
336 | - if (!$pages) { |
|
337 | - throw new InvalidArgumentException('Could not find a page with given id.'); |
|
338 | - } |
|
339 | - |
|
340 | - $page = reset($pages); |
|
341 | - |
|
342 | - // Use page access token instead of user access token. |
|
343 | - $headers = [ |
|
344 | - 'Authorization' => 'Bearer ' . $page->access_token, |
|
345 | - ]; |
|
346 | - |
|
347 | - // Refresh proof for API call. |
|
348 | - $parameters = $status + [ |
|
349 | - 'appsecret_proof' => hash_hmac('sha256', $page->access_token, $this->clientSecret), |
|
350 | - ]; |
|
351 | - |
|
352 | - $response = $this->apiRequest("{$pageId}/feed", 'POST', $parameters, $headers); |
|
353 | - |
|
354 | - return $response; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * {@inheritdoc} |
|
359 | - */ |
|
360 | - public function getUserPages($writable = false) |
|
361 | - { |
|
362 | - $pages = $this->apiRequest('me/accounts'); |
|
363 | - |
|
364 | - if (!$writable) { |
|
365 | - return $pages->data; |
|
366 | - } |
|
367 | - |
|
368 | - // Filter user pages by CREATE_CONTENT permission. |
|
369 | - return array_filter($pages->data, function ($page) { |
|
370 | - return in_array('CREATE_CONTENT', $page->tasks); |
|
371 | - }); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * {@inheritdoc} |
|
376 | - */ |
|
377 | - public function getUserActivity($stream = 'me') |
|
378 | - { |
|
379 | - $apiUrl = $stream == 'me' ? 'me/feed' : 'me/home'; |
|
380 | - |
|
381 | - $response = $this->apiRequest($apiUrl); |
|
382 | - |
|
383 | - $data = new Data\Collection($response); |
|
384 | - |
|
385 | - if (!$data->exists('data')) { |
|
386 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
387 | - } |
|
388 | - |
|
389 | - $activities = []; |
|
390 | - |
|
391 | - foreach ($data->filter('data')->toArray() as $item) { |
|
392 | - $activities[] = $this->fetchUserActivity($item); |
|
393 | - } |
|
394 | - |
|
395 | - return $activities; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param $item |
|
400 | - * |
|
401 | - * @return User\Activity |
|
402 | - */ |
|
403 | - protected function fetchUserActivity($item) |
|
404 | - { |
|
405 | - $userActivity = new User\Activity(); |
|
406 | - |
|
407 | - $item = new Data\Collection($item); |
|
408 | - |
|
409 | - $userActivity->id = $item->get('id'); |
|
410 | - $userActivity->date = $item->get('created_time'); |
|
411 | - |
|
412 | - if ('video' == $item->get('type') || 'link' == $item->get('type')) { |
|
413 | - $userActivity->text = $item->get('link'); |
|
414 | - } |
|
415 | - |
|
416 | - if (empty($userActivity->text) && $item->exists('story')) { |
|
417 | - $userActivity->text = $item->get('link'); |
|
418 | - } |
|
419 | - |
|
420 | - if (empty($userActivity->text) && $item->exists('message')) { |
|
421 | - $userActivity->text = $item->get('message'); |
|
422 | - } |
|
423 | - |
|
424 | - if (!empty($userActivity->text) && $item->exists('from')) { |
|
425 | - $userActivity->user->identifier = $item->filter('from')->get('id'); |
|
426 | - $userActivity->user->displayName = $item->filter('from')->get('name'); |
|
427 | - |
|
428 | - $userActivity->user->profileURL = $this->getProfileUrl($userActivity->user->identifier); |
|
429 | - |
|
430 | - $userActivity->user->photoURL = $this->apiBaseUrl . $userActivity->user->identifier; |
|
431 | - $userActivity->user->photoURL .= '/picture?width=150&height=150'; |
|
432 | - } |
|
433 | - |
|
434 | - return $userActivity; |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Get profile URL. |
|
439 | - * |
|
440 | - * @param int $identity User ID. |
|
441 | - * @return string|null NULL when identity is not provided. |
|
442 | - */ |
|
443 | - protected function getProfileUrl($identity) |
|
444 | - { |
|
445 | - if (!is_numeric($identity)) { |
|
446 | - return null; |
|
447 | - } |
|
448 | - |
|
449 | - return sprintf($this->profileUrlTemplate, $identity); |
|
450 | - } |
|
159 | + if (strpos($this->scope, 'user_link') !== false) { |
|
160 | + $fields[] = 'link'; |
|
161 | + } |
|
162 | + |
|
163 | + if (strpos($this->scope, 'user_gender') !== false) { |
|
164 | + $fields[] = 'gender'; |
|
165 | + } |
|
166 | + |
|
167 | + // Note that en_US is needed for gender fields to match convention. |
|
168 | + $locale = $this->config->get('locale') ?: 'en_US'; |
|
169 | + $response = $this->apiRequest('me', 'GET', [ |
|
170 | + 'fields' => implode(',', $fields), |
|
171 | + 'locale' => $locale, |
|
172 | + ]); |
|
173 | + |
|
174 | + $data = new Data\Collection($response); |
|
175 | + |
|
176 | + if (!$data->exists('id')) { |
|
177 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
178 | + } |
|
179 | + |
|
180 | + $userProfile = new User\Profile(); |
|
181 | + |
|
182 | + $userProfile->identifier = $data->get('id'); |
|
183 | + $userProfile->displayName = $data->get('name'); |
|
184 | + $userProfile->firstName = $data->get('first_name'); |
|
185 | + $userProfile->lastName = $data->get('last_name'); |
|
186 | + $userProfile->profileURL = $data->get('link'); |
|
187 | + $userProfile->webSiteURL = $data->get('website'); |
|
188 | + $userProfile->gender = $data->get('gender'); |
|
189 | + $userProfile->language = $data->get('locale'); |
|
190 | + $userProfile->description = $data->get('about'); |
|
191 | + $userProfile->email = $data->get('email'); |
|
192 | + |
|
193 | + // Fallback for profile URL in case Facebook does not provide "pretty" link with username (if user set it). |
|
194 | + if (empty($userProfile->profileURL)) { |
|
195 | + $userProfile->profileURL = $this->getProfileUrl($userProfile->identifier); |
|
196 | + } |
|
197 | + |
|
198 | + $userProfile->region = $data->filter('hometown')->get('name'); |
|
199 | + |
|
200 | + $photoSize = $this->config->get('photo_size') ?: '150'; |
|
201 | + |
|
202 | + $userProfile->photoURL = $this->apiBaseUrl . $userProfile->identifier; |
|
203 | + $userProfile->photoURL .= '/picture?width=' . $photoSize . '&height=' . $photoSize; |
|
204 | + |
|
205 | + $userProfile->emailVerified = $userProfile->email; |
|
206 | + |
|
207 | + $userProfile = $this->fetchUserRegion($userProfile); |
|
208 | + |
|
209 | + $userProfile = $this->fetchBirthday($userProfile, $data->get('birthday')); |
|
210 | + |
|
211 | + return $userProfile; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Retrieve the user region. |
|
216 | + * |
|
217 | + * @param User\Profile $userProfile |
|
218 | + * |
|
219 | + * @return \Hybridauth\User\Profile |
|
220 | + */ |
|
221 | + protected function fetchUserRegion(User\Profile $userProfile) |
|
222 | + { |
|
223 | + if (!empty($userProfile->region)) { |
|
224 | + $regionArr = explode(',', $userProfile->region); |
|
225 | + |
|
226 | + if (count($regionArr) > 1) { |
|
227 | + $userProfile->city = trim($regionArr[0]); |
|
228 | + $userProfile->country = trim($regionArr[1]); |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + return $userProfile; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Retrieve the user birthday. |
|
237 | + * |
|
238 | + * @param User\Profile $userProfile |
|
239 | + * @param string $birthday |
|
240 | + * |
|
241 | + * @return \Hybridauth\User\Profile |
|
242 | + */ |
|
243 | + protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
244 | + { |
|
245 | + $result = (new Data\Parser())->parseBirthday($birthday); |
|
246 | + |
|
247 | + $userProfile->birthYear = (int)$result[0]; |
|
248 | + $userProfile->birthMonth = (int)$result[1]; |
|
249 | + $userProfile->birthDay = (int)$result[2]; |
|
250 | + |
|
251 | + return $userProfile; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * /v2.0/me/friends only returns the user's friends who also use the app. |
|
256 | + * In the cases where you want to let people tag their friends in stories published by your app, |
|
257 | + * you can use the Taggable Friends API. |
|
258 | + * |
|
259 | + * https://developers.facebook.com/docs/apps/faq#unable_full_friend_list |
|
260 | + */ |
|
261 | + public function getUserContacts() |
|
262 | + { |
|
263 | + $contacts = []; |
|
264 | + |
|
265 | + $apiUrl = 'me/friends?fields=link,name'; |
|
266 | + |
|
267 | + do { |
|
268 | + $response = $this->apiRequest($apiUrl); |
|
269 | + |
|
270 | + $data = new Data\Collection($response); |
|
271 | + |
|
272 | + if (!$data->exists('data')) { |
|
273 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
274 | + } |
|
275 | + |
|
276 | + if (!$data->filter('data')->isEmpty()) { |
|
277 | + foreach ($data->filter('data')->toArray() as $item) { |
|
278 | + $contacts[] = $this->fetchUserContact($item); |
|
279 | + } |
|
280 | + } |
|
281 | + |
|
282 | + if ($data->filter('paging')->exists('next')) { |
|
283 | + $apiUrl = $data->filter('paging')->get('next'); |
|
284 | + |
|
285 | + $pagedList = true; |
|
286 | + } else { |
|
287 | + $pagedList = false; |
|
288 | + } |
|
289 | + } while ($pagedList); |
|
290 | + |
|
291 | + return $contacts; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Parse the user contact. |
|
296 | + * |
|
297 | + * @param array $item |
|
298 | + * |
|
299 | + * @return \Hybridauth\User\Contact |
|
300 | + */ |
|
301 | + protected function fetchUserContact($item) |
|
302 | + { |
|
303 | + $userContact = new User\Contact(); |
|
304 | + |
|
305 | + $item = new Data\Collection($item); |
|
306 | + |
|
307 | + $userContact->identifier = $item->get('id'); |
|
308 | + $userContact->displayName = $item->get('name'); |
|
309 | + |
|
310 | + $userContact->profileURL = $item->exists('link') |
|
311 | + ?: $this->getProfileUrl($userContact->identifier); |
|
312 | + |
|
313 | + $userContact->photoURL = $this->apiBaseUrl . $userContact->identifier . '/picture?width=150&height=150'; |
|
314 | + |
|
315 | + return $userContact; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * {@inheritdoc} |
|
320 | + */ |
|
321 | + public function setPageStatus($status, $pageId) |
|
322 | + { |
|
323 | + $status = is_string($status) ? ['message' => $status] : $status; |
|
324 | + |
|
325 | + // Post on user wall. |
|
326 | + if ($pageId === 'me') { |
|
327 | + return $this->setUserStatus($status); |
|
328 | + } |
|
329 | + |
|
330 | + // Retrieve writable user pages and filter by given one. |
|
331 | + $pages = $this->getUserPages(true); |
|
332 | + $pages = array_filter($pages, function ($page) use ($pageId) { |
|
333 | + return $page->id == $pageId; |
|
334 | + }); |
|
335 | + |
|
336 | + if (!$pages) { |
|
337 | + throw new InvalidArgumentException('Could not find a page with given id.'); |
|
338 | + } |
|
339 | + |
|
340 | + $page = reset($pages); |
|
341 | + |
|
342 | + // Use page access token instead of user access token. |
|
343 | + $headers = [ |
|
344 | + 'Authorization' => 'Bearer ' . $page->access_token, |
|
345 | + ]; |
|
346 | + |
|
347 | + // Refresh proof for API call. |
|
348 | + $parameters = $status + [ |
|
349 | + 'appsecret_proof' => hash_hmac('sha256', $page->access_token, $this->clientSecret), |
|
350 | + ]; |
|
351 | + |
|
352 | + $response = $this->apiRequest("{$pageId}/feed", 'POST', $parameters, $headers); |
|
353 | + |
|
354 | + return $response; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * {@inheritdoc} |
|
359 | + */ |
|
360 | + public function getUserPages($writable = false) |
|
361 | + { |
|
362 | + $pages = $this->apiRequest('me/accounts'); |
|
363 | + |
|
364 | + if (!$writable) { |
|
365 | + return $pages->data; |
|
366 | + } |
|
367 | + |
|
368 | + // Filter user pages by CREATE_CONTENT permission. |
|
369 | + return array_filter($pages->data, function ($page) { |
|
370 | + return in_array('CREATE_CONTENT', $page->tasks); |
|
371 | + }); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * {@inheritdoc} |
|
376 | + */ |
|
377 | + public function getUserActivity($stream = 'me') |
|
378 | + { |
|
379 | + $apiUrl = $stream == 'me' ? 'me/feed' : 'me/home'; |
|
380 | + |
|
381 | + $response = $this->apiRequest($apiUrl); |
|
382 | + |
|
383 | + $data = new Data\Collection($response); |
|
384 | + |
|
385 | + if (!$data->exists('data')) { |
|
386 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
387 | + } |
|
388 | + |
|
389 | + $activities = []; |
|
390 | + |
|
391 | + foreach ($data->filter('data')->toArray() as $item) { |
|
392 | + $activities[] = $this->fetchUserActivity($item); |
|
393 | + } |
|
394 | + |
|
395 | + return $activities; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param $item |
|
400 | + * |
|
401 | + * @return User\Activity |
|
402 | + */ |
|
403 | + protected function fetchUserActivity($item) |
|
404 | + { |
|
405 | + $userActivity = new User\Activity(); |
|
406 | + |
|
407 | + $item = new Data\Collection($item); |
|
408 | + |
|
409 | + $userActivity->id = $item->get('id'); |
|
410 | + $userActivity->date = $item->get('created_time'); |
|
411 | + |
|
412 | + if ('video' == $item->get('type') || 'link' == $item->get('type')) { |
|
413 | + $userActivity->text = $item->get('link'); |
|
414 | + } |
|
415 | + |
|
416 | + if (empty($userActivity->text) && $item->exists('story')) { |
|
417 | + $userActivity->text = $item->get('link'); |
|
418 | + } |
|
419 | + |
|
420 | + if (empty($userActivity->text) && $item->exists('message')) { |
|
421 | + $userActivity->text = $item->get('message'); |
|
422 | + } |
|
423 | + |
|
424 | + if (!empty($userActivity->text) && $item->exists('from')) { |
|
425 | + $userActivity->user->identifier = $item->filter('from')->get('id'); |
|
426 | + $userActivity->user->displayName = $item->filter('from')->get('name'); |
|
427 | + |
|
428 | + $userActivity->user->profileURL = $this->getProfileUrl($userActivity->user->identifier); |
|
429 | + |
|
430 | + $userActivity->user->photoURL = $this->apiBaseUrl . $userActivity->user->identifier; |
|
431 | + $userActivity->user->photoURL .= '/picture?width=150&height=150'; |
|
432 | + } |
|
433 | + |
|
434 | + return $userActivity; |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Get profile URL. |
|
439 | + * |
|
440 | + * @param int $identity User ID. |
|
441 | + * @return string|null NULL when identity is not provided. |
|
442 | + */ |
|
443 | + protected function getProfileUrl($identity) |
|
444 | + { |
|
445 | + if (!is_numeric($identity)) { |
|
446 | + return null; |
|
447 | + } |
|
448 | + |
|
449 | + return sprintf($this->profileUrlTemplate, $identity); |
|
450 | + } |
|
451 | 451 | } |
@@ -17,77 +17,77 @@ |
||
17 | 17 | */ |
18 | 18 | class Spotify extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'user-read-email'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - public $apiBaseUrl = 'https://api.spotify.com/v1/'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - public $authorizeUrl = 'https://accounts.spotify.com/authorize'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://accounts.spotify.com/api/token'; |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - protected $apiDocumentation = 'https://developer.spotify.com/documentation/general/guides/authorization-guide/'; |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - public function getUserProfile() |
|
49 | - { |
|
50 | - $response = $this->apiRequest('me'); |
|
51 | - |
|
52 | - $data = new Data\Collection($response); |
|
53 | - |
|
54 | - if (!$data->exists('id')) { |
|
55 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
56 | - } |
|
57 | - |
|
58 | - $userProfile = new User\Profile(); |
|
59 | - |
|
60 | - $userProfile->identifier = $data->get('id'); |
|
61 | - $userProfile->displayName = $data->get('display_name'); |
|
62 | - $userProfile->email = $data->get('email'); |
|
63 | - $userProfile->emailVerified = $data->get('email'); |
|
64 | - $userProfile->profileURL = $data->filter('external_urls')->get('spotify'); |
|
65 | - $userProfile->photoURL = $data->filter('images')->get('url'); |
|
66 | - $userProfile->country = $data->get('country'); |
|
67 | - |
|
68 | - if ($data->exists('birthdate')) { |
|
69 | - $this->fetchBirthday($userProfile, $data->get('birthdate')); |
|
70 | - } |
|
71 | - |
|
72 | - return $userProfile; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Fetch use birthday |
|
77 | - * |
|
78 | - * @param User\Profile $userProfile |
|
79 | - * @param $birthday |
|
80 | - * |
|
81 | - * @return User\Profile |
|
82 | - */ |
|
83 | - protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
84 | - { |
|
85 | - $result = (new Data\Parser())->parseBirthday($birthday); |
|
86 | - |
|
87 | - $userProfile->birthDay = (int)$result[0]; |
|
88 | - $userProfile->birthMonth = (int)$result[1]; |
|
89 | - $userProfile->birthYear = (int)$result[2]; |
|
90 | - |
|
91 | - return $userProfile; |
|
92 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'user-read-email'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + public $apiBaseUrl = 'https://api.spotify.com/v1/'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + public $authorizeUrl = 'https://accounts.spotify.com/authorize'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://accounts.spotify.com/api/token'; |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + protected $apiDocumentation = 'https://developer.spotify.com/documentation/general/guides/authorization-guide/'; |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + public function getUserProfile() |
|
49 | + { |
|
50 | + $response = $this->apiRequest('me'); |
|
51 | + |
|
52 | + $data = new Data\Collection($response); |
|
53 | + |
|
54 | + if (!$data->exists('id')) { |
|
55 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
56 | + } |
|
57 | + |
|
58 | + $userProfile = new User\Profile(); |
|
59 | + |
|
60 | + $userProfile->identifier = $data->get('id'); |
|
61 | + $userProfile->displayName = $data->get('display_name'); |
|
62 | + $userProfile->email = $data->get('email'); |
|
63 | + $userProfile->emailVerified = $data->get('email'); |
|
64 | + $userProfile->profileURL = $data->filter('external_urls')->get('spotify'); |
|
65 | + $userProfile->photoURL = $data->filter('images')->get('url'); |
|
66 | + $userProfile->country = $data->get('country'); |
|
67 | + |
|
68 | + if ($data->exists('birthdate')) { |
|
69 | + $this->fetchBirthday($userProfile, $data->get('birthdate')); |
|
70 | + } |
|
71 | + |
|
72 | + return $userProfile; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Fetch use birthday |
|
77 | + * |
|
78 | + * @param User\Profile $userProfile |
|
79 | + * @param $birthday |
|
80 | + * |
|
81 | + * @return User\Profile |
|
82 | + */ |
|
83 | + protected function fetchBirthday(User\Profile $userProfile, $birthday) |
|
84 | + { |
|
85 | + $result = (new Data\Parser())->parseBirthday($birthday); |
|
86 | + |
|
87 | + $userProfile->birthDay = (int)$result[0]; |
|
88 | + $userProfile->birthMonth = (int)$result[1]; |
|
89 | + $userProfile->birthYear = (int)$result[2]; |
|
90 | + |
|
91 | + return $userProfile; |
|
92 | + } |
|
93 | 93 | } |
@@ -17,47 +17,47 @@ |
||
17 | 17 | */ |
18 | 18 | class Seznam extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $apiBaseUrl = 'https://login.szn.cz/'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $authorizeUrl = 'https://login.szn.cz/api/v1/oauth/auth'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $accessTokenUrl = 'https://login.szn.cz/api/v1/oauth/token'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $apiDocumentation = 'https://vyvojari.seznam.cz/oauth/doc'; |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - public function getUserProfile() |
|
44 | - { |
|
45 | - $response = $this->apiRequest('api/v1/user', 'GET', ['format' => 'json']); |
|
46 | - |
|
47 | - $data = new Data\Collection($response); |
|
48 | - |
|
49 | - if (!$data->exists('oauth_user_id')) { |
|
50 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
51 | - } |
|
52 | - |
|
53 | - $userProfile = new User\Profile(); |
|
54 | - |
|
55 | - $userProfile->identifier = $data->get('oauth_user_id'); |
|
56 | - $userProfile->email = $data->get('account_name'); |
|
57 | - $userProfile->firstName = $data->get('firstname'); |
|
58 | - $userProfile->lastName = $data->get('lastname'); |
|
59 | - $userProfile->photoURL = $data->get('avatar_url'); |
|
60 | - |
|
61 | - return $userProfile; |
|
62 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $apiBaseUrl = 'https://login.szn.cz/'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $authorizeUrl = 'https://login.szn.cz/api/v1/oauth/auth'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $accessTokenUrl = 'https://login.szn.cz/api/v1/oauth/token'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $apiDocumentation = 'https://vyvojari.seznam.cz/oauth/doc'; |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + public function getUserProfile() |
|
44 | + { |
|
45 | + $response = $this->apiRequest('api/v1/user', 'GET', ['format' => 'json']); |
|
46 | + |
|
47 | + $data = new Data\Collection($response); |
|
48 | + |
|
49 | + if (!$data->exists('oauth_user_id')) { |
|
50 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
51 | + } |
|
52 | + |
|
53 | + $userProfile = new User\Profile(); |
|
54 | + |
|
55 | + $userProfile->identifier = $data->get('oauth_user_id'); |
|
56 | + $userProfile->email = $data->get('account_name'); |
|
57 | + $userProfile->firstName = $data->get('firstname'); |
|
58 | + $userProfile->lastName = $data->get('lastname'); |
|
59 | + $userProfile->photoURL = $data->get('avatar_url'); |
|
60 | + |
|
61 | + return $userProfile; |
|
62 | + } |
|
63 | 63 | } |
@@ -15,104 +15,104 @@ |
||
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 | - * |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - public function parseBirthday($birthday) |
|
113 | - { |
|
114 | - $birthday = date_parse((string) $birthday); |
|
115 | - |
|
116 | - return [$birthday['year'], $birthday['month'], $birthday['day']]; |
|
117 | - } |
|
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 | + * |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + public function parseBirthday($birthday) |
|
113 | + { |
|
114 | + $birthday = date_parse((string) $birthday); |
|
115 | + |
|
116 | + return [$birthday['year'], $birthday['month'], $birthday['day']]; |
|
117 | + } |
|
118 | 118 | } |
@@ -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((string)$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((string)$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 | } |