package com.diagbot.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;

import java.util.Date;
import java.util.Map;

/**
 * @Description: Jwt Token解析工具类
 * @author: gaodm
 * @time: 2018/10/29 9:36
 */
public final class JwtUtil {

    static final String TOKEN_PREFIX = "Bearer";

    /**
     * 解析token获取用户ID
     *
     * @param token 输入token
     * @return 用户ID
     */
    public static String getUserId(String token) {
        DecodedJWT jwt = decodedJWT(token);
        Map<String, Claim> claims = jwt.getClaims();
        Claim claim = claims.get("user_id");
        return claim.asInt().toString();
    }

    /**
     * 解析token获取用户ID
     *
     * @param token 输入token
     * @return 用户ID
     */
    public static Date getExp(String token) {
        DecodedJWT jwt = decodedJWT(token);
        Map<String, Claim> claims = jwt.getClaims();
        Claim claim = claims.get("exp");
        return claim.asDate();
    }

    /**
     * 解析token
     *
     * @param token 输入token
     * @return 解析结果
     */
    public static DecodedJWT decodedJWT(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt;
        } catch (JWTDecodeException e) {
            e.printStackTrace();
            throw new CommonException(CommonErrorCode.ANALYZER_TOKEN_FAIL);
        }
    }

    public static void main(String[] args) {
        String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxOSwidXNlcl9uYW1lIjoic3EiLCJzY29wZSI6WyJzZXJ2aWNlIl0sImV4cCI6MTU0MDg3MDE4MCwiYXV0aG9yaXRpZXMiOlsiL3VzZXJSZW5ld2Fscy9kZWxldGVSZW5ld2Fsc0luZm9zO0FMTCIsIi9wcm9kdWN0T3JkZXIvZ2V0SW5mb3JtYXRpb25BdmFpbGFibGVBbGw7QUxMIiwiL3VzZXIvdXBkYXRlUGVyc29uSW5mbztBTEwiLCIvdXNlckluZm8vdXBkYXRlT3JnYW5pemF0aW9ucztBTEw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qneEI3TbtRdmLAeUOYV1K5cmeBpHkTZmcS6nk2p30REH6DI8HWnl59qAv0-MwPiguqYpcFJ1dqnMDh8ZIdv5Xj4LUHh3lLLjRxo6_jr9FVUnF_R87BFQ91w4NYdYJy0uJYOBvk4dL16ZgGgc_cpYcncmp0KzCTk-r7_-UFlrNbW8HV-05uVPQY7CCnvLDwG7iQQR2xNZA80wS6EcvUBJijMe8zKw_bp3BTiBbVeTnyQMfAgHFCMAZqpIyuulpmHFGrLplLmJg";
        //        System.out.println(GsonUtil.toJson(decodedJWT(token)));
        System.out.println(getUserId(token));
        System.out.println(getExp(token));
    }
}
