package com.zhiqiyun.open.router.config;

import com.dliyun.oap.framework.exception.TransportException;
import com.dliyun.oap.framework.security.TransportSecurity;
import com.dliyun.oap.framework.security.algorithm.Rsa;
import com.zhiqiyun.open.core.models.app.AppKeyInfo;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class DesTransportSecurity implements TransportSecurity {

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @Override
    public String responseEncrypt(String appKey, String s) throws TransportException {
        if (StringUtils.isNotBlank(appKey)) {
            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(appKey);
            s = Rsa.encryptByPrivateKey(s, appKeyInfo.getPrivateKey());
        }
        return s;
    }

    @Override
    public String requestDecrypt(String appKey, String s) throws TransportException {
        if (StringUtils.isNotBlank(appKey)) {
            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(appKey);
            s = Rsa.decryptByPrivateKey(s, appKeyInfo.getPrivateKey());
        }
        return s;
    }
}
