Skip to content

update auth0 related configuration #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The following parameters can be set in config files or in env variables:

- `AUTH0_URL`: Auth0 URL, used to get TC M2M token
- `AUTH0_AUDIENCE`: Auth0 audience, used to get TC M2M token
- `AUTH0_AUDIENCE_FOR_BUS_API`: Auth0 audience, used to get TC M2M token to be used in bus api client
- `AUTH0_AUDIENCE_UBAHN`: Auth0 audience for U-Bahn
- `TOKEN_CACHE_TIME`: Auth0 token cache time, used to get TC M2M token
- `AUTH0_CLIENT_ID`: Auth0 client id, used to get TC M2M token
- `AUTH0_CLIENT_SECRET`: Auth0 client secret, used to get TC M2M token
Expand Down
2 changes: 1 addition & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
VALID_ISSUERS: process.env.VALID_ISSUERS || '["https://api.topcoder-dev.com", "https://api.topcoder.com", "https://topcoder-dev.auth0.com/", "https://auth.topcoder-dev.com/"]',
AUTH0_URL: process.env.AUTH0_URL,
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE,
AUTH0_AUDIENCE_FOR_BUS_API: process.env.AUTH0_AUDIENCE_FOR_BUS_API,
AUTH0_AUDIENCE_UBAHN: process.env.AUTH0_AUDIENCE_UBAHN,
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
Expand Down
2 changes: 1 addition & 1 deletion config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
AUTH0_URL: 'http://example.com',
AUTH0_AUDIENCE: 'http://example.com',
AUTH0_AUDIENCE_FOR_BUS_API: 'http://example.com',
AUTH0_AUDIENCE_UBAHN: 'http://example.com',
AUTH0_CLIENT_ID: 'fake_id',
AUTH0_CLIENT_SECRET: 'fake_secret'
}
2 changes: 1 addition & 1 deletion docker/sample.api.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ES_HOST=<ES Host Endpoint>

AUTH0_URL=<AUTH0 URL>
AUTH0_AUDIENCE=<AUTH0 Audience>
AUTH0_AUDIENCE_FOR_BUS_API=<AUTH0 Audience For Bus Api>
AUTH0_AUDIENCE_UBAHN=<AUTH0 Audience For Bus Api>
TOKEN_CACHE_TIME=500000
AUTH0_CLIENT_ID=<AUTH0 Client ID>
AUTH0_CLIENT_SECRET=<AUTH0 Client Secret>
66 changes: 32 additions & 34 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ AWS.config.region = config.esConfig.AWS_REGION

const m2mAuth = require('tc-core-library-js').auth.m2m

// const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL']))

const topcoderM2M = m2mAuth({
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE_FOR_BUS_API,
const m2mForUbahn = m2mAuth({
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE_UBAHN,
..._.pick(config, ['AUTH0_URL', 'TOKEN_CACHE_TIME', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AUTH0_PROXY_SERVER_URL'])
})
}
)

let busApiClient

Expand All @@ -42,10 +42,8 @@ function getBusApiClient () {
if (busApiClient) {
return busApiClient
}
busApiClient = busApi({
AUTH0_AUDIENCE: config.AUTH0_AUDIENCE_FOR_BUS_API,
..._.pick(config, ['AUTH0_URL', 'TOKEN_CACHE_TIME', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'BUSAPI_URL', 'KAFKA_ERROR_TOPIC', 'AUTH0_PROXY_SERVER_URL'])
})
busApiClient = busApi(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'BUSAPI_URL', 'KAFKA_ERROR_TOPIC', 'AUTH0_PROXY_SERVER_URL'])
)
return busApiClient
}

Expand Down Expand Up @@ -210,16 +208,16 @@ function getESClient () {
* Function to get M2M token
* @returns {Promise}
*/
const getM2Mtoken = async () => {
const getM2MToken = async () => {
return await m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}

/*
* Function to get M2M token to access topcoder resources(e.g. /v3/users)
* Function to get M2M token for U-Bahn
* @returns {Promise}
*/
const getTopcoderM2MToken = async () => {
return await topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
const getM2MUbahnToken = async () => {
return await m2mForUbahn.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}

/**
Expand Down Expand Up @@ -247,7 +245,7 @@ function encodeQueryString (queryObj, nesting = '') {
* @returns {String} user id.
*/
async function getUserIds (userId) {
const token = await getM2Mtoken()
const token = await getM2MToken()
const q = {
enrich: true,
externalProfile: {
Expand Down Expand Up @@ -320,7 +318,7 @@ function isDocumentMissingException (err) {
async function getProjects (currentUser, criteria = {}) {
let token
if (currentUser.hasManagePermission || currentUser.isMachine) {
const m2mToken = await getM2Mtoken()
const m2mToken = await getM2MToken()
token = `Bearer ${m2mToken}`
} else {
token = currentUser.jwtToken
Expand Down Expand Up @@ -351,7 +349,7 @@ async function getProjects (currentUser, criteria = {}) {
* @returns {Object} the user
*/
async function getTopcoderUserById (userId) {
const token = await getTopcoderM2MToken()
const token = await getM2MToken()
const res = await request
.get(config.TOPCODER_USERS_API)
.query({ filter: `id=${userId}` })
Expand All @@ -371,7 +369,7 @@ async function getTopcoderUserById (userId) {
* @returns the request result
*/
async function getUserById (userId, enrich) {
const token = await getM2Mtoken()
const token = await getM2MToken()
const res = await request
.get(`${config.TC_API}/users/${userId}` + (enrich ? '?enrich=true' : ''))
.set('Authorization', `Bearer ${token}`)
Expand All @@ -389,29 +387,29 @@ async function getUserById (userId, enrich) {
}

/**
* Function to create user in ubhan
* Function to create user in ubahn
* @param {Object} data the user data
* @returns the request result
*/
async function createUbhanUser ({ handle, firstName, lastName }) {
const token = await getM2Mtoken()
async function createUbahnUser ({ handle, firstName, lastName }) {
const token = await getM2MUbahnToken()
const res = await request
.post(`${config.TC_API}/users`)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send({ handle, firstName, lastName })
localLogger.debug({ context: 'createUbhanUser', message: `response body: ${JSON.stringify(res.body)}` })
localLogger.debug({ context: 'createUbahnUser', message: `response body: ${JSON.stringify(res.body)}` })
return _.pick(res.body, ['id'])
}

/**
* Function to create external profile for a ubhan user
* Function to create external profile for a ubahn user
* @param {String} userId the user id(with uuid format)
* @param {Object} data the profile data
*/
async function createUserExternalProfile (userId, { organizationId, externalId }) {
const token = await getM2Mtoken()
const token = await getM2MUbahnToken()
const res = await request
.post(`${config.TC_API}/users/${userId}/externalProfiles`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -427,7 +425,7 @@ async function createUserExternalProfile (userId, { organizationId, externalId }
* @returns the request result
*/
async function getMembers (handles) {
const token = await getM2Mtoken()
const token = await getM2MToken()
const handlesStr = _.map(handles, handle => {
return '%22' + handle.toLowerCase() + '%22'
}).join(',')
Expand All @@ -451,7 +449,7 @@ async function getMembers (handles) {
async function getProjectById (currentUser, id) {
let token
if (currentUser.hasManagePermission || currentUser.isMachine) {
const m2mToken = await getM2Mtoken()
const m2mToken = await getM2MToken()
token = `Bearer ${m2mToken}`
} else {
token = currentUser.jwtToken
Expand Down Expand Up @@ -484,7 +482,7 @@ async function getProjectById (currentUser, id) {
* @returns the request result
*/
async function getTopcoderSkills (criteria) {
const token = await getM2Mtoken()
const token = await getM2MToken()
try {
const res = await request
.get(`${config.TC_API}/skills`)
Expand Down Expand Up @@ -516,7 +514,7 @@ async function getTopcoderSkills (criteria) {
* @returns the request result
*/
async function getSkillById (skillId) {
const token = await getM2Mtoken()
const token = await getM2MToken()
const res = await request
.get(`${config.TC_API}/skills/${skillId}`)
.set('Authorization', `Bearer ${token}`)
Expand Down Expand Up @@ -556,17 +554,17 @@ async function getUserSkill (token, userId) {
* Fetch the user info from /v3/users and create a new user in /v5/users.
*
* @params {Object} currentUser the user who perform this operation
* @returns {String} the ubhan user id
* @returns {String} the ubahn user id
*/
async function ensureUbhanUserId (currentUser) {
async function ensureUbahnUserId (currentUser) {
try {
return await getUserId(currentUser.userId)
} catch (err) {
if (!(err instanceof errors.NotFoundError)) {
throw err
}
const topcoderUser = await getTopcoderUserById(currentUser.userId)
const user = await createUbhanUser(_.pick(topcoderUser, ['handle', 'firstName', 'lastName']))
const user = await createUbahnUser(_.pick(topcoderUser, ['handle', 'firstName', 'lastName']))
await createUserExternalProfile(user.id, { organizationId: config.ORG_ID, externalId: currentUser.userId })
return user.id
}
Expand All @@ -589,7 +587,7 @@ async function ensureJobById (jobId) {
* @returns {Object} the user data
*/
async function ensureUserById (userId) {
const token = await getM2Mtoken()
const token = await getM2MToken()
try {
const res = await request
.get(`${config.TC_API}/users/${userId}`)
Expand Down Expand Up @@ -625,7 +623,7 @@ function getAuditM2Muser () {
* @returns the result
*/
async function checkIsMemberOfProject (userId, projectId) {
const m2mToken = await getM2Mtoken()
const m2mToken = await getM2MToken()
const res = await request
.get(`${config.TC_API}/projects/${projectId}`)
.set('Authorization', `Bearer ${m2mToken}`)
Expand All @@ -649,10 +647,10 @@ module.exports = {
if (userId === config.m2m.M2M_AUDIT_USER_ID) {
return config.m2m.M2M_AUDIT_USER_ID
}
return ensureUbhanUserId({ userId })
return ensureUbahnUserId({ userId })
},
getM2Mtoken,
getTopcoderM2MToken,
getM2MToken,
getM2MUbahnToken,
postEvent,
getBusApiClient,
isDocumentMissingException,
Expand Down
6 changes: 3 additions & 3 deletions src/services/JobService.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ async function updateJob (currentUser, id, data) {
}
let job = await Job.findById(id)
const oldValue = job.toJSON()
const ubhanUserId = await helper.getUserId(currentUser.userId)
const ubahnUserId = await helper.getUserId(currentUser.userId)
if (!currentUser.hasManagePermission && !currentUser.isMachine) {
// Check whether user can update the job.
// Note that there is no need to check if user is member of the project associated with the job here
// because user who created the job must be the member of the project associated with the job
if (ubhanUserId !== job.createdBy) {
if (ubahnUserId !== job.createdBy) {
throw new errors.ForbiddenError('You are not allowed to perform this action!')
}
}

data.updatedAt = new Date()
data.updatedBy = ubhanUserId
data.updatedBy = ubahnUserId

await job.update(data)
await helper.postEvent(config.TAAS_JOB_UPDATE_TOPIC, { id, ...data }, { oldValue: oldValue })
Expand Down