1 | <?php |
||
18 | class Authenticator implements AuthenticatorInterface |
||
19 | { |
||
20 | /** |
||
21 | * The application ID. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $appId; |
||
26 | |||
27 | /** |
||
28 | * The application secret. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $appSecret; |
||
33 | |||
34 | /** |
||
35 | * A storage to use to store data between requests. |
||
36 | * |
||
37 | * @var DataStorageInterface storage |
||
38 | */ |
||
39 | private $storage; |
||
40 | |||
41 | /** |
||
42 | * @var RequestManagerInterface |
||
43 | */ |
||
44 | private $requestManager; |
||
45 | |||
46 | /** |
||
47 | * @param RequestManagerInterface $requestManager |
||
48 | * @param string $appId |
||
49 | * @param string $appSecret |
||
50 | */ |
||
51 | 14 | public function __construct(RequestManagerInterface $requestManager, $appId, $appSecret) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 3 | public function fetchNewAccessToken(LinkedInUrlGeneratorInterface $urlGenerator) |
|
88 | |||
89 | /** |
||
90 | * Retrieves an access token for the given authorization code |
||
91 | * (previously generated from www.linkedin.com on behalf of |
||
92 | * a specific user). The authorization code is sent to www.linkedin.com |
||
93 | * and a legitimate access token is generated provided the access token |
||
94 | * and the user for which it was generated all match, and the user is |
||
95 | * either logged in to LinkedIn or has granted an offline access permission. |
||
96 | * |
||
97 | * @param LinkedInUrlGeneratorInterface $urlGenerator |
||
98 | * @param string $code An authorization code. |
||
99 | * |
||
100 | * @return AccessToken An access token exchanged for the authorization code. |
||
101 | * |
||
102 | * @throws LinkedInException |
||
103 | */ |
||
104 | 6 | protected function getAccessTokenFromCode(LinkedInUrlGeneratorInterface $urlGenerator, $code) |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 1 | public function getLoginUrl(LinkedInUrlGeneratorInterface $urlGenerator, $options = []) |
|
149 | { |
||
150 | // Generate a state |
||
151 | 1 | $this->establishCSRFTokenState(isset($options['state'])?$options['state']:null); |
|
152 | |||
153 | // Build request params |
||
154 | 1 | $requestParams = array_merge([ |
|
155 | 1 | 'response_type' => 'code', |
|
156 | 1 | 'client_id' => $this->appId, |
|
157 | 1 | 'state' => $this->getStorage()->get('state'), |
|
158 | 1 | 'redirect_uri' => null, |
|
159 | 1 | ], $options); |
|
160 | |||
161 | // Save the redirect url for later |
||
162 | 1 | $this->getStorage()->set('redirect_uri', $requestParams['redirect_uri']); |
|
163 | |||
164 | // if 'scope' is passed as an array, convert to space separated list |
||
165 | 1 | $scopeParams = isset($options['scope']) ? $options['scope'] : null; |
|
166 | 1 | if ($scopeParams) { |
|
167 | //if scope is an array |
||
168 | 1 | if (is_array($scopeParams)) { |
|
169 | 1 | $requestParams['scope'] = implode(' ', $scopeParams); |
|
170 | 1 | } elseif (is_string($scopeParams)) { |
|
171 | //if scope is a string with ',' => make it to an array |
||
172 | $requestParams['scope'] = str_replace(',', ' ', $scopeParams); |
||
173 | } |
||
174 | 1 | } |
|
175 | |||
176 | 1 | return $urlGenerator->getUrl('www', 'oauth/v2/authorization', $requestParams); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get the authorization code from the query parameters, if it exists, |
||
181 | * and otherwise return null to signal no authorization code was |
||
182 | * discovered. |
||
183 | * |
||
184 | * @return string|null The authorization code, or null if the authorization code not exists. |
||
185 | * |
||
186 | * @throws LinkedInException on invalid CSRF tokens |
||
187 | */ |
||
188 | 4 | protected function getCode() |
|
221 | |||
222 | /** |
||
223 | * Lays down a CSRF state token for this process. |
||
224 | */ |
||
225 | 1 | protected function establishCSRFTokenState($predefinedState = null) |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function clearStorage() |
||
245 | |||
246 | /** |
||
247 | * @return DataStorageInterface |
||
248 | */ |
||
249 | 2 | protected function getStorage() |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | public function setStorage(DataStorageInterface $storage) |
|
267 | |||
268 | /** |
||
269 | * @return RequestManager |
||
270 | */ |
||
271 | 3 | protected function getRequestManager() |
|
275 | } |
||
276 |