1 | <?php |
||
13 | class Authenticator |
||
14 | { |
||
15 | /** |
||
16 | * The Application ID. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $appId; |
||
21 | |||
22 | /** |
||
23 | * The Application App Secret. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $appSecret; |
||
28 | |||
29 | /** |
||
30 | * A CSRF state variable to assist in the defense against CSRF attacks. |
||
31 | */ |
||
32 | protected $state; |
||
33 | |||
34 | /** |
||
35 | * @var DataStorageInterface storage |
||
36 | */ |
||
37 | private $storage; |
||
38 | |||
39 | /** |
||
40 | * @var RequestManager |
||
41 | */ |
||
42 | private $requestManager; |
||
43 | |||
44 | /** |
||
45 | * @param RequestManager $requestManager |
||
46 | * @param string $appId |
||
47 | * @param string $appSecret |
||
48 | */ |
||
49 | public function __construct(RequestManager $requestManager, $appId, $appSecret) |
||
55 | |||
56 | /** |
||
57 | * Determines and returns the user access token using the authorization code. The intent is to |
||
58 | * return a valid access token, or null if one is determined to not be available. |
||
59 | * |
||
60 | * @param UrlGeneratorInterface $urlGenerator |
||
61 | * |
||
62 | * @return AccessToken|null A valid user access token, or null if one could not be determined. |
||
63 | * |
||
64 | * @throws LinkedInApiException |
||
65 | */ |
||
66 | public function fetchNewAccessToken(UrlGeneratorInterface $urlGenerator) |
||
91 | |||
92 | /** |
||
93 | * Retrieves an access token for the given authorization code |
||
94 | * (previously generated from www.linkedin.com on behalf of |
||
95 | * a specific user). The authorization code is sent to www.linkedin.com |
||
96 | * and a legitimate access token is generated provided the access token |
||
97 | * and the user for which it was generated all match, and the user is |
||
98 | * either logged in to LinkedIn or has granted an offline access permission. |
||
99 | * |
||
100 | * @param UrlGeneratorInterface $urlGenerator |
||
101 | * @param string $code An authorization code. |
||
102 | * |
||
103 | * @return AccessToken|null An access token exchanged for the authorization code, or |
||
104 | * null if an access token could not be generated. |
||
105 | */ |
||
106 | protected function getAccessTokenFromCode(UrlGeneratorInterface $urlGenerator, $code) |
||
107 | { |
||
108 | if (empty($code)) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | $redirectUri = $this->getStorage()->get('redirect_url'); |
||
113 | try { |
||
114 | $url = $urlGenerator->getUrl('www', 'uas/oauth2/accessToken'); |
||
115 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
||
116 | $body = http_build_query( |
||
117 | [ |
||
118 | 'grant_type' => 'authorization_code', |
||
119 | 'code' => $code, |
||
120 | 'redirect_uri' => $redirectUri, |
||
121 | 'client_id' => $this->appId, |
||
122 | 'client_secret' => $this->appSecret, |
||
123 | ] |
||
124 | ); |
||
125 | |||
126 | $response = ResponseConverter::convertToArray($this->getRequestManager()->sendRequest('POST', $url, $headers, $body)); |
||
|
|||
127 | } catch (LinkedInApiException $e) { |
||
128 | // most likely that user very recently revoked authorization. |
||
129 | // In any event, we don't have an access token, so say so. |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | if (empty($response)) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $tokenData = array_merge(array('access_token' => null, 'expires_in' => null), $response); |
||
138 | $token = new AccessToken($tokenData['access_token'], $tokenData['expires_in']); |
||
139 | |||
140 | if (!$token->hasToken()) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | return $token; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get a Login URL for use with redirects. By default, full page redirect is |
||
149 | * assumed. If you are using the generated URL with a window.open() call in |
||
150 | * JavaScript, you can pass in display=popup as part of the $params. |
||
151 | * |
||
152 | * The parameters: |
||
153 | * - redirect_uri: the url to go to after a successful login |
||
154 | * - scope: comma (or space) separated list of requested extended permissions |
||
155 | * |
||
156 | * @param UrlGeneratorInterface $urlGenerator |
||
157 | * @param array $options Provide custom parameters |
||
158 | * |
||
159 | * @return string The URL for the login flow |
||
160 | */ |
||
161 | public function getLoginUrl(UrlGeneratorInterface $urlGenerator, $options = array()) |
||
201 | |||
202 | /** |
||
203 | * Get the authorization code from the query parameters, if it exists, |
||
204 | * and otherwise return null to signal no authorization code was |
||
205 | * discoverable. |
||
206 | * |
||
207 | * @return string|null The authorization code, or null if the authorization code could not be determined. |
||
208 | * |
||
209 | * @throws LinkedInApiException |
||
210 | */ |
||
211 | protected function getCode() |
||
245 | |||
246 | /** |
||
247 | * Lays down a CSRF state token for this process. |
||
248 | */ |
||
249 | protected function establishCSRFTokenState() |
||
256 | |||
257 | /** |
||
258 | * Clear the storage. |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function clearStorage() |
||
268 | |||
269 | /** |
||
270 | * Get the state, use this to verify the CSRF token. |
||
271 | * |
||
272 | * |
||
273 | * @return string|null |
||
274 | */ |
||
275 | protected function getState() |
||
283 | |||
284 | /** |
||
285 | * @param $state |
||
286 | * |
||
287 | * @return $this |
||
288 | */ |
||
289 | protected function setState($state) |
||
295 | |||
296 | /** |
||
297 | * @return DataStorageInterface |
||
298 | */ |
||
299 | protected function getStorage() |
||
307 | |||
308 | /** |
||
309 | * @param DataStorageInterface $storage |
||
310 | * |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function setStorage(DataStorageInterface $storage) |
||
319 | |||
320 | /** |
||
321 | * @return RequestManager |
||
322 | */ |
||
323 | protected function getRequestManager() |
||
327 | } |
||
328 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: