
package com.dxhy.auth.client.jwt;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.dxhy.auth.client.config.ServiceAuthConfig;
import com.dxhy.auth.client.feign.ServiceAuthFeign;
import com.dxhy.common.exception.auth.ClientTokenException;
import com.dxhy.common.jwt.IJWTInfo;
import com.dxhy.common.jwt.JWTHelper;
import com.dxhy.common.msg.BaseResponse;
import com.dxhy.common.msg.ObjectRestResponse;

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

/**
 *
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:52:05
 *
 */
@Configuration
@Slf4j
@EnableScheduling
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class ServiceAuthUtil {
    @Resource
    private ServiceAuthConfig serviceAuthConfig;

    @Lazy
    @Resource
    private ServiceAuthFeign serviceAuthFeign;

    private List<String> allowedClientModel;
    private String clientTokenModel;

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

    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshAllowedClient() {
        log.debug("refresh allowedClient.....");
        if (serviceAuthConfig.isAuthClientStatus()) {
            BaseResponse resp =
                serviceAuthFeign.getAllowedClient(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
            if (resp.getCode() == 200) {
                ObjectRestResponse<List<String>> allowedClient = (ObjectRestResponse<List<String>>)resp;
                this.allowedClientModel = allowedClient.getData();
            }
        }

    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshClientToken() {
        log.debug("refresh client token.....");
        if (serviceAuthConfig.isAuthClientStatus()) {
            BaseResponse resp =
                serviceAuthFeign.getAccessToken(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
            if (resp.getCode() == 200) {
                ObjectRestResponse<String> clientToken = (ObjectRestResponse<String>)resp;
                this.clientTokenModel = clientToken.getData();
            }
        }

    }

    public String getClientToken() {
        if (this.clientTokenModel == null) {
            this.refreshClientToken();
        }
        return clientTokenModel;
    }

    public List<String> getAllowedClient() {
        if (this.allowedClientModel == null) {
            this.refreshAllowedClient();
        }
        return allowedClientModel;
    }
}
