Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
node js backend security header

im trying to make a login endpoint/api, and i am sending the user login data through the authentication headers, is that okay for the security? the authorization headers is actually contains an object, it is encoded to a Base64, it contains user data like hashed password (not hashed yet in this code), username, and some sort of a serverkey (to authorize if it is the right client that sending an api request), just wanna make sure if it is secure or not..

const aFunction = (req, res) => {
        require('crypto').randomBytes(48, function(err, buffer) {
            const token = buffer.toString('hex');
            const auth = JSON.parse(Buffer.from(req.headers.authorization, 'base64').toString('ascii')) 
        
            if(auth.serverkey == version["serverkey"]){
                loginUser(auth.username,token).then(data =>{
                
                    if (data.rows.length < 1 || data.rows[0].password != auth.password) {
                        res.send({
                            status: "failed", 
                            message: "Username/Password is wrong", 
                            status_code: 400
                        })
                    }else{
                        res.send({
                            status: "success", 
                            message: "successfully logged in", 
                            status_code: 200, token: token
                        })

                    }

                })
            }else{
                res.send({
                    status: "failed", 
                    message: "Unauthorized Client", 
                    status_code: 401
                })
            }
        });
        
            

        
    }

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would add a check for if (req.secure) to your code, and reject any non-https request coming your way. (Don't do this if your nodejs program is behind a reverse proxy.)

Users with browsers can see everything you send back and forth to your server, whether via https or http. (devtools) So, if by reading your headers they can figure out how to send you malicious headers, then somebody will. And base64 encoding has exactly the same security as no encoding at all. So salt and hash your passwords. Use the bcrypt package or jwts.

I would handle errors this way, by calling next() with an error parameter rather than by using .send() to deliver a failure message. Use this sort of code.

const createError = require('http-errors')
 ...
const myfunction (req, res, next) {
  ...
  if (!req.secure) return next(createError(403, 'https required'))
  ...
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda