-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudfront-function.js
More file actions
52 lines (45 loc) · 1.94 KB
/
cloudfront-function.js
File metadata and controls
52 lines (45 loc) · 1.94 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
function handler(event) {
var request = event.request;
var headers = request.headers;
var cookies = headers.cookie;
var uri = request.uri;
// Authentication check for all pages except callback and static resources
var needsAuthentication = !(uri.startsWith('/callback') ||
uri.endsWith('.js') ||
uri.endsWith('.css') ||
uri.endsWith('.png') ||
uri.endsWith('.jpg') ||
uri.endsWith('.html') ||
uri.endsWith('.svg'));
// Map root to index.html internally (won't show in URL)
if (uri === '/' || uri === '') {
request.uri = '/index.html';
// But don't return yet - check authentication first
}
// Check if we need to authenticate and no valid token exists
if (needsAuthentication) {
// Look for the authentication cookie
var hasValidToken = cookies && cookies.value &&
(cookies.value.includes('CognitoIdentityServiceProvider.idToken') ||
cookies.value.includes('CognitoIdentityServiceProvider.accessToken'));
if (!hasValidToken) {
// Not authenticated, redirect to Cognito login
var host = request.headers.host && request.headers.host.value;
var redirectURL = "https://COGNITO_LOGIN_URL/login?" +
"client_id=CLIENT_ID" +
"response_type=token&" +
"scope=email+openid+profile&" +
"redirect_uri=https://" + host + "/callback.html";
return {
statusCode: 302,
statusDescription: "Found",
headers: {
"location": { value: redirectURL },
"cache-control": { value: "no-cache, no-store, must-revalidate" }
}
};
}
}
// If we reached here, the request is either authenticated or doesn't need authentication
return request;
}