2023.01.11 - [Etc] - 나이스 본인 인증 access_token 발급받기(PHP)
<?php | |
$req_no = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890'), 10, 30); | |
$access_token = '4c202da8-생략-ab25415dae3d'; | |
$client_id = '15af79fd-생략-982b79abcc1a'; | |
$enc64 = base64_encode($access_token.':'.time().':'.$client_id); | |
$req_dtim = date('YmdHis'); | |
$post_data = [ | |
'dataHeader' => [ | |
'CNTY_CD' => 'ko' | |
], | |
'dataBody' => [ | |
'req_dtim' => $req_dtim, | |
'req_no' => $req_no, | |
'enc_mode' => '1' | |
] | |
]; | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_URL => 'https://svc.niceapi.co.kr:22001/digital/niceid/api/v1.0/common/crypto/token', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => json_encode($post_data), | |
CURLOPT_HTTPHEADER => [ | |
'Content-Type: application/json', | |
'Authorization: bearer '.$enc64, | |
'client_id: '.$client_id, | |
'ProductID: 2101979031' | |
] | |
]); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($response); | |
if($result->dataHeader->GW_RSLT_CD == 1200) { | |
session_start(); | |
$tmp = base64_encode(hash('sha256', $req_dtim.$req_no.$result->dataBody->token_val, true)); | |
$key = substr($tmp, 0, 16); | |
$iv = substr($tmp, -16); | |
$hmac_key = substr($tmp, 0, 32); | |
$_SESSION['nice_key' => $key, 'nice_iv' => $iv]; | |
$data = [ | |
'requestno' => $req_no, | |
'returnurl' => 'https://example.com/callback.php', | |
'sitecode' => $result->dataBody->site_code, | |
'methodtype' => 'post', | |
'popupyn' => 'Y', | |
'receivedata' => 'xxxxdddeee' | |
]; | |
$send_data = json_encode($data); | |
$encoding_data = base64_encode(openssl_encrypt($send_data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv)); | |
$hmac = hash_hmac('sha256', $encoding_data, $hmac_key, true); | |
$intigrety_value = base64_encode($hmac); | |
} | |
?> | |
<form id="nForm"> | |
<input type="text" name="m" value="service" /> | |
<input type="text" name="token_version_id" value="<?=$result->dataBody->token_version_id?>" /> | |
<input type="text" name="enc_data" value="<?=$encoding_data?>"> | |
<input type="text" name="integrity_value" value="<?=$intigrety_value?>"> | |
<button type="button" id="btnCertification">본인 인증</button> | |
</form> | |
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script> | |
<script> | |
$(function() { | |
$("#btnCertification").on("click", function() { | |
window.open("", "nice", "width=300, height=300, scrollbars=no"); | |
$("#nForm").attr({ | |
target: "nice", | |
action: "https://nice.checkplus.co.kr/CheckPlusSafeModel/checkplus.cb" | |
}).submit(); | |
}); | |
}); | |
</script> |
9: CNTY_CD에는 그냥 ko를 넣으시면 됩니다.
12: req_dtim에는 시간이 들어가야 하는데 년월일시분초(20230101010101) 형식으로 들어가야 합니다.
13: req_no에는 30글자 아무 문장이나 들어가면 된다고 합니다. 저는 영문 대소문자, 숫자를 합쳐서 30글자 랜덤으로...
14: enc_mode에는 그냥 1 넣으시면 됩니다. 가이드에 그렇게 되어 있습니다.
이렇게 9 ~ 14까지 다 합쳐서 json 형식으로 인코딩해서 post로 전송하면 됩니다.
26: Authorization: bearer 코드어쩌고저쩌고 이 형식으로 띄어쓰기가 맞아야 합니다. 그리고 뒤에 $enc64라고 된 변수에는 위에서 보시듯이 access token:1673915863:client_id 이 세 가지의 값을 콜론(:)으로 연결해서 base64로 인코딩을 한 값입니다. access token은 앞에서 이미 했고 중간에 숫자 10자는 unix timestamp입니다. 그냥 현재 시간을 찍어서 보내면 됩니다. 그리고 client id는 마이 페이지에 있습니다.
28: ProductID: 자신이 구매(?)한 상품 번호입니다. 이것 또한 마이 페이지에서 확인해 보시면 됩니다.
45: returnurl은 나이스 서버에서 인증(?) 값을 되돌려 주는데 그걸 받을 페이지 주소를 넣으시면 됩니다. url 전체를 입력해야 합니다. 예를 들어 https://내홈페이지주소/callback.php 이렇게 보냈다면 callback.php 파일에서 작업하시면 됩니다. get 형식으로 enc_data라는 키로 값이 넘어오니 찍어보면 어떤 값이 넘어오는지 확인이 가능합니다. 51, 52번째 줄을 거꾸로 하시면 됩니다.
'Programming > PHP' 카테고리의 다른 글
달력 (0) | 2023.11.08 |
---|---|
[Laravel]php artisan schema:dump (0) | 2023.10.20 |
나이스 본인 인증 - access_token 발급받기 (0) | 2023.01.11 |
인스타그램 피드 갖고 오기 - 1, 앱 만들기 (0) | 2022.12.28 |
배열에서 값(value)으로 삭제하기 (0) | 2022.12.26 |