Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/LicenseGenerator.java
....@@ -1,3 +1,6 @@
1
+/*
2
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+ */
14 package net.curisit.securis;
25
36 import java.io.File;
....@@ -24,23 +27,35 @@
2427 import org.apache.logging.log4j.LogManager;
2528 import org.apache.logging.log4j.Logger;
2629
30
+import jakarta.inject.Singleton;
31
+
2732 /**
28
- * License generator and signer
29
- *
30
- * @author roberto <roberto.sanchez@curisit.net>
31
- */
32
-@javax.inject.Singleton
33
+* LicenseGenerator
34
+* <p>
35
+* Factory for building and signing {@link LicenseBean} instances. Uses a process-wide
36
+* singleton and expects a PKCS#8 private key at:
37
+* <code>~/.SeCuris/keys/securis_private_key.pkcs8</code>.
38
+*
39
+* @author JRA
40
+* Last reviewed by JRA on Oct 5, 2025.
41
+*/
42
+@Singleton
3343 public class LicenseGenerator {
3444
3545 private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class);
3646
3747 private static LicenseGenerator singleton = new LicenseGenerator();
3848
49
+ /**
50
+ * getInstance<p>
51
+ * Singleton accessor.
52
+ */
3953 public static LicenseGenerator getInstance() {
4054 return singleton;
4155 }
4256
4357 /**
58
+ * generateLicense<p>
4459 * Generate a license bean with the specified data
4560 *
4661 * @param req
....@@ -66,12 +81,14 @@
6681 }
6782
6883 /**
69
- * Generate a license file using a {@link LicenseBean}
70
- *
71
- * @param license
72
- * @param file
73
- * @throws SeCurisException
74
- */
84
+ * save
85
+ * <p>
86
+ * Persist a pretty-printed JSON representation of the signed license to disk.
87
+ *
88
+ * @param license source license
89
+ * @param file target file path
90
+ * @throws SeCurisException if serialization or IO fails
91
+ */
7592 public void save(LicenseBean license, File file) throws SeCurisException {
7693 SignedLicenseBean signedLic = new SignedLicenseBean(license);
7794 byte[] json;
....@@ -91,15 +108,14 @@
91108 }
92109
93110 /**
94
- *
95
- * @param licBean
96
- * @return
97
- * @throws NoSuchAlgorithmException
98
- * @throws IOException
99
- * @throws InvalidKeySpecException
100
- * @throws InvalidKeyException
101
- * @throws SignatureException
102
- */
111
+ * sign
112
+ * <p>
113
+ * Compute a Base64 signature for the given license and set it into the bean.
114
+ *
115
+ * @param licBean license to sign (in-place)
116
+ * @return Base64 signature string
117
+ * @throws SeCurisException if the signature process fails
118
+ */
103119 public String sign(LicenseBean licBean) throws SeCurisException {
104120 SignatureHelper sh = SignatureHelper.getInstance();
105121
....@@ -114,16 +130,8 @@
114130 byte[] signatureData = signature.sign();
115131 licBean.setSignature(Base64.encodeBase64String(signatureData));
116132 return licBean.getSignature();
117
- } catch (NoSuchAlgorithmException e) {
118
- LOG.error("Error signing license for " + licBean, e);
119
- } catch (InvalidKeyException e) {
120
- LOG.error("Error signing license for " + licBean, e);
121
- } catch (InvalidKeySpecException e) {
122
- LOG.error("Error signing license for " + licBean, e);
123
- } catch (IOException e) {
124
- LOG.error("Error signing license for " + licBean, e);
125
- } catch (SignatureException e) {
126
- LOG.error("Error signing license for " + licBean, e);
133
+ } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException | IOException | SignatureException e) {
134
+ LOG.error("Error signing license for {}", licBean, e);
127135 }
128136 throw new SeCurisException("License could not be generated");
129137 }