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 1 commit
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>
50 changes: 24 additions & 26 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-Bhan
* @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 @@ -394,7 +392,7 @@ async function getUserById (userId, enrich) {
* @returns the request result
*/
async function createUbhanUser ({ handle, firstName, lastName }) {
const token = await getM2Mtoken()
const token = await getM2MUbahnToken()
const res = await request
.post(`${config.TC_API}/users`)
.set('Authorization', `Bearer ${token}`)
Expand All @@ -411,7 +409,7 @@ async function createUbhanUser ({ handle, firstName, lastName }) {
* @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 @@ -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 @@ -651,8 +649,8 @@ module.exports = {
}
return ensureUbhanUserId({ userId })
},
getM2Mtoken,
getTopcoderM2MToken,
getM2MToken,
getM2MUbahnToken,
postEvent,
getBusApiClient,
isDocumentMissingException,
Expand Down