Outlook.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Overtrue\Socialite\Providers;
  3. use Overtrue\Socialite\User;
  4. class Outlook extends Base
  5. {
  6. public const NAME = 'outlook';
  7. protected array $scopes = ['User.Read'];
  8. protected string $scopeSeparator = ' ';
  9. protected function getAuthUrl(): string
  10. {
  11. return $this->buildAuthUrlFromBase('https://login.microsoftonline.com/common/oauth2/v2.0/authorize');
  12. }
  13. protected function getTokenUrl(): string
  14. {
  15. return 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
  16. }
  17. /**
  18. * @param string $token
  19. * @param array|null $query
  20. *
  21. * @return array
  22. * @throws \GuzzleHttp\Exception\GuzzleException
  23. */
  24. protected function getUserByToken(string $token, ?array $query = []): array
  25. {
  26. $response = $this->getHttpClient()->get(
  27. 'https://graph.microsoft.com/v1.0/me',
  28. ['headers' => [
  29. 'Accept' => 'application/json',
  30. 'Authorization' => 'Bearer '.$token,
  31. ],
  32. ]
  33. );
  34. return \json_decode($response->getBody()->getContents(), true) ?? [];
  35. }
  36. /**
  37. * @param array $user
  38. *
  39. * @return \Overtrue\Socialite\User
  40. */
  41. protected function mapUserToObject(array $user): User
  42. {
  43. return new User([
  44. 'id' => $user['id'] ?? null,
  45. 'nickname' => null,
  46. 'name' => $user['displayName'] ?? null,
  47. 'email' => $user['userPrincipalName'] ?? null,
  48. 'avatar' => null,
  49. ]);
  50. }
  51. /**
  52. * @param string $code
  53. *
  54. * @return array|string[]
  55. */
  56. protected function getTokenFields(string $code): array
  57. {
  58. return parent::getTokenFields($code) + [
  59. 'grant_type' => 'authorization_code',
  60. ];
  61. }
  62. }