1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
| | package net.curisit.securis.services;
| |
| | import java.io.IOException;
| | import java.util.Date;
| | import java.util.List;
| |
| | import javax.inject.Inject;
| | import javax.inject.Provider;
| | import javax.persistence.EntityManager;
| | import javax.ws.rs.Consumes;
| | import javax.ws.rs.GET;
| | import javax.ws.rs.POST;
| | import javax.ws.rs.Path;
| | import javax.ws.rs.Produces;
| | import javax.ws.rs.core.Context;
| | import javax.ws.rs.core.MediaType;
| | import javax.ws.rs.core.Response;
| |
| | import net.curisit.securis.LicenseGenerator;
| | import net.curisit.securis.LicenseManager;
| | import net.curisit.securis.SeCurisException;
| | import net.curisit.securis.beans.LicenseBean;
| | import net.curisit.securis.beans.RequestBean;
| | import net.curisit.securis.beans.SignedLicenseBean;
| | import net.curisit.securis.beans.StatusBean;
| | import net.curisit.securis.db.BlockedRequest;
| | import net.curisit.securis.db.License;
| | import net.curisit.securis.db.LicenseHistory;
| | import net.curisit.securis.db.LicenseStatus;
| | import net.curisit.securis.db.Pack;
| | import net.curisit.securis.db.User;
| | import net.curisit.securis.security.BasicSecurityContext;
| | import net.curisit.securis.security.Securable;
| | import net.curisit.securis.services.exception.SeCurisServiceException;
| | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
| | import net.curisit.securis.services.helpers.LicenseHelper;
| | import net.curisit.securis.utils.JsonUtils;
| | import net.curisit.securis.utils.LicUtils;
| | import net.curisit.securis.utils.SignatureHelper;
| | import net.curisit.securis.utils.TokenHelper;
| |
| | import org.apache.commons.lang.time.DateUtils;
| | import org.apache.logging.log4j.LogManager;
| | import org.apache.logging.log4j.Logger;
| | import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
| |
| | import com.google.inject.persist.Transactional;
| |
| | /**
| | * External API to be accessed by third parties
| | *
| | * @author roberto <roberto.sanchez@curisit.net>
| | */
| | @Path("/api")
| | public class ApiResource {
| |
| | @SuppressWarnings("unused")
| | private static final Logger LOG = LogManager.getLogger(ApiResource.class);
| |
| | @Inject
| | TokenHelper tokenHelper;
| |
| | @Inject
| | private LicenseHelper licenseHelper;
| |
| | @Inject
| | Provider<EntityManager> emProvider;
| |
| | @Inject
| | LicenseGenerator licenseGenerator;
| |
| | private static final String CLIENT_USERNAME = "_client";
| |
| | public ApiResource() {
| | }
| |
| | /**
| | *
| | * @return Simple text message to check API status
| | */
| | @GET
| | @Path("/")
| | @Produces({
| | MediaType.TEXT_PLAIN
| | })
| | public Response index() {
| | return Response.ok("SeCuris API. Date: " + new Date()).build();
| | }
| |
| | /**
| | *
| | * @return Simple text message to check API status
| | */
| | @GET
| | @Path("/ping")
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | public Response ping() {
| | StatusBean status = new StatusBean();
| | status.setDate(new Date());
| | status.setMessage(LicenseManager.PING_MESSAGE);
| | return Response.ok(status).build();
| | }
| |
| | /**
| | * Request a new license file based in a RequestBean object sent as
| | * parameter
| | *
| | * @param mpfdi
| | * @param bsc
| | * @return
| | * @throws IOException
| | * @throws SeCurisServiceException
| | */
| | @POST
| | @Path("/request")
| | @Consumes(MediaType.APPLICATION_JSON)
| | // TODO: Enable this: @Securable
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | @Transactional
| | public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException,
| | SeCurisException {
| | LOG.info("Request to get license: {}", request);
| |
| | SignedLicenseBean lic = createLicense(request, emProvider.get(), false);
| |
| | return Response.ok(lic).build();
| | }
| |
| | /**
| | * Returns a License file in JSON format from an uploaded Request file
| | *
| | * @param mpfdi
| | * @param bsc
| | * @return
| | * @throws IOException
| | * @throws SeCurisServiceException
| | * @throws SeCurisException
| | */
| | @POST
| | @Path("/request")
| | @Consumes(MediaType.MULTIPART_FORM_DATA)
| | @Securable
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | @Transactional
| | @SuppressWarnings("unchecked")
| | public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException,
| | SeCurisServiceException, SeCurisException {
| | RequestBean req = new RequestBean();
| | req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
| | req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null));
| | req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
| | req.setArch(mpfdi.getFormDataPart("arch", String.class, null));
| | req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
| | req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
| | req.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
| |
| | return createFromRequest(req, bsc);
| | }
| |
| | /**
| | * Create a new License file based in a previous one
| | *
| | * @param request
| | * @param bsc
| | * @return
| | * @throws IOException
| | * @throws SeCurisServiceException
| | * @throws SeCurisException
| | */
| | @POST
| | @Path("/renew")
| | @Consumes(MediaType.APPLICATION_JSON)
| | // TODO: Enable this: @Securable
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | @Transactional
| | public Response renewFromPreviousLicense(LicenseBean previousLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException,
| | SeCurisException {
| | LOG.info("Renew license: {}", previousLic);
| |
| | if (previousLic.getExpirationDate().after(DateUtils.addMonths(new Date(), 1))) {
| | throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "The license is still valid, not ready for renew");
| | }
| |
| | SignedLicenseBean lic = createLicense(previousLic, emProvider.get(), true);
| |
| | return Response.ok(lic).build();
| | }
| |
| | /**
| | * License validation on server side, in this case we validate that the
| | * current licenses has not been cancelled.
| | *
| | * @param currentLic
| | * @param bsc
| | * @return
| | * @throws IOException
| | * @throws SeCurisServiceException
| | * @throws SeCurisException
| | */
| | @POST
| | @Path("/validate")
| | @Consumes(MediaType.APPLICATION_JSON)
| | // TODO: Enable this: @Securable
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | @Transactional
| | public Response validate(LicenseBean currentLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
| | LOG.info("Validate license: {}", currentLic);
| | EntityManager em = emProvider.get();
| | 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();
| | }
| |
| | /**
| | * Returns a new License file in JSON format based in a previous license
| | *
| | * @param mpfdi
| | * @param bsc
| | * @return
| | * @throws IOException
| | * @throws SeCurisServiceException
| | * @throws SeCurisException
| | */
| | @POST
| | @Path("/renew")
| | @Consumes(MediaType.MULTIPART_FORM_DATA)
| | @Securable
| | @Produces({
| | MediaType.APPLICATION_JSON
| | })
| | @Transactional
| | @SuppressWarnings("unchecked")
| | public Response renewFromLicenseFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException,
| | SeCurisServiceException, SeCurisException {
| | LicenseBean lic = new LicenseBean();
| |
| | lic.setAppName(mpfdi.getFormDataPart("appName", String.class, null));
| | lic.setArch(mpfdi.getFormDataPart("arch", String.class, null));
| | lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
| | lic.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
| | lic.setLicenseTypeCode(mpfdi.getFormDataPart("licenseCode", String.class, null));
| | lic.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
| | lic.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
| | lic.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
| | lic.setExpirationDate(mpfdi.getFormDataPart("expirationDate", Date.class, null));
| | LOG.info("Lic expires at: {}", lic.getExpirationDate());
| | if (lic.getExpirationDate().after(DateUtils.addMonths(new Date(), 1))) {
| | throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "The license is still valid, not ready for renew");
| | }
| |
| | return createFromRequest(lic, bsc);
| | }
| |
| | /**
| | * 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 createLicense(RequestBean req, EntityManager em, boolean renew) throws SeCurisServiceException {
| | LicenseBean previousLicenseBean = null;
| | 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");
| | }
| | } else {
| | lic = new License();
| | }
| |
| | if (!renew) {
| | License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
| | if (existingLicense != null) {
| | throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There is already an active license for current request data");
| | }
| | }
| | Pack pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
| |
| | if (!renew && pack.getNumAvailables() <= 0) {
| | throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
| | }
| | SignedLicenseBean signedLicense;
| | try {
| | String licCode;
| | if (renew) {
| | licCode = previousLicenseBean.getLicenseCode();
| | } else {
| | licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em));
| | }
| | Date expirationDate = licenseHelper.getExpirationDateFromPack(pack, !renew);
| |
| | 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(signedLicense, RequestBean.class));
| | 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, CLIENT_USERNAME);
| | if (!renew) {
| |
| | lic.setPack(pack);
| | lic.setCreatedBy(user);
| | lic.setCreationTimestamp(new Date());
| | lic.setStatus(LicenseStatus.PRE_ACTIVE);
| | lic.setCode(signedLicense.getLicenseCode());
| | lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
| | em.persist(lic);
| | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
| | if (lic.getStatus() == LicenseStatus.ACTIVE) {
| | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
| | }
| | } else {
| | lic.setStatus(LicenseStatus.ACTIVE);
| | em.merge(lic);
| | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
| | }
| |
| | return signedLicense;
| | }
| |
| | }
|
|