1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 18:29:17 +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) {
dbapi.loadAccessToken(tokenId) log.error('Bad Access Token Header')
.then(function(token) { return res.status(401).json({
var jwt = token.jwt success: false
, data = jwtutil.decode(jwt, req.options.secret) , description: 'Bad Credentials'
})
}
if (data) { dbapi.loadAccessToken(tokenId)
dbapi.loadUser(data.email) .then(function(token) {
.then(function(user) { if (!token) {
if (user) { return res.status(401).json({
req.user = user success: false
next() , description: 'Bad Credentials'
} })
}) }
var jwt = token.jwt
, data = jwtutil.decode(jwt, req.options.secret)
if (!data) {
return res.status(500).json({
success: false
})
}
dbapi.loadUser(data.email)
.then(function(user) {
if (user) {
req.user = user
next()
} else { } else {
res.status(500).json({ return res.status(500).json({
success: false success: false
}) })
} }
}) })
.catch(function(err) { .catch(function(err) {
log.error('Failed to load token: ', err.stack) log.error('Failed to load user: ', err.stack)
res.status(401).json({
success: false,
description: 'Bad Credentials'
}) })
})
} else {
log.error('Bad Access Token Header')
res.status(401).json({
success: false,
description: 'Bad Credentials'
}) })
} .catch(function(err) {
log.error('Failed to load token: ', err.stack)
return 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'
}) })
} }
} }