From 94c288b4f8d353c44b64e40c0863c7fce6782293 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 24 Sep 2015 17:26:14 +0000
Subject: [PATCH] #2756 fix - chnaged API to allow activation by code and other UI changes
---
securis/src/main/java/net/curisit/securis/services/ApiResource.java | 219 ++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 152 insertions(+), 67 deletions(-)
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 743be97..20fe504 100644
--- a/securis/src/main/java/net/curisit/securis/services/ApiResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/ApiResource.java
@@ -68,7 +68,7 @@
@Inject
LicenseGenerator licenseGenerator;
- private static final String CLIENT_USERNAME = "_client";
+ public static final String API_CLIENT_USERNAME = "_client";
public ApiResource() {
}
@@ -115,7 +115,7 @@
@POST
@Path("/request")
@Consumes(MediaType.APPLICATION_JSON)
- // TODO: Enable this: @Securable
+ @Securable
@Produces({
MediaType.APPLICATION_JSON
})
@@ -123,7 +123,7 @@
public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
@HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
LOG.info("Request to get license: {}", request);
- SignedLicenseBean lic = createLicense(request, em, false, nameOrReference, userEmail);
+ SignedLicenseBean lic = createLicense(request, em, nameOrReference, userEmail);
return Response.ok(lic).build();
}
@@ -151,6 +151,8 @@
@HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
@HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
RequestBean req = new RequestBean();
+ req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
+ req.setActivationCode(mpfdi.getFormDataPart("activationCode", String.class, null));
req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null));
req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
@@ -175,7 +177,7 @@
@POST
@Path("/renew")
@Consumes(MediaType.APPLICATION_JSON)
- // TODO: Enable this: @Securable
+ @Securable
@Produces({
MediaType.APPLICATION_JSON
})
@@ -214,7 +216,7 @@
@POST
@Path("/validate")
@Consumes(MediaType.APPLICATION_JSON)
- // TODO: Enable this: @Securable
+ @Securable
@Produces({
MediaType.APPLICATION_JSON
})
@@ -226,7 +228,6 @@
throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired");
}
- // EntityManager em = emProvider.get();
try {
SignatureHelper.getInstance().validateSignature(currentLic);
} catch (SeCurisException ex) {
@@ -261,6 +262,8 @@
SeCurisServiceException, SeCurisException {
LicenseBean lic = new LicenseBean();
+ lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
+ lic.setActivationCode(mpfdi.getFormDataPart("activationName", String.class, null));
lic.setAppName(mpfdi.getFormDataPart("appName", String.class, null));
lic.setArch(mpfdi.getFormDataPart("arch", String.class, null));
lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
@@ -279,7 +282,7 @@
}
private SignedLicenseBean renewLicense(RequestBean req, EntityManager em) throws SeCurisServiceException {
- return createLicense(req, em, true, null, null);
+ return renewLicense(req, em);
}
/**
@@ -292,16 +295,38 @@
* @return
* @throws SeCurisServiceException
*/
- private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew, String nameOrReference, String email)
- throws SeCurisServiceException {
- LicenseBean previousLicenseBean = null;
+ private SignedLicenseBean createLicense(RequestBean req, EntityManager em, String nameOrReference, String email) throws SeCurisServiceException {
License lic = null;
- if (renew) {
- previousLicenseBean = (LicenseBean) req;
- lic = License.findLicenseByCode(previousLicenseBean.getLicenseCode(), em);
- if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled");
+
+ if (req.getActivationCode() != null) {
+ lic = License.findLicenseByActivationCode(req.getActivationCode(), em);
+ if (lic == null) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The given activation code is invalid: " + req.getActivationCode());
}
+ if (lic.getStatus() == LicenseStatus.ACTIVE) {
+ RequestBean initialRequest;
+ try {
+ initialRequest = JsonUtils.json2object(lic.getRequestData(), RequestBean.class);
+ if (!req.match(initialRequest)) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "There is already an active license for given activation code: "
+ + req.getActivationCode());
+ } else {
+ return JsonUtils.json2object(lic.getLicenseData(), SignedLicenseBean.class);
+ }
+ } catch (SeCurisException e) {
+ LOG.error("Error getting existing license", e);
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Original request is wrong");
+ }
+ } else {
+ if (req.getAppCode() != null && !req.getAppCode().equals(lic.getPack().getLicenseType().getApplication().getCode())) {
+ LOG.error("Activation code {} belongs to app: {} but was sent by: {}", req.getActivationCode(), lic.getPack().getLicenseType()
+ .getApplication().getCode(), req.getAppCode());
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The given activation code belongs to a different application: "
+ + req.getActivationCode());
+ }
+ }
+ // We validate if the HW is the same, otherwise an error is
+ // thrown
} else {
try {
lic = License.findValidLicenseByRequestData(JsonUtils.toJSON(req), em);
@@ -321,40 +346,126 @@
lic = new License();
}
}
+
Pack pack;
- try {
- pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
- } catch (NoResultException e) {
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "No pack found for code: " + req.getPackCode());
- }
+ if (lic.getActivationCode() == null) {
+ try {
+ pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
+ } catch (NoResultException e) {
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "No pack found for code: " + req.getPackCode());
+ }
- if (!renew && pack.getNumAvailables() <= 0) {
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
- }
- if (!renew && lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) {
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
- }
+ if (pack.getNumAvailables() <= 0) {
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
+ }
+ if (lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) {
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
+ }
- if (!req.getCustomerCode().equals(pack.getOrganization().getCode())) {
- throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Customer code is not valid: " + req.getCustomerCode());
- }
+ if (!req.getCustomerCode().equals(pack.getOrganization().getCode())) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Customer code is not valid: " + req.getCustomerCode());
+ }
- if (!req.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
- throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "License type code is not valid: " + req.getLicenseTypeCode());
+ if (!req.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "License type code is not valid: "
+ + req.getLicenseTypeCode());
+ }
+ } else {
+ pack = lic.getPack();
}
-
SignedLicenseBean signedLicense;
try {
String licCode;
- if (renew || lic.getStatus() == LicenseStatus.REQUESTED) {
- licCode = lic.getCode();
- } else {
+ if (lic.getCode() == null) {
licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em));
+ } else {
+ licCode = lic.getCode();
}
- Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, !renew);
+ Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, lic.getActivationCode() == null);
LicenseBean lb = licenseGenerator.generateLicense(req, licenseHelper.extractPackMetadata(pack.getMetadata()), expirationDate, licCode,
pack.getAppName());
+ signedLicense = new SignedLicenseBean(lb);
+ } catch (SeCurisException e) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
+ }
+ try {
+ lic.setRequestData(JsonUtils.toJSON(req));
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
+ }
+ lic.setLicenseData(JsonUtils.toJSON(signedLicense));
+ } catch (SeCurisException e) {
+ LOG.error("Error generating license JSON", e);
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
+ }
+
+ lic.setModificationTimestamp(new Date());
+ lic.setExpirationDate(signedLicense.getExpirationDate());
+ User user = em.find(User.class, API_CLIENT_USERNAME);
+ if (lic.getStatus() != LicenseStatus.REQUESTED) {
+ lic.setPack(pack);
+ lic.setCreatedBy(user);
+ lic.setCreationTimestamp(new Date());
+ if (lic.getActivationCode() != null) {
+ lic.setStatus(LicenseStatus.ACTIVE);
+ } else {
+ lic.setStatus(pack.isLicensePreactivation() ? LicenseStatus.PRE_ACTIVE : LicenseStatus.REQUESTED);
+ }
+ lic.setCode(signedLicense.getLicenseCode());
+ lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
+ if (lic.getEmail() == null || "".equals(lic.getEmail())) {
+ lic.setEmail(email);
+ }
+ if (lic.getFullName() == null || "".equals(lic.getFullName())) {
+ lic.setFullName(nameOrReference);
+ }
+ em.persist(lic);
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
+ if (lic.getActivationCode() != null) {
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE, "Activated by code on creation"));
+ } else {
+ if (pack.isLicensePreactivation()) {
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
+ } else {
+ LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode());
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
+ }
+ }
+ } else {
+ lic.setStatus(LicenseStatus.PRE_ACTIVE);
+ em.merge(lic);
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request"));
+ }
+
+ return signedLicense;
+ }
+
+ /**
+ * Creates a new signed license from request data or from previous license
+ * if It's a renew
+ *
+ * @param req
+ * @param em
+ * @param renew
+ * @return
+ * @throws SeCurisServiceException
+ */
+ private SignedLicenseBean renewLicense(LicenseBean previousLicenseBean, EntityManager em) throws SeCurisServiceException {
+
+ License lic = License.findLicenseByCode(previousLicenseBean.getLicenseCode(), em);
+ if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled");
+ }
+
+ Pack pack = lic.getPack();
+ SignedLicenseBean signedLicense;
+ try {
+ String licCode = lic.getCode();
+ Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, false);
+
+ LicenseBean lb = licenseGenerator.generateLicense(previousLicenseBean, licenseHelper.extractPackMetadata(pack.getMetadata()),
+ expirationDate, licCode, pack.getAppName());
signedLicense = new SignedLicenseBean(lb);
} catch (SeCurisException e) {
throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
@@ -372,37 +483,11 @@
lic.setModificationTimestamp(new Date());
lic.setExpirationDate(signedLicense.getExpirationDate());
- User user = em.find(User.class, CLIENT_USERNAME);
- if (!renew && lic.getStatus() != LicenseStatus.REQUESTED) {
- lic.setPack(pack);
- lic.setCreatedBy(user);
- lic.setCreationTimestamp(new Date());
- if (pack.isLicensePreactivation()) {
- lic.setStatus(LicenseStatus.PRE_ACTIVE);
- } else {
- lic.setStatus(LicenseStatus.REQUESTED);
- }
- lic.setCode(signedLicense.getLicenseCode());
- lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
- lic.setEmail(email);
- lic.setFullName(nameOrReference);
- em.persist(lic);
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
- if (pack.isLicensePreactivation()) {
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
- } else {
- LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode());
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
- }
- } else {
- lic.setStatus(renew ? LicenseStatus.ACTIVE : LicenseStatus.PRE_ACTIVE);
- em.merge(lic);
- if (renew) {
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
- } else {
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request"));
- }
- }
+ User user = em.find(User.class, API_CLIENT_USERNAME);
+
+ lic.setStatus(LicenseStatus.ACTIVE);
+ em.merge(lic);
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
return signedLicense;
}
--
Gitblit v1.3.2