OutlookProvider.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 OutlookProvider.
  16. */
  17. class OutlookProvider extends AbstractProvider implements ProviderInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected $scopes = ['User.Read'];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected $scopeSeparator = ' ';
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function getAuthUrl($state)
  31. {
  32. return $this->buildAuthUrlFromBase('https://login.microsoftonline.com/common/oauth2/v2.0/authorize', $state);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function getTokenUrl()
  38. {
  39. return 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function getUserByToken(AccessTokenInterface $token)
  45. {
  46. $response = $this->getHttpClient()->get(
  47. 'https://graph.microsoft.com/v1.0/me',
  48. ['headers' => [
  49. 'Accept' => 'application/json',
  50. 'Authorization' => 'Bearer '.$token->getToken(),
  51. ],
  52. ]
  53. );
  54. return json_decode($response->getBody()->getContents(), true);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function mapUserToObject(array $user)
  60. {
  61. return new User([
  62. 'id' => $this->arrayItem($user, 'id'),
  63. 'nickname' => null,
  64. 'name' => $this->arrayItem($user, 'displayName'),
  65. 'email' => $this->arrayItem($user, 'userPrincipalName'),
  66. 'avatar' => null,
  67. ]);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function getTokenFields($code)
  73. {
  74. return array_merge(parent::getTokenFields($code), [
  75. 'grant_type' => 'authorization_code',
  76. ]);
  77. }
  78. }