package cn.unitid.matrix.pkiservice.test.svs;

import cn.unitid.matrix.pkiservice.sdk.ApiException;
import cn.unitid.matrix.pkiservice.sdk.PkiClient;
import cn.unitid.matrix.pkiservice.sdk.PkiOkhttpClient;
import cn.unitid.matrix.pkiservice.sdk.consts.SubjectEnum;
import cn.unitid.matrix.pkiservice.sdk.svs.request.CertVerifyContentRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.CertVerifyContentResponse;

/**
 * 作者： 徐霁
 * <p>
 * 时间： 2021/5/24 13:07
 * <p>
 * 描述信息： 证书校验
 */
public class CertVerifyContentTest {

    public static void main(String[] args) {

        // 提供的URL
        String url = "http://localhost:9200";

        // 您的appKey
        String appKey = "idAuyrX2Ef";

        // 您的appSecret
        String appSecret = "ypxc73irzHOooQSXWq2Gyta1W8g99n6a";

        // 1.原客户端
        PkiClient pkiClient = new PkiOkhttpClient(url, appKey, appSecret);

        // 2.调用出错，自动重试客户端
        String certBase64 = "MIICHjCCAcSgAwIBAgINAP9PMCf3zzUlKMT42jAKBggqgRzPVQGDdTBJMQswCQYDVQQGEwJDTjESMBAGA1UECAwJ5rGf6IuP55yBMRIwEAYDVQQHDAnljZfkuqzluIIxEjAQBgNVBAMMCXRlc3RTTTJDQTAeFw0yMjEyMTIwMjE0MDBaFw0yMzEyMTIwMjE0MDBaMFAxCzAJBgNVBAYTAkNOMTAwLgYDVQQtDCdkZmU2YjEzNGY3Mzc0NWZlYTQxOTAyYTQ3YWM2Nzk2NDIwMjIxMjYxDzANBgNVBAMMBua1i+ivlTBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABK1Znl91w6jfyLC04Qe3SOicLo4oN/HJT619yl1WEJQpiVnJhMUyA4KpLgpaSWTHLS26DP63ApS1sWekMw+75wajgYkwgYYwCQYDVR0TBAIwADAdBgNVHQ4EFgQUl2ToK12uAU1qBL537AZKj++CcYMwKQYDVR0lBCIwIAYIKwYBBQUHAwIGCCsGAQUFBwMEBgorBgEEAYI3CgMEMA4GA1UdDwEB/wQEAwIDODAfBgNVHSMEGDAWgBQTcZFDuLKDKvEWLGWUNnpKpY0mJzAKBggqgRzPVQGDdQNIADBFAiEAkzooU87PUStmM+x1voKIGteUUOdu0OZ5d8Q90DAPJC0CIHJiq6+KK0oZ2c2pug6NoNsqy9UXSaRms2VXw2YR2U+m";

        String subjectValue = "测试";

        CertVerifyContentRequest request = new CertVerifyContentRequest();
        // 证书内容
        request.setCert(certBase64);
        // 验证证书是否失效
        request.setVerifyNotAfter(true);
        // 验证证书是否生效
        request.setVerifyNotBefore(true);

        // 是否验证证书链
        request.setVerifyChain(true);

        // 验证证书主题是否为指定内容
        request.setSubject(SubjectEnum.CN);
        request.setSubjectValue(subjectValue);

        // 是否校验crl
        request.setVerifyCrl(Boolean.FALSE);

        // OCSP 验证
        request.setOcsp(true);
        request.setCaCert("MIIBzTCCAXCgAwIBAgIGAXKnMKNyMAwGCCqBHM9VAYN1BQAwSTELMAkGA1UEBhMC\n" +
                "Q04xDjAMBgNVBAoTBUdNU1NMMRAwDgYDVQQLEwdQS0kvU00yMRgwFgYDVQQDEw9S\n" +
                "b290Q0EgZm9yIFRlc3QwIhgPMjAxNTEyMzExNjAwMDBaGA8yMDM1MTIzMDE2MDAw\n" +
                "MFowSTELMAkGA1UEBhMCQ04xDjAMBgNVBAoTBUdNU1NMMRAwDgYDVQQLEwdQS0kv\n" +
                "U00yMRgwFgYDVQQDEw9Sb290Q0EgZm9yIFRlc3QwWTATBgcqhkjOPQIBBggqgRzP\n" +
                "VQGCLQNCAATj+apYlL+ddWXZ7+mFZXZJGbcJFXUN+Fszz6humeyWZP4qEEr2N0+a\n" +
                "Zdwo/21ft232yo0jPLzdscKB261zSQXSoz4wPDAZBgNVHQ4EEgQQnGnsD7oaOcWv\n" +
                "6CTrspwSBDAPBgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIAxjAMBggqgRzP\n" +
                "VQGDdQUAA0kAMEYCIQCEnW5BlQh0vmsOLxSoXYc/7zs++wWyFc1tnBHENR4ElwIh\n" +
                "AI1Lwu6in1ruflZhzseWulXwcITf3bm/Y5X1g1XFWQUH");

        try {
            CertVerifyContentResponse response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
                System.out.println("成功：" + response.getBody());
            } else {
                System.out.println("失败：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}
