Skip to content

Commit 7bfd235

Browse files
author
sachin-maheshwari
authored
Merge pull request #26 from topcoder-platform/feature/m2mtoken
m2m token
2 parents d8b60be + 36123de commit 7bfd235

File tree

10 files changed

+374
-223
lines changed

10 files changed

+374
-223
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, 'feature/notification-email-improvements']
85+
only: dev
8686
- "build-prod":
8787
filters:
8888
branches:

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ Configuration for the notification server is at `config/default.js`.
1313
The following parameters can be set in config files or in env variables:
1414
- LOG_LEVEL: the log level
1515
- PORT: the notification server port
16-
- authSecret: TC auth secret
17-
- authDomain: TC auth domain
18-
- validIssuers: TC auth valid issuers
16+
- AUTH_SECRET: TC auth secret
17+
- VALID_ISSUERS: TC auth valid issuers
1918
- jwksUri: TC auth JWKS URI
2019
- DATABASE_URL: URI to PostgreSQL database
2120
- DATABASE_OPTIONS: database connection options
@@ -84,9 +83,8 @@ In case it expires, you may get a new token in this way:
8483

8584
## Local deployment
8685
- for local development environment you can set variables as following:
87-
- `authSecret`, `authDomain`, `validIssuers` can get from [tc-project-service config](https://github.com/topcoder-platform/tc-project-service/blob/dev/config/default.json)
86+
- `AUTH_SECRET`,`VALID_ISSUERS` can get from [tc-project-service config](https://github.com/topcoder-platform/tc-project-service/blob/dev/config/default.json)
8887
- `PORT=4000` because **connect-app** call this port by default
89-
- `jwksUri` - any
9088
- `KAFKA_TOPIC_IGNORE_PREFIX=joan-26673.` (with point at the end)
9189
- `TC_API_V4_BASE_URL=https://api.topcoder-dev.com/v4`
9290
- `TC_API_V3_BASE_URL=https://api.topcoder-dev.com/v3`

config/default.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ module.exports = {
55
ENV: process.env.ENV,
66
LOG_LEVEL: process.env.LOG_LEVEL,
77
PORT: process.env.PORT,
8-
authSecret: process.env.authSecret,
9-
authDomain: process.env.authDomain,
10-
jwksUri: process.env.jwksUri,
8+
AUTH_SECRET: process.env.authSecret,
119
DATABASE_URL: process.env.DATABASE_URL,
1210
DATABASE_OPTIONS: {
1311
dialect: 'postgres',
@@ -21,7 +19,7 @@ module.exports = {
2119
},
2220
},
2321

24-
validIssuers: process.env.validIssuers ? process.env.validIssuers.replace(/\\"/g, '') : null,
22+
VALID_ISSUERS: process.env.validIssuers ? process.env.validIssuers.replace(/\\"/g, '') : null,
2523
KAFKA_URL: process.env.KAFKA_URL,
2624
KAFKA_TOPIC_IGNORE_PREFIX: process.env.KAFKA_TOPIC_IGNORE_PREFIX,
2725
KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID,
@@ -44,4 +42,14 @@ module.exports = {
4442
ENABLE_DEV_MODE: process.env.ENABLE_DEV_MODE || true,
4543
DEV_MODE_EMAIL: process.env.DEV_MODE_EMAIL,
4644
API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/notifications',
45+
46+
// Configuration for generating machine to machine auth0 token.
47+
// The token will be used for calling another internal API.
48+
AUTH0_URL: process.env.AUTH0_URL,
49+
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE,
50+
// The token will be cached.
51+
// We define the time period of the cached token.
52+
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME || 86400000,
53+
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
54+
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
4755
};

connect/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ module.exports = {
1919

2020
// id of the BOT user which creates post with various events in discussions
2121
TCWEBSERVICE_ID: process.env.TCWEBSERVICE_ID || '22838965',
22+
2223
};

connect/service.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
* Service to get data from TopCoder API
33
*/
44
const request = require('superagent');
5-
const config = require('./config');
5+
const config = require('config');
66
const _ = require('lodash');
7+
const tcCoreLibAuth = require('tc-core-library-js').auth;
8+
const m2m = tcCoreLibAuth.m2m(config);
79

810
/**
911
* Get project details
@@ -68,23 +70,32 @@ const getRoleMembers = (roleId) => request
6870
*/
6971
const getUsersById = (ids) => {
7072
const query = _.map(ids, (id) => 'userId:' + id).join(' OR ');
71-
return request
72-
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,email,handle,firstName,lastName&query=${query}`)
73-
.set('accept', 'application/json')
74-
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
75-
.then((res) => {
76-
if (!_.get(res, 'body.result.success')) {
77-
throw new Error(`Failed to get users by id: ${ids}`);
78-
}
73+
return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
74+
.then((token) => {
75+
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
7976

80-
const users = _.get(res, 'body.result.content');
81-
return users;
82-
}).catch((err) => {
83-
const errorDetails = _.get(err, 'response.body.result.content.message');
84-
throw new Error(
85-
`Failed to get users by ids: ${ids}.` +
86-
(errorDetails ? ' Server response: ' + errorDetails : '')
87-
);
77+
return request
78+
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,email,handle,firstName,lastName&query=${query}`)
79+
.set('accept', 'application/json')
80+
.set('authorization', `Bearer ${token}`)
81+
.then((res) => {
82+
if (!_.get(res, 'body.result.success')) {
83+
throw new Error(`Failed to get users by id: ${ids}`);
84+
}
85+
86+
const users = _.get(res, 'body.result.content');
87+
return users;
88+
}).catch((err) => {
89+
const errorDetails = _.get(err, 'response.body.result.content.message');
90+
throw new Error(
91+
`Failed to get users by ids: ${ids}.` +
92+
(errorDetails ? ' Server response: ' + errorDetails : '')
93+
);
94+
});
95+
})
96+
.catch((err) => {
97+
err.message = 'Error generating m2m token: ' + err.message;
98+
throw err;
8899
});
89100
};
90101

@@ -97,24 +108,32 @@ const getUsersById = (ids) => {
97108
*/
98109
const getUsersByHandle = (handles) => {
99110
const query = _.map(handles, (handle) => 'handle:' + handle).join(' OR ');
100-
return request
101-
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,handle,firstName,lastName&query=${query}`)
102-
.set('accept', 'application/json')
103-
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
104-
.then((res) => {
105-
if (!_.get(res, 'body.result.success')) {
106-
throw new Error(`Failed to get users by handle: ${handles}`);
107-
}
111+
return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
112+
.then((token) => {
113+
if (!token && config.TC_ADMIN_TOKEN) token = config.TC_ADMIN_TOKEN;
108114

109-
const users = _.get(res, 'body.result.content');
115+
return request
116+
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,handle,firstName,lastName&query=${query}`)
117+
.set('accept', 'application/json')
118+
.set('authorization', `Bearer ${token}`)
119+
.then((res) => {
120+
if (!_.get(res, 'body.result.success')) {
121+
throw new Error(`Failed to get users by handle: ${handles}`);
122+
}
123+
const users = _.get(res, 'body.result.content');
110124

111-
return users;
112-
}).catch((err) => {
113-
const errorDetails = _.get(err, 'response.body.result.content.message');
114-
throw new Error(
115-
`Failed to get users by handles: ${handles}.` +
116-
(errorDetails ? ' Server response: ' + errorDetails : '')
117-
);
125+
return users;
126+
}).catch((err) => {
127+
const errorDetails = _.get(err, 'response.body.result.content.message');
128+
throw new Error(
129+
`Failed to get users by handles: ${handles}.` +
130+
(errorDetails ? ' Server response: ' + errorDetails : '')
131+
);
132+
});
133+
})
134+
.catch((err) => {
135+
err.message = 'Error generating m2m token: ' + err.message;
136+
throw err;
118137
});
119138
};
120139

0 commit comments

Comments
 (0)