Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 562ed64

Browse files
Merge pull request #24 from topcoder-platform/Issue_23
Import groups for a new user
2 parents 985daf9 + 90f9c26 commit 562ed64

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ The following parameters can be set in config files or in env variables:
6464
- ELASTICCLOUD_ID: The elastic cloud id, if your elasticsearch instance is hosted on elastic cloud. DO NOT provide a value for ES_HOST if you are using this
6565
- ELASTICCLOUD_USERNAME: The elastic cloud username for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud
6666
- ELASTICCLOUD_PASSWORD: The elastic cloud password for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud
67+
- AUTH0_URL: The auth0 url, Default is 'https://topcoder-dev.auth0.com/oauth/token'
68+
- AUTH0_AUDIENCE: The auth0 audience for accessing ubahn api(s), Default is 'https://m2m.topcoder-dev.com/'
69+
- AUTH0_CLIENT_ID: The auth0 client id
70+
- AUTH0_CLIENT_SECRET: The auth0 client secret
71+
- AUTH0_PROXY_SERVER_URL: The auth0 proxy server url
72+
- TOKEN_CACHE_TIME: The token cache time
73+
- TOPCODER_GROUP_API: The topcoder groups api, Default is 'https://api.topcoder-dev.com/v5/groups'
6774

6875
There is a `/health` endpoint that checks for the health of the app. This sets up an expressjs server and listens on the environment variable `PORT`. It's not part of the configuration file and needs to be passed as an environment variable
6976

config/default.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ module.exports = {
1414
// Kafka group id
1515
KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID || 'ubahn-processor-es',
1616

17+
TOPCODER_GROUP_API: process.env.TOPCODER_GROUP_API || 'https://api.topcoder-dev.com/v5/groups',
18+
19+
AUTH0_URL: process.env.AUTH0_URL || 'https://topcoder-dev.auth0.com/oauth/token', // Auth0 credentials
20+
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE || 'https://m2m.topcoder-dev.com/',
21+
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,
22+
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
23+
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
24+
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,
25+
1726
UBAHN_CREATE_TOPIC: process.env.UBAHN_CREATE_TOPIC || 'u-bahn.action.create',
1827
UBAHN_UPDATE_TOPIC: process.env.UBAHN_UPDATE_TOPIC || 'u-bahn.action.update',
1928
UBAHN_DELETE_TOPIC: process.env.UBAHN_DELETE_TOPIC || 'u-bahn.action.delete',

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"get-parameter-names": "^0.3.0",
3535
"lodash": "^4.17.19",
3636
"no-kafka": "^3.4.3",
37+
"axios": "^0.19.2",
38+
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6",
3739
"topcoder-healthcheck-dropin": "^1.0.3",
3840
"winston": "^3.2.1"
3941
},

src/common/helper.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const elasticsearch = require('@elastic/elasticsearch')
88
const _ = require('lodash')
99
const Joi = require('@hapi/joi')
1010
const { Mutex } = require('async-mutex')
11+
const axios = require('axios')
12+
const logger = require('./logger')
13+
const m2mAuth = require('tc-core-library-js').auth.m2m
14+
const topcoderM2M = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
1115

1216
AWS.config.region = config.ES.AWS_REGION
1317

@@ -18,6 +22,34 @@ let transactionId
1822
const esClientMutex = new Mutex()
1923
const mutexReleaseMap = {}
2024

25+
/* Function to get M2M token
26+
* (Topcoder APIs only)
27+
* @returns {Promise}
28+
*/
29+
async function getTopcoderM2Mtoken () {
30+
return topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
31+
}
32+
33+
/**
34+
* Returns the user in Topcoder identified by the email
35+
* @param {String} email The user email
36+
*/
37+
async function getUserGroup (memberId) {
38+
const url = config.TOPCODER_GROUP_API
39+
const token = await getTopcoderM2Mtoken()
40+
const params = { memberId, membershipType: 'user', page: 1 }
41+
42+
logger.debug(`request GET ${url} with params: ${JSON.stringify(params)}`)
43+
let groups = []
44+
let groupRes = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
45+
while (groupRes.data.length > 0) {
46+
groups = _.concat(groups, _.map(groupRes.data, g => _.pick(g, 'id', 'name')))
47+
params.page = params.page + 1
48+
groupRes = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
49+
}
50+
return groups
51+
}
52+
2153
/**
2254
* Get Kafka options
2355
* @return {Object} the Kafka options
@@ -201,6 +233,7 @@ module.exports = {
201233
getESClient,
202234
validProperties,
203235
getUser,
236+
getUserGroup,
204237
updateUser,
205238
getOrg,
206239
updateOrg,

src/services/ProcessorService.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ async function processCreate (message, transactionId) {
4949
user[userResource.propertyName] = []
5050
}
5151

52+
// import groups for a new user
53+
if (resource === 'externalprofile' && message.payload.externalId) {
54+
const userGroups = await helper.getUserGroup(message.payload.externalId)
55+
user[config.get('ES.USER_GROUP_PROPERTY_NAME')] = _.unionBy(user[config.get('ES.USER_GROUP_PROPERTY_NAME')], userGroups, 'id')
56+
}
57+
5258
// check the resource does not exist
5359
if (_.some(user[userResource.propertyName], [userResource.relateKey, relateId])) {
5460
logger.error(`Can't create existed ${resource} with the ${userResource.relateKey}: ${relateId}, userId: ${message.payload.userId}`)

0 commit comments

Comments
 (0)