Merge pull request #126 from shocknet/hotfix/encrypted-auth-token

Backwards-compatible encrypted token functionality
This commit is contained in:
CapDog 2020-07-30 16:46:37 -04:00 committed by GitHub
commit 89c34950b1
2 changed files with 38 additions and 32 deletions

View file

@ -260,8 +260,14 @@ module.exports = async (
} }
const decryptedKey = Encryption.decryptKey({ deviceId, message: req.body.encryptionKey }); const decryptedKey = Encryption.decryptKey({ deviceId, message: req.body.encryptionKey });
const decryptedMessage = Encryption.decryptMessage({ message: req.body.data, key: decryptedKey, iv: req.body.iv }) const decryptedMessage = Encryption.decryptMessage({ message: req.body.data, key: decryptedKey, iv: req.body.iv });
const decryptedToken = req.body.token ? Encryption.decryptMessage({ message: req.body.token, key: decryptedKey, iv: req.body.iv }) : null;
req.body = JSON.parse(decryptedMessage); req.body = JSON.parse(decryptedMessage);
if (decryptedToken) {
req.headers.authorization = decryptedToken;
}
return next(); return next();
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
@ -273,6 +279,37 @@ module.exports = async (
} }
}) })
app.use(async (req, res, next) => {
logger.info('Route:', req.path)
if (unprotectedRoutes[req.method][req.path]) {
next()
} else {
try {
const response = await auth.validateToken(
req.headers.authorization.replace('Bearer ', '')
)
if (response.valid) {
next()
} else {
res.status(401).json({
field: 'authorization',
errorMessage:
"The authorization token you've supplied is invalid"
})
}
} catch (err) {
logger.error(
!req.headers.authorization
? 'Please add an Authorization header'
: err
)
res
.status(401)
.json({ field: 'authorization', errorMessage: 'Please log in' })
}
}
})
app.use(async (req, res, next) => { app.use(async (req, res, next) => {
try { try {
if (unprotectedRoutes[req.method][req.path]) { if (unprotectedRoutes[req.method][req.path]) {

View file

@ -166,37 +166,6 @@ const server = program => {
app.use(compression()) app.use(compression())
app.use(async (req, res, next) => {
logger.info('Route:', req.path)
if (unprotectedRoutes[req.method][req.path]) {
next()
} else {
try {
const response = await auth.validateToken(
req.headers.authorization.replace('Bearer ', '')
)
if (response.valid) {
next()
} else {
res.status(401).json({
field: 'authorization',
errorMessage:
"The authorization token you've supplied is invalid"
})
}
} catch (err) {
logger.error(
!req.headers.authorization
? 'Please add an Authorization header'
: err
)
res
.status(401)
.json({ field: 'authorization', errorMessage: 'Please log in' })
}
}
})
app.use((req, res, next) => { app.use((req, res, next) => {
if (process.env.ROUTE_LOGGING === 'true') { if (process.env.ROUTE_LOGGING === 'true') {
if (sensitiveRoutes[req.method][req.path]) { if (sensitiveRoutes[req.method][req.path]) {