Skip to content

m2m implementation #14

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

Merged
merged 3 commits into from
May 23, 2018
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ workflows:
- "build-dev":
filters:
branches:
only: dev
only: ['dev','feature/m2mtoken-support']
- "build-prod":
filters:
branches:
Expand Down
11 changes: 10 additions & 1 deletion common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,19 @@ function validateEventPayload (event) {
}
}

function verifyTokenScope(req, scope) {
const isMachineToken = _.get(req, 'authUser.isMachine', false);
const scopes = _.get(req, 'authUser.scopes', []);
if (isMachineToken && !(_.indexOf(scopes, scope) >= 0)) {
throw createError.Unauthorized("Check your token scope.")
}
}

module.exports = {
buildService,
verifyJwtToken,
signJwtToken,
validateEvent,
validateEventPayload
validateEventPayload,
verifyTokenScope
}
7 changes: 5 additions & 2 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ module.exports = {
KAFKA_TOPIC_PREFIX: process.env.KAFKA_TOPIC_PREFIX || '',
ALLOWED_SERVICES: process.env.ALLOWED_SERVICES || ['project-service', 'message-service'],
TC_EMAIL_SERVICE_URL: process.env.TC_EMAIL_SERVICE_URL,
TC_EMAIL_SERVICE_TOKEN: process.env.TC_EMAIL_SERVICE_TOKEN,
TC_EMAIL_SERVICE_CACHE_PERIOD: process.env.TC_EMAIL_SERVICE_CACHE_PERIOD || (3600 * 1000),

// Configuration for generating machine to machine auth0 token.
// The token will be used for calling another internal API.
AUTH0_URL: process.env.AUTH0_URL || '',
Expand All @@ -26,4 +25,8 @@ module.exports = {
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME || 86400000,
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
AUTH0_CLIENT_Secret: process.env.AUTH0_CLIENT_SECRET,
SCOPES: {
"writeBusApi": "write:bus_api",
"readBusTopics": "read:bus_topics"
}
}
3 changes: 3 additions & 0 deletions controllers/EventController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* The Event controller.
*/
const MessageBusService = require('../services/MessageBusService')
const helper = require('../common/helper')
const config = require('config')

/**
* Create a new event.
Expand All @@ -11,6 +13,7 @@ const MessageBusService = require('../services/MessageBusService')
* @param {Function} next the next middleware
*/
async function create (req, res, next) {
helper.verifyTokenScope(req, config.SCOPES.writeBusApi)
await MessageBusService.postEvent(req.body)
res.status(204).end()
next()
Expand Down
3 changes: 3 additions & 0 deletions controllers/TopicController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* The Topic controller.
*/
const MessageBusService = require('../services/MessageBusService')
const helper = require('../common/helper')
const config = require('config')

/**
* Get all topic names.
Expand All @@ -11,6 +13,7 @@ const MessageBusService = require('../services/MessageBusService')
* @param {Function} next the next middleware
*/
async function getAll (req, res, next) {
helper.verifyTokenScope(req, config.SCOPES.readBusTopics)
const topics = await MessageBusService.getAllTopics()
res.send(topics)
next()
Expand Down
7 changes: 1 addition & 6 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ AUTH_DOMAIN=$(eval "echo \$${ENV}_AUTH_DOMAIN")
VALID_ISSUERS=$(eval "echo \$${ENV}_VALID_ISSUERS")

TC_EMAIL_SERVICE_URL=$(eval "echo \$${ENV}_TC_EMAIL_SERVICE_URL")
TC_EMAIL_SERVICE_TOKEN=$(eval "echo \$${ENV}_TC_EMAIL_SERVICE_TOKEN")

AUTH0_URL=$(eval "echo \$${ENV}_AUTH0_URL")
AUTH0_AUDIENCE=$(eval "echo \$${ENV}_AUTH0_AUDIENCE")
Expand Down Expand Up @@ -157,10 +156,6 @@ make_task_def(){
"name": "TC_EMAIL_SERVICE_URL",
"value": "%s"
},
{
"name": "TC_EMAIL_SERVICE_TOKEN",
"value": "%s"
},
{
"name": "AUTH0_URL",
"value": "%s"
Expand Down Expand Up @@ -200,7 +195,7 @@ make_task_def(){
}
]'

task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $KAFKA_URL "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $LOG_LEVEL $JWT_TOKEN_SECRET "$KAFKA_TOPIC_PREFIX" "$ALLOWED_SERVICES" $JWT_TOKEN_EXPIRES_IN "$API_VERSION" $PORT "$AUTH_DOMAIN" "$VALID_ISSUERS" $TC_EMAIL_SERVICE_URL $TC_EMAIL_SERVICE_TOKEN "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $KAFKA_URL "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $LOG_LEVEL $JWT_TOKEN_SECRET "$KAFKA_TOPIC_PREFIX" "$ALLOWED_SERVICES" $JWT_TOKEN_EXPIRES_IN "$API_VERSION" $PORT "$AUTH_DOMAIN" "$VALID_ISSUERS" $TC_EMAIL_SERVICE_URL "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
}

register_definition() {
Expand Down
28 changes: 18 additions & 10 deletions services/PlaceholderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ const Joi = require('joi')
const config = require('config')
const request = require('superagent')
const cache = require('memory-cache')
const tcCoreLibAuth = require('tc-core-library-js').auth
const m2m = tcCoreLibAuth.m2m(config)


/**
* Get all email template placeholders name.
*
* @returns {Array} list with email template placeholders name
*/
async function getAllPlaceholders (name) {
async function getAllPlaceholders(name) {
const cachedData = cache.get(`placeholders-${name}`)
if (cachedData == null) {
const data = await request
.get(`${config.TC_EMAIL_SERVICE_URL}/templates/${name}`)
.set('accept', 'json')
.set('authorization', `Bearer ${config.TC_EMAIL_SERVICE_TOKEN}`)
const parsedData = JSON.parse(data.text)

cache.put(`placeholders-${name}`, parsedData, config.TC_EMAIL_SERVICE_CACHE_PERIOD)

return parsedData
try {
const token = await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
const data = await request
.get(`${config.TC_EMAIL_SERVICE_URL}/templates/${name}`)
.set('accept', 'json')
.set('authorization', `Bearer ${token}`)
const parsedData = JSON.parse(data.text)

cache.put(`placeholders-${name}`, parsedData, config.TC_EMAIL_SERVICE_CACHE_PERIOD)

return parsedData
} catch (err) {
console.log(`Error generating m2m token: ${err.message}`)
}
}

return cachedData
Expand Down