-
Notifications
You must be signed in to change notification settings - Fork 21
/
access-helpers.js
executable file
·151 lines (126 loc) · 4.24 KB
/
access-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const { getUser, inTenant } = require('../db');
const { log } = require('./logging');
const USDR_TENANT_ID = 1;
const USDR_AGENCY_ID = 0;
const USDR_EMAIL_DOMAIN = 'usdigitalresponse.org';
function isUSDR(user) {
return user.tenant_id === USDR_TENANT_ID;
}
function isUSDRSuperAdmin(user) {
// Note: this function assumes an augmented user object from db.getUser(), not just a raw DB row
// (necessary for role_name field)
return (
isUSDR(user)
&& user.agency_id === USDR_AGENCY_ID
&& user.role_name === 'admin'
// TODO: Right now there are a bunch of non-USDR users in USDR tenant in prod, so we need to
// restrict this further. But this will also prevent USDR volunteers from having this permission.
&& user.email.endsWith(`@${USDR_EMAIL_DOMAIN}`)
);
}
/**
* Determine if the user's agency or subagencies includes the given agency.
*
* @param {Object} user
* @param {Number} agencyId
* @returns {Boolean} true if the agency is a subagency or same agency of the user
* */
function isAuthorizedForAgency(user, agencyId) {
const subagency = user.agency.subagencies.find(
(agency) => agency.id === agencyId,
);
if (subagency) {
return true;
}
return false;
}
/**
* Determine if a user is authorized for an agency.
*
* @param {Object} user
* @param {...Number} agencyIds
* @returns {Boolean} true if the agency is in the same tenant as the user
* */
async function isUserAuthorized(user, ...agencyIds) {
return inTenant(user.tenant_id, agencyIds);
}
async function getAdminAuthInfo(req) {
if (!req.signedCookies.userId) {
throw new Error('Unauthorized');
}
const user = await getUser(req.signedCookies.userId);
if (!user) {
throw new Error('Unauthorized');
}
if (user.role_name !== 'admin') {
throw new Error('Unauthorized');
}
let selectedAgency = user.agency_id;
const requestAgency = Number(req.params.organizationId);
if (!Number.isNaN(requestAgency)) {
const authorized = await isUserAuthorized(user, requestAgency);
if (!authorized) {
throw new Error('Unauthorized');
}
selectedAgency = requestAgency;
}
return { user, selectedAgency };
}
async function requireAdminUser(req, res, next) {
try {
const { user, selectedAgency } = await getAdminAuthInfo(req);
req.session = { ...req.session, user, selectedAgency };
} catch (err) {
res.sendStatus(403);
return;
}
next();
}
async function requireUser(req, res, next) {
if (!req.signedCookies.userId) {
res.sendStatus(403);
return;
}
const user = await getUser(req.signedCookies.userId);
if (!user) {
res.sendStatus(403);
return;
}
if (req.params.organizationId && user.role_name === 'staff' && (req.params.organizationId !== user.agency_id.toString())) {
res.sendStatus(403); // Staff are restricted to their own agency.
return;
}
// User NOT required to be admin; but if they ARE, they must satisfy admin rules.
if (user.role_name === 'admin') {
await requireAdminUser(req, res, next);
return;
}
req.session = { ...req.session, user, selectedAgency: user.agency_id };
next();
}
async function requireUSDRSuperAdminUser(req, res, next) {
await requireAdminUser(req, res, () => {
if (!isUSDRSuperAdmin(req.session.user)) {
res.sendStatus(403);
return;
}
next();
});
}
async function isMicrosoftSafeLinksRequest(req, res, next) {
const userAgent = req.headers['user-agent'] || '';
const nativeHost = req.headers['x-native-host'] || '';
if (userAgent.toLowerCase().includes('oneoutlook') || nativeHost.toLowerCase().includes('oneoutlook')) {
log.info({
'user-agent': userAgent,
'native-host': nativeHost,
headers: Object.keys(req.headers),
}, 'Microsoft Safe Links request');
res.json({ message: 'Success' });
return;
}
next();
}
module.exports = {
requireAdminUser, requireUser, isAuthorizedForAgency, isUserAuthorized, isUSDR, isUSDRSuperAdmin, requireUSDRSuperAdminUser, getAdminAuthInfo, isMicrosoftSafeLinksRequest,
};