1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.dem.objects; |
7 | |
|
8 | |
import java.text.ParseException; |
9 | |
import java.util.Date; |
10 | |
|
11 | |
import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters; |
12 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
13 | |
import pt.digitalis.utils.common.DateUtils; |
14 | |
import pt.digitalis.utils.common.StringUtils; |
15 | |
import pt.digitalis.utils.crypto.IEncryptor; |
16 | |
import pt.digitalis.utils.crypto.exeption.CryptoException; |
17 | |
import pt.digitalis.utils.crypto.impl.EncryptorBase64Impl; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class LicenseImpl implements ILicense { |
31 | |
|
32 | |
|
33 | 0 | private static IEncryptor encryptor = null; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private static final String FULL_VERSION_DATE = "00000000"; |
39 | |
|
40 | |
|
41 | 0 | private static final char[] hideInPlainSight = {(char) 97, (char) 115, (char) 100, (char) 97, (char) 97, |
42 | 0 | (char) 115, (char) 100, (char) 102, (char) 56, (char) 115, (char) 97, (char) 100, (char) 57, |
43 | 0 | (char) 102, (char) 56, (char) 55, (char) 97, (char) 115}; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static final String SPLITER_REGISTRATION_KEY = "-&-"; |
49 | |
|
50 | |
|
51 | 0 | private static final String variableToReturn = new String(hideInPlainSight); |
52 | |
|
53 | |
|
54 | |
private LicenseEditionType edition; |
55 | |
|
56 | |
|
57 | |
private String entityId; |
58 | |
|
59 | |
|
60 | 0 | private Date expirationDate = null; |
61 | |
|
62 | |
|
63 | |
private String key; |
64 | |
|
65 | |
|
66 | 0 | private Boolean keyChecked = false; |
67 | |
|
68 | |
|
69 | |
private String name; |
70 | |
|
71 | |
|
72 | 0 | private boolean registrable = true; |
73 | |
|
74 | |
|
75 | 0 | private boolean validKey = false; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 0 | public LicenseImpl() |
81 | 0 | {} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | public LicenseImpl(String name) |
90 | |
{ |
91 | 0 | this.name = name; |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | 0 | public LicenseImpl(String name, String key) |
103 | |
{ |
104 | 0 | this.name = name; |
105 | 0 | this.key = key; |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private boolean checkKey() |
112 | |
{ |
113 | 0 | String client = getClientName(); |
114 | 0 | boolean keyOk = false; |
115 | |
|
116 | |
try |
117 | |
{ |
118 | |
|
119 | 0 | if (client == null || "".equals(client)) |
120 | |
{ |
121 | 0 | DIFLogger.getLogger().debug("The client name is not configured."); |
122 | 0 | return false; |
123 | |
} |
124 | 0 | if (this.entityId == null) |
125 | |
{ |
126 | 0 | DIFLogger.getLogger().debug("The entityID cannot be null."); |
127 | 0 | return false; |
128 | |
} |
129 | |
|
130 | 0 | String theClient = StringUtils.removeAccents(client); |
131 | 0 | String theEntity = StringUtils.removeAccents(this.entityId); |
132 | |
|
133 | |
|
134 | 0 | if (!simpleKeyCheck(this.key)) |
135 | |
{ |
136 | 0 | return false; |
137 | |
} |
138 | |
|
139 | |
|
140 | 0 | String[] str = this.key.split(SPLITER_REGISTRATION_KEY); |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | 0 | String aux = getEncryptator().decrypt(str[0]); |
146 | |
|
147 | |
|
148 | 0 | String clientEnc = getEncryptator().decrypt(aux, theEntity); |
149 | |
|
150 | |
|
151 | 0 | keyOk = clientEnc.equals(theClient); |
152 | |
|
153 | |
|
154 | 0 | if (keyOk) |
155 | |
{ |
156 | |
|
157 | 0 | aux = getEncryptator().decrypt(str[1]); |
158 | |
|
159 | |
|
160 | 0 | aux = getEncryptator().decrypt(aux, theEntity); |
161 | |
|
162 | |
|
163 | 0 | String dateEnc = getEncryptator().decrypt(aux, theClient); |
164 | |
|
165 | |
|
166 | 0 | if (!dateEnc.equals(FULL_VERSION_DATE)) |
167 | |
{ |
168 | |
try |
169 | |
{ |
170 | 0 | this.expirationDate = DateUtils.stringToSimpleDate(dateEnc); |
171 | 0 | keyOk = !this.hasExpired(); |
172 | |
} |
173 | 0 | catch (ParseException pe) |
174 | |
{ |
175 | 0 | DIFLogger.getLogger().debug(pe.getMessage()); |
176 | 0 | keyOk = false; |
177 | |
} |
178 | |
} |
179 | |
} |
180 | |
|
181 | 0 | this.edition = LicenseEditionType.STANDARD; |
182 | 0 | if (keyOk && str.length > 2) |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | |
|
187 | 0 | aux = getEncryptator().decrypt(str[2]); |
188 | |
|
189 | |
|
190 | 0 | aux = getEncryptator().decrypt(aux, theEntity); |
191 | |
|
192 | |
|
193 | 0 | String decryptEdition = getEncryptator().decrypt(aux, theClient); |
194 | |
|
195 | 0 | if (LicenseEditionType.PREMIUM.name().equals(decryptEdition)) |
196 | |
{ |
197 | 0 | this.edition = LicenseEditionType.PREMIUM; |
198 | |
} |
199 | |
} |
200 | 0 | catch (CryptoException ce) |
201 | |
{ |
202 | 0 | keyOk = false; |
203 | |
} |
204 | |
} |
205 | 0 | this.name = clientEnc; |
206 | |
} |
207 | 0 | catch (CryptoException ce) |
208 | |
{ |
209 | 0 | DIFLogger.getLogger().debug(ce.getMessage()); |
210 | 0 | keyOk = false; |
211 | |
} |
212 | 0 | return keyOk; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public String generateKey(String client, String entity, String date, LicenseEditionType edition) |
220 | |
{ |
221 | 0 | if (edition == null) |
222 | |
{ |
223 | 0 | edition = LicenseEditionType.STANDARD; |
224 | |
} |
225 | |
|
226 | 0 | if (date == null || "".equals(date.trim())) |
227 | |
{ |
228 | 0 | date = FULL_VERSION_DATE; |
229 | |
} |
230 | |
String theClient; |
231 | 0 | if (client == null || "".equals(client.trim())) |
232 | |
{ |
233 | 0 | theClient = StringUtils.removeAccents(this.getClientName()); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | theClient = StringUtils.removeAccents(client); |
238 | |
} |
239 | |
|
240 | 0 | String theApp = StringUtils.removeAccents(entity); |
241 | |
|
242 | 0 | String result = null; |
243 | 0 | String phaseOne = null; |
244 | 0 | String phaseTwo = null; |
245 | 0 | String phaseThree = null; |
246 | |
try |
247 | |
{ |
248 | |
|
249 | |
|
250 | |
|
251 | 0 | String aux = getEncryptator().encrypt(theClient, theApp); |
252 | |
|
253 | |
|
254 | 0 | phaseOne = getEncryptator().encrypt(aux); |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | 0 | aux = getEncryptator().encrypt(date, theClient); |
260 | |
|
261 | 0 | aux = getEncryptator().encrypt(aux, theApp); |
262 | |
|
263 | 0 | phaseTwo = getEncryptator().encrypt(aux); |
264 | |
|
265 | |
|
266 | 0 | aux = getEncryptator().encrypt(edition.name(), theClient); |
267 | |
|
268 | 0 | aux = getEncryptator().encrypt(aux, theApp); |
269 | |
|
270 | 0 | phaseThree = getEncryptator().encrypt(aux); |
271 | |
|
272 | |
|
273 | 0 | result = phaseOne + SPLITER_REGISTRATION_KEY + phaseTwo + SPLITER_REGISTRATION_KEY + phaseThree; |
274 | |
|
275 | 0 | return result; |
276 | |
} |
277 | 0 | catch (CryptoException ce) |
278 | |
{ |
279 | 0 | DIFLogger.getLogger().info(ce.getMessage()); |
280 | |
|
281 | 0 | return null; |
282 | |
} |
283 | |
} |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
protected String getClientName() |
291 | |
{ |
292 | 0 | return DIFGeneralConfigurationParameters.getInstance().getClient(); |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public LicenseEditionType getEdition() |
299 | |
{ |
300 | |
|
301 | 0 | LicenseEditionType result = null; |
302 | 0 | if (isRegistered()) |
303 | |
{ |
304 | 0 | result = this.edition; |
305 | |
} |
306 | 0 | return result; |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
private IEncryptor getEncryptator() |
315 | |
{ |
316 | 0 | if (encryptor == null) |
317 | |
{ |
318 | 0 | encryptor = new EncryptorBase64Impl(); |
319 | 0 | encryptor.setSeed(getSeed()); |
320 | |
} |
321 | 0 | return encryptor; |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public Date getExpirationDate() |
328 | |
{ |
329 | 0 | return this.expirationDate; |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
public String getKey() |
336 | |
{ |
337 | 0 | return key; |
338 | |
} |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
public String getName() |
344 | |
{ |
345 | 0 | return name; |
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
protected String getSeed() |
354 | |
{ |
355 | 0 | return variableToReturn; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
public boolean hasExpirationDate() |
362 | |
{ |
363 | 0 | return this.getExpirationDate() != null; |
364 | |
} |
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
public boolean hasExpired() |
370 | |
{ |
371 | 0 | if (this.expirationDate == null) |
372 | 0 | return false; |
373 | |
|
374 | 0 | Date dateCur = DateUtils.getTrimmedDate(); |
375 | |
|
376 | 0 | return (dateCur.after(this.expirationDate)); |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
public boolean isRegistered() |
383 | |
{ |
384 | |
|
385 | 0 | if (this.keyChecked) |
386 | |
{ |
387 | 0 | if (this.hasExpirationDate()) |
388 | |
{ |
389 | 0 | return !this.hasExpired(); |
390 | |
} |
391 | 0 | return this.isValidKey(); |
392 | |
} |
393 | |
else |
394 | |
{ |
395 | 0 | this.setValidKey(this.checkKey()); |
396 | 0 | this.keyChecked = isValidKey(); |
397 | |
} |
398 | 0 | return isValidKey(); |
399 | |
} |
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
public boolean isRegistrable() |
405 | |
{ |
406 | 0 | return registrable; |
407 | |
} |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
private boolean isValidKey() |
415 | |
{ |
416 | 0 | return validKey; |
417 | |
} |
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
public boolean register(String key, String entityId) |
423 | |
{ |
424 | 0 | if (isRegistrable()) |
425 | |
{ |
426 | 0 | this.key = key; |
427 | 0 | this.entityId = entityId; |
428 | 0 | this.keyChecked = false; |
429 | 0 | return isRegistered(); |
430 | |
} |
431 | |
else |
432 | 0 | return true; |
433 | |
|
434 | |
} |
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
public void setName(String name) |
440 | |
{ |
441 | 0 | this.name = name; |
442 | 0 | } |
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
public void setRegistrable(boolean registrable) |
448 | |
{ |
449 | 0 | this.registrable = registrable; |
450 | 0 | } |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
private void setValidKey(boolean validKey) |
459 | |
{ |
460 | 0 | this.validKey = validKey; |
461 | 0 | } |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
private boolean simpleKeyCheck(String _regKeyToCheck) |
471 | |
{ |
472 | |
|
473 | 0 | if (_regKeyToCheck == null || _regKeyToCheck.indexOf(SPLITER_REGISTRATION_KEY) == -1) |
474 | |
{ |
475 | 0 | return false; |
476 | |
} |
477 | 0 | return true; |
478 | |
} |
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
public void unregister() |
484 | |
{ |
485 | 0 | key = null; |
486 | 0 | validKey = false; |
487 | 0 | this.keyChecked = false; |
488 | 0 | this.expirationDate = null; |
489 | 0 | } |
490 | |
|
491 | |
} |