Skip to content

Commit 48d9a20

Browse files
author
sachin-maheshwari
authored
Merge pull request #45 from topcoder-platform/feature/m2mtoken
deprecate static Admin token generation.
2 parents d853d42 + 00d3b7a commit 48d9a20

File tree

6 files changed

+82
-99
lines changed

6 files changed

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

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ The following parameters can be set in config files or in env variables:
5050
- `TC_API_V3_BASE_URL`: the TopCoder API V3 base URL
5151
- `TC_API_V4_BASE_URL`: the TopCoder API V4 base URL
5252
- `MESSAGE_API_BASE_URL`: the TopCoder message service API base URL
53-
- `TC_ADMIN_TOKEN`: the admin token to access TopCoder API - same for V3 and V4
5453
- **Topcder specific**<br>
5554
Also it has probably temporary variables of TopCoder role ids for 'Connect Manager', 'Connect Copilot' and 'administrator':
5655
- `CONNECT_MANAGER_ROLE_ID`: 8,
@@ -94,29 +93,13 @@ so we can run `node test/token 305384` to generate a token to manage notificatio
9493
The generated token is already configured in the Postman notification server API environment TOKEN variable.
9594
You may reuse it during review.
9695

97-
98-
## TC API Admin Token
99-
100-
An admin token is needed to access TC API. This is already configured Postman notification
101-
server API environment TC_ADMIN_TOKEN variable.
102-
In case it expires, you may get a new token in this way:
103-
104-
- use Chrome to browse connect.topcoder-dev.com
105-
- open developer tools, click the Network tab
106-
- log in with suser1 / Topcoder123, or mess / appirio123
107-
- once logged in, open some project, for example https://connect.topcoder-dev.com/projects/1936 and in the network inspector
108-
look for the call to the project api and get the token from the auth header, see
109-
http://pokit.org/get/img/68cdd34f3d205d6d9bd8bddb07bdc216.jpg
110-
111-
11296
## Local deployment
11397
- for local development environment you can set variables as following:
11498
- `AUTH_SECRET`,`VALID_ISSUERS` can get from [tc-project-service config](https://github.com/topcoder-platform/tc-project-service/blob/dev/config/default.json)
11599
- `PORT=4000` because **connect-app** call this port by default
116100
- `KAFKA_TOPIC_IGNORE_PREFIX=joan-26673.` (with point at the end)
117101
- `TC_API_V4_BASE_URL=https://api.topcoder-dev.com/v4`
118102
- `TC_API_V3_BASE_URL=https://api.topcoder-dev.com/v3`
119-
- `TC_ADMIN_TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJUb3Bjb2RlciBVc2VyIiwiYWRtaW5pc3RyYXRvciJdLCJpc3MiOiJodHRwczovL2FwaS50b3Bjb2Rlci1kZXYuY29tIiwiaGFuZGxlIjoic3VzZXIxIiwiZXhwIjoxNTEzNDAxMjU4LCJ1c2VySWQiOiI0MDE1MzkzOCIsImlhdCI6MTUwOTYzNzYzOSwiZW1haWwiOiJtdHdvbWV5QGJlYWtzdGFyLmNvbSIsImp0aSI6IjIzZTE2YjA2LWM1NGItNDNkNS1iY2E2LTg0ZGJiN2JiNDA0NyJ9.REds35fdBvY7CMDGGFyT_tOD7DxGimFfVzIyEy9YA0Y` or follow section **TC API Admin Token** to obtain a new one if expired
120103
- `KAFKA_URL`, `KAFKA_CLIENT_CERT` and `KAFKA_CLIENT_CERT_KEY` get from [tc-bus-api readme](https://github.com/topcoder-platform/tc-bus-api/tree/dev)
121104
- if you are willing to use notifications API which is hosted by the notifications server locally, you will need to use some patched `tc-core-library-js` module, which skips verification of user token. Because we don't know Topcoder `AUTH_SECRET` locally. So you can install this fork:
122105
```

config/default.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module.exports = {
2121
VALID_ISSUERS: process.env.validIssuers ? process.env.validIssuers.replace(/\\"/g, '') : null,
2222
// keep it here for dev purposes, it's only needed by modified version of tc-core-library-js
2323
// which skips token validation when locally deployed
24-
JWKS_URI: process.env.jwksUri,
2524

2625
KAFKA_URL: process.env.KAFKA_URL,
2726
KAFKA_TOPIC_IGNORE_PREFIX: process.env.KAFKA_TOPIC_IGNORE_PREFIX,

connect/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module.exports = {
77
TC_API_V3_BASE_URL: process.env.TC_API_V3_BASE_URL || 'https://api.topcoder-dev.com/v3',
88
TC_API_V4_BASE_URL: process.env.TC_API_V4_BASE_URL || 'https://api.topcoder-dev.com/v4',
99
MESSAGE_API_BASE_URL: process.env.MESSAGE_API_BASE_URL || 'https://api.topcoder-dev.com/v5',
10-
TC_ADMIN_TOKEN: process.env.TC_ADMIN_TOKEN,
1110

1211
// Probably temporary variables for TopCoder role ids for 'Connect Manager', 'Connect Copilot' and 'administrator'
1312
// These are values for development backend. For production backend they may be different.

connect/service.js

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,65 @@ const { logger } = require('../index');
1414
*
1515
* @return {Promise} promise resolved to project details
1616
*/
17-
const getProject = (projectId) => request
18-
.get(`${config.TC_API_V4_BASE_URL}/projects/${projectId}`)
19-
.set('accept', 'application/json')
20-
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
21-
.then((res) => {
22-
if (!_.get(res, 'body.result.success')) {
23-
throw new Error(`Failed to get project details of project id: ${projectId}`);
24-
}
25-
26-
const project = _.get(res, 'body.result.content');
27-
28-
return project;
29-
}).catch((err) => {
30-
const errorDetails = _.get(err, 'response.body.result.content.message');
31-
throw new Error(
32-
`Failed to get project details of project id: ${projectId}.` +
33-
(errorDetails ? ' Server response: ' + errorDetails : '')
34-
);
35-
});
36-
17+
const getProject = (projectId) => {
18+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
19+
.then((token) => {
20+
return request
21+
.get(`${config.TC_API_V4_BASE_URL}/projects/${projectId}`)
22+
.set('accept', 'application/json')
23+
.set('authorization', `Bearer ${token}`)
24+
.then((res) => {
25+
if (!_.get(res, 'body.result.success')) {
26+
throw new Error(`Failed to get project details of project id: ${projectId}`);
27+
}
28+
const project = _.get(res, 'body.result.content');
29+
return project;
30+
}).catch((err) => {
31+
const errorDetails = _.get(err, 'response.body.result.content.message');
32+
throw new Error(
33+
`Failed to get project details of project id: ${projectId}.` +
34+
(errorDetails ? ' Server response: ' + errorDetails : '')
35+
);
36+
});
37+
})
38+
.catch((err) => {
39+
err.message = 'Error generating m2m token: ' + err.message;
40+
throw err;
41+
});
42+
};
3743
/**
3844
* Get role members
3945
*
4046
* @param {String} roleId role id
4147
*
4248
* @return {Promise} promise resolved to role members ids list
4349
*/
44-
const getRoleMembers = (roleId) => request
45-
.get(`${config.TC_API_V3_BASE_URL}/roles/${roleId}?fields=subjects`)
46-
.set('accept', 'application/json')
47-
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
48-
.then((res) => {
49-
if (!_.get(res, 'body.result.success')) {
50-
throw new Error(`Failed to get role members of role id: ${roleId}`);
51-
}
52-
53-
const members = _.get(res, 'body.result.content.subjects');
54-
55-
return members;
56-
}).catch((err) => {
57-
const errorDetails = _.get(err, 'response.body.result.content.message');
58-
throw new Error(
59-
`Failed to get role members of role id: ${roleId}.` +
60-
(errorDetails ? ' Server response: ' + errorDetails : '')
61-
);
62-
});
50+
const getRoleMembers = (roleId) => {
51+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
52+
.then((token) => {
53+
return request
54+
.get(`${config.TC_API_V3_BASE_URL}/roles/${roleId}?fields=subjects`)
55+
.set('accept', 'application/json')
56+
.set('authorization', `Bearer ${token}`)
57+
.then((res) => {
58+
if (!_.get(res, 'body.result.success')) {
59+
throw new Error(`Failed to get role membrs of role id: ${roleId}`);
60+
}
61+
const members = _.get(res, 'body.result.content.subjects');
62+
return members;
63+
}).catch((err) => {
64+
const errorDetails = _.get(err, 'response.body.result.content.message');
65+
throw new Error(
66+
`Failed to get role membrs of role id: ${roleId}.` +
67+
(errorDetails ? ' Server response: ' + errorDetails : '')
68+
);
69+
});
70+
})
71+
.catch((err) => {
72+
err.message = 'Error generating m2m token: ' + err.message;
73+
throw err;
74+
});
75+
};
6376

6477
/**
6578
* Get users details by ids
@@ -76,8 +89,6 @@ const getUsersById = (ids) => {
7689
throw err;
7790
})
7891
.then((token) => {
79-
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
80-
8192
return request
8293
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,email,handle,firstName,lastName&query=${query}`)
8394
.set('accept', 'application/json')
@@ -115,8 +126,6 @@ const getUsersByHandle = (handles) => {
115126
throw err;
116127
})
117128
.then((token) => {
118-
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
119-
120129
return request
121130
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,handle,firstName,lastName&query=${query}`)
122131
.set('accept', 'application/json')
@@ -146,26 +155,34 @@ const getUsersByHandle = (handles) => {
146155
*
147156
* @return {Promise} promise resolved to topic details
148157
*/
149-
const getTopic = (topicId) => request
150-
.get(`${config.MESSAGE_API_BASE_URL}/topics/${topicId}/read`)
151-
.set('accept', 'application/json')
152-
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
153-
.then((res) => {
154-
if (!_.get(res, 'body.result.success')) {
155-
throw new Error(`Failed to get topic details of topic id: ${topicId}`);
156-
}
157-
158-
return _.get(res, 'body.result.content');
159-
}).catch((err) => {
160-
if (logger) {
161-
logger.error(err, `Error while calling ${config.MESSAGE_API_BASE_URL}/topics/${topicId}/read`);
162-
}
163-
const errorDetails = _.get(err, 'response.body.result.content.message');
164-
throw new Error(
165-
`Failed to get topic details of topic id: ${topicId}.` +
166-
(errorDetails ? ' Server response: ' + errorDetails : '')
167-
);
168-
});
158+
const getTopic = (topicId, logger) => {
159+
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
160+
.then((token) => {
161+
return request
162+
.get(`${config.MESSAGE_API_BASE_URL}/topics/${topicId}/read`)
163+
.set('accept', 'application/json')
164+
.set('authorization', `Bearer ${token}`)
165+
.then((res) => {
166+
if (!_.get(res, 'body.result.success')) {
167+
throw new Error(`Failed to get topic details of topic id: ${topicId}`);
168+
}
169+
return _.get(res, 'body.result.content');
170+
}).catch((err) => {
171+
if (logger) {
172+
logger.error(err, `Error while calling ${config.MESSAGE_API_BASE_URL}/topics/${topicId}/read`);
173+
}
174+
const errorDetails = _.get(err, 'response.body.result.content.message');
175+
throw new Error(
176+
`Failed to get topic details of topic id: ${topicId}.` +
177+
(errorDetails ? ' Server response: ' + errorDetails : '')
178+
);
179+
});
180+
})
181+
.catch((err) => {
182+
err.message = 'Error generating m2m token: ' + err.message;
183+
throw err;
184+
});
185+
};
169186

170187
module.exports = {
171188
getProject,

deploy.sh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ KAFKA_GROUP_ID=$(eval "echo \$${ENV}_KAFKA_GROUP_ID")
3737
KAFKA_TOPIC_IGNORE_PREFIX=$(eval "echo \$${ENV}_KAFKA_TOPIC_IGNORE_PREFIX")
3838
KAFKA_URL=$(eval "echo \$${ENV}_KAFKA_URL")
3939
AUTHSECRET=$(eval "echo \$${ENV}_AUTHSECRET")
40-
AUTHDOMAIN=$(eval "echo \$${ENV}_AUTHDOMAIN")
4140
VALID_ISSUERS=$(eval "echo \$${ENV}_VALID_ISSUERS")
42-
JWKSURI=$(eval "echo \$${ENV}_JWKSURI")
4341
TC_API_BASE_URL=$(eval "echo \$${ENV}_TC_API_BASE_URL")
44-
TC_ADMIN_TOKEN=$(eval "echo \$${ENV}_TC_ADMIN_TOKEN")
4542
LOG_LEVEL=$(eval "echo \$${ENV}_LOG_LEVEL")
4643
PORT=$(eval "echo \$${ENV}_PORT")
4744

@@ -155,14 +152,6 @@ make_task_def(){
155152
"name": "authSecret",
156153
"value": "%s"
157154
},
158-
{
159-
"name": "authDomain",
160-
"value": "%s"
161-
},
162-
{
163-
"name": "jwksUri",
164-
"value": "%s"
165-
},
166155
{
167156
"name": "TC_API_BASE_URL",
168157
"value": "%s"
@@ -183,10 +172,6 @@ make_task_def(){
183172
"name": "MESSAGE_API_BASE_URL",
184173
"value": "%s"
185174
},
186-
{
187-
"name": "TC_ADMIN_TOKEN",
188-
"value": "%s"
189-
},
190175
{
191176
"name": "ENABLE_EMAILS",
192177
"value": "%s"
@@ -266,7 +251,7 @@ make_task_def(){
266251
}
267252
]'
268253

269-
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID "$KAFKA_TOPIC_IGNORE_PREFIX" $KAFKA_URL $DATABASE_URL $AUTHSECRET "$AUTHDOMAIN" "$JWKSURI" $TC_API_BASE_URL $TC_API_V3_BASE_URL $TC_API_V4_BASE_URL $TC_API_V5_BASE_URL $MESSAGE_API_BASE_URL $TC_ADMIN_TOKEN $ENABLE_EMAILS $MENTION_EMAIL $REPLY_EMAIL_PREFIX $REPLY_EMAIL_DOMAIN $ENABLE_DEV_MODE $DEV_MODE_EMAIL $LOG_LEVEL $VALID_ISSUERS $PORT "$API_CONTEXT_PATH" "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
254+
task_def=$(printf "$task_template" $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID "$KAFKA_TOPIC_IGNORE_PREFIX" $KAFKA_URL $DATABASE_URL $AUTHSECRET $TC_API_BASE_URL $TC_API_V3_BASE_URL $TC_API_V4_BASE_URL $TC_API_V5_BASE_URL $MESSAGE_API_BASE_URL $ENABLE_EMAILS $MENTION_EMAIL $REPLY_EMAIL_PREFIX $REPLY_EMAIL_DOMAIN $ENABLE_DEV_MODE $DEV_MODE_EMAIL $LOG_LEVEL $VALID_ISSUERS $PORT "$API_CONTEXT_PATH" "$AUTH0_URL" "$AUTH0_AUDIENCE" $AUTH0_CLIENT_ID "$AUTH0_CLIENT_SECRET" $TOKEN_CACHE_TIME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER $ENV)
270255
}
271256

272257
register_definition() {

0 commit comments

Comments
 (0)