Skip to content

Commit da1eb6e

Browse files
author
sachin-maheshwari
authored
Merge pull request #14 from topcoder-platform/feature/m2mtoken-support
m2m implementation
2 parents ca0fa36 + 03e7d0b commit da1eb6e

File tree

7 files changed

+41
-20
lines changed

7 files changed

+41
-20
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ workflows:
8282
- "build-dev":
8383
filters:
8484
branches:
85-
only: dev
85+
only: ['dev','feature/m2mtoken-support']
8686
- "build-prod":
8787
filters:
8888
branches:

common/helper.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,19 @@ function validateEventPayload (event) {
189189
}
190190
}
191191

192+
function verifyTokenScope(req, scope) {
193+
const isMachineToken = _.get(req, 'authUser.isMachine', false);
194+
const scopes = _.get(req, 'authUser.scopes', []);
195+
if (isMachineToken && !(_.indexOf(scopes, scope) >= 0)) {
196+
throw createError.Unauthorized("Check your token scope.")
197+
}
198+
}
199+
192200
module.exports = {
193201
buildService,
194202
verifyJwtToken,
195203
signJwtToken,
196204
validateEvent,
197-
validateEventPayload
205+
validateEventPayload,
206+
verifyTokenScope
198207
}

config/default.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ module.exports = {
1414
KAFKA_TOPIC_PREFIX: process.env.KAFKA_TOPIC_PREFIX || '',
1515
ALLOWED_SERVICES: process.env.ALLOWED_SERVICES || ['project-service', 'message-service'],
1616
TC_EMAIL_SERVICE_URL: process.env.TC_EMAIL_SERVICE_URL,
17-
TC_EMAIL_SERVICE_TOKEN: process.env.TC_EMAIL_SERVICE_TOKEN,
1817
TC_EMAIL_SERVICE_CACHE_PERIOD: process.env.TC_EMAIL_SERVICE_CACHE_PERIOD || (3600 * 1000),
19-
18+
2019
// Configuration for generating machine to machine auth0 token.
2120
// The token will be used for calling another internal API.
2221
AUTH0_URL: process.env.AUTH0_URL || '',
@@ -26,4 +25,8 @@ module.exports = {
2625
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME || 86400000,
2726
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
2827
AUTH0_CLIENT_Secret: process.env.AUTH0_CLIENT_SECRET,
28+
SCOPES: {
29+
"writeBusApi": "write:bus_api",
30+
"readBusTopics": "read:bus_topics"
31+
}
2932
}

controllers/EventController.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* The Event controller.
33
*/
44
const MessageBusService = require('../services/MessageBusService')
5+
const helper = require('../common/helper')
6+
const config = require('config')
57

68
/**
79
* Create a new event.
@@ -11,6 +13,7 @@ const MessageBusService = require('../services/MessageBusService')
1113
* @param {Function} next the next middleware
1214
*/
1315
async function create (req, res, next) {
16+
helper.verifyTokenScope(req, config.SCOPES.writeBusApi)
1417
await MessageBusService.postEvent(req.body)
1518
res.status(204).end()
1619
next()

controllers/TopicController.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* The Topic controller.
33
*/
44
const MessageBusService = require('../services/MessageBusService')
5+
const helper = require('../common/helper')
6+
const config = require('config')
57

68
/**
79
* Get all topic names.
@@ -11,6 +13,7 @@ const MessageBusService = require('../services/MessageBusService')
1113
* @param {Function} next the next middleware
1214
*/
1315
async function getAll (req, res, next) {
16+
helper.verifyTokenScope(req, config.SCOPES.readBusTopics)
1417
const topics = await MessageBusService.getAllTopics()
1518
res.send(topics)
1619
next()

deploy.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ AUTH_DOMAIN=$(eval "echo \$${ENV}_AUTH_DOMAIN")
4949
VALID_ISSUERS=$(eval "echo \$${ENV}_VALID_ISSUERS")
5050

5151
TC_EMAIL_SERVICE_URL=$(eval "echo \$${ENV}_TC_EMAIL_SERVICE_URL")
52-
TC_EMAIL_SERVICE_TOKEN=$(eval "echo \$${ENV}_TC_EMAIL_SERVICE_TOKEN")
5352

5453
AUTH0_URL=$(eval "echo \$${ENV}_AUTH0_URL")
5554
AUTH0_AUDIENCE=$(eval "echo \$${ENV}_AUTH0_AUDIENCE")
@@ -157,10 +156,6 @@ make_task_def(){
157156
"name": "TC_EMAIL_SERVICE_URL",
158157
"value": "%s"
159158
},
160-
{
161-
"name": "TC_EMAIL_SERVICE_TOKEN",
162-
"value": "%s"
163-
},
164159
{
165160
"name": "AUTH0_URL",
166161
"value": "%s"
@@ -200,7 +195,7 @@ make_task_def(){
200195
}
201196
]'
202197

203-
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)
198+
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)
204199
}
205200

206201
register_definition() {

services/PlaceholderService.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,32 @@ const Joi = require('joi')
66
const config = require('config')
77
const request = require('superagent')
88
const cache = require('memory-cache')
9+
const tcCoreLibAuth = require('tc-core-library-js').auth
10+
const m2m = tcCoreLibAuth.m2m(config)
11+
912

1013
/**
1114
* Get all email template placeholders name.
1215
*
1316
* @returns {Array} list with email template placeholders name
1417
*/
15-
async function getAllPlaceholders (name) {
18+
async function getAllPlaceholders(name) {
1619
const cachedData = cache.get(`placeholders-${name}`)
1720
if (cachedData == null) {
18-
const data = await request
19-
.get(`${config.TC_EMAIL_SERVICE_URL}/templates/${name}`)
20-
.set('accept', 'json')
21-
.set('authorization', `Bearer ${config.TC_EMAIL_SERVICE_TOKEN}`)
22-
const parsedData = JSON.parse(data.text)
23-
24-
cache.put(`placeholders-${name}`, parsedData, config.TC_EMAIL_SERVICE_CACHE_PERIOD)
25-
26-
return parsedData
21+
try {
22+
const token = await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
23+
const data = await request
24+
.get(`${config.TC_EMAIL_SERVICE_URL}/templates/${name}`)
25+
.set('accept', 'json')
26+
.set('authorization', `Bearer ${token}`)
27+
const parsedData = JSON.parse(data.text)
28+
29+
cache.put(`placeholders-${name}`, parsedData, config.TC_EMAIL_SERVICE_CACHE_PERIOD)
30+
31+
return parsedData
32+
} catch (err) {
33+
console.log(`Error generating m2m token: ${err.message}`)
34+
}
2735
}
2836

2937
return cachedData

0 commit comments

Comments
 (0)