서버 API 접근 보안 » 이력 » 버전 16
이름없음, 2021/08/05 04:55
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 | 13 | 이름없음 | !picture385-2.png! |
26 | 12 | 이름없음 | |
27 | 14 | 이름없음 | 1) 모바일에서 Did 를 생성하고 함께 나오는 verkey를 보관합니다. |
28 | 1 | 이름없음 | |
29 | 14 | 이름없음 | 2) Header에 이용하는 X-Auth-Key 를 생성합니다. |
30 | 1 | 이름없음 | |
31 | 14 | 이름없음 | (Key + did + User-Agent + timestamp) 의 Hash 값을 입력합니다. |
32 | |||
33 | ※ TestBed에서는 User-Agent 로 "Test/1.0", Key 값으로 "1234567890abcdefghijklmnopqrstuvwxyz" 을 사용합니다. |
||
34 | |||
35 | 3) /auth/token를 통하여 access token 을 발급 받으며 Body 에는 did, verkey, timestamp 정보를 전달합니다. |
||
36 | |||
37 | timestamp 값은 2)의 Hash 값에 사용된 timestamp 값으로 할당 하여야 하며 서버 시간과 10분 차이 까지 유효합니다 |
||
38 | |||
39 | 4) /auth/token API 외에 다른 API를 호출시에 X-AUTH-TOKEN 파라미터에 access token 를 포함하여 호출합니다. |
||
40 | 12 | 이름없음 | |
41 | ※ access token 은 6시간 유효 기간을 가지고 있어 만료된 후에는 재사용 불가합니다. |
||
42 | |||
43 | API 호출하기 위하여는 access token을 재발급 하여 호출합니다. |
||
44 | |||
45 | 16 | 이름없음 | h2. X-Auth-Key 값 생성하기 |
46 | 15 | 이름없음 | |
47 | <pre><code class="java"> |
||
48 | import java.security.MessageDigest; |
||
49 | import java.security.NoSuchAlgorithmException; |
||
50 | |||
51 | public class main { |
||
52 | public static void main(String[] args) throws Exception { |
||
53 | |||
54 | String hash = null; |
||
55 | String userAgent = "Test/1.0"; |
||
56 | String appKey = "1234567890abcdefghijklmnopqrstuvwxyz"; |
||
57 | String did = "G5rw9qAMbozGxySHkMaztD"; |
||
58 | long timestamp = System.currentTimeMillis(); |
||
59 | |||
60 | try { |
||
61 | hash = sha256(new String(appKey + did + userAgent + timestamp)); |
||
62 | }catch (NoSuchAlgorithmException e){ |
||
63 | e.printStackTrace(); |
||
64 | } |
||
65 | System.out.println(timestamp + "," + sha256(new String(appKey + did + userAgent + timestamp))); |
||
66 | } |
||
67 | |||
68 | public static String sha256(String msg) throws NoSuchAlgorithmException { |
||
69 | MessageDigest md = MessageDigest.getInstance("SHA-256"); |
||
70 | md.update(msg.getBytes()); |
||
71 | return bytesToHex(md.digest()); |
||
72 | } |
||
73 | |||
74 | public static String bytesToHex(byte[] bytes) { |
||
75 | StringBuilder builder = new StringBuilder(); |
||
76 | for (byte b: bytes) { |
||
77 | builder.append(String.format("%02x", b)); |
||
78 | } |
||
79 | return builder.toString(); |
||
80 | } |
||
81 | } |
||
82 | </code></pre> |
||
83 | |||
84 | |||
85 | 7 | 이름없음 | h2(#wiki-id-API서버접근보안-관련자료). 관련 자료 |
86 | 1 | 이름없음 | |
87 | * "JSON Web Token (JWT)":https://tools.ietf.org/html/rfc7519 |