Signing jwt token with refresh token as payload
How I understand jwt based authentication should work:
401 Unauthorized . How I want to implement it:
If the desciption above is correct, is there anything wrong with putting the refresh token as payload for the signed jwt token?:
function signToken(id, role, refresh_token) {
return jwt.sign(
{
_id: id,
role: role,
refresh_token: refresh_token
},
config.jwt.secret,
{
expiresIn: 60*60*24, // expires in 1 day
}
);
}
And in that way always sending both the refresh token and jwt token in every request? By doing this, the client would not have to send a seperate request with the refresh token to get a new jwt token:
Auth middleware would look something like this:
function authenticate (req, res, next) {
// Token attached to request in previous middleware
let token = req.token
// verifies secret and checks exp
jwt.verify(token, config.jwt.secret, {ignoreExpiration: true}, (err, decoded) => {
if (err) {
return res.status(500).json({ success: false, message: err.message });
}
//Expired token, try to refresh
if(decoded.exp*1000 < Date.now()){
User.findById(decoded._id, (err, user) => {
if(err) {return res.send(err);}
if(!user){
return res.status(500).json({ success: false, message: 'Token owner not found.' });
}
//if refresh token found, and not expired, refresh expiry time
let isValidToken = _(user.refresh_tokens)
.filter( (token) => {return token.expires>Date.now()} )
.any({token: decoded.refresh_token});
if(!isValidToken){
req.user = {role: 'Guest'};
return next();
}
let newToken = signToken(decoded._id, decoded.role, decoded.refresh_token);
req.token = newToken;
req.user = decoded;
return next();
})
} else {
// if everything is good, save to request for use in other routes
req.user = decoded;
return next();
}
});
}
链接地址: http://www.djcxy.com/p/22034.html
上一篇: REST和身份验证变体
下一篇: 用刷新令牌签署jwt令牌作为有效负载
