审查视图

vendor/overtrue/socialite/src/Providers/LinkedinProvider.php 5.0 KB
郭盛 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php

/*
 * This file is part of the overtrue/socialite.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Overtrue\Socialite\Providers;

use Overtrue\Socialite\AccessTokenInterface;
use Overtrue\Socialite\ProviderInterface;
use Overtrue\Socialite\User;

/**
 * Class LinkedinProvider.
 *
 * @see https://developer.linkedin.com/docs/oauth2 [Authenticating with OAuth 2.0]
 */
class LinkedinProvider extends AbstractProvider implements ProviderInterface
{
    /**
     * The scopes being requested.
     *
     * @var array
     */
30
    protected $scopes = ['r_liteprofile', 'r_emailaddress'];
郭盛 authored
31 32

    /**
33
     * {@inheritdoc}
郭盛 authored
34
     */
35 36 37 38
    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state);
    }
郭盛 authored
39 40

    /**
41 42 43 44 45
     * Get the access token for the given code.
     *
     * @param string $code
     *
     * @return \Overtrue\Socialite\AccessToken
郭盛 authored
46
     */
47
    public function getAccessToken($code)
郭盛 authored
48
    {
49 50 51 52
        $response = $this->getHttpClient()
            ->post($this->getTokenUrl(), ['form_params' => $this->getTokenFields($code)]);

        return $this->parseAccessToken($response->getBody());
郭盛 authored
53 54 55 56 57 58 59
    }

    /**
     * {@inheritdoc}
     */
    protected function getTokenUrl()
    {
60
        return 'https://www.linkedin.com/oauth/v2/accessToken';
郭盛 authored
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    }

    /**
     * Get the POST fields for the token request.
     *
     * @param string $code
     *
     * @return array
     */
    protected function getTokenFields($code)
    {
        return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
    }

    /**
     * {@inheritdoc}
     */
    protected function getUserByToken(AccessTokenInterface $token)
    {
80 81
        $basicProfile = $this->getBasicProfile($token);
        $emailAddress = $this->getEmailAddress($token);
郭盛 authored
82
83 84 85 86 87 88 89 90 91 92 93 94 95
        return array_merge($basicProfile, $emailAddress);
    }

    /**
     * Get the basic profile fields for the user.
     *
     * @param string $token
     *
     * @return array
     */
    protected function getBasicProfile($token)
    {
        $url = 'https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))';
郭盛 authored
96 97 98 99

        $response = $this->getHttpClient()->get($url, [
            'headers' => [
                'Authorization' => 'Bearer '.$token,
100
                'X-RestLi-Protocol-Version' => '2.0.0',
郭盛 authored
101 102 103
            ],
        ]);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        return (array) json_decode($response->getBody(), true);
    }

    /**
     * Get the email address for the user.
     *
     * @param string $token
     *
     * @return array
     */
    protected function getEmailAddress($token)
    {
        $url = 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))';

        $response = $this->getHttpClient()->get($url, [
            'headers' => [
                'Authorization' => 'Bearer '.$token,
                'X-RestLi-Protocol-Version' => '2.0.0',
            ],
        ]);

        return (array) $this->arrayItem(json_decode($response->getBody(), true), 'elements.0.handle~');
郭盛 authored
126 127 128 129 130 131 132
    }

    /**
     * {@inheritdoc}
     */
    protected function mapUserToObject(array $user)
    {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        $preferredLocale = $this->arrayItem($user, 'firstName.preferredLocale.language').'_'.$this->arrayItem($user, 'firstName.preferredLocale.country');
        $firstName = $this->arrayItem($user, 'firstName.localized.'.$preferredLocale);
        $lastName = $this->arrayItem($user, 'lastName.localized.'.$preferredLocale);
        $name = $firstName.' '.$lastName;

        $images = (array) $this->arrayItem($user, 'profilePicture.displayImage~.elements', []);
        $avatars = array_filter($images, function ($image) {
            return $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] === 100;
        });
        $avatar = array_shift($avatars);
        $originalAvatars = array_filter($images, function ($image) {
            return $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] === 800;
        });
        $originalAvatar = array_shift($originalAvatars);
郭盛 authored
148 149
        return new User([
            'id' => $this->arrayItem($user, 'id'),
150 151
            'nickname' => $name,
            'name' => $name,
郭盛 authored
152
            'email' => $this->arrayItem($user, 'emailAddress'),
153 154
            'avatar' => $avatar ? $this->arrayItem($avatar, 'identifiers.0.identifier') : null,
            'avatar_original' => $originalAvatar ? $this->arrayItem($originalAvatar, 'identifiers.0.identifier') : null,
郭盛 authored
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        ]);
    }

    /**
     * Set the user fields to request from LinkedIn.
     *
     * @param array $fields
     *
     * @return $this
     */
    public function fields(array $fields)
    {
        $this->fields = $fields;

        return $this;
    }
171 172 173 174 175 176 177 178 179 180

    /**
     * Determine if the provider is operating as stateless.
     *
     * @return bool
     */
    protected function isStateless()
    {
        return true;
    }
郭盛 authored
181
}