SocialTalkController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Themes\kbgolf\pro\app\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. use Illuminate\Support\Facades\Redirect;
  6. use SocialiteProviders\Apple\Provider;
  7. use Laravel\Socialite\Facades\Socialite;
  8. use Laravel\Socialite\Two\GoogleProvider;
  9. use Laravel\Socialite\Two\FacebookProvider;
  10. use SocialiteProviders\Kakao\KakaoProvider;
  11. use App\Providers\Socialite\OAuth2\DaboryProvider;
  12. use App\Services\AppleTokenService;
  13. use SocialiteProviders\Naver\Provider as NaverProvider;
  14. use Illuminate\Http\Request;
  15. class SocialTalkController extends Controller
  16. {
  17. private $appleTokenService;
  18. private $callApiService;
  19. private $oauth2Info;
  20. private $target;
  21. public function __construct(CallApiService $callApiService, AppleTokenService $appleTokenService)
  22. {
  23. $this->callApiService = $callApiService;
  24. $this->appleTokenService = $appleTokenService;
  25. }
  26. public function redirectToProvider($provider)
  27. {
  28. $this->oauth2Info = request('oauth2Info');
  29. $brandCode = 'member-'.$provider;
  30. $response = $this->callApiService->callApi([
  31. 'url' => 'setup-page',
  32. 'data' => [
  33. 'PageVars' => [
  34. 'Query' => "(setup_code = 'sso-client' and brand_code = '{$brandCode}') and is_on_use = '1'",
  35. 'Limit' => 100
  36. ]
  37. ],
  38. ]);
  39. if (!empty($response['Page'])) {
  40. $setupJson = $response['Page'][0]['SetupJson'];
  41. $setupArray = json_decode($setupJson, true);
  42. $redirectUri = route('social.login', ['provider' => $brandCode]);
  43. if (json_last_error() === JSON_ERROR_NONE) {
  44. $clientId = $setupArray['ClientId'] ?? null;
  45. // dd($clientId);
  46. }
  47. }
  48. if ($provider == 'naver') {
  49. $url = 'https://nid.naver.com/oauth2.0/authorize?' . http_build_query([
  50. 'response_type' => 'code',
  51. 'client_id' => $clientId,
  52. 'redirect_uri' => $redirectUri,
  53. 'state' => csrf_token(),
  54. ]);
  55. } elseif ($provider == 'kakao') {
  56. $url = 'https://kauth.kakao.com/oauth/authorize?' . http_build_query([
  57. 'response_type' => 'code',
  58. 'client_id' => $clientId,
  59. 'redirect_uri' => $redirectUri,
  60. 'state' => csrf_token(),
  61. ]);
  62. }
  63. return redirect($url);
  64. }
  65. public function login($provider)
  66. {
  67. [$this->target, $ssoBrand] = explode('-', $provider);
  68. if ($this->target == 'member') {
  69. if ($ssoBrand == 'naver') {
  70. return Redirect::away('https://talk.naver.com/ct/w5u3g9');
  71. }elseif($ssoBrand == 'kakao'){
  72. return Redirect::away('https://pf.kakao.com/_aAAGj/chat');
  73. }
  74. }
  75. }
  76. //사용안함
  77. public function setConfig($provider, $target)
  78. {
  79. $config = [
  80. 'client_id' => $this->oauth2Info['ClientId'],
  81. 'client_secret' => $this->oauth2Info['ClientSecret'],
  82. 'redirect' => "/social/$target-$provider/callback"
  83. ];
  84. switch ($provider) {
  85. case 'dabory':
  86. $driver = Socialite::buildProvider(DaboryProvider::class, [
  87. 'client_id' => config('app.api.main.ClientId'),
  88. 'client_secret' => config('app.api.main.ClientSecret'),
  89. 'redirect' => $config['redirect']
  90. ]);
  91. break;
  92. case 'facebook':
  93. $driver = Socialite::buildProvider(FacebookProvider::class, $config);
  94. break;
  95. case 'kakao':
  96. $driver = Socialite::buildProvider(KakaoProvider::class, $config);
  97. break;
  98. case 'google':
  99. $driver = Socialite::buildProvider(GoogleProvider::class, $config);
  100. break;
  101. case 'naver':
  102. $driver = Socialite::buildProvider(NaverProvider::class, $config);
  103. break;
  104. case 'apple':
  105. $config['client_secret'] = $this->appleTokenService->generate(
  106. $this->oauth2Info['PrivateKey'],
  107. $this->oauth2Info['ClientId'],
  108. $this->oauth2Info['TeamId'],
  109. $this->oauth2Info['KeyId'],
  110. );
  111. $driver = Socialite::buildProvider(Provider::class, $config);
  112. break;
  113. }
  114. return $driver;
  115. }
  116. }