toss.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @author crosscert
  3. * @version 1.0.1
  4. * @update 2020.10.12
  5. * @description
  6. * [2020.10.12] Toss 인증 및 본인확인 및 전자서명을 위한 라이브러리
  7. */
  8. var __crosscerttoss = function( __SANDBOX ) {
  9. var crosscert = __SANDBOX.usWebToolkit;
  10. var SANDBOX = __SANDBOX;
  11. var clientID = "4611ea0a9f78781b68e2208e371ce355";
  12. var clientSecret = "d56cb9c26f1f24e51dda2bc8aa7c4ab74bb1dae1260134813c23a3e30b3cdf95";
  13. var tossURL = __SANDBOX.ESVS.tossURL?__SANDBOX.ESVS.tossURL:"https://cert-alpha.toss.im";
  14. var errormsg = "일시적인 오류가 발생했습니다.\n잠시 후 다시 시도해주세요.";
  15. var esdownmsg = "[이지싸인] 앱 다운로드 링크를 문자로 발송했습니다.\n휴대폰에서 확인해 주세요.";
  16. var csdownmsg = "[클라우드사인] 앱 다운로드 링크를 문자로 발송했습니다.\n휴대폰에서 확인해 주세요.";
  17. function doXHRObject(url, req, callbackFUNC) {
  18. var myRep = {};
  19. // TODO: DELETE LATER
  20. // --------------------------------------- /
  21. // document.getElementById('reqData').value = '';
  22. // document.getElementById('resData').value = '';
  23. // document.getElementById('reqData').value = req;
  24. // --------------------------------------- /
  25. var originalReq = JSON.parse(req);
  26. if (url == undefined) {
  27. myRep.operation = originalReq.operation;
  28. myRep.messageNumber = originalReq.messageNumber;
  29. myRep.resultCode = '0051';
  30. myRep.resultMessage = 'Initialization failed, please check server url.';
  31. // TODO: DELETE LATER
  32. // --------------------------------------- /
  33. // document.getElementById('resData').value = JSON.stringify(myRep);
  34. // --------------------------------------- /
  35. callbackFUNC(JSON.stringify(myRep));
  36. return;
  37. }
  38. var xhr = createCORSRequest('POST', url);
  39. if (!xhr) {
  40. throw new Error('CORS not supported');
  41. }
  42. ;
  43. xhr.onload = function() {
  44. // TODO: DELETE LATER
  45. // --------------------------------------- /
  46. // document.getElementById('resData').value = xhr.responseText;
  47. // --------------------------------------- /
  48. callbackFUNC(xhr.responseText);
  49. };
  50. xhr.onerror = function() {
  51. myRep.operation = originalReq.operation;
  52. myRep.messageNumber = originalReq.messageNumber;
  53. myRep.resultCode = '0052';
  54. myRep.resultMessage = 'unexpected answer from Security Server :'
  55. + xhr.status;
  56. // TODO: DELETE LATER
  57. // --------------------------------------- /
  58. document.getElementById('resData').value = JSON.stringify(myRep);
  59. // --------------------------------------- /
  60. callbackFUNC(JSON.stringify(myRep));
  61. };
  62. xhr.send(req);
  63. };
  64. function createCORSRequest(method, url) {
  65. var xhr = new XMLHttpRequest();
  66. if ('withCredentials' in xhr) {
  67. xhr.open(method, url, true); // async(option) : true/false
  68. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  69. } else if (typeof XDomainRequest != 'undefined') {
  70. xhr = new XDomainRequest();
  71. xhr.open(method, url);
  72. } else {
  73. xhr = null;
  74. }
  75. return xhr;
  76. };
  77. return {
  78. reqGetAccessToken : function() {
  79. /* Auth token 을 이용한 Access Token 획득
  80. /api/auth/token
  81. Header
  82. Authorization : Basic + " " + Base64(clientID:clientSecret)
  83. Content-Type : application/x-www-form-urlencoded
  84. Body
  85. grant_type=client_credentials
  86. response
  87. {
  88. "access_token":"a4821aad-6b07-468a-9cf3-66602e5146e1",
  89. "token_type":"bearer",
  90. "scope":"read write"
  91. }
  92. */
  93. },
  94. reqLogin : function(accessToken) {
  95. /* 간편 로그인 진행
  96. /api/v1/sign/login/request
  97. Header
  98. Authorization : Bearer + " " + Base64(accessToken)
  99. Content-Type : application/json
  100. Body
  101. { "serviceName" : "간편로그인 테스트", "triggerType" : "PUSH"}
  102. response
  103. {
  104. "resultType": "SUCCESS",
  105. "success": {
  106. "txId": "ee86e2d0-a22c-411b-a4a3-6d7a2e4f373e",
  107. "requestedDt": "2020-05-27T12:10:47+09:00"
  108. }
  109. }
  110. */
  111. },
  112. reqCheck : function(accessToken, txId) {
  113. /* 간편 로그인 상태 체크
  114. /api/v1/sign/login/check
  115. Header
  116. Authorization : Bearer + " " + Base64(accessToken)
  117. Content-Type : application/json
  118. Body
  119. { "txId" : txId}
  120. response
  121. status : REQUESTED, IN_PROGRESS, COMPLETED, EXPIRED
  122. {
  123. "resultType": "SUCCESS",
  124. "success": {
  125. "txId": "06b589da-cbdb-45aa-ad67-2826361ac8c2",
  126. "status": "IN_PROGRESS",
  127. "requestedDt": "2020-05-27T12:15:23+09:00"
  128. }
  129. }
  130. */
  131. },
  132. reqResult : function(accessToken,txId) {
  133. /* 간편 로그인 결과
  134. /api/v1/sign/login/result
  135. Header
  136. Authorization : Bearer + " " + Base64(accessToken)
  137. Content-Type : application/json
  138. Body
  139. { "txId" : txId}
  140. response
  141. {
  142. "resultType": "SUCCESS",
  143. "success": {
  144. "txId": "06b589da-cbdb-45aa-ad67-2826361ac8c2",
  145. "status": "COMPLETED",
  146. "name": "김토스",
  147. "phone": "01012345678",
  148. "birthday": "19910301",
  149. "gender": "MALE",
  150. “authorizationCode”: “......”,
  151. "identifier": "50066179726…...",
  152. "completedDt": "2020-05-27T12:19:58+09:00",
  153. "requestedDt": "2020-05-27T12:15:23+09:00"
  154. }
  155. }
  156. */
  157. }
  158. }
  159. };