jerkjs 2.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/LICENSE +200 -0
- package/README.md +171 -0
- package/doc/EXTENSION_MANUAL.md +958 -0
- package/doc/FIREWALL_MANUAL.md +419 -0
- package/doc/HOOKS_REFERENCE_IMPROVED.md +599 -0
- package/doc/MANUAL_API_SDK.md +539 -0
- package/doc/MANUAL_MVC.md +397 -0
- package/doc/MARIADB_TOKENS_IMPLEMENTATION.md +113 -0
- package/doc/MIDDLEWARE_MANUAL.md +521 -0
- package/doc/OAUTH2_GOOGLE_MANUAL.md +408 -0
- package/doc/frontend-and-sessions.md +356 -0
- package/examples/advanced/controllers/productController.js +64 -0
- package/examples/advanced/controllers/userController.js +85 -0
- package/examples/advanced/routes.json +51 -0
- package/examples/advanced_example.js +93 -0
- package/examples/basic/controllers/userController.js +85 -0
- package/examples/basic_example.js +72 -0
- package/examples/frontend/README.md +71 -0
- package/examples/frontend/app.js +71 -0
- package/examples/frontend/controllers/apiController.js +39 -0
- package/examples/frontend/controllers/authController.js +220 -0
- package/examples/frontend/controllers/formController.js +47 -0
- package/examples/frontend/controllers/messageController.js +96 -0
- package/examples/frontend/controllers/pageController.js +178 -0
- package/examples/frontend/controllers/staticController.js +167 -0
- package/examples/frontend/routes.json +90 -0
- package/examples/mvc_example/app.js +138 -0
- package/examples/mvc_example/views/home/index.html +26 -0
- package/examples/mvc_example/views/home/simple.html +3 -0
- package/examples/mvc_example/views/layout.html +23 -0
- package/examples/mvc_example/views/test.html +3 -0
- package/examples/mvc_example/views/user/invalid.html +6 -0
- package/examples/mvc_example/views/user/list.html +36 -0
- package/examples/mvc_example/views/user/notfound.html +6 -0
- package/examples/mvc_example/views/user/profile.html +11 -0
- package/examples/mvc_routes_example/app.js +34 -0
- package/examples/mvc_routes_example/controllers/mainController.js +27 -0
- package/examples/mvc_routes_example/controllers/productController.js +47 -0
- package/examples/mvc_routes_example/controllers/userController.js +76 -0
- package/examples/mvc_routes_example/routes.json +30 -0
- package/examples/mvc_routes_example/views/layout.html +31 -0
- package/examples/mvc_routes_example/views/main/index.html +11 -0
- package/examples/mvc_routes_example/views/product/catalog.html +24 -0
- package/examples/mvc_routes_example/views/user/invalid.html +6 -0
- package/examples/mvc_routes_example/views/user/list.html +40 -0
- package/examples/mvc_routes_example/views/user/notfound.html +6 -0
- package/examples/mvc_routes_example/views/user/profile.html +18 -0
- package/examples/public/README.md +92 -0
- package/examples/public/app.js +72 -0
- package/examples/public/controllers/healthController.js +20 -0
- package/examples/public/controllers/mainController.js +22 -0
- package/examples/public/controllers/userController.js +139 -0
- package/examples/public/routes.json +51 -0
- package/examples/v2/README.md +72 -0
- package/examples/v2/app.js +74 -0
- package/examples/v2/app_fixed.js +74 -0
- package/examples/v2/controllers/authController.js +64 -0
- package/examples/v2/controllers/mainController.js +24 -0
- package/examples/v2/controllers/protectedController.js +12 -0
- package/examples/v2/controllers/userController.js +16 -0
- package/examples/v2/package.json +27 -0
- package/examples/v2/routes.json +30 -0
- package/examples/v2/test_api.sh +47 -0
- package/examples/v2/tokens_example.sqlite +0 -0
- package/examples/v2.1_firewall_demo/README.md +113 -0
- package/examples/v2.1_firewall_demo/app.js +182 -0
- package/examples/v2.1_firewall_demo/package.json +27 -0
- package/examples/v2.1_hooks_demo/README.md +85 -0
- package/examples/v2.1_hooks_demo/app.js +101 -0
- package/examples/v2.1_hooks_demo/controllers/hooksController.js +29 -0
- package/examples/v2.1_hooks_demo/controllers/mainController.js +18 -0
- package/examples/v2.1_hooks_demo/package.json +27 -0
- package/examples/v2.1_hooks_demo/routes.json +16 -0
- package/examples/v2.1_openapi_demo/README.md +82 -0
- package/examples/v2.1_openapi_demo/app.js +296 -0
- package/examples/v2.1_openapi_demo/package.json +26 -0
- package/examples/v2_cors/README.md +82 -0
- package/examples/v2_cors/app.js +108 -0
- package/examples/v2_cors/package.json +23 -0
- package/examples/v2_json_auth/README.md +83 -0
- package/examples/v2_json_auth/app.js +72 -0
- package/examples/v2_json_auth/controllers/authController.js +67 -0
- package/examples/v2_json_auth/controllers/mainController.js +16 -0
- package/examples/v2_json_auth/controllers/protectedController.js +12 -0
- package/examples/v2_json_auth/controllers/tokenController.js +28 -0
- package/examples/v2_json_auth/controllers/userController.js +15 -0
- package/examples/v2_json_auth/package.json +26 -0
- package/examples/v2_json_auth/routes.json +37 -0
- package/examples/v2_json_auth/tokens.json +20 -0
- package/examples/v2_mariadb_auth/README.md +94 -0
- package/examples/v2_mariadb_auth/app.js +81 -0
- package/examples/v2_mariadb_auth/controllers/authController.js +95 -0
- package/examples/v2_mariadb_auth/controllers/mainController.js +31 -0
- package/examples/v2_mariadb_auth/controllers/protectedController.js +12 -0
- package/examples/v2_mariadb_auth/controllers/userController.js +17 -0
- package/examples/v2_mariadb_auth/package.json +27 -0
- package/examples/v2_mariadb_auth/routes.json +37 -0
- package/examples/v2_no_auth/README.md +75 -0
- package/examples/v2_no_auth/app.js +72 -0
- package/examples/v2_no_auth/controllers/healthController.js +14 -0
- package/examples/v2_no_auth/controllers/mainController.js +19 -0
- package/examples/v2_no_auth/controllers/productController.js +31 -0
- package/examples/v2_no_auth/controllers/publicController.js +16 -0
- package/examples/v2_no_auth/package.json +22 -0
- package/examples/v2_no_auth/routes.json +37 -0
- package/examples/v2_oauth/README.md +70 -0
- package/examples/v2_oauth/app.js +90 -0
- package/examples/v2_oauth/controllers/mainController.js +45 -0
- package/examples/v2_oauth/controllers/oauthController.js +247 -0
- package/examples/v2_oauth/controllers/protectedController.js +13 -0
- package/examples/v2_oauth/controllers/userController.js +17 -0
- package/examples/v2_oauth/package.json +26 -0
- package/examples/v2_oauth/routes.json +44 -0
- package/examples/v2_openapi/README.md +77 -0
- package/examples/v2_openapi/app.js +222 -0
- package/examples/v2_openapi/controllers/authController.js +52 -0
- package/examples/v2_openapi/controllers/mainController.js +26 -0
- package/examples/v2_openapi/controllers/productController.js +17 -0
- package/examples/v2_openapi/controllers/userController.js +27 -0
- package/examples/v2_openapi/package.json +26 -0
- package/examples/v2_openapi/routes.json +37 -0
- package/generate_token.js +10 -0
- package/index.js +85 -0
- package/jerk.jpg +0 -0
- package/lib/core/handler.js +86 -0
- package/lib/core/hooks.js +224 -0
- package/lib/core/router.js +204 -0
- package/lib/core/securityEnhancedServer.js +752 -0
- package/lib/core/server.js +369 -0
- package/lib/loader/controllerLoader.js +175 -0
- package/lib/loader/routeLoader.js +341 -0
- package/lib/middleware/auditLogger.js +208 -0
- package/lib/middleware/authenticator.js +565 -0
- package/lib/middleware/compressor.js +218 -0
- package/lib/middleware/cors.js +135 -0
- package/lib/middleware/firewall.js +443 -0
- package/lib/middleware/rateLimiter.js +210 -0
- package/lib/middleware/session.js +301 -0
- package/lib/middleware/validator.js +193 -0
- package/lib/mvc/controllerBase.js +207 -0
- package/lib/mvc/viewEngine.js +752 -0
- package/lib/utils/configParser.js +223 -0
- package/lib/utils/logger.js +145 -0
- package/lib/utils/mariadbTokenAdapter.js +226 -0
- package/lib/utils/openapiGenerator.js +140 -0
- package/lib/utils/sqliteTokenAdapter.js +224 -0
- package/lib/utils/tokenManager.js +254 -0
- package/package.json +47 -0
- package/v2examplle/v2_json_auth/README.md +83 -0
- package/v2examplle/v2_json_auth/app.js +72 -0
- package/v2examplle/v2_json_auth/controllers/authController.js +67 -0
- package/v2examplle/v2_json_auth/controllers/mainController.js +16 -0
- package/v2examplle/v2_json_auth/controllers/protectedController.js +12 -0
- package/v2examplle/v2_json_auth/controllers/tokenController.js +28 -0
- package/v2examplle/v2_json_auth/controllers/userController.js +15 -0
- package/v2examplle/v2_json_auth/package.json +26 -0
- package/v2examplle/v2_json_auth/routes.json +37 -0
- package/v2examplle/v2_json_auth/tokens.json +20 -0
- package/v2examplle/v2_mariadb_auth/README.md +94 -0
- package/v2examplle/v2_mariadb_auth/app.js +81 -0
- package/v2examplle/v2_mariadb_auth/controllers/authController.js +95 -0
- package/v2examplle/v2_mariadb_auth/controllers/mainController.js +31 -0
- package/v2examplle/v2_mariadb_auth/controllers/protectedController.js +12 -0
- package/v2examplle/v2_mariadb_auth/controllers/userController.js +17 -0
- package/v2examplle/v2_mariadb_auth/package.json +27 -0
- package/v2examplle/v2_mariadb_auth/routes.json +37 -0
- package/v2examplle/v2_sqlite_auth/README.md +72 -0
- package/v2examplle/v2_sqlite_auth/app.js +74 -0
- package/v2examplle/v2_sqlite_auth/app_fixed.js +74 -0
- package/v2examplle/v2_sqlite_auth/controllers/authController.js +64 -0
- package/v2examplle/v2_sqlite_auth/controllers/mainController.js +24 -0
- package/v2examplle/v2_sqlite_auth/controllers/protectedController.js +12 -0
- package/v2examplle/v2_sqlite_auth/controllers/userController.js +16 -0
- package/v2examplle/v2_sqlite_auth/package.json +27 -0
- package/v2examplle/v2_sqlite_auth/routes.json +30 -0
- package/v2examplle/v2_sqlite_auth/test_api.sh +47 -0
- package/v2examplle/v2_sqlite_auth/tokens_example.sqlite +0 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Ejemplo API con Autenticación JSON
|
|
2
|
+
|
|
3
|
+
Este ejemplo demuestra cómo crear una API con autenticación basada en tokens estáticos almacenados en un archivo JSON utilizando el Framework API SDK.
|
|
4
|
+
|
|
5
|
+
## Características
|
|
6
|
+
|
|
7
|
+
- Autenticación JWT con tokens gestionados a través de almacenamiento JSON
|
|
8
|
+
- Rutas protegidas y públicas
|
|
9
|
+
- Sistema de login para obtener tokens
|
|
10
|
+
- Controladores organizados por funcionalidad
|
|
11
|
+
- Tokens almacenados en un archivo JSON local
|
|
12
|
+
|
|
13
|
+
## Configuración
|
|
14
|
+
|
|
15
|
+
1. Asegúrate de tener instaladas las dependencias del framework API SDK
|
|
16
|
+
2. El archivo de tokens `tokens.json` se creará automáticamente al iniciar la aplicación
|
|
17
|
+
|
|
18
|
+
## Uso
|
|
19
|
+
|
|
20
|
+
1. Inicia el servidor:
|
|
21
|
+
```bash
|
|
22
|
+
node app.js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
2. El servidor escuchará en `http://localhost:8091`
|
|
26
|
+
|
|
27
|
+
## Endpoints
|
|
28
|
+
|
|
29
|
+
- `GET /` - Página de inicio (público)
|
|
30
|
+
- `POST /login` - Iniciar sesión y obtener token (público)
|
|
31
|
+
- `GET /protected` - Contenido protegido (requiere token)
|
|
32
|
+
- `GET /profile` - Perfil de usuario (requiere token)
|
|
33
|
+
- `GET /tokens` - Ver tokens almacenados (requiere token)
|
|
34
|
+
|
|
35
|
+
## Ejemplo de uso
|
|
36
|
+
|
|
37
|
+
1. Iniciar sesión:
|
|
38
|
+
```bash
|
|
39
|
+
curl -X POST http://localhost:8091/login \
|
|
40
|
+
-H "Content-Type: application/json" \
|
|
41
|
+
-d '{"username": "admin", "password": "password"}'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
2. Acceder a contenido protegido:
|
|
45
|
+
```bash
|
|
46
|
+
curl -X GET http://localhost:8091/protected \
|
|
47
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
3. Ver perfil de usuario:
|
|
51
|
+
```bash
|
|
52
|
+
curl -X GET http://localhost:8091/profile \
|
|
53
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
4. Ver tokens almacenados:
|
|
57
|
+
```bash
|
|
58
|
+
curl -X GET http://localhost:8091/tokens \
|
|
59
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Estructura del proyecto
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
examples/v2_json_auth/
|
|
66
|
+
├── app.js # Punto de entrada de la aplicación
|
|
67
|
+
├── routes.json # Definición de rutas
|
|
68
|
+
├── controllers/ # Controladores de las rutas
|
|
69
|
+
│ ├── mainController.js
|
|
70
|
+
│ ├── authController.js
|
|
71
|
+
│ ├── protectedController.js
|
|
72
|
+
│ ├── userController.js
|
|
73
|
+
│ └── tokenController.js
|
|
74
|
+
├── tokens.json # Archivo de almacenamiento de tokens (generado automáticamente)
|
|
75
|
+
└── README.md
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Seguridad
|
|
79
|
+
|
|
80
|
+
- Los tokens se gestionan a través del sistema de TokenManager del framework
|
|
81
|
+
- Los tokens expiran después de 1 hora
|
|
82
|
+
- Las rutas protegidas requieren un token válido en el header Authorization
|
|
83
|
+
- El archivo de tokens se almacena localmente en formato JSON
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const {
|
|
2
|
+
APIServer,
|
|
3
|
+
Authenticator,
|
|
4
|
+
RouteLoader,
|
|
5
|
+
Logger,
|
|
6
|
+
TokenManager
|
|
7
|
+
} = require('../../index.js');
|
|
8
|
+
|
|
9
|
+
async function startServer() {
|
|
10
|
+
// Crear instancia del servidor
|
|
11
|
+
const server = new APIServer({
|
|
12
|
+
port: 8091,
|
|
13
|
+
host: 'localhost'
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Crear instancia del logger
|
|
17
|
+
const logger = new Logger({ level: 'info' });
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
// Crear instancia del TokenManager con almacenamiento en JSON
|
|
21
|
+
const tokenManager = new TokenManager({
|
|
22
|
+
storage: 'json',
|
|
23
|
+
tokenFile: './tokens.json'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Crear instancia del autenticador
|
|
27
|
+
const authenticator = new Authenticator({ logger });
|
|
28
|
+
|
|
29
|
+
// Registrar estrategia de autenticación JWT que utiliza el TokenManager con almacenamiento JSON
|
|
30
|
+
authenticator.use('jwt-json', async (req, options = {}) => {
|
|
31
|
+
const authHeader = req.headers.authorization;
|
|
32
|
+
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
33
|
+
|
|
34
|
+
if (!token) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Validar el token usando el TokenManager
|
|
39
|
+
const secret = 'super-secret-key-for-json-example';
|
|
40
|
+
const decoded = tokenManager.validateToken(token, secret);
|
|
41
|
+
|
|
42
|
+
if (decoded) {
|
|
43
|
+
// Verificar si el token está almacenado en el archivo JSON (opcional, dependiendo del enfoque)
|
|
44
|
+
// En este caso, simplemente verificamos que el token sea válido
|
|
45
|
+
req.user = decoded;
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return false;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Agregar el autenticador al servidor para que pueda ser usado por el RouteLoader
|
|
53
|
+
server.authenticator = authenticator;
|
|
54
|
+
|
|
55
|
+
// Cargar rutas desde archivo JSON
|
|
56
|
+
const routeLoader = new RouteLoader();
|
|
57
|
+
await routeLoader.loadRoutes(server, './routes.json');
|
|
58
|
+
|
|
59
|
+
// Iniciar el servidor
|
|
60
|
+
server.start();
|
|
61
|
+
|
|
62
|
+
logger.info('Servidor iniciado en http://localhost:8091');
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.error('Error iniciando el servidor:', error.message);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Iniciar el servidor
|
|
70
|
+
startServer();
|
|
71
|
+
|
|
72
|
+
module.exports = { startServer };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const jwt = require('jsonwebtoken');
|
|
2
|
+
const { TokenManager } = require('../../../index.js');
|
|
3
|
+
|
|
4
|
+
// TokenManager para este controlador
|
|
5
|
+
const tokenManager = new TokenManager({
|
|
6
|
+
storage: 'json',
|
|
7
|
+
tokenFile: './tokens.json'
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const authController = {
|
|
11
|
+
login: async (req, res) => {
|
|
12
|
+
try {
|
|
13
|
+
const { username, password } = req.body;
|
|
14
|
+
|
|
15
|
+
// Validación simple de credenciales (esto debería ser más robusto en producción)
|
|
16
|
+
if (!username || !password) {
|
|
17
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
18
|
+
res.end(JSON.stringify({ error: 'Nombre de usuario y contraseña requeridos' }));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Simulación de autenticación (en una aplicación real, esto verificaría contra una base de datos)
|
|
23
|
+
if (username === 'admin' && password === 'password') {
|
|
24
|
+
// Generar un token JWT
|
|
25
|
+
const payload = {
|
|
26
|
+
userId: 1,
|
|
27
|
+
username: username,
|
|
28
|
+
role: 'admin'
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Secret para firmar el token (en producción, debería estar en variables de entorno)
|
|
32
|
+
const secret = 'super-secret-key-for-json-example';
|
|
33
|
+
const token = jwt.sign(payload, secret, { expiresIn: '1h' });
|
|
34
|
+
|
|
35
|
+
// En este ejemplo con almacenamiento JSON, no almacenamos el token en sí
|
|
36
|
+
// pero podríamos registrar información sobre él si es necesario
|
|
37
|
+
// Alternativamente, podríamos usar el TokenManager para generar un par de tokens
|
|
38
|
+
const tokenPair = tokenManager.generateTokenPair(
|
|
39
|
+
{ userId: 1, username: username, role: 'admin' },
|
|
40
|
+
{
|
|
41
|
+
jwtSecret: secret,
|
|
42
|
+
refreshSecret: 'refresh-' + secret,
|
|
43
|
+
accessExpiresIn: '15m',
|
|
44
|
+
refreshExpiresIn: '7d'
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Usamos el token de acceso
|
|
49
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
50
|
+
res.end(JSON.stringify({
|
|
51
|
+
message: 'Inicio de sesión exitoso',
|
|
52
|
+
token: tokenPair.accessToken,
|
|
53
|
+
user: payload
|
|
54
|
+
}));
|
|
55
|
+
} else {
|
|
56
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
57
|
+
res.end(JSON.stringify({ error: 'Credenciales inválidas' }));
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error('Error en login:', error);
|
|
61
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
62
|
+
res.end(JSON.stringify({ error: 'Error interno del servidor' }));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
module.exports = authController;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const mainController = {
|
|
2
|
+
home: (req, res) => {
|
|
3
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
4
|
+
res.end(JSON.stringify({
|
|
5
|
+
message: 'Bienvenido a la API de ejemplo con autenticación JSON',
|
|
6
|
+
endpoints: {
|
|
7
|
+
'POST /login': 'Iniciar sesión y obtener token',
|
|
8
|
+
'GET /protected': 'Contenido protegido (requiere token)',
|
|
9
|
+
'GET /profile': 'Perfil de usuario (requiere token)',
|
|
10
|
+
'GET /tokens': 'Ver tokens almacenados (requiere token)'
|
|
11
|
+
}
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
module.exports = mainController;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const protectedController = {
|
|
2
|
+
getProtectedData: (req, res) => {
|
|
3
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
4
|
+
res.end(JSON.stringify({
|
|
5
|
+
message: 'Datos protegidos accesados exitosamente',
|
|
6
|
+
user: req.user,
|
|
7
|
+
timestamp: new Date().toISOString()
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports = protectedController;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { TokenManager } = require('../../../index.js');
|
|
2
|
+
|
|
3
|
+
// TokenManager para este controlador
|
|
4
|
+
const tokenManager = new TokenManager({
|
|
5
|
+
storage: 'json',
|
|
6
|
+
tokenFile: './tokens.json'
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const tokenController = {
|
|
10
|
+
getTokens: (req, res) => {
|
|
11
|
+
try {
|
|
12
|
+
// Obtener todos los tokens almacenados
|
|
13
|
+
const tokens = tokenManager.getAllTokens ? tokenManager.getAllTokens() : 'Método no disponible en esta implementación';
|
|
14
|
+
|
|
15
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
16
|
+
res.end(JSON.stringify({
|
|
17
|
+
message: 'Tokens almacenados',
|
|
18
|
+
tokens: tokens || 'No se pudo obtener la lista de tokens'
|
|
19
|
+
}));
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error('Error obteniendo tokens:', error);
|
|
22
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
23
|
+
res.end(JSON.stringify({ error: 'Error obteniendo tokens' }));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = tokenController;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const userController = {
|
|
2
|
+
getProfile: (req, res) => {
|
|
3
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
4
|
+
res.end(JSON.stringify({
|
|
5
|
+
profile: {
|
|
6
|
+
id: req.user.userId,
|
|
7
|
+
username: req.user.username || 'Usuario',
|
|
8
|
+
role: req.user.role || 'guest'
|
|
9
|
+
},
|
|
10
|
+
message: 'Perfil de usuario obtenido exitosamente'
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = userController;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "api-sdk-json-auth-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Ejemplo de API con autenticación JSON usando el Framework API SDK",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node app.js",
|
|
8
|
+
"dev": "nodemon app.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"api",
|
|
12
|
+
"sdk",
|
|
13
|
+
"authentication",
|
|
14
|
+
"json",
|
|
15
|
+
"jwt",
|
|
16
|
+
"framework"
|
|
17
|
+
],
|
|
18
|
+
"author": "API SDK Framework",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"jsonwebtoken": "^9.0.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"nodemon": "^3.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"path": "/",
|
|
4
|
+
"method": "GET",
|
|
5
|
+
"controller": "./controllers/mainController.js",
|
|
6
|
+
"handler": "home",
|
|
7
|
+
"auth": "none"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"path": "/login",
|
|
11
|
+
"method": "POST",
|
|
12
|
+
"controller": "./controllers/authController.js",
|
|
13
|
+
"handler": "login",
|
|
14
|
+
"auth": "none"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"path": "/protected",
|
|
18
|
+
"method": "GET",
|
|
19
|
+
"controller": "./controllers/protectedController.js",
|
|
20
|
+
"handler": "getProtectedData",
|
|
21
|
+
"auth": "jwt-json"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"path": "/profile",
|
|
25
|
+
"method": "GET",
|
|
26
|
+
"controller": "./controllers/userController.js",
|
|
27
|
+
"handler": "getProfile",
|
|
28
|
+
"auth": "jwt-json"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"path": "/tokens",
|
|
32
|
+
"method": "GET",
|
|
33
|
+
"controller": "./controllers/tokenController.js",
|
|
34
|
+
"handler": "getTokens",
|
|
35
|
+
"auth": "jwt-json"
|
|
36
|
+
}
|
|
37
|
+
]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"example_token": {
|
|
3
|
+
"userId": 0,
|
|
4
|
+
"createdAt": "2026-01-14T00:00:00.000Z",
|
|
5
|
+
"expiresAt": "2026-01-15T00:00:00.000Z",
|
|
6
|
+
"revoked": false
|
|
7
|
+
},
|
|
8
|
+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiYWRtaW4iLCJ0b2tlblR5cGUiOiJyZWZyZXNoIiwiaWF0IjoxNzY4NDMzODc2LCJleHAiOjE3NjkwMzg2NzZ9.50INL6kmOd9A2mQiBRBlHaXHk31Tw7SgQMH_uRh6NVo": {
|
|
9
|
+
"userId": 1,
|
|
10
|
+
"createdAt": "2026-01-14T23:37:56.407Z",
|
|
11
|
+
"expiresAt": "2026-01-21T23:37:56.407Z",
|
|
12
|
+
"revoked": false
|
|
13
|
+
},
|
|
14
|
+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiYWRtaW4iLCJ0b2tlblR5cGUiOiJyZWZyZXNoIiwiaWF0IjoxNzY4NDM4NzU4LCJleHAiOjE3NjkwNDM1NTh9.WoMURKAizVPkMM0-lqtl1-mP0L8OMM6INeG5Bv79ixU": {
|
|
15
|
+
"userId": 1,
|
|
16
|
+
"createdAt": "2026-01-15T00:59:18.846Z",
|
|
17
|
+
"expiresAt": "2026-01-22T00:59:18.846Z",
|
|
18
|
+
"revoked": false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Ejemplo API con Autenticación MariaDB
|
|
2
|
+
|
|
3
|
+
Este ejemplo demuestra cómo crear una API con autenticación basada en tokens almacenados en MariaDB utilizando el Framework API SDK.
|
|
4
|
+
|
|
5
|
+
## Características
|
|
6
|
+
|
|
7
|
+
- Autenticación JWT con tokens almacenados en MariaDB
|
|
8
|
+
- Rutas protegidas y públicas
|
|
9
|
+
- Sistema de login/logout para obtener y revocar tokens
|
|
10
|
+
- Controladores organizados por funcionalidad
|
|
11
|
+
- Revocación de tokens en la base de datos
|
|
12
|
+
|
|
13
|
+
## Configuración
|
|
14
|
+
|
|
15
|
+
1. Asegúrate de tener instaladas las dependencias del framework API SDK
|
|
16
|
+
2. Instala mariadb si aún no está instalado: `npm install mariadb`
|
|
17
|
+
3. Asegúrate de tener MariaDB instalado y en ejecución
|
|
18
|
+
4. Crea la base de datos `token_db` en MariaDB
|
|
19
|
+
|
|
20
|
+
## Preparación de la base de datos
|
|
21
|
+
|
|
22
|
+
Antes de ejecutar la aplicación, asegúrate de tener MariaDB en ejecución y crea la base de datos:
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
CREATE DATABASE IF NOT EXISTS token_db;
|
|
26
|
+
USE token_db;
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
La tabla de tokens se creará automáticamente al iniciar la aplicación.
|
|
30
|
+
|
|
31
|
+
## Uso
|
|
32
|
+
|
|
33
|
+
1. Inicia el servidor:
|
|
34
|
+
```bash
|
|
35
|
+
node app.js
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
2. El servidor escuchará en `http://localhost:8090`
|
|
39
|
+
|
|
40
|
+
## Endpoints
|
|
41
|
+
|
|
42
|
+
- `GET /` - Página de inicio (público)
|
|
43
|
+
- `POST /login` - Iniciar sesión y obtener token (público)
|
|
44
|
+
- `GET /protected` - Contenido protegido (requiere token)
|
|
45
|
+
- `GET /profile` - Perfil de usuario (requiere token)
|
|
46
|
+
- `POST /logout` - Cerrar sesión y revocar token (requiere token)
|
|
47
|
+
|
|
48
|
+
## Ejemplo de uso
|
|
49
|
+
|
|
50
|
+
1. Iniciar sesión:
|
|
51
|
+
```bash
|
|
52
|
+
curl -X POST http://localhost:8090/login \
|
|
53
|
+
-H "Content-Type: application/json" \
|
|
54
|
+
-d '{"username": "admin", "password": "password"}'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
2. Acceder a contenido protegido:
|
|
58
|
+
```bash
|
|
59
|
+
curl -X GET http://localhost:8090/protected \
|
|
60
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
3. Ver perfil de usuario:
|
|
64
|
+
```bash
|
|
65
|
+
curl -X GET http://localhost:8090/profile \
|
|
66
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
4. Cerrar sesión y revocar token:
|
|
70
|
+
```bash
|
|
71
|
+
curl -X POST http://localhost:8090/logout \
|
|
72
|
+
-H "Authorization: Bearer TU_TOKEN_AQUI"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Estructura del proyecto
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
examples/v2_mariadb_auth/
|
|
79
|
+
├── app.js # Punto de entrada de la aplicación
|
|
80
|
+
├── routes.json # Definición de rutas
|
|
81
|
+
├── controllers/ # Controladores de las rutas
|
|
82
|
+
│ ├── mainController.js
|
|
83
|
+
│ ├── authController.js
|
|
84
|
+
│ ├── protectedController.js
|
|
85
|
+
│ └── userController.js
|
|
86
|
+
└── README.md
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Seguridad
|
|
90
|
+
|
|
91
|
+
- Los tokens se almacenan en una base de datos MariaDB
|
|
92
|
+
- Los tokens expiran después de 1 hora
|
|
93
|
+
- Las rutas protegidas requieren un token válido en el header Authorization
|
|
94
|
+
- Los tokens pueden ser revocados mediante el endpoint de logout
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const {
|
|
2
|
+
APIServer,
|
|
3
|
+
Authenticator,
|
|
4
|
+
RouteLoader,
|
|
5
|
+
Logger,
|
|
6
|
+
MariaDBTokenAdapter
|
|
7
|
+
} = require('../../index.js');
|
|
8
|
+
|
|
9
|
+
async function startServer() {
|
|
10
|
+
// Crear instancia del servidor
|
|
11
|
+
const server = new APIServer({
|
|
12
|
+
port: 8090,
|
|
13
|
+
host: 'localhost'
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Crear instancia del logger
|
|
17
|
+
const logger = new Logger({ level: 'info' });
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
// Crear instancia del adaptador de tokens MariaDB
|
|
21
|
+
const tokenAdapter = new MariaDBTokenAdapter({
|
|
22
|
+
host: 'localhost',
|
|
23
|
+
user: 'root',
|
|
24
|
+
password: '', // Dejar vacío si no hay contraseña para root
|
|
25
|
+
database: 'token_db',
|
|
26
|
+
tableName: 'mariadb_tokens'
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Inicializar el adaptador de tokens
|
|
30
|
+
await tokenAdapter.initialize();
|
|
31
|
+
logger.info('MariaDB Token Adapter inicializado correctamente');
|
|
32
|
+
|
|
33
|
+
// Crear instancia del autenticador
|
|
34
|
+
const authenticator = new Authenticator({ logger });
|
|
35
|
+
|
|
36
|
+
// Registrar estrategia de autenticación JWT que utiliza el adaptador MariaDB
|
|
37
|
+
authenticator.use('jwt-mariadb', async (req, options = {}) => {
|
|
38
|
+
const authHeader = req.headers.authorization;
|
|
39
|
+
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
40
|
+
|
|
41
|
+
if (!token) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Validar el token contra la base de datos MariaDB
|
|
46
|
+
const tokenRecord = await tokenAdapter.validateToken(token);
|
|
47
|
+
|
|
48
|
+
if (tokenRecord) {
|
|
49
|
+
// Agregar información del usuario a la solicitud
|
|
50
|
+
req.user = {
|
|
51
|
+
userId: tokenRecord.user_id,
|
|
52
|
+
tokenType: tokenRecord.token_type,
|
|
53
|
+
expiresAt: tokenRecord.expires_at
|
|
54
|
+
};
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Agregar el autenticador al servidor para que pueda ser usado por el RouteLoader
|
|
62
|
+
server.authenticator = authenticator;
|
|
63
|
+
|
|
64
|
+
// Cargar rutas desde archivo JSON
|
|
65
|
+
const routeLoader = new RouteLoader();
|
|
66
|
+
await routeLoader.loadRoutes(server, './routes.json');
|
|
67
|
+
|
|
68
|
+
// Iniciar el servidor
|
|
69
|
+
server.start();
|
|
70
|
+
|
|
71
|
+
logger.info('Servidor iniciado en http://localhost:8090');
|
|
72
|
+
} catch (error) {
|
|
73
|
+
logger.error('Error iniciando el servidor:', error.message);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Iniciar el servidor
|
|
79
|
+
startServer();
|
|
80
|
+
|
|
81
|
+
module.exports = { startServer };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const jwt = require('jsonwebtoken');
|
|
2
|
+
const { MariaDBTokenAdapter } = require('../../../index.js');
|
|
3
|
+
|
|
4
|
+
// Adaptador de tokens para este controlador
|
|
5
|
+
const tokenAdapter = new MariaDBTokenAdapter({
|
|
6
|
+
host: 'localhost',
|
|
7
|
+
user: 'root',
|
|
8
|
+
password: '',
|
|
9
|
+
database: 'token_db',
|
|
10
|
+
tableName: 'mariadb_tokens'
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Asegurarse de que el adaptador esté inicializado
|
|
14
|
+
tokenAdapter.initialize().catch(console.error);
|
|
15
|
+
|
|
16
|
+
const authController = {
|
|
17
|
+
login: async (req, res) => {
|
|
18
|
+
try {
|
|
19
|
+
const { username, password } = req.body;
|
|
20
|
+
|
|
21
|
+
// Validación simple de credenciales (esto debería ser más robusto en producción)
|
|
22
|
+
if (!username || !password) {
|
|
23
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
24
|
+
res.end(JSON.stringify({ error: 'Nombre de usuario y contraseña requeridos' }));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Simulación de autenticación (en una aplicación real, esto verificaría contra una base de datos)
|
|
29
|
+
if (username === 'admin' && password === 'password') {
|
|
30
|
+
// Generar un token JWT
|
|
31
|
+
const payload = {
|
|
32
|
+
userId: 1,
|
|
33
|
+
username: username,
|
|
34
|
+
role: 'admin'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Secret para firmar el token (en producción, debería estar en variables de entorno)
|
|
38
|
+
const secret = 'super-secret-key-for-mariadb-example';
|
|
39
|
+
const token = jwt.sign(payload, secret, { expiresIn: '1h' });
|
|
40
|
+
|
|
41
|
+
// Guardar el token en la base de datos MariaDB
|
|
42
|
+
await tokenAdapter.saveToken(
|
|
43
|
+
token,
|
|
44
|
+
{ userId: 1 },
|
|
45
|
+
'access',
|
|
46
|
+
new Date(Date.now() + 60 * 60 * 1000) // 1 hora
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
50
|
+
res.end(JSON.stringify({
|
|
51
|
+
message: 'Inicio de sesión exitoso',
|
|
52
|
+
token: token,
|
|
53
|
+
user: payload
|
|
54
|
+
}));
|
|
55
|
+
} else {
|
|
56
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
57
|
+
res.end(JSON.stringify({ error: 'Credenciales inválidas' }));
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error('Error en login:', error);
|
|
61
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
62
|
+
res.end(JSON.stringify({ error: 'Error interno del servidor' }));
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
logout: async (req, res) => {
|
|
67
|
+
try {
|
|
68
|
+
const authHeader = req.headers.authorization;
|
|
69
|
+
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
|
|
70
|
+
|
|
71
|
+
if (!token) {
|
|
72
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
73
|
+
res.end(JSON.stringify({ error: 'Token no proporcionado' }));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Revocar el token en la base de datos MariaDB
|
|
78
|
+
const revoked = await tokenAdapter.revokeToken(token);
|
|
79
|
+
|
|
80
|
+
if (revoked) {
|
|
81
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
82
|
+
res.end(JSON.stringify({ message: 'Sesión cerrada exitosamente' }));
|
|
83
|
+
} else {
|
|
84
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
85
|
+
res.end(JSON.stringify({ error: 'No se pudo revocar el token' }));
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error('Error en logout:', error);
|
|
89
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
90
|
+
res.end(JSON.stringify({ error: 'Error interno del servidor' }));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
module.exports = authController;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const jwt = require('jsonwebtoken');
|
|
2
|
+
const { MariaDBTokenAdapter } = require('../../../index.js');
|
|
3
|
+
|
|
4
|
+
// Adaptador de tokens para este controlador
|
|
5
|
+
const tokenAdapter = new MariaDBTokenAdapter({
|
|
6
|
+
host: 'localhost',
|
|
7
|
+
user: 'root',
|
|
8
|
+
password: '',
|
|
9
|
+
database: 'token_db',
|
|
10
|
+
tableName: 'mariadb_tokens'
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Asegurarse de que el adaptador esté inicializado
|
|
14
|
+
tokenAdapter.initialize().catch(console.error);
|
|
15
|
+
|
|
16
|
+
const mainController = {
|
|
17
|
+
home: (req, res) => {
|
|
18
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
19
|
+
res.end(JSON.stringify({
|
|
20
|
+
message: 'Bienvenido a la API de ejemplo con autenticación MariaDB',
|
|
21
|
+
endpoints: {
|
|
22
|
+
'POST /login': 'Iniciar sesión y obtener token',
|
|
23
|
+
'GET /protected': 'Contenido protegido (requiere token)',
|
|
24
|
+
'GET /profile': 'Perfil de usuario (requiere token)',
|
|
25
|
+
'POST /logout': 'Cerrar sesión y revocar token (requiere token)'
|
|
26
|
+
}
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = mainController;
|