This repository was archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathgithub.index.js
More file actions
180 lines (174 loc) · 7.2 KB
/
github.index.js
File metadata and controls
180 lines (174 loc) · 7.2 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const qs = require('querystring');
const fs = require('fs');
const jwt = require('jsonwebtoken');
const cookie = require('cookie');
const jwkToPem = require('jwk-to-pem');
const auth = require('./auth.js');
const axios = require('axios');
var config;
exports.handler = (event, context, callback) => {
if (typeof config == 'undefined') {
config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
}
mainProcess(event, context, callback);
};
function mainProcess(event, context, callback) {
// Get request, request headers, and querystring dictionary
const request = event.Records[0].cf.request;
const headers = request.headers;
const queryDict = qs.parse(request.querystring);
if (event.Records[0].cf.config.hasOwnProperty('test')) {
config.AUTH_REQUEST.redirect_uri = event.Records[0].cf.config.test + config.CALLBACK_PATH;
config.TOKEN_REQUEST.redirect_uri = event.Records[0].cf.config.test + config.CALLBACK_PATH;
}
if (request.uri.startsWith(config.CALLBACK_PATH)) {
console.log("Callback from GitHub received");
/** Verify code is in querystring */
if (!queryDict.code || !queryDict.state) {
unauthorized("No code or state found.", callback);
}
config.TOKEN_REQUEST.code = queryDict.code;
config.TOKEN_REQUEST.state = queryDict.state;
/** Exchange code for authorization token */
const postData = qs.stringify(config.TOKEN_REQUEST);
console.log("Requesting access token.");
axios.post(config.TOKEN_ENDPOINT, postData)
.then(function (response) {
console.log(response);
var responseQueryString = qs.parse(response.data);
/** Get authenticated user's login */
if (responseQueryString.error) {
internalServerError("Error while getting token: " + responseQueryString.error_description, callback);
} else {
const authorization = responseQueryString.token_type + ' ' + responseQueryString.access_token;
axios.get('https://api.github.com/user', { headers: { 'Authorization': authorization } })
.then(function (response) {
console.log(response);
/** Check if authenticated user's login is a member of given org */
if (!response.data.hasOwnProperty('login')) {
internalServerError('Unable to find login', callback);
}
var username = response.data.login;
var orgsGet = 'https://api.github.com/orgs/' + config.ORGANIZATION + '/members/' + username;
console.log("Checking ORG membership.");
axios.get(orgsGet, { headers: { 'Authorization': authorization } })
.then(function (response) {
console.log(response);
/** Set cookie upon verified membership */
if (response.status == 204) {
console.log("Setting cookie and redirecting.");
const nextLocation = {
"status": "302",
"statusDescription": "Found",
"body": "ID token retrieved.",
"headers": {
"location": [{
"key": "Location",
"value": event.Records[0].cf.config.hasOwnProperty('test') ? (config.AUTH_REQUEST.redirect_uri + queryDict.state) : queryDict.state
}],
"set-cookie": [{
"key": "Set-Cookie",
"value": cookie.serialize('TOKEN', jwt.sign(
{},
config.PRIVATE_KEY.trim(),
{
audience: headers.host[0].value,
subject: auth.getSubject(username),
expiresIn: config.SESSION_DURATION,
algorithm: 'RS256'
} // Options
), {
SameSite: 'None; Secure'
})
}],
},
};
callback(null, nextLocation);
} else {
console.log("User not a member of required ORG. Unauthorized.");
unauthorized('Unauthorized. User ' + response.login + ' is not a member of required organization.', callback);
}
})
.catch(function (error) {
internalServerError('Error checking membership: ' + error.message, callback);
});
})
.catch(function (error) {
internalServerError('Error getting user: ' + error.message, callback);
});
}
})
.catch(function (error) {
internalServerError('Error getting token: ' + error.message, callback);
});
} else if ("cookie" in headers
&& "TOKEN" in cookie.parse(headers["cookie"][0].value)) {
// Verify the JWT, the payload email, and that the email ends with configured hosted domain
jwt.verify(cookie.parse(headers["cookie"][0].value).TOKEN, config.PUBLIC_KEY.trim(), { algorithms: ['RS256'] }, function (err, decoded) {
if (err) {
switch (err.name) {
case 'TokenExpiredError':
console.log("Token expired, redirecting to OIDC provider.");
redirect(request, headers, callback);
break;
case 'JsonWebTokenError':
console.log("JWT error, unauthorized.");
unauthorized(err.message, callback);
break;
default:
console.log("Unknown JWT error, unauthorized.");
unauthorized('Unauthorized. User ' + decoded.sub + ' is not permitted.', callback);
}
} else {
console.log("Authorizing user.");
auth.isAuthorized(decoded, request, callback, unauthorized, internalServerError, config);
}
});
} else {
console.log("Redirecting to GitHub.");
redirect(request, headers, callback);
}
}
function redirect(request, headers, callback) {
config.AUTH_REQUEST.state = request.uri;
// Redirect to Authorization Server
var querystring = qs.stringify(config.AUTH_REQUEST);
const response = {
status: "302",
statusDescription: "Found",
body: "Redirecting to OAuth2 provider",
headers: {
"location": [{
"key": "Location",
"value": config.AUTHORIZATION_ENDPOINT + '?' + querystring
}],
"set-cookie": [{
"key": "Set-Cookie",
"value": cookie.serialize('TOKEN', '', { path: '/', expires: new Date(1970, 1, 1, 0, 0, 0, 0), SameSite: 'None; Secure' })
}],
},
};
callback(null, response);
}
function unauthorized(body, callback) {
const response = {
"status": "401",
"statusDescription": "Unauthorized",
"body": body,
"headers": {
"set-cookie": [{
"key": "Set-Cookie",
"value": cookie.serialize('TOKEN', '', { path: '/', expires: new Date(1970, 1, 1, 0, 0, 0, 0), SameSite: 'None; Secure' })
}],
},
};
callback(null, response);
}
function internalServerError(body, callback) {
const response = {
"status": "500",
"statusDescription": "Internal Server Error",
"body": body,
};
callback(null, response);
}