Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adicionando tempo de expiração do token e melhorando tratamento de segurança #96

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ const jwt = require('jsonwebtoken');
const config = require('config');

module.exports = (req, res, next) => {
const token = req.header('Authorization');
if (!token) {
const authHeader = req.header('Authorization');
if (!authHeader) {
const err = new Error('Access denied. No token provided.');
err.status = 401;
return next(err);
}

const parts = authHeader.split(' ');

if (!parts.length === 2) {
return res.status(401).send({ error: 'Token error' });
}

const [scheme, token] = parts;

if (!/^Bearer$/i.test(scheme)) {
return res.status(401).send({ error: 'Token Malformatted' });
}

try {
const decoded = jwt.verify(token, config.get('jwtPrivateKey'));
req.user = decoded;
Expand Down
2 changes: 1 addition & 1 deletion modules/tokenGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken');
const config = require('config');

function sendToken (req) {
const token = jwt.sign({ username: req.body.username }, config.get('jwtPrivateKey'), { expiresIn: '7 days' });
const token = jwt.sign({ username: req.body.username }, config.get('jwtPrivateKey'), { expiresIn: 604800 });
amintasvrp marked this conversation as resolved.
Show resolved Hide resolved
return token;
}

Expand Down