forked from aws-samples/cloudfront-authorization-at-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
157 lines (148 loc) · 4.17 KB
/
index.ts
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
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import {
parse as parseQueryString,
stringify as stringifyQueryString,
} from "querystring";
import { CloudFrontRequestHandler } from "aws-lambda";
import {
getCompleteConfig,
extractAndParseCookies,
generateCookieHeaders,
httpPostWithRetry,
createErrorHtml,
} from "../shared/shared";
let CONFIG: ReturnType<typeof getCompleteConfig>;
export const handler: CloudFrontRequestHandler = async (event) => {
if (!CONFIG) {
CONFIG = getCompleteConfig();
CONFIG.logger.debug("Configuration loaded:", CONFIG);
}
CONFIG.logger.debug("Event:", event);
const request = event.Records[0].cf.request;
const domainName = request.headers["host"][0].value;
let redirectedFromUri = `https://${domainName}`;
try {
const { requestedUri, nonce: currentNonce } = parseQueryString(
request.querystring
);
redirectedFromUri += requestedUri || "";
const {
idToken,
accessToken,
refreshToken,
nonce: originalNonce,
} = extractAndParseCookies(
request.headers,
CONFIG.clientId,
CONFIG.cookieCompatibility
);
validateRefreshRequest(
currentNonce,
originalNonce,
idToken,
accessToken,
refreshToken
);
let headers: { "Content-Type": string; Authorization?: string } = {
"Content-Type": "application/x-www-form-urlencoded",
};
if (CONFIG.clientSecret !== "") {
const encodedSecret = Buffer.from(
`${CONFIG.clientId}:${CONFIG.clientSecret}`
).toString("base64");
headers["Authorization"] = `Basic ${encodedSecret}`;
}
let tokens = {
id_token: idToken!,
access_token: accessToken!,
refresh_token: refreshToken!,
};
let cookieHeadersEventType: keyof typeof generateCookieHeaders;
try {
const body = stringifyQueryString({
grant_type: "refresh_token",
client_id: CONFIG.clientId,
refresh_token: refreshToken,
});
const res = await httpPostWithRetry(
`https://${CONFIG.cognitoAuthDomain}/oauth2/token`,
body,
{ headers },
CONFIG.logger
).catch((err) => {
throw new Error(`Failed to refresh tokens: ${err}`);
});
tokens.id_token = res.data.id_token;
tokens.access_token = res.data.access_token;
cookieHeadersEventType = "newTokens";
} catch (err) {
cookieHeadersEventType = "refreshFailed";
}
const response = {
status: "307",
statusDescription: "Temporary Redirect",
headers: {
location: [
{
key: "location",
value: redirectedFromUri,
},
],
"set-cookie": generateCookieHeaders[cookieHeadersEventType]({
tokens,
domainName,
...CONFIG,
}),
...CONFIG.cloudFrontHeaders,
},
};
CONFIG.logger.debug("Returning response:\n", response);
return response;
} catch (err) {
const response = {
body: createErrorHtml({
title: "Refresh issue",
message: "We can't refresh your sign-in because of a",
expandText: "technical problem",
details: err.toString(),
linkUri: redirectedFromUri,
linkText: "Try again",
}),
status: "200",
headers: {
...CONFIG.cloudFrontHeaders,
"content-type": [
{
key: "Content-Type",
value: "text/html; charset=UTF-8",
},
],
},
};
CONFIG.logger.debug("Returning response:\n", response);
return response;
}
};
function validateRefreshRequest(
currentNonce?: string | string[],
originalNonce?: string,
idToken?: string,
accessToken?: string,
refreshToken?: string
) {
if (!originalNonce) {
throw new Error(
"Your browser didn't send the nonce cookie along, but it is required for security (prevent CSRF)."
);
} else if (currentNonce !== originalNonce) {
throw new Error("Nonce mismatch");
}
Object.entries({ idToken, accessToken, refreshToken }).forEach(
([tokenType, token]) => {
if (!token) {
throw new Error(`Missing ${tokenType}`);
}
}
);
}