| .. | .. |
|---|
| 33 | 33 | */ |
|---|
| 34 | 34 | public class LicenseGenerator { |
|---|
| 35 | 35 | |
|---|
| 36 | | - private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 36 | + private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 37 | 37 | |
|---|
| 38 | | - private static LicenseGenerator singleton = new LicenseGenerator(); |
|---|
| 38 | + private static LicenseGenerator singleton = new LicenseGenerator(); |
|---|
| 39 | 39 | |
|---|
| 40 | | - private LicenseGenerator() { |
|---|
| 41 | | - } |
|---|
| 40 | + private LicenseGenerator() {} |
|---|
| 42 | 41 | |
|---|
| 43 | | - public static LicenseGenerator getInstance() { |
|---|
| 44 | | - return singleton; |
|---|
| 45 | | - } |
|---|
| 42 | + public static LicenseGenerator getInstance() { |
|---|
| 43 | + return singleton; |
|---|
| 44 | + } |
|---|
| 46 | 45 | |
|---|
| 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); |
|---|
| 46 | + /** |
|---|
| 47 | + * Generate a license bean with the specified data |
|---|
| 48 | + * |
|---|
| 49 | + * @param hw |
|---|
| 50 | + * @param customerCode |
|---|
| 51 | + * - e.g: "BP" |
|---|
| 52 | + * @param maxInstances |
|---|
| 53 | + * @param maxUsers |
|---|
| 54 | + * @param maxTimeThreshold |
|---|
| 55 | + * Max time between synchronizations expressed in days |
|---|
| 56 | + * @return |
|---|
| 57 | + * @throws SeCurisException |
|---|
| 58 | + */ |
|---|
| 59 | + public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseType, String licenseCode) |
|---|
| 60 | + throws SeCurisException { |
|---|
| 61 | + LOG.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), |
|---|
| 62 | + req.getAppCode())); |
|---|
| 63 | + LicenseBean license = new LicenseBean(req); |
|---|
| 64 | + license.setLicenseType(licenseType); |
|---|
| 65 | + license.setLicenseCode(licenseCode); |
|---|
| 66 | + license.setExpirationDate(expirationDate); |
|---|
| 67 | + license.setMetadata(metadata); |
|---|
| 68 | + sign(license); |
|---|
| 68 | 69 | |
|---|
| 69 | | - return license; |
|---|
| 70 | | - } |
|---|
| 70 | + return license; |
|---|
| 71 | + } |
|---|
| 71 | 72 | |
|---|
| 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 | | - } |
|---|
| 73 | + /** |
|---|
| 74 | + * Generate a license file using a {@link LicenseBean} |
|---|
| 75 | + * |
|---|
| 76 | + * @param license |
|---|
| 77 | + * @param file |
|---|
| 78 | + * @throws SeCurisException |
|---|
| 79 | + */ |
|---|
| 80 | + public void save(LicenseBean license, File file) throws SeCurisException { |
|---|
| 81 | + SignedLicenseBean signedLic = new SignedLicenseBean(license); |
|---|
| 82 | + byte[] json; |
|---|
| 83 | + try { |
|---|
| 84 | + json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8"); |
|---|
| 85 | + Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
|---|
| 86 | + } catch (UnsupportedEncodingException e) { |
|---|
| 87 | + LOG.error("Error creating json doc from license: " + license, e); |
|---|
| 88 | + throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 89 | + } catch (IOException e) { |
|---|
| 90 | + LOG.error("Error creating license file: " + file, e); |
|---|
| 91 | + throw new SeCurisException("Error creating json doc from license: " + license, e); |
|---|
| 92 | + } |
|---|
| 92 | 93 | |
|---|
| 93 | | - LOG.info("License saved in {}", file); |
|---|
| 94 | + LOG.info("License saved in {}", file); |
|---|
| 94 | 95 | |
|---|
| 95 | | - } |
|---|
| 96 | + } |
|---|
| 96 | 97 | |
|---|
| 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(); |
|---|
| 98 | + /** |
|---|
| 99 | + * TODO: This method should be removed from client code. |
|---|
| 100 | + * |
|---|
| 101 | + * @param licBean |
|---|
| 102 | + * @return |
|---|
| 103 | + * @throws NoSuchAlgorithmException |
|---|
| 104 | + * @throws IOException |
|---|
| 105 | + * @throws InvalidKeySpecException |
|---|
| 106 | + * @throws InvalidKeyException |
|---|
| 107 | + * @throws SignatureException |
|---|
| 108 | + */ |
|---|
| 109 | + public String sign(LicenseBean licBean) throws SeCurisException { |
|---|
| 110 | + SignatureHelper sh = SignatureHelper.getInstance(); |
|---|
| 110 | 111 | |
|---|
| 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"))); |
|---|
| 112 | + Signature signature; |
|---|
| 113 | + try { |
|---|
| 114 | + signature = Signature.getInstance(SignatureHelper.SIGNATURE_GENERATION_ALGORITHM); |
|---|
| 115 | + signature.initSign(sh.generatePrivateKey(new File("/Users/cproberto/Documents/wsPython/doky/tests/securis_private_key.pkcs8"))); |
|---|
| 115 | 116 | |
|---|
| 116 | | - sh.prepareSignature(signature, licBean); |
|---|
| 117 | + sh.prepareSignature(signature, licBean); |
|---|
| 117 | 118 | |
|---|
| 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 | | - } |
|---|
| 119 | + byte[] signatureData = signature.sign(); |
|---|
| 120 | + licBean.setSignature(Base64.encodeBase64String(signatureData)); |
|---|
| 121 | + return licBean.getSignature(); |
|---|
| 122 | + } catch (NoSuchAlgorithmException e) { |
|---|
| 123 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 124 | + } catch (InvalidKeyException e) { |
|---|
| 125 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 126 | + } catch (InvalidKeySpecException e) { |
|---|
| 127 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 128 | + } catch (IOException e) { |
|---|
| 129 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 130 | + } catch (SignatureException e) { |
|---|
| 131 | + LOG.error("Error signing license for " + licBean, e); |
|---|
| 132 | + } |
|---|
| 133 | + throw new SeCurisException("License could not be generated"); |
|---|
| 134 | + } |
|---|
| 134 | 135 | |
|---|
| 135 | | - public static void main(String[] args) throws SeCurisException { |
|---|
| 136 | + public static void main(String[] args) throws SeCurisException { |
|---|
| 136 | 137 | |
|---|
| 137 | | - RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req")); |
|---|
| 138 | | - Map<String, Object> metadata = new TreeMap<>(); |
|---|
| 139 | | -// CurisData parameters: |
|---|
| 140 | | -// metadata.put("maxUsers", 5); |
|---|
| 141 | | -// metadata.put("maxSessionUsers", 100); |
|---|
| 142 | | - // curisIntegrity |
|---|
| 143 | | - metadata.put("maxUsers", 0); |
|---|
| 144 | | - metadata.put("maxInstances", 0); |
|---|
| 145 | | - metadata.put("timeThreshold", 0); |
|---|
| 146 | | - metadata.put("datasetPrefix", "BP"); |
|---|
| 147 | | - metadata.put("extendedMode", true); |
|---|
| 148 | | - Date expirationDate = new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10)); |
|---|
| 149 | | - LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, expirationDate, "CI-01", "LIC-CURISTEC-0001"); |
|---|
| 150 | | - LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/AxelLicCI.lic")); |
|---|
| 138 | + RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req")); |
|---|
| 139 | + Map<String, Object> metadata = new TreeMap<>(); |
|---|
| 140 | + // CurisData parameters: |
|---|
| 141 | + // metadata.put("maxUsers", 5); |
|---|
| 142 | + // metadata.put("maxSessionUsers", 100); |
|---|
| 143 | + // curisIntegrity |
|---|
| 144 | + metadata.put("maxUsers", 0); |
|---|
| 145 | + metadata.put("maxInstances", 0); |
|---|
| 146 | + metadata.put("timeThreshold", 0); |
|---|
| 147 | + metadata.put("datasetPrefix", "BP"); |
|---|
| 148 | + metadata.put("extendedMode", true); |
|---|
| 149 | + Date expirationDate = new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10)); |
|---|
| 150 | + LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, expirationDate, "CI-01", "LIC-CURISTEC-0001"); |
|---|
| 151 | + LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/AxelLicCI.lic")); |
|---|
| 151 | 152 | |
|---|
| 152 | | - System.out.print("License expires at: " + expirationDate.getTime()); |
|---|
| 153 | | - |
|---|
| 153 | + System.out.print("License expires at: " + expirationDate.getTime()); |
|---|
| 154 | 154 | |
|---|
| 155 | | - } |
|---|
| 155 | + } |
|---|
| 156 | 156 | } |
|---|