package com.dxhy.common.jwt;

import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.util.StringHelper;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

/**
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:52:48
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public class JWTHelper {
    private static final RsaKeyHelper RSA_KEY_HELPER = new RsaKeyHelper();

    /**
     * 密钥加密token
     */
    public static String generateToken(IJWTInfo jwtInfo, String priKeyPath, int expire) throws Exception {
        return Jwts.builder().setSubject(jwtInfo.getLoginName()).claim(CommonConstants.JWT_KEY_USER_ID, jwtInfo.getId())
            .claim(CommonConstants.JWT_KEY_NAME, jwtInfo.getUserName())
            .claim(CommonConstants.TOKEN_DATE, System.currentTimeMillis()).claim(CommonConstants.TOKEN_EXPIRE, expire)
            .signWith(SignatureAlgorithm.RS256, RSA_KEY_HELPER.getPrivateKey(priKeyPath)).compact();
    }

    /**
     * 密钥加密token
     */
    public static String generateToken(IJWTInfo jwtInfo, byte[] priKey, int expire) throws Exception {
        return Jwts.builder().setSubject(jwtInfo.getLoginName())
            .claim(CommonConstants.CONTEXT_KEY_USER, jwtInfo.getUserInfo())
            .claim(CommonConstants.JWT_KEY_USER_ID, jwtInfo.getId())
            .claim(CommonConstants.JWT_KEY_NAME, jwtInfo.getUserName())
            .claim(CommonConstants.TOKEN_DATE, System.currentTimeMillis()).claim(CommonConstants.TOKEN_EXPIRE, expire)
            .signWith(SignatureAlgorithm.RS256, RSA_KEY_HELPER.getPrivateKey(priKey)).compact();
    }

    /**
     * 公钥解析token
     */
    public static Jws<Claims> parserToken(String token, String pubKeyPath) throws Exception {
        return Jwts.parser().setSigningKey(RSA_KEY_HELPER.getPublicKey(pubKeyPath)).parseClaimsJws(token);
    }

    /**
     * 公钥解析token
     */
    public static Jws<Claims> parserToken(String token, byte[] pubKey) throws Exception {
        return Jwts.parser().setSigningKey(RSA_KEY_HELPER.getPublicKey(pubKey)).parseClaimsJws(token);
    }

    /**
     * 获取token中的用户信息
     */
    public static IJWTInfo getInfoFromToken(String token, String pubKeyPath) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
        Claims body = claimsJws.getBody();
        return new JWTInfo(StringHelper.getObjectValue(body.get(CommonConstants.CONTEXT_KEY_USER)),
            StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_NAME)),
            StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_USER_ID)), body.getSubject());
    }

    /**
     * 获取token中的用户信息
     */
    public static IJWTInfo getInfoFromToken(String token, byte[] pubKey) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKey);
        Claims body = claimsJws.getBody();
        return new JWTInfo(StringHelper.getObjectValue(body.get(CommonConstants.CONTEXT_KEY_USER)),
            StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_NAME)),
            StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_USER_ID)), body.getSubject());
    }
}
