token-verify-att 1.0.0
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/index.js +78 -0
- package/package.json +18 -0
package/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const cache = require("node-cache");
|
|
3
|
+
|
|
4
|
+
const tokenCache = new cache({ stdTTL: 3600 }); // Cache con tiempo de vida de 1 hora
|
|
5
|
+
|
|
6
|
+
async function getToken(req) {
|
|
7
|
+
const body = req.body;
|
|
8
|
+
const cachedToken = tokenCache.get(body.token_key);
|
|
9
|
+
console.log("Usando token en caché");
|
|
10
|
+
if (cachedToken && cachedToken.expires_at > Date.now()) {
|
|
11
|
+
return cachedToken;
|
|
12
|
+
}
|
|
13
|
+
return requestNewToken(body);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async function requestNewToken(request) {
|
|
19
|
+
try {
|
|
20
|
+
// codificacion de credenciales en base64
|
|
21
|
+
const credentials = Buffer.from(`${request.client_id}:${request.client_secret}`).toString('base64');
|
|
22
|
+
const data = {
|
|
23
|
+
grant_type: request.grant_type
|
|
24
|
+
}
|
|
25
|
+
console.log("Solicitando nuevo token...");
|
|
26
|
+
|
|
27
|
+
// consumo de API de creacion de token
|
|
28
|
+
const response = await axios.post(
|
|
29
|
+
request.URL_service_auth, data,
|
|
30
|
+
{
|
|
31
|
+
headers:
|
|
32
|
+
{
|
|
33
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
34
|
+
"Authorization": `Basic ${credentials}`
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
// extraccion de datos del token
|
|
40
|
+
const { access_token, expires_in, token_type, scope } = response.data;
|
|
41
|
+
const expires_at = Date.now() + expires_in * 1000;
|
|
42
|
+
|
|
43
|
+
// almacenamiento del token en cache
|
|
44
|
+
const exito = cargaTokenCache(response.data.access_token, response.data.expires_in, expires_at, request.token_key);
|
|
45
|
+
console.log("Nuevo token obtenido y almacenado en caché: ", exito);
|
|
46
|
+
|
|
47
|
+
return { access_token, expires_at, expires_in, token_type, scope };
|
|
48
|
+
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error("Error al solicitar token:", error.response?.data || error);
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function revocaToken(token) {
|
|
57
|
+
// Lógica para revocar el token
|
|
58
|
+
console.log(`Token ${token} revocado.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function cargaTokenCache(token, expiresIn, expiresAt, tokenKey) {
|
|
62
|
+
|
|
63
|
+
const authData = {
|
|
64
|
+
access_token: token,
|
|
65
|
+
expires_in: expiresIn,
|
|
66
|
+
expires_at: expiresAt
|
|
67
|
+
};
|
|
68
|
+
const clave = tokenKey;
|
|
69
|
+
|
|
70
|
+
const exito = tokenCache.set(clave, authData);
|
|
71
|
+
|
|
72
|
+
return exito;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = {
|
|
76
|
+
getToken,
|
|
77
|
+
revocaToken
|
|
78
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "token-verify-att",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private" : false,
|
|
5
|
+
"description": "Libreria para verificar tokens de autenticación",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"token",
|
|
9
|
+
"verificación",
|
|
10
|
+
"autenticación",
|
|
11
|
+
"seguridad"
|
|
12
|
+
],
|
|
13
|
+
"author": "LJMS",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
}
|
|
18
|
+
}
|