Commit eb78e092 authored by Adrian's avatar Adrian
Browse files

Merge branch 'master' of gitlab.agetic.gob.bo:dbarra/sitio-firebase

parents 0a1d0923 31769de5
'use strict';
const config = {
autorizacion: 'AAAAPEVgJjw:APA91bFN3ZmibnDgcs6YOyf8CgwxF0ZPkUlZvncHkuFsIozznmYAcvKRnR7rFyDIFpYdntMZzbzWj4QYtOXGjXdxmRt0R5jFfw15LCXASK9fJCqdoItWb4dcdiDRGifQUOeePDdyVq_v',
SECRET_TOKEN: 'AGETIC_SUPER_SECRET_',
autorizacion: 'AAAAi0s6FBw:APA91bGF_pnVmZRW0l7X7pgLvPcmbRL14HdoqzcklDxu6aO7z7ui7_JKofMf_U4LPO0xvkvVPleN8QIdVjIQ1WMvSLwV0I1R28QWzNaKdRAD7MBd31h2W9z_bf-p_AJg7I_22P67gIzy',
usuarios: [
{
usuario: 'admin',
......
const express = require('express');
const config = require('./config.js');
const jwt = require('jsonwebtoken');
const axios = require('axios');
const fs = require('fs')
const morgan = require('morgan')
const path = require('path')
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
......@@ -9,6 +13,18 @@ const corsOptions = {
origin: '*' // PONER UNA URL
}
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'logs.log'), { flags: 'a' })
app.use(morgan(function (tokens, req, res) {
return [
`URL: ${req.originalUrl}`,
` - `,
`DATE: ${new Date()}`,
` - `,
`USER: ${req.usuario}`
].join(' ')
}, { stream: accessLogStream }))
app.use(bodyParser.json({ limit: '10mb' }));
app.use(cors(corsOptions));
......@@ -28,36 +44,57 @@ async function mensajeError (res, mensajeError) {
})
}
async function AuthorizationMiddleware (req, res, next) {
try {
const { authorization } = req.headers;
if (!authorization) {
throw new Error('No autorizado.');
}
const token = authorization.split('Bearer ');
if (!token[1]) {
throw new Error('No se envio el token de autorizacion.');
}
const decoded = await jwt.verify(token[1], config.SECRET_TOKEN);
req.usuario = decoded.usuario
next();
} catch (error) {
mensajeError(res, error.message);
}
}
app.post('/login', async function (req, res) {
try {
console.log('___________________________________________________________________________________________________');
console.log(req.body);
console.log('¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯');
const { usuario, password } = req.body
let existe = false;
let respuesta = {
usuario,
token: null
}
for (const usuarioConfig of config.usuarios) {
if (usuarioConfig.usuario === usuario && usuarioConfig.password === password ) {
existe = true;
respuesta.token = jwt.sign({ usuario }, config.SECRET_TOKEN, { expiresIn: 60 * 60 });
}
}
if (!existe) {
throw new Error('El usuario no existe');
}
mensajeExito(res, { usuario, login: existe });
mensajeExito(res, respuesta);
} catch (error) {
mensajeError(res, error.message);
}
});
app.post ('/send', async function (req, res) {
app.post ('/send', AuthorizationMiddleware, async function (req, res) {
try {
const { titulo, text} = req.body;
const init = {
method: 'POST',
url: 'https://fcm.googleapis.com/fcm/send',
headers : {
Authorization: `key=${config.autorizacion}`,
'content-type': 'application/json'
"Authorization":`key=${config.autorizacion}`,
'Content-Type': 'application/json',
},
data: {
notification: {
......@@ -69,10 +106,12 @@ app.post ('/send', async function (req, res) {
data: {
foo: 'bar'
},
priority: 'High'
priority: 'High',
to: ''
}
}
const repsuesta = await axios(init);
// const repsuesta = {};
mensajeExito(res, repsuesta.data);
} catch (error) {
if (error.response) {
......
......@@ -62,6 +62,14 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"requires": {
"safe-buffer": "5.1.2"
}
},
"body-parser": {
"version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
......@@ -114,6 +122,11 @@
"concat-map": "0.0.1"
}
},
"buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
},
"bytes": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
......@@ -308,6 +321,14 @@
"integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=",
"dev": true
},
"ecdsa-sig-formatter": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
"requires": {
"safe-buffer": "^5.0.1"
}
},
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
......@@ -616,6 +637,49 @@
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
"dev": true
},
"jsonwebtoken": {
"version": "8.5.1",
"resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
"integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
"requires": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^5.6.0"
},
"dependencies": {
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"jwa": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
"requires": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"jws": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
"requires": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"latest-version": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/latest-version/-/latest-version-3.1.0.tgz",
......@@ -625,6 +689,41 @@
"package-json": "^4.0.0"
}
},
"lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
},
"lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
},
"lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
},
"lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
},
"lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
},
"lowercase-keys": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
......@@ -694,6 +793,28 @@
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"morgan": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
"integrity": "sha512-HQStPIV4y3afTiCYVxirakhlCfGkI161c76kKFca7Fk1JusM//Qeo1ej2XaMniiNeaZklMVrh3vTtIzpzwbpmA==",
"requires": {
"basic-auth": "~2.0.0",
"debug": "2.6.9",
"depd": "~1.1.2",
"on-finished": "~2.3.0",
"on-headers": "~1.0.1"
},
"dependencies": {
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
......@@ -884,6 +1005,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
},
"p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
......@@ -1022,8 +1148,7 @@
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
},
"semver-diff": {
"version": "2.1.0",
......
......@@ -9,10 +9,12 @@
},
"dependencies": {
"axios": "^0.19.0",
"cors": "^2.8.5",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"moment": "^2.24.0"
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"morgan": "^1.9.1"
},
"devDependencies": {
"nodemon": "^2.0.1"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment