rsanchez
2015-09-22 7d8713e88c7872b195f7e4d02ac4812536ae196b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package net.curisit.securis;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.LicUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LicenseValidator {
    private static final Logger LOG = LogManager.getLogger(LicenseValidator.class);
    private static LicenseValidator singleton = new LicenseValidator();
    protected static byte[] LOGO_SECRET;
    static {
        try {
            LOGO_SECRET = "Logo ipsum s3cr3t test áíóú".getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            LOG.error("Unexpected error getting LOGO secret", e);
        }
    }
    private LicenseValidator() {
    }
    public static LicenseValidator getInstance() {
        return singleton;
    }
    /**
     * @return CRC string for customer logo
     */
    public String getCrcLogo() {
        InputStream is = getClass().getClassLoader().getResourceAsStream("images/logo_customer.png");
        try {
            String shaLogo = LicUtils.sha256(IOUtils.toByteArray(is), LOGO_SECRET);
            return shaLogo;
        } catch (IOException e) {
            LOG.warn("Customer logo was not found in images/logo_customer.png");
            return null;
        }
    }
    public void validateLogo(RequestBean reqBean) throws SeCurisException {
        if (reqBean.getCrcLogo() == null) {
            LOG.info("Customer logo is not included in license file and won't be validated");
        } else {
            String currentCRC = getCrcLogo();
            if (!currentCRC.equals(reqBean.getCrcLogo()))
                throw new SeCurisException("License logo validation failed for request data: " + JsonUtils.toJSON(reqBean));
        }
    }
    public void validateHW(RequestBean reqBean, String licTypeCode, String customerCode, String packCode) throws SeCurisException {
        RequestBean currentHW = ReqGenerator.getInstance().createRequest(licTypeCode, customerCode, packCode);
        if (!currentHW.match(reqBean)) {
            throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"
                    + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
        }
    }
    public void validateHW(RequestBean reqBean, String appCode, String activationCode) throws SeCurisException {
        RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, activationCode);
        if (!currentHW.match(reqBean)) {
            throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"
                    + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
        }
    }
}