Statistics
| Branch: | Revision:

m2u-upass-core / src / my / com / upass / secure / metric / SecureMetricTokenImport.java @ 0:02300db8682b

History | View | Annotate | Download (17.1 KB)

1
/*
2
 * Copyright (c) 2012 Penril Datability (M) Sdn Bhd All rights reserved.
3
 *
4
 * This software is copyrighted. Under the copyright laws, this software
5
 * may not be copied, in whole or in part, without prior written consent
6
 * of Penril Datability (M) Sdn Bhd or its assignees. This software is
7
 * provided under the terms of a license between Penril Datability (M)
8
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
9
 * license.
10
 */
11
package my.com.upass.secure.metric;
12

    
13
import java.util.HashMap;
14
import java.util.Map;
15

    
16
import ft.otp.core.api.License;
17
import ft.otp.core.api.OTPCore;
18
import ft.otp.core.api.OTPCoreFactory;
19
import ft.otp.core.api.Version;
20
import ft.otp.core.cfg.Config;
21
import ft.otp.core.entity.LicenseInfo;
22
import ft.otp.core.exception.OTPCoreException;
23

    
24
import my.com.upass.Constants;
25
import my.com.upass.UPassController;
26
import my.com.upass.dao.DAOFactoryProvider;
27
import my.com.upass.dao.UserDAO;
28
import my.com.upass.pojo.UserTokenBean;
29

    
30
/*
31
 * <pre>
32
 * PROGRAMMER: Enson Yeoh
33
 * CHANGE-NO:
34
 * TASK-NO:
35
 * DATE CREATED: May 7, 2013
36
 * TAG AS:
37
 * REASON(S):
38
 * MODIFICATION:
39
 * </pre>
40
 */
41
public class SecureMetricTokenImport  {
42
        private static final String TOKENOWNER = "TokenOwner";
43
        private static Map<String, UserTokenBean> tokenInfo = new HashMap<String, UserTokenBean>();
44
        
45
        private static OTPCore otpcore                         = getOTPCore();
46
        private static final String configFile         = "otpcore.xml";
47
        
48
        private static OTPCore getOTPCore() {
49
                final OTPCore core = OTPCoreFactory.getOTPCore();
50
                        try {
51
                                core.initialize(configFile, 0);
52
                                Thread hook = new Thread() {
53
                                        public void run() {
54
                                                core.terminal();
55
                                        }
56
                                };
57
                                Runtime.getRuntime().addShutdownHook(hook);
58
                                
59
                        } catch (OTPCoreException e) {
60
                                e.printStackTrace();
61
                                System.exit(1);
62
                        }
63
                return core;
64
        }
65
        
66
        private int totalToken;        
67
        private int successCount;
68
        private int rejectedCount;
69
        
70
        /**
71
         * This will return the updated map that contains token info
72
         * 
73
         * @return
74
         */
75
        public Map<String, UserTokenBean> getTokenInfo() {
76
                return tokenInfo;
77
        }
78
        
79
        /**
80
         * this will override the existing total token
81
         * 
82
         * @param totalToken
83
         */
84
        public void setTotalToken(int totalToken)
85
        {
86
                this.totalToken = totalToken;
87
        }
88
        
89
        /**
90
         * This will return the updated total token
91
         * 
92
         * @return
93
         */
94
        public int getTotalToken()
95
        {
96
                return totalToken;
97
        }
98
        
99
        /**
100
         * this will override the existing success count
101
         * 
102
         * @param sucessCount
103
         */
104
        public void setSuccessCount(int successCount)
105
        {
106
                this.successCount = successCount;
107
        }
108
        
109
        /**
110
         * This will return the updated success count
111
         * 
112
         * @return
113
         */
114
        public int getSuccessCount()
115
        {
116
                return successCount;
117
        }
118
        
119
        /**
120
         * this will override the existing rejected count
121
         * 
122
         * @param rejectedCount
123
         */
124
        public void setRejectedCount(int rejectedCount)
125
        {
126
                this.rejectedCount = rejectedCount;
127
        }
128
        
129
        /**
130
         * This will return the updated rejected count
131
         * 
132
         * @return
133
         */
134
        public int getRejectedCount()
135
        {
136
                return rejectedCount;
137
        }
138
        
139
        //To display imported tokens
140
        public void displayToken(String serialNumber)  {                                
141
                System.out.println("--Info----------------------------------------------");
142
                System.out.println("SERIAL NUMBER....." + tokenInfo.get(serialNumber).getVserialNumber());
143
                System.out.println("TOKEN_MODEL......." + tokenInfo.get(serialNumber).getVdpModel());
144
                System.out.println("USE_COUNT........." + tokenInfo.get(serialNumber).getVuseCount());
145
                System.out.println("ERR_COUNT........." + tokenInfo.get(serialNumber).getVerrorCount());
146
                System.out.println("LAST_TIME_USED...." + tokenInfo.get(serialNumber).getVdateLastUsed());
147
                System.out.println("----------------------------------------------------");
148
        }
149
        
150
        @SuppressWarnings ("unused")
151
        private long getTokenOwner(String userAlias)  {
152
                UPassController upc = new UPassController();
153
                return upc.UA_GetUserIDByAlias(userAlias);
154
        }
155
        
156
        /**
157
         * import TNK and LIC files to retrieve tokens.
158
         * 
159
         * @param tnkfilePath
160
         * @param licFilePath
161
         * @return rc = converted SM return code
162
         * @throws Exception 
163
         */
164
        public int importTNK(String tnkFilePath, String licFilePath) throws Exception {
165
                int rc = Constants.ERR_SUCCESS;                
166
                
167
                long TokenOwnerID = 0;
168
                UserDAO userDao = null;
169
                
170
                /* Get token owner by DAO */
171
                try {
172
                        userDao = DAOFactoryProvider.getDAOFactory ().getUserDAO ();
173
                        TokenOwnerID = userDao.getUserFromStore (TOKENOWNER).getUserID ();
174
                        
175
                } catch (Exception e) {
176
                        e.printStackTrace();
177
                        return Constants.ERR_SYSTEM_NOT_READY;
178
                }
179
                
180
                if (TokenOwnerID==0)  {
181
                        return Constants.ERR_SYSTEM_NOT_READY;
182
                }
183
                
184
                System.out.println("Import file from " + tnkFilePath + " - " + licFilePath);
185
                
186
                //add license
187
        rc = addLicense(licFilePath);
188
        
189
        //import token file
190
        rc = importToken(tnkFilePath);
191
                        
192
                return rc;        
193
        }        
194
        
195
        //get version information 
196
        public int getOtpCoreVersion() throws Exception {
197
                
198
                int rc = Constants.ERR_SUCCESS;
199
                
200
                Version version = otpcore.getVersion();
201
                
202
                if (version != null) {
203
                    System.out.println("Get version succeed, the version is: " + version.getMajorVersion() + "."
204
                        + version.getMinorVersion() + "!");
205
                    System.out.println("The version description: " + version.getDescription());
206
                } else {
207
                    System.out.println("Get version fail!");
208
                }
209
                
210
                return rc;
211
    }
212
        
213
        //set config
214
        public int setOtpCoreConfig() throws Exception {
215
                
216
                int rc = Constants.ERR_SUCCESS;
217
                
218
                try {
219

    
220
                                Config config = otpcore.getConfig();
221
                                
222
                                if (config != null) {
223
                                        config.setCacheCount(1);
224
                                        
225
                                        otpcore.setConfig(config);
226
                                        
227
                                        System.out.println("Set config succeed!");
228
                                } else {
229
                                        System.out.println("Get config fail!");
230
                                }
231
                                
232
                } catch (OTPCoreException e) {
233
                        rc = e.getErrorCode();
234
                        rc = errorCodeConversion(rc);
235
                }
236
                
237
                return rc;
238
    }
239
        
240
        //reload config
241
        public int reloadOtpCoreConfig() throws Exception {        
242
                
243
                int rc = Constants.ERR_SUCCESS;
244
                
245
                try {
246

    
247
                                otpcore.reloadConfig(configFile);
248
                                
249
                                System.out.println("Reload config succeed");
250
        
251
                } catch (OTPCoreException e) {
252
                        rc = e.getErrorCode();
253
                        rc = errorCodeConversion(rc);
254
                }
255
                
256
                return rc;
257
    }
258
        
259
        //test DB connection
260
        public int testDBConnection() throws Exception {
261
                
262
                int rc = Constants.ERR_SUCCESS;
263
                
264
                try {
265
                        //initialize Authentication Server SDK
266
                        int intResult = otpcore.initialize(configFile, 0);
267
                        
268
                        if (intResult == 0) {
269
                                boolean result = otpcore.connectTest(otpcore.getConfig(), false);                        
270
                                
271
                                if (result) {
272
                            System.out.println("Connect test succeed!");
273
                            
274
                        } else {
275
                            System.out.println("Connect test fail!");
276
                        }
277
                                
278
                                //The function is used to terminate the calling of the Authentication Server SDK and release the related resource.
279
                        otpcore.terminal();
280
                        
281
                        } else {
282
                                System.out.println("Init failed");
283
                        }
284
                } catch (OTPCoreException e) {
285
                        rc = e.getErrorCode();
286
                        rc = errorCodeConversion(rc);
287
                }
288
                
289
                return rc;
290
    }
291
        
292
        //add token file
293
        public int importToken(String tnkFilePath) throws Exception {
294
                
295
                int rc = Constants.ERR_SUCCESS;
296
                
297
                try {
298
                
299
                                //Import token(s)
300
                                otpcore.importToken(tnkFilePath);
301
        
302
                } catch (OTPCoreException e) {
303
                        rc = e.getErrorCode();
304
                        rc = errorCodeConversion(rc);
305
                }
306
                
307
                return rc;
308
    }
309
        
310
        //add license file
311
        public int addLicense(String licFilePath) throws Exception {
312
                
313
                int rc = Constants.ERR_SUCCESS;
314
                
315
                try {
316
        
317
                                License license = otpcore.getLicense(licFilePath);
318
                                
319
                                if (license != null) {
320
                                        LicenseInfo licInfo = new LicenseInfo();
321
                                        licInfo.setLicId(license.getLicId());
322
                                        licInfo.setLicInfo(license.getLicInfo());
323
                                        licInfo.setLicTyep(license.getLicScale());
324
                                        licInfo.setIssuer(license.getIssuer());
325
                                        licInfo.setOwner(license.getOwner());
326
                                        // Add to database
327
                                        otpcore.addLicense(licInfo);
328
        
329
                                }                
330
                        
331
                } catch (OTPCoreException e) {
332
                        rc = e.getErrorCode();
333
                        rc = errorCodeConversion(rc);
334
                }
335
                
336
                return rc;
337
    }
338
        
339
        // update license
340
        public int updateLicense(String licFilePath) throws Exception {
341
                
342
                int rc = Constants.ERR_SUCCESS;
343
                
344
                try {
345

    
346
                                otpcore.updateLicense(licFilePath);
347
                                
348
                                System.out.println("Update license succeed");
349

    
350
                } catch (OTPCoreException e) {
351
                        rc = e.getErrorCode();
352
                        rc = errorCodeConversion(rc);
353
                }
354
                
355
                return rc;
356
                
357
    }
358
        
359
        private int errorCodeConversion(int errorCode) throws Exception {                
360
                switch(errorCode) {
361
                        case 0x80001000: return Constants.ERR_SUCCESS;
362
                        case 0x80001001: return Constants.OTPR_CORE_COMMON_FAIL;
363
                        case 0x80001002: return Constants.OTPR_CORE_COMMON_INVALID_PARAMETER;
364
                        case 0x80001003: return Constants.OTPR_CORE_COMMON_INVALID_LICENSE;
365
                        case 0x80001004: return Constants.OTPR_CORE_COMMON_EXPIRED_LICENSE;
366
                        case 0x80002000: return Constants.OTPR_CORE_AUTH_OK;
367
                        case 0x80002001: return Constants.OTPR_CORE_UID_EMPTY;
368
                        case 0x80002002: return Constants.OTPR_CORE_OTP_EMPTY;
369
                        case 0x80002003: return Constants.OTPR_CORE_PIN_EMPTY;
370
                        case 0x80002004: return Constants.OTPR_CORE_TSN_EMPTY;
371
                        case 0x80002005: return Constants.OTPR_CORE_INVALID_UID;
372
                        case 0x80002006: return Constants.OTPR_CORE_INVALID_OTP;
373
                        case 0x80002007: return Constants.OTPR_CORE_INVALID_TOKENKEY;
374
                        case 0x80002008: return Constants.OTPR_CORE_INVALID_AUTHNUM;
375
                        case 0x80002009: return Constants.OTPR_CORE_NEED_SYNC;
376
                        case 0x8000200a: return Constants.OTPR_CORE_INVALID_TIMEDRIFT;
377
                        case 0x8000200b: return Constants.OTPR_CORE_INVALID_USERPIN;
378
                        case 0x8000200c: return Constants.OTPR_CORE_INVALID_DBUPIN;
379
                        case 0x8000200d: return Constants.OTPR_CORE_ERR_GETUSER;
380
                        case 0x8000200e: return Constants.OTPR_CORE_ERR_GETTOKEN;
381
                        case 0x8000200f: return Constants.OTPR_CORE_TOKEN_LOCKED;
382
                        case 0x80002010: return Constants.OTPR_CORE_LOGIN_LOCKED;
383
                        case 0x80002011: return Constants.OTPR_CORE_PIN_NOTINIT;
384
                        case 0x80002012: return Constants.OTPR_CORE_INVALID_TOKENTYPE;
385
                        case 0x80002013: return Constants.OTPR_CORE_INVALID_TOKENSN;
386
                        case 0x80002014: return Constants.OTPR_CORE_ERR_GETASSIGNED;
387
                        case 0x80002015: return Constants.OTPR_CORE_ERR_GETTOKENNUM;
388
                        case 0x80002016: return Constants.OTPR_CORE_TOKEN_BINDED;
389
                        case 0x80002017: return Constants.OTPR_CORE_BIND_EXCEED;
390
                        case 0x80002018: return Constants.OTPR_CORE_NEED_VERIFYPIN;
391
                        case 0x80002019: return Constants.OTPR_CORE_USER_INACTIVE;
392
                        case 0x80002020: return Constants.OTPR_CORE_AGENT_EMPTY;
393
                        case 0x80002021: return Constants.OTPR_CORE_HOST_EMPTY;
394
                        case 0x80002022: return Constants.OTPR_CORE_SHARE_KEY_EMPTY;
395
                        case 0x8000300f: return Constants.OTPR_CORE_DB_INVALID_CONFIGFILE;
396
                        case 0x80003010: return Constants.OTPR_CORE_DB_INVALID_DBTYPE;
397
                        case 0x80003011: return Constants.OTPR_CORE_DB_ERROR_CONNECT;
398
                        case 0x80003012: return Constants.OTPR_CORE_DB_DATASOURCE_NOTFOUND;
399
                        case 0x80003013: return Constants.OTPR_CORE_DB_DATABASE_NOTFOUND;
400
                        case 0x80003014: return Constants.OTPR_CORE_DB_SERVER_NOTFOUND;
401
                        case 0x80003015: return Constants.OTPR_CORE_DB_INVALID_AUTH;
402
                        case 0x80003016: return Constants.OTPR_CORE_DB_NOTBEUSED_CONNECT;
403
                        case 0x80003017: return Constants.OTPR_CORE_DB_TOOMANY_CONNECT;
404
                        case 0x80003018: return Constants.OTPR_CORE_DB_USERSOURCE_ESTOP;
405
                        case 0x80003019: return Constants.OTPR_CORE_DB_USER_EXISTS;
406
                        case 0x8000301a: return Constants.OTPR_CORE_DB_USER_NOTEXISTS;
407
                        case 0x8000301b: return Constants.OTPR_CORE_DB_RECORD_NOTEXISTS;
408
                        case 0x8000301c: return Constants.OTPR_CORE_DB_TOKEN_EXISTS;
409
                        case 0x8000301d: return Constants.OTPR_CORE_DB_TOKEN_NOTEXISTS;
410
                        case 0x8000301e: return Constants.OTPR_CORE_DB_AGENT_EXISTS;
411
                        case 0x8000301f: return Constants.OTPR_CORE_DB_AGENT_NOTEXISTS;
412
                        case 0x80003020: return Constants.OTPR_CORE_DB_HOST_EXISTS;
413
                        case 0x80003021: return Constants.OTPR_CORE_DB_HOST_NOTEXISTS;
414
                        case 0x80003022: return Constants.OTPR_CORE_DB_INVALID_USERSOURCETYPE;
415
                        case 0x80003023: return Constants.OTPR_CORE_DB_NOTENOUGH_BUFFER;
416
                        case 0x80003024: return Constants.OTPR_CORE_DB_ERROR_QUERY;
417
                        case 0x80003025: return Constants.OTPR_CORE_DB_ADD_USER_FAILED;
418
                        case 0x80003026: return Constants.OTPR_CORE_DB_DEL_USER_FAILED;
419
                        case 0x80003027: return Constants.OTPR_CORE_DB_QUERY_USER_FAILED;
420
                        case 0x80003028: return Constants.OTPR_CORE_DB_UPDATE_USER_FAILED;
421
                        case 0x80003029: return Constants.OTPR_CORE_DB_ADD_TOKEN_FAILED;
422
                        case 0x80003030: return Constants.OTPR_CORE_DB_DEL_TOKEN_FAILED;
423
                        case 0x80003031: return Constants.OTPR_CORE_DB_QUERY_TOKEN_FAILED;
424
                        case 0x80003032: return Constants.OTPR_CORE_DB_UPDATE_TOKEN_FAILED;
425
                        case 0x80003033: return Constants.OTPR_CORE_DB_BIND_USER_TOKEN_FAILED;
426
                        case 0x80003034: return Constants.OTPR_CORE_DB_UNBIND_USER_TOKEN_FAILED;
427
                        case 0x80003035: return Constants.OTPR_CORE_DB_USER_TOKEN_UNBIND;
428
                        case 0x80003036: return Constants.OTPR_CORE_DB_ADD_AGENT_FAILED;
429
                        case 0x80003037: return Constants.OTPR_CORE_DB_DEL_AGENT_FAILED;
430
                        case 0x80003038: return Constants.OTPR_CORE_DB_CHANGE_AGENT_FAILED;
431
                        case 0x80003039: return Constants.OTPR_CORE_DB_QUERY_AGENT_FAILED;
432
                        case 0x80003040: return Constants.OTPR_CORE_DB_ADD_HOST_FAILED;
433
                        case 0x80003041: return Constants.OTPR_CORE_DB_DEL_HOST_FAILED;
434
                        case 0x80003042: return Constants.OTPR_CORE_DB_CHANGE_HOST_FAILED;
435
                        case 0x80003043: return Constants.OTPR_CORE_DB_QUERY_HOST_FAILED;
436
                        case 0x80003044: return Constants.OTPR_CORE_DB_ADD_AGENT_HOST_FAILED;
437
                        case 0x80003045: return Constants.OTPR_CORE_DB_DEL_AGENT_HOST_FAILED;
438
                        case 0x80003046: return Constants.OTPR_CORE_DB_CHANGE_AGENT_HOST_FAILED;
439
                        case 0x80003047: return Constants.OTPR_CORE_DB_QUERY_AGENT_HOST_FAILED;
440
                        case 0x80003048: return Constants.OTPR_CORE_DB_ADD_LOG_FAILED;
441
                        case 0x80003049: return Constants.OTPR_CORE_DB_DEL_LOG_FAILED;
442
                        case 0x80003050: return Constants.OTPR_CORE_DB_CHANGE_LOG_FAILED;
443
                        case 0x80003051: return Constants.OTPR_CORE_DB_QUERY_LOG_FAILED;
444
                        case 0x80003052: return Constants.OTPR_CORE_DB_ADD_CONFIG_FAILED;
445
                        case 0x80003053: return Constants.OTPR_CORE_DB_DEL_CONFIG_FAILED;
446
                        case 0x80003054: return Constants.OTPR_CORE_DB_CHANGE_CONFIG_FAILED;
447
                        case 0x80003055: return Constants.OTPR_CORE_DB_QUERY_CONFIG_FAILED;
448
                        case 0x80003056: return Constants.OTPR_CORE_DB_ADD_ADMIN_GROUP_FAILED;
449
                        case 0x80003057: return Constants.OTPR_CORE_DB_DEL_ADMIN_GROUP_FAILED;
450
                        case 0x80003058: return Constants.OTPR_CORE_DB_CHANGE_ADMIN_GROUP_FAILED;
451
                        case 0x80003059: return Constants.OTPR_CORE_DB_QUERY_ADMIN_GROUP_FAILED;
452
                        case 0x80003060: return Constants.OTPR_CORE_DB_ADD_ADMIN_LOG_FAILED;
453
                        case 0x80003061: return Constants.OTPR_CORE_DB_DEL_ADMIN_LOG_FAILED;
454
                        case 0x80003062: return Constants.OTPR_CORE_DB_CHANGE_ADMIN_LOG_FAILED;
455
                        case 0x80003063: return Constants.OTPR_CORE_DB_QUERY_ADMIN_LOG_FAILED;
456
                        case 0x80003064: return Constants.OTPR_CORE_DB_ADD_ADMIN_USER_FAILED;
457
                        case 0x80003065: return Constants.OTPR_CORE_DB_DEL_ADMIN_USER_FAILED;
458
                        case 0x80003066: return Constants.OTPR_CORE_DB_CHANGE_ADMIN_USER_FAILED;
459
                        case 0x80003067: return Constants.OTPR_CORE_DB_QUERY_ADMIN_USER_FAILED;
460
                        case 0x80003068: return Constants.OTPR_CORE_DB_ADD_DOMAIN_FAILED;
461
                        case 0x80003069: return Constants.OTPR_CORE_DB_DEL_DOMAIN_FAILED;
462
                        case 0x80003070: return Constants.OTPR_CORE_DB_CHANGE_DOMAIN_FAILED;
463
                        case 0x80003071: return Constants.OTPR_CORE_DB_QUERY_DOMAIN_FAILED;
464
                        case 0x80003072: return Constants.OTPR_CORE_DB_ADD_TEMP_USER_FAILED;
465
                        case 0x80003073: return Constants.OTPR_CORE_DB_DEL_TEMP_USER_FAILED;
466
                        case 0x80003074: return Constants.OTPR_CORE_DB_CHANGE_TEMP_USER_FAILED;
467
                        case 0x80003075: return Constants.OTPR_CORE_DB_QUERY_TEMP_USER_FAILED;
468
                        case 0x80003076: return Constants.OTPR_CORE_DB_RELOAD_CONNECTION_FAILED;
469
                        case 0x80003077: return Constants.OTPR_CORE_COMM_EXPIRED_LOG4J;
470
                        case 0x80003201: return Constants.OTPR_CORE_DB_ADD_APPGROUP_FAILED;
471
                        case 0x80003202: return Constants.OTPR_CORE_DB_DEL_APPGROUP_FAILED;
472
                        case 0x80003203: return Constants.OTPR_CORE_DB_CHANGE_APPGROUP_FAILED;
473
                        case 0x80003204: return Constants.OTPR_CORE_DB_QUERY_APPGROUP_FAILED;
474
                        case 0x80003205: return Constants.OTPR_CORE_DB_ADD_USER_GROUP_FAILED;
475
                        case 0x80003206: return Constants.OTPR_CORE_DB_DEL_USER_GROUP_FAILED;
476
                        case 0x80003207: return Constants.OTPR_CORE_DB_CHANGE_USER_GROUP_FAILED;
477
                        case 0x80003208: return Constants.OTPR_CORE_DB_QUERY_USER_GROUP_FAILED;
478
                        case 0x80003212: return Constants.OTPR_CORE_DB_ADD_ZONE_FAILED;
479
                        case 0x80003213: return Constants.OTPR_CORE_DB_DEL_ZONE_FAILED;
480
                        case 0x80003214: return Constants.OTPR_CORE_DB_CHANGE_ZONE_FAILED;
481
                        case 0x80003215: return Constants.OTPR_CORE_DB_QUERY_ZONE_FAILED;
482
                        case 0x80003216: return Constants.OTPR_CORE_DB_ADD_LICENSE_FAILED;
483
                        case 0x80003217: return Constants.OTPR_CORE_DB_DEL_LICENSE_FAILED;
484
                        case 0x80003218: return Constants.OTPR_CORE_DB_CHANGE_LICENSE_FAILED;
485
                        case 0x80003219: return Constants.OTPR_CORE_DB_QUERY_LICENSE_FAILED;
486
                        case 0x80003226: return Constants.OTPR_CORE_DB_LDAP_INIT_FAILED;
487
                        default: throw new Exception("Invalid error code.");
488
                }
489
        }
490
}