From e168a1dee09a0a539fe45b45452317c901a2ce97 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Thu, 17 Jul 2014 14:23:29 +0000
Subject: [PATCH] #0 feature - Added patch to create licenses

---
 securis/pom.xml                                                  |    5 +
 securis/src/patch/java/net/curisit/securis/LicenseGenerator.java |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 157 insertions(+), 0 deletions(-)

diff --git a/securis/pom.xml b/securis/pom.xml
index 3ce0141..31ab19f 100644
--- a/securis/pom.xml
+++ b/securis/pom.xml
@@ -46,6 +46,11 @@
   		<artifactId>resteasy-jaxrs</artifactId>
   		<version>3.0.5.Final</version>
   	</dependency>
+  	<dependency>
+  		<groupId>net.curisit</groupId>
+  		<artifactId>securis-client</artifactId>
+  		<version>0.9.4-SNAPSHOT</version>
+  	</dependency>
   </dependencies>
 	  <build>
 		<plugins>
diff --git a/securis/src/patch/java/net/curisit/securis/LicenseGenerator.java b/securis/src/patch/java/net/curisit/securis/LicenseGenerator.java
new file mode 100644
index 0000000..3675c26
--- /dev/null
+++ b/securis/src/patch/java/net/curisit/securis/LicenseGenerator.java
@@ -0,0 +1,152 @@
+package net.curisit.securis;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.InvalidKeySpecException;
+import java.text.MessageFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.TreeMap;
+
+import net.curisit.securis.beans.LicenseBean;
+import net.curisit.securis.beans.RequestBean;
+import net.curisit.securis.beans.SignedLicenseBean;
+import net.curisit.securis.utils.JsonUtils;
+import net.curisit.securis.utils.SignatureHelper;
+
+import org.apache.commons.net.util.Base64;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * License generator and signer
+ * 
+ * @author roberto <roberto.sanchez@curisit.net>
+ */
+public class LicenseGenerator {
+
+	private static final Logger log = LogManager.getLogger(LicenseGenerator.class);
+
+	private static LicenseGenerator singleton = new LicenseGenerator();
+
+	private LicenseGenerator() {
+	}
+
+	public static LicenseGenerator getInstance() {
+		return singleton;
+	}
+
+	/**
+	 * Generate a license bean with the specified data
+	 * 
+	 * @param hw
+	 * @param customerCode
+	 *            - e.g: "BP"
+	 * @param maxInstances
+	 * @param maxUsers
+	 * @param maxTimeThreshold
+	 *            Max time between synchronizations expressed in days
+	 * @return
+	 * @throws SeCurisException
+	 */
+	public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseType, String licenseCode) throws SeCurisException {
+		log.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode()));
+		LicenseBean license = new LicenseBean(req);
+		license.setLicenseType(licenseType);
+		license.setLicenseCode(licenseCode);
+		license.setExpirationDate(expirationDate);
+		license.setMetadata(metadata);
+		sign(license);
+
+		return license;
+	}
+
+	/**
+	 * Generate a license file using a {@link LicenseBean}
+	 * 
+	 * @param license
+	 * @param file
+	 * @throws SeCurisException
+	 */
+	public void save(LicenseBean license, File file) throws SeCurisException {
+		SignedLicenseBean signedLic = new SignedLicenseBean(license);
+		byte[] json;
+		try {
+			json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
+			Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
+		} catch (UnsupportedEncodingException e) {
+			log.error("Error creating json doc from license: " + license, e);
+			throw new SeCurisException("Error creating json doc from license: " + license, e);
+		} catch (IOException e) {
+			log.error("Error creating license file: " + file, e);
+			throw new SeCurisException("Error creating json doc from license: " + license, e);
+		}
+
+		log.info("License saved in {}", file);
+
+	}
+
+	/**
+	 * TODO: This method should be removed from client code.
+	 * 
+	 * @param licBean
+	 * @return
+	 * @throws NoSuchAlgorithmException
+	 * @throws IOException
+	 * @throws InvalidKeySpecException
+	 * @throws InvalidKeyException
+	 * @throws SignatureException
+	 */
+	public String sign(LicenseBean licBean) throws SeCurisException {
+		SignatureHelper sh = SignatureHelper.getInstance();
+
+		Signature signature;
+		try {
+			signature = Signature.getInstance(SignatureHelper.SIGNATURE_GENERATION_ALGORITHM);
+			signature.initSign(sh.generatePrivateKey(new File("/Users/cproberto/Documents/wsPython/doky/tests/securis_private_key.pkcs8")));
+
+			sh.prepareSignature(signature, licBean);
+
+			byte[] signatureData = signature.sign();
+			licBean.setSignature(Base64.encodeBase64String(signatureData));
+			return licBean.getSignature();
+		} catch (NoSuchAlgorithmException e) {
+			log.error("Error signing license for " + licBean, e);
+		} catch (InvalidKeyException e) {
+			log.error("Error signing license for " + licBean, e);
+		} catch (InvalidKeySpecException e) {
+			log.error("Error signing license for " + licBean, e);
+		} catch (IOException e) {
+			log.error("Error signing license for " + licBean, e);
+		} catch (SignatureException e) {
+			log.error("Error signing license for " + licBean, e);
+		}
+		throw new SeCurisException("License could not be generated");
+	}
+
+	public static void main(String[] args) throws SeCurisException {
+		Paths.get(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req").toURI());
+
+		RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Downloads/license-2.req"));
+		Map<String, Object> metadata = new TreeMap<>();
+		metadata.put("maxUsers", 0);
+		metadata.put("maxInstances", 0);
+		metadata.put("timeThreshold", 0);
+		metadata.put("datasetPrefix", "BP");
+		metadata.put("extendedMode", true);
+
+		LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, new Date(new Date().getTime() + 3600 * 1000), "CI-CS-01", "LIC-CESAR-00001");
+		LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Desktop/license_cesar.lic"));
+
+		System.out.print("os.arch: " + System.getProperty("os.arch") + " " + System.getProperty("os.name"));
+
+	}
+}

--
Gitblit v1.3.2