package com.usoftchina.bi.core.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.usoftchina.bi.core.exception.MyException;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class GetTokenDataUtil {
    //公共密钥
    public static String SECRET = "YouRuanKeJiBi";

    public static Map<String, String> getTokenData(String token){
        DecodedJWT jwt = null;

        JWTVerifier verifier = null;
        try {
            verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try{
            jwt = verifier.verify(token);
        }catch (MyException e){
            throw new MyException(-505, "token过期");
        }
        Map<String, Claim> claimMap = jwt.getClaims();
        Map<String, String> resultMap = new HashMap<String, String>();
        claimMap.forEach((k,v) -> resultMap.put(k, v.asString()));
        return resultMap;
    }
}
