1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

refactor security handler

This commit is contained in:
Vishal Banthia 2015-12-14 15:06:46 +09:00
parent fa9ee605f3
commit 87fcf19406

View file

@ -16,46 +16,59 @@ function accessTokenAuth(req, res, next) {
, tokenId = authHeader[1] , tokenId = authHeader[1]
if (format !== 'bearer') { if (format !== 'bearer') {
res.status(401).json({ return res.status(401).json({
success: false success: false
, description: 'Authorization header should be in "bearer $AUTH_TOKEN" format' , description: 'Authorization header should be in "bearer $AUTH_TOKEN" format'
}) })
} }
if (tokenId) { if (!tokenId) {
log.error('Bad Access Token Header')
return res.status(401).json({
success: false
, description: 'Bad Credentials'
})
}
dbapi.loadAccessToken(tokenId) dbapi.loadAccessToken(tokenId)
.then(function(token) { .then(function(token) {
if (!token) {
return res.status(401).json({
success: false
, description: 'Bad Credentials'
})
}
var jwt = token.jwt var jwt = token.jwt
, data = jwtutil.decode(jwt, req.options.secret) , data = jwtutil.decode(jwt, req.options.secret)
if (data) { if (!data) {
return res.status(500).json({
success: false
})
}
dbapi.loadUser(data.email) dbapi.loadUser(data.email)
.then(function(user) { .then(function(user) {
if (user) { if (user) {
req.user = user req.user = user
next() next()
}
})
} else { } else {
res.status(500).json({ return res.status(500).json({
success: false success: false
}) })
} }
}) })
.catch(function(err) {
log.error('Failed to load user: ', err.stack)
})
})
.catch(function(err) { .catch(function(err) {
log.error('Failed to load token: ', err.stack) log.error('Failed to load token: ', err.stack)
res.status(401).json({ return res.status(401).json({
success: false, success: false
description: 'Bad Credentials' , description: 'Bad Credentials'
}) })
}) })
} else {
log.error('Bad Access Token Header')
res.status(401).json({
success: false,
description: 'Bad Credentials'
})
}
} }
// Request is coming from browser app // Request is coming from browser app
// TODO: Remove this once frontend become stateless // TODO: Remove this once frontend become stateless
@ -68,7 +81,7 @@ function accessTokenAuth(req, res, next) {
next() next()
} }
else { else {
res.json(500, { return res.status(500).json({
success: false success: false
}) })
} }
@ -77,8 +90,8 @@ function accessTokenAuth(req, res, next) {
} }
else { else {
res.status(401).json({ res.status(401).json({
success: false, success: false
description: 'Requires Authentication' , description: 'Requires Authentication'
}) })
} }
} }