applay-utils 1.8.28 → 1.8.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applay-utils",
3
- "version": "1.8.28",
3
+ "version": "1.8.32",
4
4
  "description": "Utilitary tools for Applay applications",
5
5
  "scripts": {
6
6
  "build": "npm version patch",
package/utils/crypto.js CHANGED
@@ -1,36 +1,47 @@
1
- var CryptoJS = require('crypto-js');
2
- var algorithm = 'aes-256-cbc';
3
- var key = process.env.key;
1
+ const CryptoJS = require('crypto-js');
2
+ const algorithm = 'aes-256-cbc';
4
3
  const sha1 = require('sha1');
5
4
 
6
- var utils = {
5
+ module.exports = {
7
6
  'genHash': (orig) => {
8
- if (!key) return null;
7
+ if (!process.env.key) return null;
9
8
  var json = Object({}, orig);
10
9
  if (json._id) { delete json._id; }
11
10
  if (json.apyId) { delete json.apyId; }
12
- var block = `KEY=>${key}|JSON=>${JSON.stringify(json)}`;
11
+ var block = `KEY=>${process.env.key}|JSON=>${JSON.stringify(json)}`;
13
12
  orig.apyId = sha1(block);
14
13
  return orig;
15
14
  },
16
15
  'verifyHash': (orig) => {
17
- if (!key) return null;
16
+ if(!process.env.key) {
17
+ console.error('Key not set, cannot verifyHash data');
18
+ return null;
19
+ }
20
+
18
21
  var json = utils.genHash(Object({}, orig));
19
22
  console.log(json.apyId, orig.apyId);
20
23
  return json.apyId == orig.apyId
21
24
  },
22
- 'setKey': (newkey)=>{key=newkey},
25
+ 'setKey': (newkey)=>{process.env.key=newkey},
23
26
  'genIV': () => {
24
27
  return Math.random().toString(36).substr(2, 30);
25
28
  },
26
29
  'encrypt': (text, iv) => {
27
- if(!key) return null;
28
- let base = CryptoJS.AES.encrypt(text, key, { iv: iv });
30
+ if(!process.env.key) {
31
+ console.error('Key not set, cannot encrypt data');
32
+ return null;
33
+ }
34
+
35
+ let base = CryptoJS.AES.encrypt(text, process.env.key, { iv: iv });
29
36
  return base.toString();
30
37
  },
31
38
  'decrypt': (text, iv) => {
32
- if(!key) return null;
33
- let data = CryptoJS.AES.decrypt(text, key, { iv: iv }).toString(CryptoJS.enc.Utf8);
39
+ if(!process.env.key) {
40
+ console.error('Key not set, cannot decrypt data');
41
+ return null;
42
+ }
43
+
44
+ let data = CryptoJS.AES.decrypt(text, process.env.key, { iv: iv }).toString(CryptoJS.enc.Utf8);
34
45
  return data;
35
46
  },
36
47
  'protect': (data) => {
@@ -38,13 +49,14 @@ var utils = {
38
49
  return { cipher: utils.encrypt(JSON.stringify(data), iv), iv: iv, protect: true, app_key: '', app_secret: '' }
39
50
  },
40
51
  'extract': (data) => {
41
- if(!key) return null;
52
+ if(!process.env.key) {
53
+ console.error('Key not set, cannot extract data');
54
+ return null;
55
+ }
42
56
  return JSON.parse(utils.decrypt(data.cipher, data.iv));
43
57
  },
44
58
  'envelope': (dataFull) => {
45
59
  dataFull.data = utils.protect(dataFull.data);
46
60
  return dataFull;
47
61
  }
48
- }
49
-
50
- module.exports=utils;
62
+ }
package/utils/jwt.js CHANGED
@@ -1,28 +1,27 @@
1
1
  const jwt = require('jsonwebtoken');
2
- var key = process.env.key;
3
- var keyportal = process.env.keyportal;
2
+ // var process.env.key = process.env.process.env.key;
3
+ // var process.env.keyportal = process.env.process.env.keyportal;
4
4
 
5
5
 
6
- var utils = {
7
- 'setKey': (newkey) => { key = newkey; },
8
- 'setKeyPortal': (newkey) => { keyportal = newkey; },
9
- 'checkPortal': (keyCheck) => keyCheck == keyportal,
6
+ module.exports = {
7
+ 'setKey': (newkey) => { process.env.key = newkey; },
8
+ 'setKeyPortal': (newkey) => { process.env.keyportal = newkey; },
9
+ 'checkPortal': (keyCheck) => keyCheck == process.env.keyportal,
10
10
  'auth': (req, res, next) => {
11
11
  const token = req.headers['x-access-token'];
12
12
  if (!token) return res.status(401).json({ auth: false, message: 'Sem token definido.' });
13
- if (!key) return res.status(401).json({ auth: false, message: 'Sem key definida.' });
14
- jwt.verify(token, key, (err, decoded) => {
13
+ if (!process.env.key) return res.status(401).json({ auth: false, message: 'Sem process.env.key definida.' });
14
+ jwt.verify(token, process.env.key, (err, decoded) => {
15
15
  if (err) return res.status(401).json({ auth: false, message: 'Falha na autenticação do token.' });
16
16
  req.tokenId = decoded.id;
17
17
  next();
18
18
  });
19
19
  },
20
20
  'login': (id, time) => {
21
- if (!key) return null;
22
- const token = jwt.sign({ id }, key, {
21
+ if (!process.env.key) return null;
22
+ const token = jwt.sign({ id }, process.env.key, {
23
23
  expiresIn: time || (60 * 30) // segundos
24
24
  });
25
25
  return token;
26
26
  }
27
- }
28
- module.exports = utils;
27
+ }