프로젝트

일반

사용자정보

Actions

서버 API 접근 보안 » 이력 » 개정판 18

« 뒤로 | 개정판 18/24 (비교(diff)) | 다음 »
이름없음, 2021/08/05 05:00


서버 API 접근 보안

API 서버 접근 허가 흐름도

Indy 구조

JWT access token 발급과 검증

1) 인증서버(Auth Server)는 검증된 DID를  제출한 모바일 앱을 위하여 access token을 생성하여 반환합니다.

2) 모바일 앱은 API 서버에 요청을 보낼 때 access token을 헤더에 담아서 보냅니다. API 서버는 요청 메시지의 헤더에 있는 access token을 검증하고 자신이 발급한 것이 맞으면 요청을 수행하고 응답을 반환합니다.

Access Token 발급 및 호출

API 를 호출하기 위해서는 Access Token 을 발급 받아야 하며 다음과 같은 흐름으로 호출합니다

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을 재발급 하여 호출합니다.

X-Auth-Key 생성하기

https://openapi.myd.world/api/#/authenticate/token 의 /auth/token REST API 의 Header 및 Body 구성을 위하여
하기 내용의 결과값을 활용 하도록 한다

결과값) timestamp, X-Auth-Key

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();
    }
}

관련 자료

이름없음이(가) 약 4년 전에 변경 · 18 revisions

클립보드 이미지 추가 (최대 크기: 97.7 MB)