applay-utils 1.2.12 → 1.2.14

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/jwt.js +21 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applay-utils",
3
- "version": "1.2.12",
3
+ "version": "1.2.14",
4
4
  "description": "Utilitary tools for Applay applications",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/utils/jwt.js CHANGED
@@ -1,31 +1,28 @@
1
1
  const jwt = require('jsonwebtoken');
2
- var key=process.env.key;
2
+ var key = process.env.key;
3
3
  var keyportal = process.env.keyportal;
4
4
 
5
5
 
6
6
  var utils = {
7
- 'setKey': (newkey) => { key=newkey; },
8
- 'setKeyPortal': (newkey) => { keyportal = newkey; },
9
- 'checkPortal': (keyCheck) => keyCheck == keyportal,
10
- 'auth': (req, res, next) => {
11
- const token = req.headers['x-access-token'];
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) => {
15
- if (err) return res.status(401).json({ auth: false, message: 'Falha na autenticação do token.' });
16
- req.userId = decoded.id.split('#')[0];
17
- if (req.headers.cliente != decoded.id.split('#')[1]) {
18
- return res.status(401).json({ auth: false, message: 'cliente diferente do token.' });
19
- }
20
- next();
21
- });
22
- },
23
- 'login': (id, time) => {
24
- if(!key) return null;
25
- const token = jwt.sign({ id }, key, {
26
- expiresIn: time||(60 * 30) // segundos
27
- });
28
- return token;
29
- }
7
+ 'setKey': (newkey) => { key = newkey; },
8
+ 'setKeyPortal': (newkey) => { keyportal = newkey; },
9
+ 'checkPortal': (keyCheck) => keyCheck == keyportal,
10
+ 'auth': (req, res, next) => {
11
+ const token = req.headers['x-access-token'];
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) => {
15
+ if (err) return res.status(401).json({ auth: false, message: 'Falha na autenticação do token.' });
16
+ req.tokenId = decoded.id;
17
+ next();
18
+ });
19
+ },
20
+ 'login': (id, time) => {
21
+ if (!key) return null;
22
+ const token = jwt.sign({ id }, key, {
23
+ expiresIn: time || (60 * 30) // segundos
24
+ });
25
+ return token;
26
+ }
30
27
  }
31
28
  module.exports = utils;