서버 API 접근 보안 » 이력 » 버전 21
이름없음, 2021/11/30 08:03
1 | 9 | 이름없음 | h1. 서버 API 접근 보안 |
---|---|---|---|
2 | 1 | 이름없음 | |
3 | 7 | 이름없음 | h2(#wiki-id-API서버접근보안-API서버접근허가흐름도). API 서버 접근 허가 흐름도 |
4 | 1 | 이름없음 | |
5 | * DID(Decentralized Identifier) + JWT(JSON Web Token) 기반 인증 |
||
6 | 11 | 이름없음 | ** 사용자 구분은 DID ( Decentralized Identity ) 로 하고 있으며 Hyperledger Indy ( https://www.hyperledger.org/use/hyperledger-indy ) 솔루션을 사용합니다 |
7 | 10 | 이름없음 | ** JWT (Jason Web Token : https://jwt.io/ ) 으로 API 접근 권한을 제한하고 있습니다. |
8 | 4 | 이름없음 | |
9 | 14 | 이름없음 | p=. !picture385-1.png(Indy 구조)! |
10 | 1 | 이름없음 | |
11 | 7 | 이름없음 | h2(#wiki-id-API서버접근보안-JWTaccesstoken발급과검증). JWT access token 발급과 검증 |
12 | 1 | 이름없음 | |
13 | 1) 인증서버(Auth Server)는 검증된 DID를 제출한 모바일 앱을 위하여 access token을 생성하여 반환합니다. |
||
14 | |||
15 | 6 | 이름없음 | !picture413-1.png! |
16 | |||
17 | 1 | 이름없음 | 2) 모바일 앱은 API 서버에 요청을 보낼 때 access token을 헤더에 담아서 보냅니다. API 서버는 요청 메시지의 헤더에 있는 access token을 검증하고 자신이 발급한 것이 맞으면 요청을 수행하고 응답을 반환합니다. |
18 | 6 | 이름없음 | |
19 | !picture413-2.png! |
||
20 | 1 | 이름없음 | |
21 | 12 | 이름없음 | h2. Access Token 발급 및 호출 |
22 | |||
23 | API 를 호출하기 위해서는 Access Token 을 발급 받아야 하며 다음과 같은 흐름으로 호출합니다 |
||
24 | |||
25 | 19 | 이름없음 | !picture886-1.png! |
26 | 12 | 이름없음 | |
27 | 14 | 이름없음 | 1) 모바일에서 Did 를 생성하고 함께 나오는 verkey를 보관합니다. |
28 | 1 | 이름없음 | |
29 | 21 | 이름없음 | 2) /auth/nonce 호출로 nonce 값을 호출합니다 |
30 | nonce 값은 이후 /auth/token 을 호출하기 위한 파라미터로 활용됩니다. |
||
31 | 1 | 이름없음 | |
32 | 21 | 이름없음 | 3) Header에 이용하는 X-Auth-Key 를 생성합니다. |
33 | 1 | 이름없음 | |
34 | 21 | 이름없음 | (Key + did + App-Version + nonce) 의 Hash 값을 입력합니다. |
35 | |||
36 | 1 | 이름없음 | ※ TestBed에서는 App-Version 로 "Test/1.0", Key 값으로 "1234567890abcdefghijklmnopqrstuvwxyz" 을 사용합니다. |
37 | 14 | 이름없음 | |
38 | 21 | 이름없음 | 4) /auth/token를 통하여 access token 을 발급 받으며 Body 에는 did, verkey, nonce정보를 전달합니다. |
39 | 14 | 이름없음 | |
40 | 21 | 이름없음 | 5) /auth/token API 외에 다른 API를 호출시에 X-AUTH-TOKEN 파라미터에 access token 를 포함하여 호출합니다. |
41 | 12 | 이름없음 | |
42 | ※ access token 은 6시간 유효 기간을 가지고 있어 만료된 후에는 재사용 불가합니다. |
||
43 | |||
44 | 1 | 이름없음 | API 호출하기 위하여는 access token을 재발급 하여 호출합니다. |
45 | 12 | 이름없음 | |
46 | 18 | 이름없음 | h2. X-Auth-Key 생성하기 |
47 | 15 | 이름없음 | |
48 | 17 | 이름없음 | https://openapi.myd.world/api/#/authenticate/token 의 /auth/token REST API 의 Header 및 Body 구성을 위하여 |
49 | 하기 내용의 결과값을 활용 하도록 한다 |
||
50 | |||
51 | 21 | 이름없음 | 결과값) X-Auth-Key |
52 | 17 | 이름없음 | |
53 | 15 | 이름없음 | <pre><code class="java"> |
54 | import java.security.MessageDigest; |
||
55 | import java.security.NoSuchAlgorithmException; |
||
56 | |||
57 | public class main { |
||
58 | public static void main(String[] args) throws Exception { |
||
59 | |||
60 | String hash = null; |
||
61 | 19 | 이름없음 | String appVersion = "Test/1.0"; |
62 | 15 | 이름없음 | String appKey = "1234567890abcdefghijklmnopqrstuvwxyz"; |
63 | String did = "G5rw9qAMbozGxySHkMaztD"; |
||
64 | 20 | 이름없음 | String nonce = ""; // /auth/nonce 로 획득한 값 |
65 | 15 | 이름없음 | |
66 | try { |
||
67 | 20 | 이름없음 | hash = sha256(new String(appKey + did + appVersion + nonce)); |
68 | 15 | 이름없음 | }catch (NoSuchAlgorithmException e){ |
69 | e.printStackTrace(); |
||
70 | } |
||
71 | 20 | 이름없음 | System.out.println(timestamp + "," + sha256(new String(appKey + did + appVersion + nonce))); |
72 | 15 | 이름없음 | } |
73 | |||
74 | public static String sha256(String msg) throws NoSuchAlgorithmException { |
||
75 | MessageDigest md = MessageDigest.getInstance("SHA-256"); |
||
76 | md.update(msg.getBytes()); |
||
77 | return bytesToHex(md.digest()); |
||
78 | } |
||
79 | |||
80 | public static String bytesToHex(byte[] bytes) { |
||
81 | StringBuilder builder = new StringBuilder(); |
||
82 | for (byte b: bytes) { |
||
83 | builder.append(String.format("%02x", b)); |
||
84 | } |
||
85 | return builder.toString(); |
||
86 | } |
||
87 | } |
||
88 | </code></pre> |
||
89 | |||
90 | |||
91 | 7 | 이름없음 | h2(#wiki-id-API서버접근보안-관련자료). 관련 자료 |
92 | 1 | 이름없음 | |
93 | * "JSON Web Token (JWT)":https://tools.ietf.org/html/rfc7519 |