Skip to content

Commit 0480a45

Browse files
author
sachin-maheshwari
authored
Merge pull request #15 from topcoder-platform/dev
prod - M2M support
2 parents f66af08 + 813292c commit 0480a45

File tree

10 files changed

+89
-3077
lines changed

10 files changed

+89
-3077
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']
8686
- "build-prod":
8787
filters:
8888
branches:

common/helper.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,10 @@ function signJwtToken (payload) {
129129
/**
130130
* Validate the event based on the source service, type, and message.
131131
*
132-
* @param {String} sourceServiceName the source service name
133132
* @param {Object} event the event
134133
*/
135-
function validateEvent (sourceServiceName, event) {
134+
function validateEvent (event) {
136135
const schema = Joi.object().keys({
137-
sourceServiceName: Joi.string().required(),
138136
event: Joi.object().keys({
139137
type: Joi
140138
.string()
@@ -146,7 +144,7 @@ function validateEvent (sourceServiceName, event) {
146144
})
147145
})
148146

149-
const { error } = Joi.validate({sourceServiceName, event}, schema)
147+
const { error } = Joi.validate({event}, schema)
150148
if (error) {
151149
throw error
152150
}
@@ -191,10 +189,19 @@ function validateEventPayload (event) {
191189
}
192190
}
193191

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+
194200
module.exports = {
195201
buildService,
196202
verifyJwtToken,
197203
signJwtToken,
198204
validateEvent,
199-
validateEventPayload
205+
validateEventPayload,
206+
verifyTokenScope
200207
}

config/default.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ module.exports = {
77
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
88
CONTEXT_PATH: process.env.API_VERSION || '/eventbus/',
99
PORT: process.env.PORT || '3000',
10-
authSecret: process.env.JWT_TOKEN_SECRET,
11-
authDomain: process.env.AUTH_DOMAIN,
12-
validIssuers: process.env.VALID_ISSUERS ? process.env.VALID_ISSUERS.replace(/\\"/g, '') : null,
10+
AUTH_SECRET: process.env.JWT_TOKEN_SECRET,
11+
VALID_ISSUERS: process.env.VALID_ISSUERS ? process.env.VALID_ISSUERS.replace(/\\"/g, '') : null,
1312
JWT_TOKEN_SECRET: process.env.JWT_TOKEN_SECRET || '',
1413
JWT_TOKEN_EXPIRES_IN: process.env.JWT_TOKEN_EXPIRES_IN || '100 days',
1514
KAFKA_TOPIC_PREFIX: process.env.KAFKA_TOPIC_PREFIX || '',
1615
ALLOWED_SERVICES: process.env.ALLOWED_SERVICES || ['project-service', 'message-service'],
1716
TC_EMAIL_SERVICE_URL: process.env.TC_EMAIL_SERVICE_URL,
18-
TC_EMAIL_SERVICE_TOKEN: process.env.TC_EMAIL_SERVICE_TOKEN,
19-
TC_EMAIL_SERVICE_CACHE_PERIOD: process.env.TC_EMAIL_SERVICE_CACHE_PERIOD || (3600 * 1000)
17+
TC_EMAIL_SERVICE_CACHE_PERIOD: process.env.TC_EMAIL_SERVICE_CACHE_PERIOD || (3600 * 1000),
18+
19+
// Configuration for generating machine to machine auth0 token.
20+
// The token will be used for calling another internal API.
21+
AUTH0_URL: process.env.AUTH0_URL || '',
22+
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE || '',
23+
// The token will be cached.
24+
// We define the time period of the cached token.
25+
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME || 86400000,
26+
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
27+
AUTH0_CLIENT_Secret: process.env.AUTH0_CLIENT_SECRET,
28+
SCOPES: {
29+
"writeBusApi": "write:bus_api",
30+
"readBusTopics": "read:bus_topics"
31+
}
2032
}

controllers/EventController.js

Lines changed: 4 additions & 1 deletion
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,7 +13,8 @@ const MessageBusService = require('../services/MessageBusService')
1113
* @param {Function} next the next middleware
1214
*/
1315
async function create (req, res, next) {
14-
await MessageBusService.postEvent(req.authUser.name, req.body)
16+
helper.verifyTokenScope(req, config.SCOPES.writeBusApi)
17+
await MessageBusService.postEvent(req.body)
1518
res.status(204).end()
1619
next()
1720
}

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: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ 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")
52+
53+
AUTH0_URL=$(eval "echo \$${ENV}_AUTH0_URL")
54+
AUTH0_AUDIENCE=$(eval "echo \$${ENV}_AUTH0_AUDIENCE")
55+
TOKEN_CACHE_TIME=$(eval "echo \$${ENV}_TOKEN_CACHE_TIME")
56+
AUTH0_CLIENT_ID=$(eval "echo \$${ENV}_AUTH0_CLIENT_ID")
57+
AUTH0_CLIENT_SECRET=$(eval "echo \$${ENV}_AUTH0_CLIENT_SECRET")
5358

5459
echo $APP_NAME
5560

@@ -147,14 +152,30 @@ make_task_def(){
147152
"name": "VALID_ISSUERS",
148153
"value": "%s"
149154
},
150-
{
151-
"name": "TC_EMAIL_SERVICE_URL",
152-
"value": "%s"
153-
},
154-
{
155-
"name": "TC_EMAIL_SERVICE_TOKEN",
156-
"value": "%s"
157-
}
155+
{
156+
"name": "TC_EMAIL_SERVICE_URL",
157+
"value": "%s"
158+
},
159+
{
160+
"name": "AUTH0_URL",
161+
"value": "%s"
162+
},
163+
{
164+
"name": "AUTH0_AUDIENCE",
165+
"value": "%s"
166+
},
167+
{
168+
"name": "AUTH0_CLIENT_ID",
169+
"value": "%s"
170+
},
171+
{
172+
"name": "AUTH0_CLIENT_SECRET",
173+
"value": "%s"
174+
},
175+
{
176+
"name": "TOKEN_CACHE_TIME",
177+
"value": "%s"
178+
}
158179
],
159180
"portMappings": [
160181
{
@@ -174,7 +195,7 @@ make_task_def(){
174195
}
175196
]'
176197

177-
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 $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)
178199
}
179200

180201
register_definition() {

0 commit comments

Comments
 (0)