1 | <?php |
||
19 | class Authenticator |
||
20 | { |
||
21 | /** |
||
22 | * The application ID. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $appId; |
||
27 | |||
28 | /** |
||
29 | * The application secret. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $appSecret; |
||
34 | |||
35 | /** |
||
36 | * A storage to use to store data between requests. |
||
37 | * |
||
38 | * @var DataStorageInterface storage |
||
39 | */ |
||
40 | private $storage; |
||
41 | |||
42 | /** |
||
43 | * @var RequestManager |
||
44 | */ |
||
45 | private $requestManager; |
||
46 | |||
47 | /** |
||
48 | * @param RequestManager $requestManager |
||
49 | * @param string $appId |
||
50 | * @param string $appSecret |
||
51 | */ |
||
52 | public function __construct(RequestManager $requestManager, $appId, $appSecret) |
||
58 | |||
59 | /** |
||
60 | * Tries to get a new access token from data storage or code. If it fails, it will return null. |
||
61 | * |
||
62 | * @param UrlGeneratorInterface $urlGenerator |
||
63 | * |
||
64 | * @return AccessToken|null A valid user access token, or null if one could not be fetched. |
||
65 | * |
||
66 | * @throws LinkedInException |
||
67 | */ |
||
68 | public function fetchNewAccessToken(UrlGeneratorInterface $urlGenerator) |
||
95 | |||
96 | /** |
||
97 | * Retrieves an access token for the given authorization code |
||
98 | * (previously generated from www.linkedin.com on behalf of |
||
99 | * a specific user). The authorization code is sent to www.linkedin.com |
||
100 | * and a legitimate access token is generated provided the access token |
||
101 | * and the user for which it was generated all match, and the user is |
||
102 | * either logged in to LinkedIn or has granted an offline access permission. |
||
103 | * |
||
104 | * @param UrlGeneratorInterface $urlGenerator |
||
105 | * @param string $code An authorization code. |
||
106 | * |
||
107 | * @return AccessToken An access token exchanged for the authorization code. |
||
108 | * |
||
109 | * @throws LinkedInException |
||
110 | */ |
||
111 | protected function getAccessTokenFromCode(UrlGeneratorInterface $urlGenerator, $code) |
||
151 | |||
152 | /** |
||
153 | * Generate a login url. |
||
154 | * |
||
155 | * @param UrlGeneratorInterface $urlGenerator |
||
156 | * @param array $options |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getLoginUrl(UrlGeneratorInterface $urlGenerator, $options = array()) |
||
161 | { |
||
162 | // Generate a state |
||
163 | $this->establishCSRFTokenState(); |
||
164 | |||
165 | // Build request params |
||
166 | $requestParams = array_merge(array( |
||
167 | 'response_type' => 'code', |
||
168 | 'client_id' => $this->appId, |
||
169 | 'state' => $this->getStorage()->get('state'), |
||
170 | ), $options); |
||
171 | |||
172 | // Look for the redirect URL |
||
173 | if (isset($options['redirect_uri'])) { |
||
174 | $requestParams['redirect_uri'] = $options['redirect_uri']; |
||
175 | } else { |
||
176 | $requestParams['redirect_uri'] = $urlGenerator->getCurrentUrl(); |
||
177 | } |
||
178 | |||
179 | // Save the redirect url for later |
||
180 | $this->getStorage()->set('redirect_url', $requestParams['redirect_uri']); |
||
181 | |||
182 | // if 'scope' is passed as an array, convert to space separated list |
||
183 | $scopeParams = isset($options['scope']) ? $options['scope'] : null; |
||
184 | if ($scopeParams) { |
||
185 | //if scope is an array |
||
186 | if (is_array($scopeParams)) { |
||
187 | $requestParams['scope'] = implode(' ', $scopeParams); |
||
188 | } elseif (is_string($scopeParams)) { |
||
189 | //if scope is a string with ',' => make it to an array |
||
190 | $requestParams['scope'] = str_replace(',', ' ', $scopeParams); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return $urlGenerator->getUrl('www', 'uas/oauth2/authorization', $requestParams); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Get the authorization code from the query parameters, if it exists, |
||
199 | * and otherwise return null to signal no authorization code was |
||
200 | * discovered. |
||
201 | * |
||
202 | * @return string|null The authorization code, or null if the authorization code not exists. |
||
203 | * |
||
204 | * @throws LinkedInException on invalid CSRF tokens |
||
205 | */ |
||
206 | protected function getCode() |
||
239 | |||
240 | /** |
||
241 | * Lays down a CSRF state token for this process. |
||
242 | */ |
||
243 | protected function establishCSRFTokenState() |
||
250 | |||
251 | /** |
||
252 | * Clear the storage. |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function clearStorage() |
||
262 | |||
263 | /** |
||
264 | * @return DataStorageInterface |
||
265 | */ |
||
266 | protected function getStorage() |
||
274 | |||
275 | /** |
||
276 | * @param DataStorageInterface $storage |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setStorage(DataStorageInterface $storage) |
||
286 | |||
287 | /** |
||
288 | * @return RequestManager |
||
289 | */ |
||
290 | protected function getRequestManager() |
||
294 | } |
||
295 |