-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.42 KB
/
index.js
File metadata and controls
43 lines (36 loc) · 1.42 KB
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
const fetch = require('node-fetch').default;
// add role names to this object to map them to group ids in your AAD tenant
const roleGroupMappings = {
'admin': '21a96550-aa02-486e-9297-e6e51b6398fc',
'reader': '33bb071c-118d-40d1-a5d7-7ced5900b973'
};
module.exports = async function (context, req) {
const user = req.body || {};
const roles = [];
for (const [role, groupId] of Object.entries(roleGroupMappings)) {
if (await isUserInGroup(groupId, user.accessToken, context)) {
roles.push(role);
}
}
context.res.json({
roles
});
}
async function isUserInGroup(groupId, bearerToken, context) {
const url = new URL('https://graph.microsoft.com/v1.0/me/memberOf');
url.searchParams.append('$filter', `id eq '${groupId}'`);
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${bearerToken}`
},
});
if (response.status !== 200) {
const responsebody = await response.json();
context.log.error('Failed to query graph.microsoft.com with http status code', response.status, 'and message:', JSON.stringify(responsebody.error.message));
return false;
}
const graphResponse = await response.json();
const matchingGroups = graphResponse.value.filter(group => group.id === groupId);
return matchingGroups.length > 0;
}