| .. | .. |
|---|
| 68 | 68 | @Inject |
|---|
| 69 | 69 | LicenseGenerator licenseGenerator; |
|---|
| 70 | 70 | |
|---|
| 71 | | - private static final String CLIENT_USERNAME = "_client"; |
|---|
| 71 | + public static final String API_CLIENT_USERNAME = "_client"; |
|---|
| 72 | 72 | |
|---|
| 73 | 73 | public ApiResource() { |
|---|
| 74 | 74 | } |
|---|
| .. | .. |
|---|
| 115 | 115 | @POST |
|---|
| 116 | 116 | @Path("/request") |
|---|
| 117 | 117 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 118 | | - // TODO: Enable this: @Securable |
|---|
| 118 | + @Securable |
|---|
| 119 | 119 | @Produces({ |
|---|
| 120 | 120 | MediaType.APPLICATION_JSON |
|---|
| 121 | 121 | }) |
|---|
| .. | .. |
|---|
| 123 | 123 | public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference, |
|---|
| 124 | 124 | @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 125 | 125 | LOG.info("Request to get license: {}", request); |
|---|
| 126 | | - SignedLicenseBean lic = createLicense(request, em, false, nameOrReference, userEmail); |
|---|
| 126 | + SignedLicenseBean lic = createLicense(request, em, nameOrReference, userEmail); |
|---|
| 127 | 127 | |
|---|
| 128 | 128 | return Response.ok(lic).build(); |
|---|
| 129 | 129 | } |
|---|
| .. | .. |
|---|
| 151 | 151 | @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference, |
|---|
| 152 | 152 | @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 153 | 153 | RequestBean req = new RequestBean(); |
|---|
| 154 | + req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null)); |
|---|
| 155 | + req.setActivationCode(mpfdi.getFormDataPart("activationCode", String.class, null)); |
|---|
| 154 | 156 | req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null)); |
|---|
| 155 | 157 | req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null)); |
|---|
| 156 | 158 | req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| .. | .. |
|---|
| 175 | 177 | @POST |
|---|
| 176 | 178 | @Path("/renew") |
|---|
| 177 | 179 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 178 | | - // TODO: Enable this: @Securable |
|---|
| 180 | + @Securable |
|---|
| 179 | 181 | @Produces({ |
|---|
| 180 | 182 | MediaType.APPLICATION_JSON |
|---|
| 181 | 183 | }) |
|---|
| .. | .. |
|---|
| 214 | 216 | @POST |
|---|
| 215 | 217 | @Path("/validate") |
|---|
| 216 | 218 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 217 | | - // TODO: Enable this: @Securable |
|---|
| 219 | + @Securable |
|---|
| 218 | 220 | @Produces({ |
|---|
| 219 | 221 | MediaType.APPLICATION_JSON |
|---|
| 220 | 222 | }) |
|---|
| .. | .. |
|---|
| 226 | 228 | throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired"); |
|---|
| 227 | 229 | } |
|---|
| 228 | 230 | |
|---|
| 229 | | - // EntityManager em = emProvider.get(); |
|---|
| 230 | 231 | try { |
|---|
| 231 | 232 | SignatureHelper.getInstance().validateSignature(currentLic); |
|---|
| 232 | 233 | } catch (SeCurisException ex) { |
|---|
| .. | .. |
|---|
| 261 | 262 | SeCurisServiceException, SeCurisException { |
|---|
| 262 | 263 | LicenseBean lic = new LicenseBean(); |
|---|
| 263 | 264 | |
|---|
| 265 | + lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null)); |
|---|
| 266 | + lic.setActivationCode(mpfdi.getFormDataPart("activationName", String.class, null)); |
|---|
| 264 | 267 | lic.setAppName(mpfdi.getFormDataPart("appName", String.class, null)); |
|---|
| 265 | 268 | lic.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 266 | 269 | lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null)); |
|---|
| .. | .. |
|---|
| 279 | 282 | } |
|---|
| 280 | 283 | |
|---|
| 281 | 284 | private SignedLicenseBean renewLicense(RequestBean req, EntityManager em) throws SeCurisServiceException { |
|---|
| 282 | | - return createLicense(req, em, true, null, null); |
|---|
| 285 | + return renewLicense(req, em); |
|---|
| 283 | 286 | } |
|---|
| 284 | 287 | |
|---|
| 285 | 288 | /** |
|---|
| .. | .. |
|---|
| 292 | 295 | * @return |
|---|
| 293 | 296 | * @throws SeCurisServiceException |
|---|
| 294 | 297 | */ |
|---|
| 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 { |
|---|
| 298 | 299 | 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()); |
|---|
| 304 | 305 | } |
|---|
| 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 |
|---|
| 305 | 330 | } else { |
|---|
| 306 | 331 | try { |
|---|
| 307 | 332 | lic = License.findValidLicenseByRequestData(JsonUtils.toJSON(req), em); |
|---|
| .. | .. |
|---|
| 321 | 346 | lic = new License(); |
|---|
| 322 | 347 | } |
|---|
| 323 | 348 | } |
|---|
| 349 | + |
|---|
| 324 | 350 | 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 | + } |
|---|
| 330 | 357 | |
|---|
| 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 | + } |
|---|
| 337 | 364 | |
|---|
| 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 | + } |
|---|
| 341 | 368 | |
|---|
| 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(); |
|---|
| 344 | 375 | } |
|---|
| 345 | | - |
|---|
| 346 | 376 | SignedLicenseBean signedLicense; |
|---|
| 347 | 377 | try { |
|---|
| 348 | 378 | String licCode; |
|---|
| 349 | | - if (renew || lic.getStatus() == LicenseStatus.REQUESTED) { |
|---|
| 350 | | - licCode = lic.getCode(); |
|---|
| 351 | | - } else { |
|---|
| 379 | + if (lic.getCode() == null) { |
|---|
| 352 | 380 | licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em)); |
|---|
| 381 | + } else { |
|---|
| 382 | + licCode = lic.getCode(); |
|---|
| 353 | 383 | } |
|---|
| 354 | | - Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, !renew); |
|---|
| 384 | + Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, lic.getActivationCode() == null); |
|---|
| 355 | 385 | |
|---|
| 356 | 386 | LicenseBean lb = licenseGenerator.generateLicense(req, licenseHelper.extractPackMetadata(pack.getMetadata()), expirationDate, licCode, |
|---|
| 357 | 387 | 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()); |
|---|
| 358 | 469 | signedLicense = new SignedLicenseBean(lb); |
|---|
| 359 | 470 | } catch (SeCurisException e) { |
|---|
| 360 | 471 | throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString()); |
|---|
| .. | .. |
|---|
| 372 | 483 | |
|---|
| 373 | 484 | lic.setModificationTimestamp(new Date()); |
|---|
| 374 | 485 | 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)); |
|---|
| 406 | 491 | |
|---|
| 407 | 492 | return signedLicense; |
|---|
| 408 | 493 | } |
|---|