141 lines
4.5 KiB
PHP
141 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MySocket;
|
|
|
|
use CodeIgniter\Config\Services;
|
|
|
|
class GoogleSocket extends MySocket
|
|
{
|
|
private $session;
|
|
private string $_access_token = "";
|
|
private string $_token_name = "access_token";
|
|
private string $_token_type = "";
|
|
private string $_google_oauth_version = "v3";
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->session = Services::session();
|
|
}
|
|
|
|
public function createAuthUrl(): string
|
|
{
|
|
$options = http_build_query([
|
|
'response_type' => 'code',
|
|
'client_id' => env('socket.google.client.id'),
|
|
'redirect_uri' => base_url(env('socket.google.client.callback_url')),
|
|
'scope' => "https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email",
|
|
'access_type' => 'offline',
|
|
'prompt' => 'consent'
|
|
]);
|
|
//기본적으로 검색할 범위를 지정하고 사용자를 Google OAuth 동의 화면으로 리디렉션합니다
|
|
return "https://accounts.google.com/o/oauth2/v2/auth?" . $options;
|
|
}
|
|
|
|
// (object) array(
|
|
// 'access_token' => 'sdfsdfsdfsdf',
|
|
// 'expires_in' => 3599,
|
|
// 'refresh_token' => 'sdfsdf',
|
|
// 'scope' => 'https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email',
|
|
// 'token_type' => 'Bearer',
|
|
// 'id_token' => 'fadfasdfsadf',
|
|
// )
|
|
public function setToken(string $access_code): void
|
|
{
|
|
$options = [
|
|
'code' => $access_code,
|
|
'client_id' => env('socket.google.client.id'),
|
|
'client_secret' => env('socket.google.client.key'),
|
|
'redirect_uri' => base_url(env('socket.google.client.callback_url')),
|
|
'grant_type' => 'authorization_code',
|
|
];
|
|
$response = $this->post("https://accounts.google.com/o/oauth2/token", $options);
|
|
$tokenInfo = json_decode($response->getBody(), true);
|
|
if (isset($tokenInfo['error'])) {
|
|
$message = sprintf(
|
|
"Google: %s에서 API 호출 실패: \n--request options--\n%s\n--response--\n%s\n",
|
|
__FUNCTION__,
|
|
var_export($options, true),
|
|
var_export($response, true)
|
|
);
|
|
log_message("error", $message);
|
|
throw new \Exception($message);
|
|
}
|
|
if (!isset($tokenInfo[$this->_token_name]) || empty($tokenInfo[$this->_token_name])) {
|
|
$message = sprintf(
|
|
"Google: Token 정보가 없습니다.\n--tokenInfo--\n%s\n",
|
|
__FUNCTION__,
|
|
var_export($tokenInfo, true)
|
|
);
|
|
log_message("error", $message);
|
|
throw new \Exception($message);
|
|
}
|
|
//토큰 Type정보 가져오기 getUserInfo()에서 사용
|
|
$this->_token_type = $tokenInfo['token_type'];
|
|
// 토큰 정보 가져오기
|
|
$this->_access_token = $tokenInfo[$this->_token_name];
|
|
// 세션에 Token 값 설정
|
|
$this->session->set($this->_token_name, $this->_access_token);
|
|
}
|
|
|
|
public function getToken(): string
|
|
{
|
|
return $this->session->get($this->_token_name);
|
|
}
|
|
|
|
// throw new \Exception(__METHOD__ . "에서 데이터 처리 필요");
|
|
// DEBUG - 2023-07-13 12:54:51 --> \Google\Service\Oauth2\Userinfo::__set_state(array(
|
|
// 'internal_gapi_mappings' =>
|
|
// 'familyName' => 'family_name',
|
|
// 'givenName' => 'given_name',
|
|
// 'verifiedEmail' => 'verified_email',
|
|
// ),
|
|
// 'modelData' =>
|
|
// array (
|
|
// 'verified_email' => true,
|
|
// 'given_name' => '이름',
|
|
// 'family_name' => '성',
|
|
// ),
|
|
// 'processed' =>
|
|
// array (
|
|
// ),
|
|
// 'email' => 'twsdfsew342s@gmail.com',
|
|
// 'familyName' => '성',
|
|
// 'gender' => NULL,
|
|
// 'givenName' => '이름',
|
|
// 'hd' => NULL,
|
|
// 'id' => '103667492432234234236838324',
|
|
// 'link' => NULL,
|
|
// 'locale' => 'ko',
|
|
// 'name' => '성이름',
|
|
// 'picture' => 'https://lh3.googleusercontent.com/a/AAcHTteFSgefsdfsdRJBkJA2tBEmg4PQrvI1Ta_5IXu5=s96-c',
|
|
// 'verifiedEmail' => true,
|
|
// ))
|
|
public function getUserInfo(): array
|
|
{
|
|
$options = ["Authorization: {$this->_token_type} {$this->getToken()}"];
|
|
$response = $this->get("https://www.googleapis.com/oauth2/{$this->_google_oauth_version}/userinfo", $options);
|
|
$userInfo = json_decode($response->getBody(), true);
|
|
if (isset($userInfo['error'])) {
|
|
$message = sprintf(
|
|
"Google: %s에서 API 호출 실패: \n--request options--\n%s\n--response--\n%s\n",
|
|
__FUNCTION__,
|
|
var_export($options, true),
|
|
var_export($response, true)
|
|
);
|
|
log_message("error", $message);
|
|
throw new \Exception($message);
|
|
}
|
|
if (isset($userInfo['email']) || empty($tokenInfo['email'])) {
|
|
$message = sprintf(
|
|
"Google: User 정보가 없습니다.\n--userInfo--\n%s\n",
|
|
__FUNCTION__,
|
|
var_export($userInfo, true)
|
|
);
|
|
log_message("error", $message);
|
|
throw new \Exception($message);
|
|
}
|
|
// 사용자정보 가져오기
|
|
return $userInfo;
|
|
}
|
|
}
|