Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token bearer configurable #74

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 5 additions & 0 deletions src/envVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ let enviromentVariables = {
message: 'Required api doc url',
optional: false,
},
IS_AUTH_TOKEN_BEARER: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@priyanka-TL can you please verify if this variable is not passed from .env. the default value is getting passed correctly

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes its working correctly

message: 'Required specification: If auth token is bearer or not',
optional: true,
default: false,
},
}

let success = true
Expand Down
17 changes: 16 additions & 1 deletion src/generics/middleware/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

// dependencies
const jwt = require('jsonwebtoken')
const isBearerRequired = process.env.IS_AUTH_TOKEN_BEARER === 'true'

var respUtil = function (resp) {
return {
Expand Down Expand Up @@ -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()
VISHNUDAS-tunerlabs marked this conversation as resolved.
Show resolved Hide resolved
} else {
token = authHeader.trim()
}

if (!req.rspObj) req.rspObj = {}
var rspObj = req.rspObj

Expand Down