DaboryProvider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Providers\Socialite\OAuth2;
  3. use Illuminate\Support\Arr;
  4. use SocialiteProviders\Manager\OAuth2\AbstractProvider;
  5. use SocialiteProviders\Manager\OAuth2\User;
  6. class DaboryProvider extends AbstractProvider
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. protected function getAuthUrl($state)
  12. {
  13. return $this->buildAuthUrlFromBase('https://accounts.dabory.com/o/oauth2/authorize', $state);
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function getTokenUrl()
  19. {
  20. return 'https://accounts.dabory.com/o/oauth2/token';
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getAccessTokenResponse($code)
  26. {
  27. $response = $this->getHttpClient()->post($this->getTokenUrl(), [
  28. 'headers' => ['Authorization' => 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret)],
  29. 'form_params' => $this->getTokenFields($code),
  30. ]);
  31. $data = json_decode($response->getBody(), true);
  32. return Arr::add($data, 'expires_in', Arr::pull($data, 'expires'));
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function getTokenFields($code)
  38. {
  39. return Arr::add(
  40. parent::getTokenFields($code), 'grant_type', 'authorization_code'
  41. );
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function getUserByToken($token)
  47. {
  48. $response = $this->getHttpClient()->get('https://accounts.dabory.com/o/oauth2/userinfo', [
  49. 'headers' => [ 'Authorization' => 'Bearer '.$token ],
  50. ]);
  51. return json_decode($response->getBody(), true);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function mapUserToObject(array $user)
  57. {
  58. return (new User)->setRaw($user)->map([
  59. 'client_id' => $user['client_id'],
  60. 'expires_in' => $user['expires_in'],
  61. 'id' => $user['sso_sub'],
  62. 'email' => $user['user_id'] ?? null,
  63. ]);
  64. }
  65. }