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