서버 API 접근 보안 » 이력 » 개정판 18
개정판 17 (이름없음, 2021/08/05 04:59) → 개정판 18/24 (이름없음, 2021/08/05 05:00)
h1. 서버 API 접근 보안
h2(#wiki-id-API서버접근보안-API서버접근허가흐름도). API 서버 접근 허가 흐름도
* DID(Decentralized Identifier) + JWT(JSON Web Token) 기반 인증
** 사용자 구분은 DID ( Decentralized Identity ) 로 하고 있으며 Hyperledger Indy ( https://www.hyperledger.org/use/hyperledger-indy ) 솔루션을 사용합니다
** JWT (Jason Web Token : https://jwt.io/ ) 으로 API 접근 권한을 제한하고 있습니다.
p=. !picture385-1.png(Indy 구조)!
h2(#wiki-id-API서버접근보안-JWTaccesstoken발급과검증). JWT access token 발급과 검증
1) 인증서버(Auth Server)는 검증된 DID를 제출한 모바일 앱을 위하여 access token을 생성하여 반환합니다.
!picture413-1.png!
2) 모바일 앱은 API 서버에 요청을 보낼 때 access token을 헤더에 담아서 보냅니다. API 서버는 요청 메시지의 헤더에 있는 access token을 검증하고 자신이 발급한 것이 맞으면 요청을 수행하고 응답을 반환합니다.
!picture413-2.png!
h2. Access Token 발급 및 호출
API 를 호출하기 위해서는 Access Token 을 발급 받아야 하며 다음과 같은 흐름으로 호출합니다
!picture385-2.png!
1) 모바일에서 Did 를 생성하고 함께 나오는 verkey를 보관합니다.
2) Header에 이용하는 X-Auth-Key 를 생성합니다.
(Key + did + User-Agent + timestamp) 의 Hash 값을 입력합니다.
※ TestBed에서는 User-Agent 로 "Test/1.0", Key 값으로 "1234567890abcdefghijklmnopqrstuvwxyz" 을 사용합니다.
3) /auth/token를 통하여 access token 을 발급 받으며 Body 에는 did, verkey, timestamp 정보를 전달합니다.
timestamp 값은 2)의 Hash 값에 사용된 timestamp 값으로 할당 하여야 하며 서버 시간과 10분 차이 까지 유효합니다
4) /auth/token API 외에 다른 API를 호출시에 X-AUTH-TOKEN 파라미터에 access token 를 포함하여 호출합니다.
※ access token 은 6시간 유효 기간을 가지고 있어 만료된 후에는 재사용 불가합니다.
API 호출하기 위하여는 access token을 재발급 하여 호출합니다.
h2. X-Auth-Key 값 생성하기
https://openapi.myd.world/api/#/authenticate/token 의 /auth/token REST API 의 Header 및 Body 구성을 위하여
하기 내용의 결과값을 활용 하도록 한다
결과값) timestamp, X-Auth-Key
<pre><code class="java">
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class main {
public static void main(String[] args) throws Exception {
String hash = null;
String userAgent = "Test/1.0";
String appKey = "1234567890abcdefghijklmnopqrstuvwxyz";
String did = "G5rw9qAMbozGxySHkMaztD";
long timestamp = System.currentTimeMillis();
try {
hash = sha256(new String(appKey + did + userAgent + timestamp));
}catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
System.out.println(timestamp + "," + sha256(new String(appKey + did + userAgent + timestamp)));
}
public static String sha256(String msg) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(msg.getBytes());
return bytesToHex(md.digest());
}
public static String bytesToHex(byte[] bytes) {
StringBuilder builder = new StringBuilder();
for (byte b: bytes) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
}
</code></pre>
h2(#wiki-id-API서버접근보안-관련자료). 관련 자료
* "JSON Web Token (JWT)":https://tools.ietf.org/html/rfc7519