|
| 1 | +const path = require("path"); |
| 2 | +const { |
| 3 | + fopen, |
| 4 | + fwrite, |
| 5 | + freadBin, |
| 6 | + fwriteBin, |
| 7 | +} = require("./autoFileSysModule.js"); |
| 8 | +const routesDir = __dirname; |
| 9 | +const files2 = __dirname + "/src/"; |
| 10 | +const path_pages = files2 + "pages/"; |
| 11 | +const forbiddenFilePath = path.join(path_pages, "forbidden.html"); |
| 12 | + |
| 13 | +// Função para ordenar bases por usuario |
| 14 | +function ordenarUsuario(file) { |
| 15 | + const data = freadBin(file); |
| 16 | + |
| 17 | + // Ordena o array de usuarios com base no usuario, do maior para o menor |
| 18 | + data.sort((a, b) => b.usuario - a.usuario); |
| 19 | + |
| 20 | + // Salva o array ordenado de volta no arquivo |
| 21 | + fwriteBin(file, data); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +function pesqUsuarioByEmail(file,email) { |
| 26 | + const data = freadBin(file); |
| 27 | + let pos = 0; |
| 28 | + |
| 29 | + for (pos = 0; pos < data.length; pos++) { |
| 30 | + var currentDB = data[pos]; |
| 31 | + const currentEmail = currentDB.email; |
| 32 | + |
| 33 | + // Verifica se e o email |
| 34 | + const authEmail = currentEmail == email; |
| 35 | + |
| 36 | + console.log("-----SISTEMA----"); |
| 37 | + console.log("TAMANHO: " + data.length); |
| 38 | + console.log("POSIÇÃO: " + pos); |
| 39 | + console.log("Pesquisando..."); |
| 40 | + console.log("e-mail : " + email + " == " + currentEmail); |
| 41 | + |
| 42 | + // Verifica se o nome,usuário e email são verdadeiros |
| 43 | + if (authEmail) { |
| 44 | + return pos; |
| 45 | + } |
| 46 | + } |
| 47 | + return -1; // Retorna -1 se não foram encontrados usuarios no vetor |
| 48 | +} |
| 49 | + |
| 50 | +function pesqUsuario(file,username) { |
| 51 | + const data = freadBin(file); |
| 52 | + let pos = 0; |
| 53 | + |
| 54 | + for (pos = 0; pos < data.length; pos++) { |
| 55 | + var currentDB = data[pos]; |
| 56 | + const currentUser = currentDB.usuario; |
| 57 | + |
| 58 | + // Verifica se e o usuario |
| 59 | + const authNome = currentUser == username; |
| 60 | + |
| 61 | + console.log("-----SISTEMA----"); |
| 62 | + console.log("TAMANHO: " + data.length); |
| 63 | + console.log("POSIÇÃO: " + pos); |
| 64 | + console.log("Pesquisando..."); |
| 65 | + console.log("User: " + username + " == " + currentUser); |
| 66 | + |
| 67 | + // Verifica se o nome,usuário e email são verdadeiros |
| 68 | + if (authNome) { |
| 69 | + return pos; |
| 70 | + } |
| 71 | + } |
| 72 | + return -1; // Retorna -1 se não foram encontrados usuarios no vetor |
| 73 | +} |
| 74 | + |
| 75 | +function getRandomInt(max) { |
| 76 | + return Math.floor(Math.random() * max); |
| 77 | +} |
| 78 | + |
| 79 | +function getRandomBin(max) { |
| 80 | + return Math.floor(Math.random() * max).toString(2); |
| 81 | +} |
| 82 | + |
| 83 | +function getRandomHex(max) { |
| 84 | + return Math.floor(Math.random() * max).toString(16); |
| 85 | +} |
| 86 | + |
| 87 | +function generateToken() { |
| 88 | + let token = ''; |
| 89 | + const maxLength = 32; // Precisamos de exatamente 32 caracteres |
| 90 | + |
| 91 | + while (token.length < maxLength) { |
| 92 | + let hex = getRandomHex(256); // Gera um valor hexadecimal |
| 93 | + token += hex; // Adiciona o valor ao token |
| 94 | + } |
| 95 | + |
| 96 | + return token.substring(0, maxLength); // Garante que o token tenha exatamente 32 caracteres |
| 97 | +} |
| 98 | + |
| 99 | +function formatDate(dateString) { |
| 100 | + const options = { |
| 101 | + year: 'numeric', |
| 102 | + month: '2-digit', |
| 103 | + day: '2-digit', |
| 104 | + hour: '2-digit', |
| 105 | + minute: '2-digit', |
| 106 | + second: '2-digit', |
| 107 | + hour12: false |
| 108 | + }; |
| 109 | + const date = new Date(dateString); |
| 110 | + return date.toLocaleString('pt-BR', options); |
| 111 | +} |
| 112 | + |
| 113 | +function validadeApiKey(req,res,key){ |
| 114 | + const keyHeader = req.headers["authorization"]; |
| 115 | + const authApi = keyHeader == key; |
| 116 | + |
| 117 | + if(!authApi){ |
| 118 | + forbidden(res); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function conversorSimEnao(value) { |
| 123 | + if (value) { |
| 124 | + return "✔Voce foi autorizado, esta tudo correto"; |
| 125 | + } |
| 126 | + return "⚠Esta faltando algo ou não foi autorizado!"; |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +function forbidden(res) { |
| 131 | + console.error(403); |
| 132 | + res.status(403); |
| 133 | + res.sendFile(forbiddenFilePath); |
| 134 | +} |
| 135 | + |
| 136 | +function unauthorized(res) { |
| 137 | + console.error(401); |
| 138 | + res.status(401); |
| 139 | + res.sendStatus(401); |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = { |
| 143 | + getRandomInt, |
| 144 | + getRandomBin, |
| 145 | + getRandomHex, |
| 146 | + generateToken, |
| 147 | + ordenarUsuario, |
| 148 | + pesqUsuario, |
| 149 | + validadeApiKey, |
| 150 | + unauthorized, |
| 151 | + forbidden, |
| 152 | + formatDate, |
| 153 | + conversorSimEnao, |
| 154 | +}; |
0 commit comments