diff --git a/src/.env.sample b/src/.env.sample index 990c872..b729489 100644 --- a/src/.env.sample +++ b/src/.env.sample @@ -14,3 +14,6 @@ SERVICE_NAME = entity-management #api doc endpoint API_DOC_URL = "/entity-management/api-doc" +#Indicate If auth token is bearer or not +IS_AUTH_TOKEN_BEARER=false + diff --git a/src/envVariables.js b/src/envVariables.js index 9a8cf28..d4d2c91 100644 --- a/src/envVariables.js +++ b/src/envVariables.js @@ -32,6 +32,11 @@ let enviromentVariables = { message: 'Required api doc url', optional: false, }, + IS_AUTH_TOKEN_BEARER: { + message: 'Required specification: If auth token is bearer or not', + optional: true, + default: false, + }, } let success = true diff --git a/src/generics/middleware/authenticator.js b/src/generics/middleware/authenticator.js index 4bc5dcd..276bdad 100644 --- a/src/generics/middleware/authenticator.js +++ b/src/generics/middleware/authenticator.js @@ -7,6 +7,7 @@ // dependencies const jwt = require('jsonwebtoken') +const isBearerRequired = process.env.IS_AUTH_TOKEN_BEARER === 'true' var respUtil = function (resp) { return { @@ -37,7 +38,21 @@ module.exports = async function (req, res, next, token = '') { delete req.headers[e] }) - var token = req.headers['x-auth-token'] + // Check if a Bearer token is required for authentication + let authHeader = req.headers['x-auth-token'] + if (isBearerRequired) { + const [authType, extractedToken] = authHeader.split(' ') + if (authType.toLowerCase() !== 'bearer') { + rspObj.errCode = CONSTANTS.apiResponses.TOKEN_INVALID_CODE + rspObj.errMsg = CONSTANTS.apiResponses.TOKEN_INVALID_MESSAGE + rspObj.responseCode = HTTP_STATUS_CODE['unauthorized'].status + return res.status(HTTP_STATUS_CODE['unauthorized'].status).send(respUtil(rspObj)) + } + token = extractedToken?.trim() + } else { + token = authHeader.trim() + } + if (!req.rspObj) req.rspObj = {} var rspObj = req.rspObj