-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathindex.ts
198 lines (188 loc) · 5.95 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2021 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 * as common from "../shared/shared";
const CONFIG = common.getCompleteConfig();
CONFIG.logger.debug("Configuration loaded:", CONFIG);
export const handler: CloudFrontRequestHandler = async (event) => {
CONFIG.logger.debug("Event:", event);
const request = event.Records[0].cf.request;
const domainName = request.headers["host"][0].value;
let requestedUri: string | string[] | undefined = "/";
let idToken: string | undefined = undefined;
try {
const querySting = parseQueryString(request.querystring);
requestedUri = querySting.requestedUri;
const cookies = common.extractAndParseCookies(
request.headers,
CONFIG.clientId,
CONFIG.cookieCompatibility
);
idToken = cookies.idToken;
validateRefreshRequest(
querySting.nonce,
cookies.nonceHmac,
cookies.nonce,
cookies.idToken,
cookies.refreshToken
);
const 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 newIdToken: string | undefined;
let newAccessToken: string | undefined;
const body = stringifyQueryString({
grant_type: "refresh_token",
client_id: CONFIG.clientId,
refresh_token: cookies.refreshToken,
});
const res = await common
.httpPostToCognitoWithRetry(
`https://${CONFIG.cognitoAuthDomain}/oauth2/token`,
Buffer.from(body),
{ headers },
CONFIG.logger
)
.catch((err) => {
throw new Error(`Failed to refresh tokens: ${err}`);
});
CONFIG.logger.info("Successfully renewed tokens");
newIdToken = res.data.id_token as string;
newAccessToken = res.data.access_token as string;
const response = {
status: "307",
statusDescription: "Temporary Redirect",
headers: {
location: [
{
key: "location",
value: `https://${domainName}${common.ensureValidRedirectPath(
requestedUri
)}`,
},
],
"set-cookie": common.generateCookieHeaders.refresh({
...CONFIG,
tokens: { id: newIdToken, access: newAccessToken },
}),
...CONFIG.cloudFrontHeaders,
},
};
CONFIG.logger.debug("Returning response:\n", JSON.stringify(response));
return response;
} catch (err) {
if (
err instanceof Error &&
err.message.includes("invalid_grant") &&
idToken
) {
// The refresh token has likely expired.
// We'll clear the refresh token cookie, so that CheckAuth won't redirect any more requests here in vain.
// Also, we'll redirect the user to where he/she came from.
// (From there CheckAuth will redirect the user to the Cognito hosted UI to sign in)
CONFIG.logger.info(
"Expiring refresh token cookie, as the refresh token has expired"
);
const response = {
status: "307",
statusDescription: "Temporary Redirect",
headers: {
location: [
{
key: "location",
value: `https://${domainName}${common.ensureValidRedirectPath(
requestedUri
)}`,
},
],
"set-cookie": common.generateCookieHeaders.refreshFailed({
tokens: {
id: idToken,
},
...CONFIG,
}),
...CONFIG.cloudFrontHeaders,
},
};
CONFIG.logger.debug("Returning response:\n", JSON.stringify(response));
return response;
}
CONFIG.logger.error(err);
const response = {
body: common.createErrorHtml({
title: "Refresh issue",
message: "We can't refresh your sign-in automatically because of a",
expandText: "technical problem",
details: `${err}`,
linkUri: `https://${domainName}${CONFIG.signOutUrl}`,
linkText: "Sign in",
}),
status: "200",
headers: {
...CONFIG.cloudFrontHeaders,
"content-type": [
{
key: "Content-Type",
value: "text/html; charset=UTF-8",
},
],
},
};
CONFIG.logger.debug("Returning response:\n", JSON.stringify(response));
return response;
}
};
function validateRefreshRequest(
currentNonce?: string | string[],
nonceHmac?: string,
originalNonce?: string,
idToken?: 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)."
);
}
if (currentNonce !== originalNonce) {
throw new Error("Nonce mismatch");
}
if (!idToken) {
throw new Error("Missing ID token");
}
if (!refreshToken) {
throw new Error("Missing refresh token");
}
// Nonce should not be too old
const nonceTimestamp = parseInt(
currentNonce.slice(0, currentNonce.indexOf("T"))
);
if (common.timestampInSeconds() - nonceTimestamp > CONFIG.nonceMaxAge) {
throw new common.RequiresConfirmationError(
`Nonce is too old (nonce is from ${new Date(
nonceTimestamp * 1000
).toISOString()})`
);
}
// Nonce should have the right signature: proving we were the ones generating it (and e.g. not malicious JS on a subdomain)
const calculatedHmac = common.sign(
currentNonce,
CONFIG.nonceSigningSecret,
CONFIG.nonceLength
);
if (calculatedHmac !== nonceHmac) {
throw new common.RequiresConfirmationError(
`Nonce signature mismatch! Expected ${calculatedHmac} but got ${nonceHmac}`
);
}
}