-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
68 lines (58 loc) · 2.07 KB
/
index.js
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
'use strict';
const debug = require('debug')('id5');
const Crypto = require('./lib/crypto');
const soap = require('strong-soap').soap;
const XMLHandler = soap.XMLHandler;
const xmlHandler = new XMLHandler();
class ID5 {
constructor(options) {
options = options || {};
this.username = options.username || throwError('username');
this.password = options.password || throwError('password');
this.wsdlUrl = options.wsdlUrl || throwError('wsdlUrl');
this.key = options.key || throwError('key');
this.iv = options.iv || throwError('iv');
debug('id5 config %j', options);
this.crypto = new Crypto({ key: this.key, iv: this.iv });
}
async validateIDInfo(realname, idcard) {
debug('checking id info: name(%s), idcard(%s)', realname, idcard);
const params = this.idInfoParams(realname, idcard);
const p = new Promise((resolve, reject) => {
soap.createClient(this.wsdlUrl, (err, client) => {
if (err) {
return reject(err);
}
debug('sending request with params (%j)', params);
client.querySingle(params, (err, result) => {
if (err) return reject(err);
resolve(result);
}, { timeout: 5000 });
});
});
try {
const result = await p;
if (!result.querySingleReturn) return false;
const xml = this.crypto.decrypt(result.querySingleReturn);
const jsonResult = xmlHandler.xmlToJson(null, xml, null);
debug('Got response result (%j)', jsonResult);
if (jsonResult.data.message.status !== '0') return false;
return jsonResult.data.policeCheckInfos.policeCheckInfo.compStatus.$value === '3';
} catch (e) {
debug('Got error (%s)', e.message);
throw e;
}
}
idInfoParams(realname, idcard) {
return {
type_: this.crypto.encrypt('1A020201'),
userName_: this.crypto.encrypt(this.username),
password_: this.crypto.encrypt(this.password),
param_: this.crypto.encrypt(`${realname},${idcard}`),
};
}
}
function throwError(optionKey) {
throw new Error(`${optionKey} is required`);
}
module.exports = ID5;