-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
75 lines (67 loc) · 1.58 KB
/
Copy pathutils.js
File metadata and controls
75 lines (67 loc) · 1.58 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
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
const github = require('octonode');
const config = require('./config');
const log = require('./log');
const createResponse = (authenticated, { username, groups } = {}) => {
if (!authenticated) {
return {
apiVersion: 'authentication.k8s.io/v1beta1',
kind: 'TokenReview',
status: {
Authenticated: false,
},
};
}
return {
apiVersion: 'authentication.k8s.io/v1beta1',
kind: 'TokenReview',
status: {
authenticated: true,
user: {
username: username,
groups: Array.isArray(groups) ? groups : undefined,
}
}
};
};
const parseUserResponse = ([status, body, headers]) => {
log.debug({ name: 'USER_RESPONSE', body });
return body.login;
};
const parseTeamResponse = ([status, body, headers]) => {
log.debug({ name: 'TEAM_RESPONSE', body });
return body.reduce((result, team) => {
const include = (
!config.assertTeamOrganization
|| team.organization.login.toLowerCase() === config.assertTeamOrganization
);
if (include) {
result.push(team.slug);
}
return result;
}, []);
};
const handleNoTeamAccess = (err) => {
if (err.statusCode === 404) {
log.info({ name: 'NO_TEAM_ACCESS' }, 'Token lacks read:org scope');
return [];
}
throw err;
};
const fetchUserData = (token) => {
const client = github.client(token);
return Promise.all([
client.getAsync('/user', {}).then(parseUserResponse),
client.getAsync('/user/teams', { per_page: 100 })
.then(parseTeamResponse)
.catch(handleNoTeamAccess),
]).then(([ username, groups]) => {
return {
username,
groups,
};
});
};
module.exports = {
createResponse,
fetchUserData,
};