From 06a02436cca61b3390278cefa32e0d812dce0da0 Mon Sep 17 00:00:00 2001 From: Pablinux Date: Mon, 23 Oct 2023 00:47:45 -0500 Subject: [PATCH] integracion auth x token --- src/controladores/controlador_General.js | 101 +++++++++++------------ src/rutas/rt_Generales.js | 8 +- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/controladores/controlador_General.js b/src/controladores/controlador_General.js index 582a54f..a03e7ea 100644 --- a/src/controladores/controlador_General.js +++ b/src/controladores/controlador_General.js @@ -30,7 +30,7 @@ controlador.app_login = (req, res) => { controlador.auth = (req, res) => { console.log(req.body); var pwd = stringTo_md5(req.body.password); - req.getConnection((error, conn,next) => { + req.getConnection((error, conn, next) => { conn.query(`SELECT * FROM usuarios WHERE n_sesion = ? and clave=?`, [req.body.usuario, pwd], (err, rows) => { if (err) { //res.json(err); @@ -66,50 +66,32 @@ controlador.login_test = (req, res) => { }; -async function keyGen(req,id) { - //var id = sql.consulta_valorCampoString("empresa_datos", "RUC", "idEmpresa", "1"); - await req.getConnection(async(error, conn,next) => { - await conn.query(`SELECT RUC FROM empresa_datos WHERE idEmpresa = ? `, [id], (err, rows) => { - if (err) { - //res.json(err); - console.log("error en la consulta: ",err); - } else { - try { - if (rows.length > 0) { - console.log(rows[0].RUC); - return base64encode(rows[0].RUC); - } - } catch (ex) { - console.log(ex); - } - } - }); - //conn.end(); - }); -} //************* GENERA TOKENS ****************// -controlador.auth_keygen = (req, res) => { - const datUsr={id,n_sesion,inf,dvc,app,usr, pwd, mail} = base64decode(req.body.data); - console.log(datUsr); - var pasw = "7c0a2d79657d70089926fe01aebf4d6f";//stringTo_md5(datUsr.pwd); - req.getConnection((error, conn,next) => { - conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.n_sesion = ? and u.clave = ? `, [1,usr,pasw], (err, rows) => { - //conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.cod_usr = 'XU0001' `, [1,id], (err, rows) => { - if (err) { - //res.json(err); - console.log("error en la consulta: ",err); +controlador.auth_keygen = async (req, res) => { + var datUsr = base64decode(req.body.data); + const jsonDat = JSON.parse(datUsr.replaceAll("'", "\"")); // Convertir a JSON + console.log("Json Obtenido: ", jsonDat); + jsonDat.pwd = stringTo_md5(jsonDat.pwd); + //const consulta = `select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = 1 and u.n_sesion = ${json.usr} and u.clave = ${json.pwd}`; + //console.log(consulta); + await req.getConnection(async (error, conn, next) => { + await conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.n_sesion = ? and u.clave = ? `, [1, jsonDat.usr, jsonDat.pwd], (errSql, rows) => { + if (errSql) { + console.log("error en la consulta: ", errSql); } else { try { if (rows.length > 0) { + delete jsonDat.pwd; console.log(rows[0].RUC); var key = base64encode(rows[0].RUC); var nom = rows[0].nombre; var ape = rows[0].apellido; - const token = jwt.sign(datUsr, config.secret.key); - var auth = {auth:[{"apellido":ape,"nombre":nom,"key":key,token:token}]}; + const token = jwt.sign(jsonDat, config.secret.key); + var auth = { auth: [{ "apellido": ape, "nombre": nom, "key": key, token: token }] }; + //console.log(auth); res.json(auth); - }else{ - res.json({auth:false, message:'Unauthorized'}); + } else { + res.json({ auth: false, message: 'Unauthorized' }); } } catch (ex) { console.log(ex); @@ -117,28 +99,43 @@ controlador.auth_keygen = (req, res) => { } }); }); - }; +//************* valida TOKENS ****************// +controlador.auth_token = async (req, res) => { + console.log(req.body.data); + var token = req.body.data; + if (!token) { + return res.status(401).json({ auth: false, message: 'No token provided' }); + } + try{ + var decoded = jwt.verify(token, config.secret.key); + res.json(decoded); + }catch(err){ + res.json({ auth: false, message: 'Unauthorized: '+err.message }); + } +}; + +//************* INIT APP ****************// controlador.init_app = (req, res) => { console.log(req.body); //const datUsr={usr, pwd, mail} = req.body; //const token = jwt.sign(datUsr, config.secret.key); //res.render(token); - res.json({token:token}); + res.json({ token: token }); }; //************* LOGIN APP ****************// controlador.login_appTK = (req, res) => { const token = req.headers['x-access-token']; - console.log("token: ",token); - if(!token){ - return res.status(401).json({auth:false, message:'No token provided'}); + console.log("token: ", token); + if (!token) { + return res.status(401).json({ auth: false, message: 'No token provided' }); + } + jwt.verify(token, config.secret, (err, decoded) => { + if (err) { + return res.status(401).json({ auth: false, message: 'Unauthorized' }); } - jwt.verify(token, config.secret, (err,decoded) => { - if(err){ - return res.status(401).json({auth:false, message:'Unauthorized'}); - } - res.json({auth:true, message:'Authorized'}); - }); + res.json({ auth: true, message: 'Authorized' }); + }); }; //************* LOGIN APP ****************// @@ -215,12 +212,12 @@ controlador.recibe_pedidos = async (req, res) => { PedUsoPrdct_iva: json.iva, PedUsoPrdct_origen: json.origen }; - ingreso_pedido(req,pedido,json.items); + ingreso_pedido(req, pedido, json.items); //await console.log(await ingreso_pedido(req,pedido)); //await ingreso_detalle(req, id_ped, json.items); }; -async function ingreso_pedido(req,pedido_data,items) { +async function ingreso_pedido(req, pedido_data, items) { var lastInsert = ""; req.getConnection((err, conn) => { conn.query('INSERT INTO PedidoUsoProduct set ?', [pedido_data], (err, result) => { @@ -230,7 +227,7 @@ async function ingreso_pedido(req,pedido_data,items) { return 0; } else { lastInsert = result.insertId; - console.log("existe id = "+lastInsert); + console.log("existe id = " + lastInsert); ingreso_detalle(req, lastInsert, items) //res.json({ sision: "ORDEN INGRESADA" }); //res.redirect('/clientes');//redireciona a la ruta inical de la app @@ -242,14 +239,14 @@ async function ingreso_detalle(req, id_ped, data) { //console.log("ID Pedido: "+id_ped); for (var i = 0; i < data.length; i++) { var items = { - PedUsoPrdct_id:id_ped, + PedUsoPrdct_id: id_ped, //PedUsoPrdct_id: 36, PedUsoPrdctDet_codigoProducto: data[i].cod, PedUsoPrdct_cant: data[i].cant, PedUsoPrdct_costo: data[i].precio, PedUsoPrdct_desct: data[i].descuento, //PedUsoPrdct_iva:data[i].iva, - PedUsoPrdct_iva:0, + PedUsoPrdct_iva: 0, PedUsoPrdct_gpPrecios: data[i].gp_precio } //console.log(items); diff --git a/src/rutas/rt_Generales.js b/src/rutas/rt_Generales.js index ad896ef..acc5f30 100644 --- a/src/rutas/rt_Generales.js +++ b/src/rutas/rt_Generales.js @@ -17,12 +17,14 @@ rutas.post('/login', controlador_init.auth);//Authenticacion de Web APP rutas.get('/login_test', controlador_init.login_test);//login testing css / dev rutas.get('/init-app', controlador_init.init_app);//init app / dev -rutas.post('/auth-keygen', controlador_init.auth_keygen);//genera TOKENS / dev rutas.post('/login_app', controlador_init.login_appTK);//login APP / dev +rutas.post('/auth-keygen', controlador_init.auth_keygen);//genera TOKENS / dev +rutas.post('/auth-token', controlador_init.auth_token);//Valida TOKENS / dev + rutas.post('/operaciones/', controlador_init.recibe_datos);//testing json reccepcion server -rutas.get('/app-tv/', controlador_init.app_tv);//app de tv y video json reccepcion server -rutas.get('/tv-online/', controlador_init.app_tv);//app de tv y video json reccepcion server +rutas.get('/app-tv/', controlador_init.app_tv);//app de tv y video json +rutas.get('/tv-online/', controlador_init.app_tv);//app de tv y video json module.exports = rutas;