forked from aws-samples/cloudfront-authorization-at-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
163 lines (153 loc) · 4.57 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
// 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;
try {
const { requestedUri, nonce: currentNonce } = parseQueryString(
request.querystring
);
const {
idToken,
refreshToken,
nonce: originalNonce,
nonceHmac,
} = common.extractAndParseCookies(
request.headers,
CONFIG.clientId,
CONFIG.cookieCompatibility
);
validateRefreshRequest(
currentNonce,
nonceHmac,
originalNonce,
idToken,
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: 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}`);
});
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", response);
return response;
} catch (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", 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}`
);
}
}