package com.dxhy.auth.client.jwt;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;

import com.dxhy.auth.client.configuration.UserAuthConfig;
import com.dxhy.common.exception.auth.UserTokenException;
import com.dxhy.common.jwt.IJWTInfo;
import com.dxhy.common.jwt.JWTHelper;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:52:10
 *
 */
@Configuration
@Slf4j
public class UserAuthUtil {
    @Resource
    private UserAuthConfig userAuthConfig;

    public IJWTInfo getInfoFromToken(String token) throws Exception {
        try {
            return JWTHelper.getInfoFromToken(token, userAuthConfig.getPubKeyByte());
        } catch (ExpiredJwtException ex) {
            throw new UserTokenException("User token expired!");
        } catch (SignatureException ex) {
            throw new UserTokenException("User token signature error!");
        } catch (IllegalArgumentException ex) {
            throw new UserTokenException("User token is null or empty!");
        }
    }
}
