From 523edc2956a42bda7d33884fcbbb6018addda4b0 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 18 Apr 2017 17:07:34 +0000
Subject: [PATCH] #3582 feature - Changed API to take in account pack data, (valid end date, status, ...) and some minor fixes

---
 securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java             |  223 +++++++++++++++++++------------------
 securis/src/main/resources/db/initial_data.sql                                            |    2 
 securis/src/main/java/net/curisit/securis/services/ApiResource.java                       |   21 +++
 securis/src/main/java/net/curisit/securis/db/User.java                                    |    2 
 securis/src/main/java/net/curisit/securis/services/LicenseResource.java                   |    1 
 securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java |   65 +++++-----
 6 files changed, 169 insertions(+), 145 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/db/User.java b/securis/src/main/java/net/curisit/securis/db/User.java
index 731280f..39d1509 100644
--- a/securis/src/main/java/net/curisit/securis/db/User.java
+++ b/securis/src/main/java/net/curisit/securis/db/User.java
@@ -295,7 +295,7 @@
 		public static final int ADMIN = 0x02;
 		public static final int BASIC = 0x04;
 		public static final int API_CLIENT = 0x80;
-		public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC }; // ALL except the special API client
+		public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC, API_CLIENT };
 	}
 
 }
diff --git a/securis/src/main/java/net/curisit/securis/services/ApiResource.java b/securis/src/main/java/net/curisit/securis/services/ApiResource.java
index 5211a5e..4e1812b 100644
--- a/securis/src/main/java/net/curisit/securis/services/ApiResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/ApiResource.java
@@ -34,6 +34,7 @@
 import net.curisit.securis.db.LicenseHistory;
 import net.curisit.securis.db.LicenseStatus;
 import net.curisit.securis.db.Pack;
+import net.curisit.securis.db.PackStatus;
 import net.curisit.securis.db.User;
 import net.curisit.securis.db.User.Rol;
 import net.curisit.securis.ioc.EnsureTransaction;
@@ -197,7 +198,9 @@
 
 	/**
 	 * License validation on server side, in this case we validate that the
-	 * current licenses has not been cancelled.
+	 * current licenses has not been cancelled and they are still in valid
+	 * period. If the pack has reached the end valid period, the license is no
+	 * longer valid.
 	 * 
 	 * @param currentLic
 	 * @param bsc
@@ -219,12 +222,22 @@
 			throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired");
 		}
 
+		License existingLic = licenseHelper.getActiveLicenseFromDB(currentLic, em);
+
+		Pack pack = existingLic.getPack();
+		if (pack.getEndValidDate().before(new Date())) {
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_PACK_IS_NOT_VALID, "The pack end valid date has been reached");
+		}
+		if (pack.getStatus() != PackStatus.ACTIVE) {
+			LOG.error("The Pack {} status is not active, is: {}", pack.getCode(), pack.getStatus());
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_PACK_IS_NOT_VALID, "The pack status is not Active");
+		}
+
 		try {
 			SignatureHelper.getInstance().validateSignature(currentLic);
 		} catch (SeCurisException ex) {
 			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "The license signature is not valid");
 		}
-		licenseHelper.assertLicenseStatusIsActive(currentLic, em);
 
 		return Response.ok(currentLic).build();
 	}
@@ -353,6 +366,10 @@
 		} else {
 			pack = lic.getPack();
 		}
+		if (pack.getStatus() != PackStatus.ACTIVE) {
+			LOG.error("The Pack {} status is not active, is: {}", pack.getCode(), pack.getStatus());
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "The pack status is not Active");
+		}
 		SignedLicenseBean signedLicense;
 		try {
 			String licCode;
diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
index a296158..452413b 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
@@ -392,6 +392,7 @@
 		lic.setCreatedBy(createdBy);
 		lic.setCreationTimestamp(new Date());
 		lic.setModificationTimestamp(lic.getCreationTimestamp());
+		lic.setMetadataObsolete(false);
 
 		em.persist(lic);
 		em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
diff --git a/securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java b/securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java
index 40846c2..29b814b 100644
--- a/securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java
+++ b/securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java
@@ -4,46 +4,47 @@
 
 public class SeCurisServiceException extends CurisException {
 
-    private int errorCode = 0;
+	private int errorCode = 0;
 
-    public SeCurisServiceException(int errorCode, String msg) {
-        super(msg);
-        this.errorCode = errorCode;
-    }
+	public SeCurisServiceException(int errorCode, String msg) {
+		super(msg);
+		this.errorCode = errorCode;
+	}
 
-    public SeCurisServiceException(String msg) {
-        super(msg);
-        this.errorCode = ErrorCodes.UNEXPECTED_ERROR;
-    }
+	public SeCurisServiceException(String msg) {
+		super(msg);
+		this.errorCode = ErrorCodes.UNEXPECTED_ERROR;
+	}
 
-    public int getStatus() {
-        return errorCode;
-    }
+	public int getStatus() {
+		return errorCode;
+	}
 
-    /**
+	/**
 	 * 
 	 */
-    private static final long serialVersionUID = 1L;
+	private static final long serialVersionUID = 1L;
 
-    public static class ErrorCodes {
-        public static int UNEXPECTED_ERROR = 1000;
-        public static int INVALID_CREDENTIALS = 1001;
-        public static int UNAUTHORIZED_ACCESS = 1002;
-        public static int NOT_FOUND = 1003;
-        public static int INVALID_FORMAT = 1004;
-        public static int WRONG_STATUS = 1005;
+	public static class ErrorCodes {
+		public static int UNEXPECTED_ERROR = 1000;
+		public static int INVALID_CREDENTIALS = 1001;
+		public static int UNAUTHORIZED_ACCESS = 1002;
+		public static int NOT_FOUND = 1003;
+		public static int INVALID_FORMAT = 1004;
+		public static int WRONG_STATUS = 1005;
 
-        public static int INVALID_LICENSE_REQUEST_DATA = 1100;
-        public static int LICENSE_NOT_READY_FOR_RENEW = 1101;
-        public static int LICENSE_DATA_IS_NOT_VALID = 1102;
-        public static int LICENSE_IS_EXPIRED = 1103;
+		public static int INVALID_LICENSE_REQUEST_DATA = 1100;
+		public static int LICENSE_NOT_READY_FOR_RENEW = 1101;
+		public static int LICENSE_DATA_IS_NOT_VALID = 1102;
+		public static int LICENSE_IS_EXPIRED = 1103;
+		public static int LICENSE_PACK_IS_NOT_VALID = 1104;
 
-        public static int INVALID_REQUEST_DATA = 1201;
-        public static int INVALID_REQUEST_DATA_FORMAT = 1202;
-        public static int BLOCKED_REQUEST_DATA = 1203;
-        public static int DUPLICATED_REQUEST_DATA = 1204;
-        public static int NO_AVAILABLE_LICENSES = 1205;
+		public static int INVALID_REQUEST_DATA = 1201;
+		public static int INVALID_REQUEST_DATA_FORMAT = 1202;
+		public static int BLOCKED_REQUEST_DATA = 1203;
+		public static int DUPLICATED_REQUEST_DATA = 1204;
+		public static int NO_AVAILABLE_LICENSES = 1205;
 
-        public static int INVALID_DATA = 1301;
-    }
+		public static int INVALID_DATA = 1301;
+	}
 }
diff --git a/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java b/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
index 512957b..d8d6e20 100644
--- a/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
+++ b/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
@@ -13,6 +13,11 @@
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import net.curisit.integrity.exception.CurisRuntimeException;
 import net.curisit.securis.beans.LicenseBean;
 import net.curisit.securis.db.License;
 import net.curisit.securis.db.LicenseHistory;
@@ -24,128 +29,128 @@
 import net.curisit.securis.services.exception.SeCurisServiceException;
 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
 
-import org.apache.commons.io.FileUtils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
 @ApplicationScoped
 public class LicenseHelper {
 
-    @SuppressWarnings("unused")
-    private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
-    private static final long MS_PER_DAY = 24L * 3600L * 1000L;
-    private static final int DEFAULT_VALID_LIC_PERIOD = 7;
+	@SuppressWarnings("unused")
+	private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
+	private static final long MS_PER_DAY = 24L * 3600L * 1000L;
 
-    @Inject
-    private UserHelper userHelper;
+	@Inject
+	private UserHelper userHelper;
 
-    /**
-     * Cancel the license
-     * 
-     * @param lic
-     * @param em
-     */
-    public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
-        lic.setStatus(LicenseStatus.CANCELLED);
-        lic.setCancelledById(bsc.getUserPrincipal().getName());
-        lic.setModificationTimestamp(new Date());
-        em.persist(lic);
+	/**
+	 * Cancel the license
+	 * 
+	 * @param lic
+	 * @param em
+	 */
+	public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
+		lic.setStatus(LicenseStatus.CANCELLED);
+		lic.setCancelledById(bsc.getUserPrincipal().getName());
+		lic.setModificationTimestamp(new Date());
+		em.persist(lic);
 
-        em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
-    }
+		em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
+	}
 
-    /**
-     * Validates that the passed license is still valid
-     * 
-     * @param lic
-     * @param reason
-     * @param bsc
-     * @param em
-     * @throws SeCurisServiceException
-     */
-    public void assertLicenseStatusIsActive(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
-        License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
-        if (lic == null) {
-            throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Current license code doesn't exist");
-        }
-        if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
-            throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "Current license in not active");
-        }
-    }
+	/**
+	 * Validates that the passed license exists and is still valid
+	 * 
+	 * @param licBean
+	 * @param em
+	 * @return The License instance in DB
+	 * @throws SeCurisServiceException
+	 */
+	public License getActiveLicenseFromDB(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
+		License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
+		if (lic == null) {
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license code doesn't exist");
+		}
+		if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license in not active");
+		}
+		return lic;
+	}
 
-    public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
-        LicenseHistory lh = new LicenseHistory();
-        lh.setLicense(lic);
-        lh.setUser(user);
-        lh.setCreationTimestamp(new Date());
-        lh.setAction(action);
-        lh.setComments(comments);
-        return lh;
-    }
+	public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
+		LicenseHistory lh = new LicenseHistory();
+		lh.setLicense(lic);
+		lh.setUser(user);
+		lh.setCreationTimestamp(new Date());
+		lh.setAction(action);
+		lh.setComments(comments);
+		return lh;
+	}
 
-    public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
-        return createLicenseHistoryAction(lic, user, action, null);
-    }
+	public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
+		return createLicenseHistoryAction(lic, user, action, null);
+	}
 
-    /**
-     * Create a license file in a temporary directory
-     * 
-     * @param lic
-     * @param licFileName
-     * @return
-     * @throws IOException
-     */
-    public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
-        File f = Files.createTempDirectory("securis-server").toFile();
-        f = new File(f, licFileName);
-        FileUtils.writeStringToFile(f, lic.getLicenseData());
-        return f;
-    }
+	/**
+	 * Create a license file in a temporary directory
+	 * 
+	 * @param lic
+	 * @param licFileName
+	 * @return
+	 * @throws IOException
+	 */
+	public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
+		File f = Files.createTempDirectory("securis-server").toFile();
+		f = new File(f, licFileName);
+		FileUtils.writeStringToFile(f, lic.getLicenseData());
+		return f;
+	}
 
-    public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
-        Map<String, Object> metadata = new HashMap<>();
-        for (PackMetadata md : packMetadata) {
-            metadata.put(md.getKey(), md.getValue());
-        }
+	public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
+		Map<String, Object> metadata = new HashMap<>();
+		for (PackMetadata md : packMetadata) {
+			metadata.put(md.getKey(), md.getValue());
+		}
 
-        return metadata;
-    }
+		return metadata;
+	}
 
-    /**
-     * If the action is a renew the expiration date is got form pack end valid
-     * date, if the action is a pre-activation the expiration date is calculated
-     * using the pack default valid period
-     * 
-     * @param pack
-     * @param isPreActivation
-     * @return
-     */
-    public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
-        Long validPeriod;
-        if (isPreActivation) {
-            validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
-        } else {
-            if (pack.getRenewValidPeriod() <= 0) {
-                return pack.getEndValidDate();
-            }
-            validPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
-        }
-        Date expirationDate = new Date(new Date().getTime() + validPeriod);
-        return expirationDate;
-    }
+	/**
+	 * If the action is a renew the expiration date is got form pack end valid
+	 * date, if the action is a pre-activation the expiration date is calculated
+	 * using the pack default valid period
+	 * 
+	 * @param pack
+	 * @param isPreActivation
+	 * @return
+	 */
+	public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
+		Long validPeriod;
+		if (pack.getEndValidDate().before(new Date())) {
+			throw new CurisRuntimeException("Pack end valid period is reached, no new licenses can be activated.");
+		}
+		if (isPreActivation) {
+			validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
+		} else {
+			if (pack.getRenewValidPeriod() <= 0) {
+				return pack.getEndValidDate();
+			}
+			long renewPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
+			long expirationPeriod = pack.getEndValidDate().getTime() - new Date().getTime();
+			validPeriod = renewPeriod < expirationPeriod ? renewPeriod : expirationPeriod;
+		}
+		Date expirationDate = new Date(new Date().getTime() + validPeriod);
+		return expirationDate;
+	}
 
-    /**
-     * Get the next free code suffis for a given Pack
-     * 
-     * @param packId
-     * @param em
-     * @return
-     */
-    public int getNextCodeSuffix(int packId, EntityManager em) {
-        TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
-        query.setParameter("packId", packId);
-        Integer lastCodeSuffix = query.getSingleResult();
-        return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
-    }
+	/**
+	 * Get the next free code suffis for a given Pack
+	 * 
+	 * @param packId
+	 * @param em
+	 * @return
+	 */
+	public int getNextCodeSuffix(int packId, EntityManager em) {
+		TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
+		query.setParameter("packId", packId);
+		Integer lastCodeSuffix = query.getSingleResult();
+		return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
+	}
 
 }
diff --git a/securis/src/main/resources/db/initial_data.sql b/securis/src/main/resources/db/initial_data.sql
index e0e47a6..46ca982 100644
--- a/securis/src/main/resources/db/initial_data.sql
+++ b/securis/src/main/resources/db/initial_data.sql
@@ -2,4 +2,4 @@
 
 #Password: securis
 insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('admin', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 2, 'Administrator', null, now());
-insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('_client', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 0, 'SeCuris client user', null, now());
\ No newline at end of file
+insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('_client', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 128, 'SeCuris client user', null, now());
\ No newline at end of file

--
Gitblit v1.3.2