ZoeApi/helpers/CheckToken.js

118 lines
2.4 KiB
JavaScript

require("dotenv-safe").load();
var jwt = require('jsonwebtoken');
//#region Encript
function LTrim(x) {
return x.replace(/^\s+/, "");
}
global.encriptPassword = function encriptPassword(password, amount = 10) {
return Cript(RPad(password, amount), " ");
}
function RPad(St, Tm) {
text = (LTrim(St) + " ").substr(0, Tm);
return text;
}
function Cript(St, Pw) {
x = "";
p = 0;
for (i = 0; i < St.length; i++) {
if (p > Pw.length) {
p = 1;
}
j = ord(Pw.substr(p, 1)) + 128;
n = ord(St.substr(i, 1));
x = x + Again(n, j);
p = 0;
}
return x;
}
function Again(n, j) {
exitloop = false;
while (exitloop == false) {
n = n ^ j;
if (n < 31) {
n = 128 + n;
} else if ((n > 127) && (n < 159)) {
n = n - 128;
} else {
exitloop = true;
}
}
return String.fromCharCode(n);
}
function ord(string) {
var str = string + ''
var code = str.charCodeAt(0)
if (code >= 0xD800 && code <= 0xDBFF) {
var hi = code
if (str.length === 1) {
return code
}
var low = str.charCodeAt(1)
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000
}
if (code >= 0xDC00 && code <= 0xDFFF) {
return code
}
return code
}
//#endregion
global.verifyJWT = function verifyJWT(req, res, next) {
//console.log(req.headers.token);
var token = decodeURI(req.headers.token);
if(!token)
token = req.params.tk || req.body.token;
//console.log(token);
//token = encriptPassword(token, token.length);
//console.log(token);
if (!token) return res.status(401).send({ auth: false, message: 'Não foi enviado token' });
jwt.verify(token, process.env.SECRET, function (err, decoded) {
if (err) {
//console.log(err);
res.status(200).send({ status: 'expired' });
}
else {
req.idusers = decoded.id;
next();
}
});
}
global.fixApostrofe = function fixApostrofe(content) {
for (let i = 0; i < 20; i++) {
content = content.replace("'", '&apos;');
}
return content;
}
global.fixAspas = function fixAspas(content) {
for (let i = 0; i < 20; i++) {
content = content.replace('"', '&quot;');
}
return content;
}
global.replaceAll = function ReplaceAll(Texto, Palavras, TrocarPor, Vezes) {
for (let i = 0; i < Vezes; i++) {
for (let o = 0; o < Palavras.length; o++) {
Texto = Texto.replace(Palavras[o], TrocarPor[o]);
}
}
return Texto;
}