Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linkedin auth considering new apis #2030

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions src/OAuth/ResourceOwner/LinkedinResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ final class LinkedinResourceOwner extends GenericOAuth2ResourceOwner
* {@inheritdoc}
*/
protected array $paths = [
'identifier' => 'id',
'nickname' => 'emailAddress',
'firstname' => 'firstName',
'lastname' => 'lastName',
'email' => 'emailAddress',
'profilepicture' => 'profilePicture',
'identifier' => 'sub',
'nickname' => 'email',
'firstname' => 'given_name',
'lastname' => 'family_name',
'email' => 'email',
'profilepicture' => 'picture',
];

/**
Expand All @@ -41,22 +41,6 @@ public function getUserInformation(array $accessToken, array $extraParameters =
{
$response = parent::getUserInformation($accessToken, $extraParameters);

$responseData = $response->getData();
// The user info returned by /me doesn't contain the email so we make an extra request to fetch it
$content = $this->httpRequest(
$this->normalizeUrl($this->options['email_url'], $extraParameters),
null,
['Authorization' => 'Bearer '.$accessToken['access_token']]
);

$emailResponse = $this->getResponseContent($content);
if (isset($emailResponse['elements']) && \count($emailResponse['elements']) > 0) {
$responseData['emailAddress'] = $emailResponse['elements'][0]['handle~']['emailAddress'];
}
// errors not handled because I don't see any relevant thing to do with them

$response->setData($responseData);

return $response;
}

Expand Down Expand Up @@ -100,7 +84,7 @@ protected function configureOptions(OptionsResolver $resolver)
'scope' => 'r_liteprofile r_emailaddress',
'authorization_url' => 'https://www.linkedin.com/oauth/v2/authorization',
'access_token_url' => 'https://www.linkedin.com/oauth/v2/accessToken',
'infos_url' => 'https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))',
'infos_url' => 'https://api.linkedin.com/v2/userinfo',
'email_url' => 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))',

'user_response_class' => LinkedinUserResponse::class,
Expand Down
52 changes: 3 additions & 49 deletions src/OAuth/Response/LinkedinUserResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,23 @@ final class LinkedinUserResponse extends PathUserResponse
*/
public function getFirstName(): ?string
{
return $this->getPreferredLocaleValue('firstname');
return $this->getValueForPath('firstname');
}

/**
* {@inheritdoc}
*/
public function getLastName(): ?string
{
return $this->getPreferredLocaleValue('lastname');
return $this->getValueForPath('lastname');
}

/**
* {@inheritdoc}
*/
public function getProfilePicture(): ?string
{
// https://docs.microsoft.com/en-us/linkedin/shared/references/v2/profile/profile-picture
/** @var array<string, string|array<string, array<int, array<string, mixed>>>> $profilePicture */
$profilePicture = $this->getValueForPath('profilepicture');
if (
!\is_array($profilePicture)
|| !isset($profilePicture['displayImage~']['elements'])
|| 0 === \count($profilePicture['displayImage~']['elements'])
) {
return null;
}

$publicElements = array_filter($profilePicture['displayImage~']['elements'], static function ($element) {
return 'PUBLIC' === $element['authorizationMethod'];
});
if (0 === \count($publicElements)) {
return null;
}

// the last images seems to always be the one with the best quality, so we take this one
$element = array_values(\array_slice($publicElements, -1))[0];

return $element['identifiers'][0]['identifier'];
return $this->getValueForPath('profilepicture');
}

/**
* Helper to extract the preferred locale value from MultiLocaleString
* https://docs.microsoft.com/en-us/linkedin/shared/references/v2/object-types#multilocalestring.
*/
private function getPreferredLocaleValue(string $path): ?string
{
/** @var array<string, array<string, string|null>> $multiLocaleString */
$multiLocaleString = $this->getValueForPath($path);

$locale = '';
if (isset($multiLocaleString['preferredLocale'])) {
$locale = $multiLocaleString['preferredLocale']['language'];
if (!empty($multiLocaleString['preferredLocale']['country'])) {
$locale .= '_'.$multiLocaleString['preferredLocale']['country'];
}
}

if (isset($multiLocaleString['localized'][$locale])) {
return $multiLocaleString['localized'][$locale];
}

$fallbackLocale = array_keys($multiLocaleString['localized'])[0];

return $multiLocaleString['localized'][$fallbackLocale];
}
}
Loading