deleted file mode 100644| .. | .. |
|---|
| 1 | | -package net.curisit.securis; |
|---|
| 2 | | - |
|---|
| 3 | | -import java.io.File; |
|---|
| 4 | | -import java.io.IOException; |
|---|
| 5 | | -import java.io.UnsupportedEncodingException; |
|---|
| 6 | | -import java.nio.file.Files; |
|---|
| 7 | | -import java.nio.file.Paths; |
|---|
| 8 | | -import java.nio.file.StandardOpenOption; |
|---|
| 9 | | -import java.security.InvalidKeyException; |
|---|
| 10 | | -import java.security.NoSuchAlgorithmException; |
|---|
| 11 | | -import java.security.Signature; |
|---|
| 12 | | -import java.security.SignatureException; |
|---|
| 13 | | -import java.security.spec.InvalidKeySpecException; |
|---|
| 14 | | -import java.text.MessageFormat; |
|---|
| 15 | | -import java.util.Date; |
|---|
| 16 | | -import java.util.Map; |
|---|
| 17 | | -import java.util.TreeMap; |
|---|
| 18 | | - |
|---|
| 19 | | -import net.curisit.securis.beans.LicenseBean; |
|---|
| 20 | | -import net.curisit.securis.beans.RequestBean; |
|---|
| 21 | | -import net.curisit.securis.beans.SignedLicenseBean; |
|---|
| 22 | | -import net.curisit.securis.utils.JsonUtils; |
|---|
| 23 | | -import net.curisit.securis.utils.SignatureHelper; |
|---|
| 24 | | - |
|---|
| 25 | | -import org.apache.commons.net.util.Base64; |
|---|
| 26 | | -import org.apache.logging.log4j.LogManager; |
|---|
| 27 | | -import org.apache.logging.log4j.Logger; |
|---|
| 28 | | - |
|---|
| 29 | | -/** |
|---|
| 30 | | - * License generator and signer |
|---|
| 31 | | - * |
|---|
| 32 | | - * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 33 | | - */ |
|---|
| 34 | | -public class LicenseGenerator { |
|---|
| 35 | | - |
|---|
| 36 | | - private static final Logger log = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 37 | | - |
|---|
| 38 | | - private static LicenseGenerator singleton = new LicenseGenerator(); |
|---|
| 39 | | - |
|---|
| 40 | | - private LicenseGenerator() { |
|---|
| 41 | | - } |
|---|
| 42 | | - |
|---|
| 43 | | - public static LicenseGenerator getInstance() { |
|---|
| 44 | | - return singleton; |
|---|
| 45 | | - } |
|---|
| 46 | | - |
|---|
| 47 | | - /** |
|---|
| 48 | | - * Generate a license bean with the specified data |
|---|
| 49 | | - * |
|---|
| 50 | | - * @param hw |
|---|
| 51 | | - * @param customerCode |
|---|
| 52 | | - * - e.g: "BP" |
|---|
| 53 | | - * @param maxInstances |
|---|
| 54 | | - * @param maxUsers |
|---|
| 55 | | - * @param maxTimeThreshold |
|---|
| 56 | | - * Max time between synchronizations expressed in days |
|---|
| 57 | | - * @return |
|---|
| 58 | | - * @throws SeCurisException |
|---|
| 59 | | - */ |
|---|
| 60 | | - public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseType, String licenseCode) throws SeCurisException { |
|---|
| 61 | | - log.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode())); |
|---|
| 62 | | - LicenseBean license = new LicenseBean(req); |
|---|
| 63 | | - license.setLicenseType(licenseType); |
|---|
| 64 | | - license.setLicenseCode(licenseCode); |
|---|
| 65 | | - license.setExpirationDate(expirationDate); |
|---|
| 66 | | - license.setMetadata(metadata); |
|---|
| 67 | | - sign(license); |
|---|
| 68 | | - |
|---|
| 69 | | - return license; |
|---|
| 70 | | - } |
|---|
| 71 | | - |
|---|
| 72 | | - /** |
|---|
| 73 | | - * Generate a license file using a {@link LicenseBean} |
|---|
| 74 | | - * |
|---|
| 75 | | - * @param license |
|---|
| 76 | | - * @param file |
|---|
| 77 | | - * @throws SeCurisException |
|---|
| 78 | | - */ |
|---|
| 79 | | - public void save(LicenseBean license, File file) throws SeCurisException { |
|---|
| 80 | | - SignedLicenseBean signedLic = new SignedLicenseBean(license); |
|---|
| 81 | | - byte[] json; |
|---|
| 82 | | - try { |
|---|
| 83 | | - json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8"); |
|---|
| 84 | | - Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
|---|
| 85 | | - } catch (UnsupportedEncodingException e) { |
|---|
| 86 | | - log.error("Error creating json doc from license: " + license, e); |
|---|
| 87 | | - throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 88 | | - } catch (IOException e) { |
|---|
| 89 | | - log.error("Error creating license file: " + file, e); |
|---|
| 90 | | - throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 91 | | - } |
|---|
| 92 | | - |
|---|
| 93 | | - log.info("License saved in {}", file); |
|---|
| 94 | | - |
|---|
| 95 | | - } |
|---|
| 96 | | - |
|---|
| 97 | | - /** |
|---|
| 98 | | - * TODO: This method should be removed from client code. |
|---|
| 99 | | - * |
|---|
| 100 | | - * @param licBean |
|---|
| 101 | | - * @return |
|---|
| 102 | | - * @throws NoSuchAlgorithmException |
|---|
| 103 | | - * @throws IOException |
|---|
| 104 | | - * @throws InvalidKeySpecException |
|---|
| 105 | | - * @throws InvalidKeyException |
|---|
| 106 | | - * @throws SignatureException |
|---|
| 107 | | - */ |
|---|
| 108 | | - public String sign(LicenseBean licBean) throws SeCurisException { |
|---|
| 109 | | - SignatureHelper sh = SignatureHelper.getInstance(); |
|---|
| 110 | | - |
|---|
| 111 | | - Signature signature; |
|---|
| 112 | | - try { |
|---|
| 113 | | - signature = Signature.getInstance(SignatureHelper.SIGNATURE_GENERATION_ALGORITHM); |
|---|
| 114 | | - signature.initSign(sh.generatePrivateKey(new File("/Users/cproberto/Documents/wsPython/doky/tests/securis_private_key.pkcs8"))); |
|---|
| 115 | | - |
|---|
| 116 | | - sh.prepareSignature(signature, licBean); |
|---|
| 117 | | - |
|---|
| 118 | | - byte[] signatureData = signature.sign(); |
|---|
| 119 | | - licBean.setSignature(Base64.encodeBase64String(signatureData)); |
|---|
| 120 | | - return licBean.getSignature(); |
|---|
| 121 | | - } catch (NoSuchAlgorithmException e) { |
|---|
| 122 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 123 | | - } catch (InvalidKeyException e) { |
|---|
| 124 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 125 | | - } catch (InvalidKeySpecException e) { |
|---|
| 126 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 127 | | - } catch (IOException e) { |
|---|
| 128 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 129 | | - } catch (SignatureException e) { |
|---|
| 130 | | - log.error("Error signing license for " + licBean, e); |
|---|
| 131 | | - } |
|---|
| 132 | | - throw new SeCurisException("License could not be generated"); |
|---|
| 133 | | - } |
|---|
| 134 | | - |
|---|
| 135 | | - public static void main(String[] args) throws SeCurisException { |
|---|
| 136 | | - Paths.get(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req").toURI()); |
|---|
| 137 | | - |
|---|
| 138 | | - RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Downloads/license-2.req")); |
|---|
| 139 | | - Map<String, Object> metadata = new TreeMap<>(); |
|---|
| 140 | | - metadata.put("maxUsers", 0); |
|---|
| 141 | | - metadata.put("maxInstances", 0); |
|---|
| 142 | | - metadata.put("timeThreshold", 0); |
|---|
| 143 | | - metadata.put("datasetPrefix", "BP"); |
|---|
| 144 | | - metadata.put("extendedMode", true); |
|---|
| 145 | | - |
|---|
| 146 | | - LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, new Date(new Date().getTime() + 3600 * 1000), "CI-CS-01", "LIC-CESAR-00001"); |
|---|
| 147 | | - LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/license_cesar.lic")); |
|---|
| 148 | | - |
|---|
| 149 | | - System.out.print("os.arch: " + System.getProperty("os.arch") + " " + System.getProperty("os.name")); |
|---|
| 150 | | - |
|---|
| 151 | | - } |
|---|
| 152 | | -} |
|---|