LinkedinProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite\Providers;
  11. use Overtrue\Socialite\AccessTokenInterface;
  12. use Overtrue\Socialite\ProviderInterface;
  13. use Overtrue\Socialite\User;
  14. /**
  15. * Class LinkedinProvider.
  16. *
  17. * @see https://developer.linkedin.com/docs/oauth2 [Authenticating with OAuth 2.0]
  18. */
  19. class LinkedinProvider extends AbstractProvider implements ProviderInterface
  20. {
  21. /**
  22. * The scopes being requested.
  23. *
  24. * @var array
  25. */
  26. protected $scopes = ['r_basicprofile', 'r_emailaddress'];
  27. /**
  28. * The fields that are included in the profile.
  29. *
  30. * @var array
  31. */
  32. protected $fields = [
  33. 'id', 'first-name', 'last-name', 'formatted-name',
  34. 'email-address', 'headline', 'location', 'industry',
  35. 'public-profile-url', 'picture-url', 'picture-urls::(original)',
  36. ];
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function getAuthUrl($state)
  41. {
  42. return $this->buildAuthUrlFromBase('https://www.linkedin.com/uas/oauth2/authorization', $state);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getTokenUrl()
  48. {
  49. return 'https://www.linkedin.com/uas/oauth2/accessToken';
  50. }
  51. /**
  52. * Get the POST fields for the token request.
  53. *
  54. * @param string $code
  55. *
  56. * @return array
  57. */
  58. protected function getTokenFields($code)
  59. {
  60. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function getUserByToken(AccessTokenInterface $token)
  66. {
  67. $fields = implode(',', $this->fields);
  68. $url = 'https://api.linkedin.com/v1/people/~:('.$fields.')';
  69. $response = $this->getHttpClient()->get($url, [
  70. 'headers' => [
  71. 'x-li-format' => 'json',
  72. 'Authorization' => 'Bearer '.$token,
  73. ],
  74. ]);
  75. return json_decode($response->getBody(), true);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function mapUserToObject(array $user)
  81. {
  82. return new User([
  83. 'id' => $this->arrayItem($user, 'id'),
  84. 'nickname' => $this->arrayItem($user, 'formattedName'),
  85. 'name' => $this->arrayItem($user, 'formattedName'),
  86. 'email' => $this->arrayItem($user, 'emailAddress'),
  87. 'avatar' => $this->arrayItem($user, 'pictureUrl'),
  88. 'avatar_original' => $this->arrayItem($user, 'pictureUrls.values.0'),
  89. ]);
  90. }
  91. /**
  92. * Set the user fields to request from LinkedIn.
  93. *
  94. * @param array $fields
  95. *
  96. * @return $this
  97. */
  98. public function fields(array $fields)
  99. {
  100. $this->fields = $fields;
  101. return $this;
  102. }
  103. }