| .. | .. |
|---|
| 8 | 8 | import java.nio.file.StandardOpenOption; |
|---|
| 9 | 9 | import java.security.InvalidKeyException; |
|---|
| 10 | 10 | import java.security.NoSuchAlgorithmException; |
|---|
| 11 | | -import java.security.NoSuchProviderException; |
|---|
| 12 | 11 | import java.security.Signature; |
|---|
| 13 | 12 | import java.security.SignatureException; |
|---|
| 14 | 13 | import java.security.spec.InvalidKeySpecException; |
|---|
| 15 | 14 | import java.text.MessageFormat; |
|---|
| 16 | 15 | import java.util.Date; |
|---|
| 17 | 16 | import java.util.Map; |
|---|
| 17 | +import java.util.TreeMap; |
|---|
| 18 | 18 | |
|---|
| 19 | 19 | import net.curisit.securis.beans.LicenseBean; |
|---|
| 20 | 20 | import net.curisit.securis.beans.RequestBean; |
|---|
| 21 | +import net.curisit.securis.beans.SignedLicenseBean; |
|---|
| 21 | 22 | import net.curisit.securis.utils.JsonUtils; |
|---|
| 22 | 23 | |
|---|
| 23 | 24 | import org.apache.commons.net.util.Base64; |
|---|
| 24 | | -import org.slf4j.Logger; |
|---|
| 25 | | -import org.slf4j.LoggerFactory; |
|---|
| 25 | +import org.apache.logging.log4j.LogManager; |
|---|
| 26 | +import org.apache.logging.log4j.Logger; |
|---|
| 26 | 27 | |
|---|
| 27 | 28 | /** |
|---|
| 28 | 29 | * License generator and signer |
|---|
| .. | .. |
|---|
| 31 | 32 | */ |
|---|
| 32 | 33 | public class LicenseGenerator { |
|---|
| 33 | 34 | |
|---|
| 34 | | - private static final Logger log = LoggerFactory.getLogger(LicenseGenerator.class); |
|---|
| 35 | + private static final Logger log = LogManager.getLogger(LicenseGenerator.class); |
|---|
| 35 | 36 | |
|---|
| 36 | 37 | private static LicenseGenerator singleton = new LicenseGenerator(); |
|---|
| 37 | 38 | |
|---|
| .. | .. |
|---|
| 55 | 56 | * @return |
|---|
| 56 | 57 | * @throws SeCurisException |
|---|
| 57 | 58 | */ |
|---|
| 58 | | - public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseCode) throws SeCurisException { |
|---|
| 59 | + public LicenseBean generateLicense(RequestBean req, Map<String, Object> metadata, Date expirationDate, String licenseType, String licenseCode) throws SeCurisException { |
|---|
| 59 | 60 | log.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode())); |
|---|
| 60 | 61 | LicenseBean license = new LicenseBean(req); |
|---|
| 62 | + license.setLicenseType(licenseType); |
|---|
| 61 | 63 | license.setLicenseCode(licenseCode); |
|---|
| 62 | 64 | license.setExpirationDate(expirationDate); |
|---|
| 63 | 65 | license.setMetadata(metadata); |
|---|
| .. | .. |
|---|
| 74 | 76 | * @throws SeCurisException |
|---|
| 75 | 77 | */ |
|---|
| 76 | 78 | public void save(LicenseBean license, File file) throws SeCurisException { |
|---|
| 79 | + SignedLicenseBean signedLic = new SignedLicenseBean(license); |
|---|
| 77 | 80 | byte[] json; |
|---|
| 78 | 81 | try { |
|---|
| 79 | | - json = JsonUtils.toJSON(license, true).getBytes("utf-8"); |
|---|
| 82 | + json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8"); |
|---|
| 80 | 83 | Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE); |
|---|
| 81 | 84 | } catch (UnsupportedEncodingException e) { |
|---|
| 82 | 85 | log.error("Error creating json doc from license: " + license, e); |
|---|
| .. | .. |
|---|
| 128 | 131 | throw new SeCurisException("License could not be generated"); |
|---|
| 129 | 132 | } |
|---|
| 130 | 133 | |
|---|
| 131 | | - public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, InvalidKeyException, SignatureException { |
|---|
| 134 | + public static void main(String[] args) throws SeCurisException { |
|---|
| 135 | + Paths.get(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req").toURI()); |
|---|
| 136 | + |
|---|
| 137 | + RequestBean req = ReqGenerator.getInstance().loadRequest(new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.req")); |
|---|
| 138 | + Map<String, Object> metadata = new TreeMap<>(); |
|---|
| 139 | + metadata.put("maxUsers", 23); |
|---|
| 140 | + metadata.put("maxInstances", 12); |
|---|
| 141 | + LicenseBean lic = LicenseGenerator.getInstance().generateLicense(req, metadata, new Date(new Date().getTime() + 3600 * 1000), "L01", "LIC-9812987123-12837129873"); |
|---|
| 142 | + LicenseGenerator.getInstance().save(lic, new File("/Users/cproberto/Documents/wsCurisIT/SeCurisClient/license.lic")); |
|---|
| 132 | 143 | |
|---|
| 133 | 144 | System.out.print("os.arch: " + System.getProperty("os.arch") + " " + System.getProperty("os.name")); |
|---|
| 134 | 145 | |
|---|