rsanchez
2015-09-24 94c288b4f8d353c44b64e40c0863c7fce6782293
securis/src/main/java/net/curisit/securis/services/ApiResource.java
....@@ -68,7 +68,7 @@
6868 @Inject
6969 LicenseGenerator licenseGenerator;
7070
71
- private static final String CLIENT_USERNAME = "_client";
71
+ public static final String API_CLIENT_USERNAME = "_client";
7272
7373 public ApiResource() {
7474 }
....@@ -115,7 +115,7 @@
115115 @POST
116116 @Path("/request")
117117 @Consumes(MediaType.APPLICATION_JSON)
118
- // TODO: Enable this: @Securable
118
+ @Securable
119119 @Produces({
120120 MediaType.APPLICATION_JSON
121121 })
....@@ -123,7 +123,7 @@
123123 public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
124124 @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
125125 LOG.info("Request to get license: {}", request);
126
- SignedLicenseBean lic = createLicense(request, em, false, nameOrReference, userEmail);
126
+ SignedLicenseBean lic = createLicense(request, em, nameOrReference, userEmail);
127127
128128 return Response.ok(lic).build();
129129 }
....@@ -151,6 +151,8 @@
151151 @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
152152 @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
153153 RequestBean req = new RequestBean();
154
+ req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
155
+ req.setActivationCode(mpfdi.getFormDataPart("activationCode", String.class, null));
154156 req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
155157 req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null));
156158 req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
....@@ -175,7 +177,7 @@
175177 @POST
176178 @Path("/renew")
177179 @Consumes(MediaType.APPLICATION_JSON)
178
- // TODO: Enable this: @Securable
180
+ @Securable
179181 @Produces({
180182 MediaType.APPLICATION_JSON
181183 })
....@@ -214,7 +216,7 @@
214216 @POST
215217 @Path("/validate")
216218 @Consumes(MediaType.APPLICATION_JSON)
217
- // TODO: Enable this: @Securable
219
+ @Securable
218220 @Produces({
219221 MediaType.APPLICATION_JSON
220222 })
....@@ -226,7 +228,6 @@
226228 throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired");
227229 }
228230
229
- // EntityManager em = emProvider.get();
230231 try {
231232 SignatureHelper.getInstance().validateSignature(currentLic);
232233 } catch (SeCurisException ex) {
....@@ -261,6 +262,8 @@
261262 SeCurisServiceException, SeCurisException {
262263 LicenseBean lic = new LicenseBean();
263264
265
+ lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
266
+ lic.setActivationCode(mpfdi.getFormDataPart("activationName", String.class, null));
264267 lic.setAppName(mpfdi.getFormDataPart("appName", String.class, null));
265268 lic.setArch(mpfdi.getFormDataPart("arch", String.class, null));
266269 lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
....@@ -279,7 +282,7 @@
279282 }
280283
281284 private SignedLicenseBean renewLicense(RequestBean req, EntityManager em) throws SeCurisServiceException {
282
- return createLicense(req, em, true, null, null);
285
+ return renewLicense(req, em);
283286 }
284287
285288 /**
....@@ -292,16 +295,38 @@
292295 * @return
293296 * @throws SeCurisServiceException
294297 */
295
- private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew, String nameOrReference, String email)
296
- throws SeCurisServiceException {
297
- LicenseBean previousLicenseBean = null;
298
+ private SignedLicenseBean createLicense(RequestBean req, EntityManager em, String nameOrReference, String email) throws SeCurisServiceException {
298299 License lic = null;
299
- if (renew) {
300
- previousLicenseBean = (LicenseBean) req;
301
- lic = License.findLicenseByCode(previousLicenseBean.getLicenseCode(), em);
302
- if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
303
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled");
300
+
301
+ if (req.getActivationCode() != null) {
302
+ lic = License.findLicenseByActivationCode(req.getActivationCode(), em);
303
+ if (lic == null) {
304
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The given activation code is invalid: " + req.getActivationCode());
304305 }
306
+ if (lic.getStatus() == LicenseStatus.ACTIVE) {
307
+ RequestBean initialRequest;
308
+ try {
309
+ initialRequest = JsonUtils.json2object(lic.getRequestData(), RequestBean.class);
310
+ if (!req.match(initialRequest)) {
311
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "There is already an active license for given activation code: "
312
+ + req.getActivationCode());
313
+ } else {
314
+ return JsonUtils.json2object(lic.getLicenseData(), SignedLicenseBean.class);
315
+ }
316
+ } catch (SeCurisException e) {
317
+ LOG.error("Error getting existing license", e);
318
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Original request is wrong");
319
+ }
320
+ } else {
321
+ if (req.getAppCode() != null && !req.getAppCode().equals(lic.getPack().getLicenseType().getApplication().getCode())) {
322
+ LOG.error("Activation code {} belongs to app: {} but was sent by: {}", req.getActivationCode(), lic.getPack().getLicenseType()
323
+ .getApplication().getCode(), req.getAppCode());
324
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The given activation code belongs to a different application: "
325
+ + req.getActivationCode());
326
+ }
327
+ }
328
+ // We validate if the HW is the same, otherwise an error is
329
+ // thrown
305330 } else {
306331 try {
307332 lic = License.findValidLicenseByRequestData(JsonUtils.toJSON(req), em);
....@@ -321,40 +346,126 @@
321346 lic = new License();
322347 }
323348 }
349
+
324350 Pack pack;
325
- try {
326
- pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
327
- } catch (NoResultException e) {
328
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "No pack found for code: " + req.getPackCode());
329
- }
351
+ if (lic.getActivationCode() == null) {
352
+ try {
353
+ pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
354
+ } catch (NoResultException e) {
355
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "No pack found for code: " + req.getPackCode());
356
+ }
330357
331
- if (!renew && pack.getNumAvailables() <= 0) {
332
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
333
- }
334
- if (!renew && lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) {
335
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
336
- }
358
+ if (pack.getNumAvailables() <= 0) {
359
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
360
+ }
361
+ if (lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) {
362
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
363
+ }
337364
338
- if (!req.getCustomerCode().equals(pack.getOrganization().getCode())) {
339
- throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Customer code is not valid: " + req.getCustomerCode());
340
- }
365
+ if (!req.getCustomerCode().equals(pack.getOrganization().getCode())) {
366
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Customer code is not valid: " + req.getCustomerCode());
367
+ }
341368
342
- if (!req.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
343
- throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "License type code is not valid: " + req.getLicenseTypeCode());
369
+ if (!req.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
370
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "License type code is not valid: "
371
+ + req.getLicenseTypeCode());
372
+ }
373
+ } else {
374
+ pack = lic.getPack();
344375 }
345
-
346376 SignedLicenseBean signedLicense;
347377 try {
348378 String licCode;
349
- if (renew || lic.getStatus() == LicenseStatus.REQUESTED) {
350
- licCode = lic.getCode();
351
- } else {
379
+ if (lic.getCode() == null) {
352380 licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em));
381
+ } else {
382
+ licCode = lic.getCode();
353383 }
354
- Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, !renew);
384
+ Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, lic.getActivationCode() == null);
355385
356386 LicenseBean lb = licenseGenerator.generateLicense(req, licenseHelper.extractPackMetadata(pack.getMetadata()), expirationDate, licCode,
357387 pack.getAppName());
388
+ signedLicense = new SignedLicenseBean(lb);
389
+ } catch (SeCurisException e) {
390
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
391
+ }
392
+ try {
393
+ lic.setRequestData(JsonUtils.toJSON(req));
394
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
395
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
396
+ }
397
+ lic.setLicenseData(JsonUtils.toJSON(signedLicense));
398
+ } catch (SeCurisException e) {
399
+ LOG.error("Error generating license JSON", e);
400
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
401
+ }
402
+
403
+ lic.setModificationTimestamp(new Date());
404
+ lic.setExpirationDate(signedLicense.getExpirationDate());
405
+ User user = em.find(User.class, API_CLIENT_USERNAME);
406
+ if (lic.getStatus() != LicenseStatus.REQUESTED) {
407
+ lic.setPack(pack);
408
+ lic.setCreatedBy(user);
409
+ lic.setCreationTimestamp(new Date());
410
+ if (lic.getActivationCode() != null) {
411
+ lic.setStatus(LicenseStatus.ACTIVE);
412
+ } else {
413
+ lic.setStatus(pack.isLicensePreactivation() ? LicenseStatus.PRE_ACTIVE : LicenseStatus.REQUESTED);
414
+ }
415
+ lic.setCode(signedLicense.getLicenseCode());
416
+ lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
417
+ if (lic.getEmail() == null || "".equals(lic.getEmail())) {
418
+ lic.setEmail(email);
419
+ }
420
+ if (lic.getFullName() == null || "".equals(lic.getFullName())) {
421
+ lic.setFullName(nameOrReference);
422
+ }
423
+ em.persist(lic);
424
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
425
+ if (lic.getActivationCode() != null) {
426
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE, "Activated by code on creation"));
427
+ } else {
428
+ if (pack.isLicensePreactivation()) {
429
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
430
+ } else {
431
+ LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode());
432
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
433
+ }
434
+ }
435
+ } else {
436
+ lic.setStatus(LicenseStatus.PRE_ACTIVE);
437
+ em.merge(lic);
438
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request"));
439
+ }
440
+
441
+ return signedLicense;
442
+ }
443
+
444
+ /**
445
+ * Creates a new signed license from request data or from previous license
446
+ * if It's a renew
447
+ *
448
+ * @param req
449
+ * @param em
450
+ * @param renew
451
+ * @return
452
+ * @throws SeCurisServiceException
453
+ */
454
+ private SignedLicenseBean renewLicense(LicenseBean previousLicenseBean, EntityManager em) throws SeCurisServiceException {
455
+
456
+ License lic = License.findLicenseByCode(previousLicenseBean.getLicenseCode(), em);
457
+ if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
458
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled");
459
+ }
460
+
461
+ Pack pack = lic.getPack();
462
+ SignedLicenseBean signedLicense;
463
+ try {
464
+ String licCode = lic.getCode();
465
+ Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, false);
466
+
467
+ LicenseBean lb = licenseGenerator.generateLicense(previousLicenseBean, licenseHelper.extractPackMetadata(pack.getMetadata()),
468
+ expirationDate, licCode, pack.getAppName());
358469 signedLicense = new SignedLicenseBean(lb);
359470 } catch (SeCurisException e) {
360471 throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
....@@ -372,37 +483,11 @@
372483
373484 lic.setModificationTimestamp(new Date());
374485 lic.setExpirationDate(signedLicense.getExpirationDate());
375
- User user = em.find(User.class, CLIENT_USERNAME);
376
- if (!renew && lic.getStatus() != LicenseStatus.REQUESTED) {
377
- lic.setPack(pack);
378
- lic.setCreatedBy(user);
379
- lic.setCreationTimestamp(new Date());
380
- if (pack.isLicensePreactivation()) {
381
- lic.setStatus(LicenseStatus.PRE_ACTIVE);
382
- } else {
383
- lic.setStatus(LicenseStatus.REQUESTED);
384
- }
385
- lic.setCode(signedLicense.getLicenseCode());
386
- lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
387
- lic.setEmail(email);
388
- lic.setFullName(nameOrReference);
389
- em.persist(lic);
390
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
391
- if (pack.isLicensePreactivation()) {
392
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
393
- } else {
394
- LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode());
395
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
396
- }
397
- } else {
398
- lic.setStatus(renew ? LicenseStatus.ACTIVE : LicenseStatus.PRE_ACTIVE);
399
- em.merge(lic);
400
- if (renew) {
401
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
402
- } else {
403
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request"));
404
- }
405
- }
486
+ User user = em.find(User.class, API_CLIENT_USERNAME);
487
+
488
+ lic.setStatus(LicenseStatus.ACTIVE);
489
+ em.merge(lic);
490
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
406491
407492 return signedLicense;
408493 }