6
6
import javax .json .Json ;
7
7
import javax .json .JsonObject ;
8
8
import javax .json .JsonReader ;
9
+ import java .io .IOException ;
9
10
import java .io .InputStream ;
10
11
import java .io .OutputStreamWriter ;
11
12
import java .io .StringReader ;
12
13
import java .net .HttpURLConnection ;
13
14
import java .net .URL ;
14
15
import java .util .Scanner ;
15
16
16
- import static org .junit .jupiter .api .Assertions .assertEquals ;
17
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
17
18
18
19
@ Disabled // 실행전에 해당라인을 삭제해주세요.
19
20
class ExampleTest {
20
21
// 0. 세션 생성기를 사전에 1회만 생성해 주세요.
21
22
TossCertSessionGenerator tossCertSessionGenerator = new TossCertSessionGenerator ();
22
23
24
+ final String accessToken = "eyJraWQiOiJjZXJ0IiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJ0ZXN0X2E4ZTIzMzM2ZDY3M2NhNzA5MjJiNDg1ZmU4MDZlYjJkIiwiYXVkIjoidGVzdF9hOGUyMzMzNmQ2NzNjYTcwOTIyYjQ4NWZlODA2ZWIyZCIsIm5iZiI6MTcwOTAyMDA5OSwic2NvcGUiOlsiY2EiXSwiaXNzIjoiaHR0cHM6Ly9jZXJ0LnRvc3MuaW0iLCJleHAiOjE3NDA1NTYwOTksImlhdCI6MTcwOTAyMDA5OSwianRpIjoiZDc3NmUxZmEtZmNkMy00MDE4LTg2MGMtZDA0NTY0YmUxY2U5In0.hQDc7eeY6-a-0tLfcsAO_Tejbmu_Sd7f80P90NtTy6T4HjEUQNji13sMdkhPeibnonE0E8d4fdsyFy2J2KQFLIqFNjV-jPypjm9XcF2yUBwBfG7Jq7k1BBuigPXTN1NistNpnE24F0nNlMzsGZi72YePIFEayFi_SQN5GUwZ9MZbQenGA9sKct0heqKxQj7wuyELgvT7dCFtZ5EU_C_DDhvgtyauGvD4ubtxj2_-SskAnr54LZhW-cDF-rdsAD9knbhcnscpZKXnGVNlXbQzgrVfWNEYlJeZ9bwagdgYh67VrC8SNBoGPuXsKU4eUV17lh_TwB9M2lPkBJLwgaJVgA" ;
25
+
23
26
@ Test
24
27
void request () throws Exception {
25
28
// 1. 개인정보가 포함되어 있는 인증요청 API 호출 전에 세션을 생성해 주세요.
@@ -34,105 +37,113 @@ void request() throws Exception {
34
37
String encryptedUserBirthday = tossCertSession .encrypt (userBirthday );
35
38
36
39
// 3. 인증요청 API 를 호출해주세요.
37
- URL url = new URL ("https://cert.toss.im/api/v2/sign/user/auth/request" );
38
- HttpURLConnection httpConn = (HttpURLConnection ) url .openConnection ();
39
- httpConn .setRequestMethod ("POST" );
40
-
41
- httpConn .setRequestProperty ("Authorization" , "Bearer eyJraWQiOiJjZXJ0IiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJ0ZXN0X2E4ZTIzMzM2ZDY3M2NhNzA5MjJiNDg1ZmU4MDZlYjJkIiwiYXVkIjoidGVzdF9hOGUyMzMzNmQ2NzNjYTcwOTIyYjQ4NWZlODA2ZWIyZCIsIm5iZiI6MTY0OTIyMjk3OCwic2NvcGUiOlsiY2EiXSwiaXNzIjoiaHR0cHM6XC9cL2NlcnQudG9zcy5pbSIsImV4cCI6MTY4MDc1ODk3OCwiaWF0IjoxNjQ5MjIyOTc4LCJqdGkiOiI4MDNjNDBjOC1iMzUxLTRmOGItYTIxNC1iNjc5MmNjMzBhYTcifQ.cjDZ0lAXbuf-KAgi3FlG1YGxvgvT3xrOYKDTstfbUz6CoNQgvd9TqI6RmsGZuona9jIP6H12Z1Xb07RIfAVoTK-J9iC5_Yp8ZDdcalsMNj51pPP8wso86rn-mKsrx1J5Rdi3GU58iKt0zGr4KzqSxUJkul9G4rY03KInwvl692HU19kYA9y8uTI4bBX--UPfQ02G0QH9HGTPHs7lZsISDtyD8sB2ikz5p7roua7U467xWy4BnRleCEWO2uUaNNGnwd7SvbjhmsRZqohs9KzDUsFjVhSiRNdHL53XJQ5zFHwDF92inRZFLu6Dw8xttPtNHwAD1kT84uXJcVMfEHtwkQ" );
42
- httpConn .setRequestProperty ("Content-Type" , "application/json" );
43
- httpConn .setDoOutput (true );
44
- OutputStreamWriter writer = new OutputStreamWriter (httpConn .getOutputStream ());
45
- /* json
46
- {
47
- "requestType": "USER_PERSONAL",
48
- "triggerType": "PUSH",
49
- "sessionKey": tossCertSession.getSessionKey(),
50
- "userName": encryptedUserName,
51
- "userPhone": encryptedUserPhone,
52
- "userBirthday": encryptedUserBirthday
53
- }
54
- */
40
+ /* json
41
+ {
42
+ "requestType": "USER_PERSONAL",
43
+ "triggerType": "PUSH",
44
+ "sessionKey": tossCertSession.getSessionKey(),
45
+ "userName": encryptedUserName,
46
+ "userPhone": encryptedUserPhone,
47
+ "userBirthday": encryptedUserBirthday
48
+ }
49
+ */
55
50
String requestBody = Json .createObjectBuilder ()
56
- .add ("requestType" , "USER_PERSONAL" )
57
- .add ("triggerType" , "PUSH" )
58
- // 3.1 세션키를 넣어주세요.
59
- .add ("sessionKey" , tossCertSession .getSessionKey ())
60
- // 3.2 tossCertSession 로 암호화된 개인정보를 넣어주세요.
61
- .add ("userName" , encryptedUserName )
62
- .add ("userPhone" , encryptedUserPhone )
63
- .add ("userBirthday" , encryptedUserBirthday )
64
- .build ()
65
- .toString ();
66
- writer .write (requestBody );
67
- writer .flush ();
68
- writer .close ();
69
-
70
- httpConn .getOutputStream ().close ();
71
- InputStream responseStream = httpConn .getResponseCode () == 200
72
- ? httpConn .getInputStream ()
73
- : httpConn .getErrorStream ();
74
- Scanner s = new Scanner (responseStream ).useDelimiter ("\\ A" );
75
- String response = s .hasNext () ? s .next () : "" ;
51
+ .add ("requestType" , "USER_PERSONAL" )
52
+ .add ("triggerType" , "PUSH" )
53
+ // 3.1 세션키를 넣어주세요.
54
+ .add ("sessionKey" , tossCertSession .getSessionKey ())
55
+ // 3.2 tossCertSession 로 암호화된 개인정보를 넣어주세요.
56
+ .add ("userName" , encryptedUserName )
57
+ .add ("userPhone" , encryptedUserPhone )
58
+ .add ("userBirthday" , encryptedUserBirthday )
59
+ .build ()
60
+ .toString ();
61
+
62
+ JsonObject responseObject = postUrl ("https://cert.toss.im/api/v2/sign/user/auth/request" , requestBody );
76
63
77
- JsonReader responseReader = Json .createReader (new StringReader (response ));
78
- JsonObject responseObject = responseReader .readObject ();
79
64
System .out .println ("\n --------------------------- 인증 요청 결과 -------------------------------------" );
80
65
System .out .println ("인증 txId: " + responseObject .getJsonObject ("success" ).getString ("txId" ));
81
66
System .out .println ("----------------------------------------------------------------------------\n " );
82
- assertEquals (1 , 1 );
83
67
}
84
68
85
69
// 결과호출을 하기전에 토스앱에서 인증을 완료해 주세요.
86
70
87
71
@ Test
88
72
void result () throws Exception {
89
73
// 0. 인증 요청 결과에서 응답받은 인증 txId 로 변경한 후 테스트 해주세요.
90
- String txId = "2122cb6d-46f9-4e72-86eb-3f71c3c97507 " ;
74
+ String txId = "인증 txId " ;
91
75
92
76
// 1. 인증 결과 조회 API 호출 전에 세션을 생성해 주세요.
93
77
TossCertSession tossCertSession = tossCertSessionGenerator .generate ();
94
78
95
79
// 2. 인증요청 API 를 호출해주세요.
96
- URL url = new URL ("https://cert.toss.im/api/v2/sign/user/auth/result" );
80
+ /* json
81
+ {
82
+ "sessionKey": tossCertSession.getSessionKey(),
83
+ "txId": txId
84
+ }
85
+ */
86
+ String requestBody = Json .createObjectBuilder ()
87
+ // 2.1 세션키를 넣어주세요.
88
+ .add ("sessionKey" , tossCertSession .getSessionKey ())
89
+ // 2.2 인증 요청 결과의 txId 를 넣어주세요.
90
+ .add ("txId" , txId )
91
+ .build ()
92
+ .toString ();
93
+
94
+ JsonObject responseObject = postUrl ("https://cert.toss.im/api/v2/sign/user/auth/result" , requestBody );
95
+
96
+ // 3. 결과를 복호화 합니다.
97
+ String encryptedCi = responseObject .getJsonObject ("success" ).getJsonObject ("personalData" ).getString ("ci" );
98
+ String ci = tossCertSession .decrypt (encryptedCi );
99
+ System .out .println ("\n --------------------------- 인증 결과 조회 CI ----------------------------------" );
100
+ System .out .println ("복호화 된 CI: " + ci );
101
+ System .out .println ("----------------------------------------------------------------------------\n " );
102
+
103
+ // 4. 인증서 유효성을 검사합니다.
104
+ String signature = responseObject .getJsonObject ("success" ).getString ("signature" );
105
+ String pemCertificate = PKCS7CertificateExtractor .extractCertificate (signature );
106
+
107
+ /* json
108
+ {
109
+ "certificate": pemCertificate
110
+ }
111
+ */
112
+ requestBody = Json .createObjectBuilder ()
113
+ // 2.1 세션키를 넣어주세요.
114
+ .add ("certificate" , pemCertificate )
115
+ // 2.2 인증 요청 결과의 txId 를 넣어주세요.
116
+ .build ()
117
+ .toString ();
118
+
119
+ responseObject = postUrl ("https://cert.toss.im/api/v1/certificate/validate" , requestBody );
120
+
121
+ assertTrue (responseObject .getJsonObject ("success" ).getBoolean ("valid" ));
122
+ assertTrue (responseObject .getJsonObject ("success" ).getBoolean ("enabled" ));
123
+ }
124
+
125
+ private JsonObject postUrl (String urlString , String requestBody ) throws IOException {
126
+ URL url = new URL (urlString );
97
127
HttpURLConnection httpConn = (HttpURLConnection ) url .openConnection ();
98
128
httpConn .setRequestMethod ("POST" );
99
129
100
- httpConn .setRequestProperty ("Authorization" , "Bearer eyJraWQiOiJjZXJ0IiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJ0ZXN0X2E4ZTIzMzM2ZDY3M2NhNzA5MjJiNDg1ZmU4MDZlYjJkIiwiYXVkIjoidGVzdF9hOGUyMzMzNmQ2NzNjYTcwOTIyYjQ4NWZlODA2ZWIyZCIsIm5iZiI6MTY0OTIyMjk3OCwic2NvcGUiOlsiY2EiXSwiaXNzIjoiaHR0cHM6XC9cL2NlcnQudG9zcy5pbSIsImV4cCI6MTY4MDc1ODk3OCwiaWF0IjoxNjQ5MjIyOTc4LCJqdGkiOiI4MDNjNDBjOC1iMzUxLTRmOGItYTIxNC1iNjc5MmNjMzBhYTcifQ.cjDZ0lAXbuf-KAgi3FlG1YGxvgvT3xrOYKDTstfbUz6CoNQgvd9TqI6RmsGZuona9jIP6H12Z1Xb07RIfAVoTK-J9iC5_Yp8ZDdcalsMNj51pPP8wso86rn-mKsrx1J5Rdi3GU58iKt0zGr4KzqSxUJkul9G4rY03KInwvl692HU19kYA9y8uTI4bBX--UPfQ02G0QH9HGTPHs7lZsISDtyD8sB2ikz5p7roua7U467xWy4BnRleCEWO2uUaNNGnwd7SvbjhmsRZqohs9KzDUsFjVhSiRNdHL53XJQ5zFHwDF92inRZFLu6Dw8xttPtNHwAD1kT84uXJcVMfEHtwkQ" );
130
+ httpConn .setRequestProperty ("Authorization" , "Bearer " + accessToken );
101
131
httpConn .setRequestProperty ("Content-Type" , "application/json" );
102
132
httpConn .setDoOutput (true );
103
133
OutputStreamWriter writer = new OutputStreamWriter (httpConn .getOutputStream ());
104
- /* json
105
- {
106
- "sessionKey": tossCertSession.getSessionKey(),
107
- "txId": txId
108
- }
109
- */
110
- String requestBody = Json .createObjectBuilder ()
111
- // 2.1 세션키를 넣어주세요.
112
- .add ("sessionKey" , tossCertSession .getSessionKey ())
113
- // 2.2 인증 요청 결과의 txId 를 넣어주세요.
114
- .add ("txId" , txId )
115
- .build ()
116
- .toString ();
134
+
117
135
writer .write (requestBody );
118
136
writer .flush ();
119
137
writer .close ();
120
138
121
139
httpConn .getOutputStream ().close ();
122
140
InputStream responseStream = httpConn .getResponseCode () == 200
123
- ? httpConn .getInputStream ()
124
- : httpConn .getErrorStream ();
141
+ ? httpConn .getInputStream ()
142
+ : httpConn .getErrorStream ();
125
143
Scanner s = new Scanner (responseStream ).useDelimiter ("\\ A" );
126
144
String response = s .hasNext () ? s .next () : "" ;
127
- JsonReader responseReader = Json .createReader (new StringReader (response ));
128
- JsonObject responseObject = responseReader .readObject ();
129
145
130
- // 3. 결과를 복호화 합니다.
131
- String encryptedCi = responseObject .getJsonObject ("success" ).getJsonObject ("personalData" ).getString ("ci" );
132
- String ci = tossCertSession .decrypt (encryptedCi );
133
- System .out .println ("\n --------------------------- 인증 결과 조회 CI ----------------------------------" );
134
- System .out .println ("복호화 된 CI: " + ci );
135
- System .out .println ("----------------------------------------------------------------------------\n " );
136
- assertEquals (1 , 1 );
146
+ JsonReader responseReader = Json .createReader (new StringReader (response ));
147
+ return responseReader .readObject ();
137
148
}
138
149
}
0 commit comments