Skip to content

Commit e0b40e5

Browse files
Restrict access based on country
1 parent 05079f8 commit e0b40e5

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

app-routes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ module.exports = (app) => {
4545
}
4646
})
4747

48+
if (def.forbiddenCountries) {
49+
actions.push(async (req, res, next) => {
50+
if (req.authUser.isMachine) {
51+
next()
52+
} else {
53+
req.authUser.userId = String(req.authUser.userId)
54+
const user = await helper.getMemberById(req.authUser.userId)
55+
if (!user || _.intersection([user.homeCountryCode, user.competitionCountryCode], def.forbiddenCountries).length > 0) {
56+
throw new errors.ForbiddenError('Access denied')
57+
}
58+
next()
59+
}
60+
})
61+
}
62+
4863
if (!def.allowAnonymous) {
4964
actions.push((req, res, next) => {
5065
if (req.authUser.isMachine) {

config/default.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,25 @@ module.exports = {
6868
RESOURCE_ROLE_CREATE_TOPIC: process.env.RESOURCE_ROLE_CREATE_TOPIC || 'challenge.action.resource.role.create',
6969
RESOURCE_ROLE_UPDATE_TOPIC: process.env.RESOURCE_ROLE_UPDATE_TOPIC || 'challenge.action.resource.role.update',
7070

71-
AUTOMATED_TESTING_NAME_PREFIX: process.env.AUTOMATED_TESTING_NAME_PREFIX || 'POSTMANE2E-'
71+
AUTOMATED_TESTING_NAME_PREFIX: process.env.AUTOMATED_TESTING_NAME_PREFIX || 'POSTMANE2E-',
72+
73+
FORBIDDEN_COUNTRIES: [
74+
'Iran',
75+
'North Korea',
76+
'Cuba',
77+
'Sudan',
78+
'Syria',
79+
'Belarus',
80+
'Russia',
81+
'Russian Federation'
82+
],
83+
FORBIDDEN_COUNTRIES_ALPHA_3: [
84+
'IRN',
85+
'PRK',
86+
'CUB',
87+
'SDN', 'SSD', // (south sudan)
88+
'SYR',
89+
'BLR',
90+
'RUS'
91+
]
7292
}

src/common/helper.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ async function getMemberInfoById (id) {
156156
return memberInfo
157157
}
158158

159+
/**
160+
* Get Member by memberId from the API
161+
* @param {String} id The user ID
162+
* @returns {Promise<void>}
163+
*/
164+
async function getMemberById (id) {
165+
const res = await getRequest(`${config.MEMBER_API_URL}?userId=${id}`)
166+
return _.get(res, 'data[0]')
167+
}
168+
159169
/**
160170
* Get Data by model id
161171
* @param {String} handle The member handle
@@ -483,5 +493,6 @@ module.exports = {
483493
getAllPages,
484494
getESClient,
485495
checkAgreedTerms,
486-
postRequest
496+
postRequest,
497+
getMemberById
487498
}

src/routes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
*/
44

55
const constants = require('../app-constants')
6-
const { SCOPES: { READ, CREATE, DELETE, UPDATE, ALL } } = require('config')
6+
const {
7+
SCOPES: { READ, CREATE, DELETE, UPDATE, ALL },
8+
FORBIDDEN_COUNTRIES,
9+
FORBIDDEN_COUNTRIES_ALPHA_3
10+
} = require('config')
711

812
module.exports = {
913
'/resources': {
@@ -20,7 +24,8 @@ module.exports = {
2024
method: 'createResource',
2125
auth: 'jwt',
2226
access: [constants.UserRoles.Admin, constants.UserRoles.Copilot, constants.UserRoles.Manager, constants.UserRoles.User],
23-
scopes: [CREATE, ALL]
27+
scopes: [CREATE, ALL],
28+
forbiddenCountries: [...FORBIDDEN_COUNTRIES, ...FORBIDDEN_COUNTRIES_ALPHA_3]
2429
},
2530
delete: {
2631
controller: 'ResourceController',

0 commit comments

Comments
 (0)