Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda