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

Sign in with LinkedIn is deprecated, here you have the new OpenId method #1054

Closed
adantart opened this issue Aug 21, 2023 · 5 comments
Closed
Labels

Comments

@adantart
Copy link

"Sign In with LinkedIn" is deprecated, use "Sign In with LinkedIn using OpenID Connect" instead

Linkedin changed its authentication method from Sign in with LinkedIn to Sign in with LinkedIn using OpenID Connect, since then the new app registered recently had this error.

So this changes has to be made on the LinkedIn Provider:

  1. in services/config, change the linkedin scopes from whatever you have (probable ['r_emailaddress', 'r_liteprofile']) to, at least, these ones:
['openid', 'profile', 'email']
  1. In LinkedInProvider.php file:
  • change the $scopes allowed
  • getByUserToken just need the basicProfile
  • getBasicProfile does not need extra PARAMS
  • getEmailAddress is not longer needed (it comes in the "userinfo" api method)
  • the mapUserToObject is now easier

My new LinkedInProvider file is now like this:

<?php

namespace Laravel\Socialite\Two;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

class LinkedInProvider extends AbstractProvider implements ProviderInterface
{
    /**
     * The scopes being requested.
     *
     * @var array
     */
    protected $scopes = ['openid', 'profile', 'email'];

    /**
     * The separating character for the requested scopes.
     *
     * @var string
     */
    protected $scopeSeparator = ' ';

    /**
     * {@inheritdoc}
     */
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state);
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
        return 'https://www.linkedin.com/oauth/v2/accessToken';
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken($token)
    {
        $basicProfile = $this->getBasicProfile($token);
        // $emailAddress = $this->getEmailAddress($token);

        return $basicProfile;
        return array_merge($basicProfile, $emailAddress);
    }

    /**
     * Get the basic profile fields for the user.
     *
     * @param  string  $token
     * @return array
     */
    protected function getBasicProfile($token)
    {
        $response = $this->getHttpClient()->get('https://api.linkedin.com/v2/userinfo', [
            RequestOptions::HEADERS => [
                'Authorization' => 'Bearer '.$token,
                'X-RestLi-Protocol-Version' => '2.0.0',
            ]            
        ]);
        return (array) json_decode($response->getBody(), true);
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {

        $preferredLocale = Arr::get($user, 'locale.language', []);
        $firstName = Arr::get($user, 'given_name', '');
        $lastName = Arr::get($user, 'family_name', '');
        $avatar = Arr::get($user, 'picture', []);
        $originalAvatar = $avatar;

        return (new User)->setRaw($user)->map([
            'id' => $user['sub'],
            'nickname' => null,
            'name' => $firstName.' '.$lastName,
            'first_name' => $firstName,
            'last_name' => $lastName,
            'email' => Arr::get($user, 'email'),
            'avatar' => $avatar,
            'avatar_original' => $originalAvatar,
        ]);
    }
}

Please, make the changes in the repository (after reviewing/updating/improving) so others can retrieve this new method.

Thank you !

P.D. Documentation here: https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2

@atymic
Copy link
Member

atymic commented Aug 21, 2023

Please create a PR with your changes.

@adantart
Copy link
Author

I don't know :-(

@stephenstack
Copy link

Same issue here, once I replace the code above, worked perfectly. If no issues, i'll try to get a PR in later.

@stephenstack
Copy link

PR added to main socialite repo laravel/socialite#658

@atymic atymic added the wontfix label Sep 11, 2023
@atymic
Copy link
Member

atymic commented Sep 11, 2023

If it doesn't get approved there feel free to PR here.

@atymic atymic closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants