728x90
반응형

출처 : https://github.com/rawady/NaverLogin-for-PHP/blob/master/php/Naver.php

rawady/NaverLogin-for-PHP
NaverLogin-for-PHP - [PHP] 네이버 아이디로 로그인-웹 라이브러리
github.com
본문으로 이동

 

<?
/**
* Naver 로그인 Api Class 0.06
* class : NaverAPI
* Author : Rawady corp. Jung Jintae
* date : 2014.5.11
* https://github.com/rawady/NaverLogin
! required PHP 5.x Higher
! required curl enable
*
* 본 클래스는 네이버 공식 라이브러리가 아닙니다.
* NHN API Reference : http://developer.naver.com/wiki/pages/NaverLogin_Web
The MIT License (MIT)
Copyright (c) 2014 Jung Jintae
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
0.06 변경
- ssl 오류 수정
- 일부 치명적인 오류 수정
0.4 포함됨
- 인증 요청
- 엑세스토큰 획득
- 사용자 정보 취득
- 로그아웃
*/
define( NAVER_OAUTH_URL, "https://nid.naver.com/oauth2.0/" );
define( NAVER_SESSION_NAME, "NHN_SESSION" );
@session_start();
class Naver{
private $tokenDatas = array();
private $access_token = ''; // oauth 엑세스 토큰
private $refresh_token = ''; // oauth 갱신 토큰
private $access_token_type = ''; // oauth 토큰 타입
private $access_token_expire = ''; // oauth 토큰 만료
private $client_id = ''; // 네이버에서 발급받은 클라이언트 아이디
private $client_secret = ''; // 네이버에서 발급받은 클라이언트 시크릿키
private $returnURL = ''; // 콜백 받을 URL ( 네이버에 등록된 콜백 URI가 우선됨)
private $state = ''; // 네이버 명세에 필요한 검증 키 (현재 버전 라이브러리에서 미검증)
private $loginMode = 'request'; // 라이브러리 작동 상태
private $returnCode = ''; // 네이버에서 리턴 받은 승인 코드
private $returnState = ''; // 네이버에서 리턴 받은 검증 코드
private $nhnConnectState = false;
// action options
private $autoClose = true;
private $showLogout = true;
private $curl = NULL;
private $drawOptions = array( "type" => "normal", "width" => "200" );
function __construct($argv = array()) {
if ( ! in_array ('curl', get_loaded_extensions())) {
echo 'curl required';
return false;
}
if($argv['CLIENT_ID']){
$this->client_id = trim($argv['CLIENT_ID']);
}
if($argv['CLIENT_SECRET']){
$this->client_secret = trim($argv['CLIENT_SECRET']);
}
if($argv['RETURN_URL']){
$this->returnURL = trim(urlencode($argv['RETURN_URL']));
}
if($argv['AUTO_CLOSE'] == false){
$this->autoClose = false;
}
if($argv['SHOW_LOGOUT'] == false){
$this->showLogout = false;
}
$this->loadSession();
if(isset($_GET['nhnMode']) && $_GET['nhnMode'] != ''){
$this->loginMode = 'logout';
$this->logout();
}
if($this->getConnectState() == false){
$this->generate_state();
if($_GET['state'] && $_GET['code']){
$this->loginMode = 'request_token';
$this->returnCode = $_GET['code'];
$this->returnState = $_GET['state'];
$this->_getAccessToken();
}
}
}
function login($options = array()){
if(isset($options['type'])){
$this->drawOptions['type'] = $options['type'];
}
if(isset($options['width'])){
$this->drawOptions['width'] = $options['width'];
}
if($this->loginMode == 'request' && (!$this->getConnectState()) || !$this->showLogout){
echo '<a href="javascript:loginNaver();"><img src="https://www.rawady.com:5014/open/idn/naver_login.png" alt="네이버 아이디로 로그인" width="'.$this->drawOptions['width'].'"></a>';
echo '
<script>
function loginNaver(){
var win = window.open(\''.NAVER_OAUTH_URL.'authorize?client_id='.$this->client_id.'&response_type=code&redirect_uri='.$this->returnURL.'&state='.$this->state.'\', \'네이버 아이디로 로그인\',\'width=320, height=480, toolbar=no, location=no\');
var timer = setInterval(function() {
if(win.closed) {
window.location.reload();
}
}, 500);
}
</script>
';
}else if($this->getConnectState()){
if($this->showLogout){
echo '<a href="http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"].'?nhnMode=logout"><img src="https://www.rawady.com:5014/open/idn/naver_logout.png" width="'.$this->drawOptions['width'].'" alt="네이버 아이디 로그아웃"/></a>';
}
}
if($this->loginMode == 'request_token'){
$this->_getAccessToken();
}
}
function logout(){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, NAVER_OAUTH_URL.'token?client_id='.$this->client_id.'&client_secret='.$this->client_secret.'&grant_type=delete&refresh_token='.$this->refresh_token.'&sercive_provider=NAVER');
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);
$retVar = curl_exec($this->curl);
curl_close($this->curl);
$this->deleteSession();
echo "<script>window.location.href = 'http://".$_SERVER["HTTP_HOST"] . $_SERVER['PHP_SELF']."';</script>";
}
function getUserProfile($retType = "JSON"){
if($this->getConnectState()){
$data = array();
$data['Authorization'] = $this->access_token_type.' '.$this->access_token;
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, 'https://apis.naver.com/nidlogin/nid/getUserProfile.xml');
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array(
'Authorization: '.$data['Authorization']
));
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);
$retVar = curl_exec($this->curl);
curl_close($this->curl);
if($retType == "JSON"){
$xml = new SimpleXMLElement($retVar);
$xmlJSON = array();
$xmlJSON['result']['resultcode'] = (string) $xml->result[0]->resultcode[0];
$xmlJSON['result']['message'] = (string) $xml->result[0]->message[0];
if($xml->result[0]->resultcode == '00'){
foreach($xml->response->children() as $response => $k){
$xmlJSON['response'][(string)$response] = (string) $k;
}
}
return json_encode($xmlJSON);
}else{
return $retVar;
}
}else{
return false;
}
}
/**
* Get AccessToken
* 발급된 엑세스 토큰을 반환합니다. 엑세스 토큰 발급은 로그인 후 자동으로 이루어집니다.
*/
function getAccess_token(){
if($this->access_token){
return $this->access_token;
}
}
/**
* 네이버 연결상태를 반환합니다.
* 엑세스 토큰 발급/저장이 이루어진 후 connected 상태가 됩니다.
*/
function getConnectState(){
return $this->nhnConnectState;
}
private function updateConnectState($strState = ''){
$this->nhnConnectState = $strState;
}
/**
* 토근을 세션에 기록합니다.
*/
private function saveSession(){
if(isset($_SESSION) && is_array($_SESSION)){
$_saveSession = array();
$_saveSession['access_token'] = $this->access_token;
$_saveSession['access_token_type'] = $this->access_token_type;
$_saveSession['refresh_token'] = $this->refresh_token;
$_saveSession['access_token_expire'] = $this->access_token_expire;
$this->tokenDatas = $_saveSession;
foreach($_saveSession as $k=>$v){
$_SESSION[NAVER_SESSION_NAME][$k] = $v;
}
}
}
private function deleteSession(){
if(isset($_SESSION) && is_array($_SESSION) && $_SESSION[NAVER_SESSION_NAME]){
$_loadSession = array();
$this->tokenDatas = $_loadSession;
unset($_SESSION[NAVER_SESSION_NAME]);
$this->access_token = '';
$this->access_token_type = '';
$this->refresh_token = '';
$this->access_token_expire = '';
$this->updateConnectState(false);
}
}
/**
* 저장된 토큰을 복원합니다.
*/
private function loadSession(){
if(isset($_SESSION) && is_array($_SESSION) && $_SESSION[NAVER_SESSION_NAME]){
$_loadSession = array();
$_loadSession['access_token'] = $_SESSION[NAVER_SESSION_NAME]['access_token'] ? $_SESSION[NAVER_SESSION_NAME]['access_token'] : '';
$_loadSession['access_token_type'] = $_SESSION[NAVER_SESSION_NAME]['access_token_type'] ? $_SESSION[NAVER_SESSION_NAME]['access_token_type'] : '';
$_loadSession['refresh_token'] = $_SESSION[NAVER_SESSION_NAME]['refresh_token'] ? $_SESSION[NAVER_SESSION_NAME]['refresh_token'] : '';
$_loadSession['access_token_expire'] = $_SESSION[NAVER_SESSION_NAME]['access_token_expire'] ? $_SESSION[NAVER_SESSION_NAME]['access_token_expire']:'';
$this->tokenDatas = $_loadSession;
$this->access_token = $this->tokenDatas['access_token'];
$this->access_token_type = $this->tokenDatas['access_token_type'];
$this->refresh_token = $this->tokenDatas['refresh_token'];
$this->access_token_expire = $this->tokenDatas['access_token_expire'];
$this->updateConnectState(true);
$this->saveSession();
}
}
private function _getAccessToken(){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, NAVER_OAUTH_URL.'token?client_id='.$this->client_id.'&client_secret='.$this->client_secret.'&grant_type=authorization_code&code='.$this->returnCode.'&state='.$this->returnState);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);
$retVar = curl_exec($this->curl);
curl_close($this->curl);
$NHNreturns = json_decode($retVar);
if(isset($NHNreturns->access_token)){
$this->access_token = $NHNreturns->access_token;
$this->access_token_type = $NHNreturns->token_type;
$this->refresh_token = $NHNreturns->refresh_token;
$this->access_token_expire = $NHNreturns->expires_in;
$this->updateConnectState(true);
$this->saveSession();
if($this->autoClose){
echo "<script>window.close();</script>";
}
}
}
private function generate_state() {
$mt = microtime();
$rand = mt_rand();
$this->state = md5( $mt . $rand );
}
}



Class 초기화

$naver = new Naver(array(
        "CLIENT_ID" => "USER_CLIENT_ID",        // (*필수)클라이언트 ID  
        "CLIENT_SECRET" => "USER_CLIENT_SECRET",    // (*필수)클라이언트 시크릿
        "RETURN_URL" => "USER_RETURN_URL",      // (*필수)콜백 URL
        "AUTO_CLOSE" => false,              // 인증 완료후 팝업 자동으로 닫힘 여부 설정 (추가 정보 기재등 추가행동 필요시 false 설정 후 추가)
        "SHOW_LOGOUT" => false              // 인증 후에 네이버 로그아웃 버튼 표시/ 또는 표시안함
        )
    );

로그인 버튼 생성

로그인 버튼은 네이버 공식 이미지로 저희 서버에서 재전송됩니다. 로그인 상태에서는 로그아웃 버튼이 표시됩니다.

// $naver->login();

<div class="login_box">
 <?=$naver->login()?>
</div>
// 로그인 버튼 크기변경시 

$naver->login(array(
    "width"=>"200"
));

사용자 정보 취득

사용자 정보는 로그인 및 인증 완료상태에서만, 작동합니다. 네이버에서 제공되는 XML 을 JSON 으로 인코딩하여 반환합니다.

(optional) $naver->getUserProfile((string) return type(JSON, XML));

// Default : json 반환
// XML 반환시 : $naver->getUserProfile('XML');
//


출처 : http://blog.publisher.name/499

PHP 클래스로 구현한 네이버 로그인 API
힘겹게(?) 찾은 개발 가이드 문서, Sample Code 그리고 하코사 미루님이 하코사에 올려둔 글을 참고하여 하나의 클래스에 담아서 만들었다.
blog.publisher.name
본문으로 이동

 

728x90
반응형

'오픈소스 > 소셜 로그인' 카테고리의 다른 글

소셜 로그인, 그리고 복습  (0) 2015.04.07
Facebook - Authentication  (0) 2015.04.07
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,