forked from aws-samples/cloudfront-authorization-at-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
246 lines (222 loc) · 7.58 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
This is a CloudFormation custom resource. It's purpose is to:
- Update a User Pool Client's redirect URL's
We need to do this in a custom resource, to support the scenario of updating a pre-existing User Pool Client
*/
import {
CloudFormationCustomResourceHandler,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
} from "aws-lambda";
import CognitoIdentityServiceProvider from "aws-sdk/clients/cognitoidentityserviceprovider";
import { sendCfnResponse, Status } from "./cfn-response";
const CUSTOM_RESOURCE_CURRENT_VERSION_NAME = "UpdatedUserPoolClientV2";
const SENTINEL = "https://example.com/will-be-replaced";
async function getUserPoolClient(props: Props) {
const userPoolId = props.UserPoolArn.split("/")[1];
const userPoolRegion = props.UserPoolArn.split(":")[3];
const cognitoClient = new CognitoIdentityServiceProvider({
region: userPoolRegion,
});
const input = {
ClientId: props.UserPoolClientId,
UserPoolId: userPoolId,
};
console.debug("Describing User Pool Client", JSON.stringify(input, null, 4));
const { UserPoolClient } = await cognitoClient
.describeUserPoolClient(input)
.promise();
if (!UserPoolClient) {
throw new Error("User Pool Client not found!");
}
return UserPoolClient;
}
async function updateUserPoolClient(
props: Props,
redirectUrisSignIn: string[],
redirectUrisSignOut: string[],
existingUserPoolClient: CognitoIdentityServiceProvider.UserPoolClientType
) {
const userPoolId = props.UserPoolArn.split("/")[1];
const userPoolRegion = props.UserPoolArn.split(":")[3];
const cognitoClient = new CognitoIdentityServiceProvider({
region: userPoolRegion,
});
// To be able to set the redirect URL's, we must enable OAuth––required by Cognito
// Vice versa, when removing redirect URL's, we must disable OAuth if there's no more redirect URL's left
// Merge OAuth scopes and flows with what is already there on the existing User Pool Client
let AllowedOAuthFlows = ["code"];
let AllowedOAuthFlowsUserPoolClient = true;
let AllowedOAuthScopes = props.OAuthScopes;
const CallbackURLs = [...new Set(redirectUrisSignIn)].filter(
(uri) => uri !== SENTINEL
);
const LogoutURLs = [...new Set(redirectUrisSignOut)].filter(
(uri) => uri !== SENTINEL
);
// If there's no redirect URI's -- switch off OAuth (to avoid a Cognito exception)
if (!CallbackURLs.length) {
AllowedOAuthFlows = [];
AllowedOAuthFlowsUserPoolClient = false;
AllowedOAuthScopes = [];
}
const input: CognitoIdentityServiceProvider.Types.UpdateUserPoolClientRequest =
{
AllowedOAuthFlows,
AllowedOAuthFlowsUserPoolClient,
AllowedOAuthScopes,
ClientId: props.UserPoolClientId,
UserPoolId: userPoolId,
CallbackURLs,
LogoutURLs,
SupportedIdentityProviders:
existingUserPoolClient.SupportedIdentityProviders, // Need to provide existing values otherwise they get cleared out :|
};
console.debug("Updating User Pool Client", JSON.stringify(input, null, 4));
await cognitoClient.updateUserPoolClient(input).promise();
}
async function undoPriorUpdate(
props: Props,
redirectUrisSignInToRemove: string[],
redirectUrisSignOutToRemove: string[]
) {
// Get existing callback URL's
const existingUserPoolClient = await getUserPoolClient(props);
const existingRedirectUrisSignIn = existingUserPoolClient.CallbackURLs || [];
const existingRedirectUrisSignOut = existingUserPoolClient.LogoutURLs || [];
// Remove the callback URL's we added to the list earlier
const redirectUrisSignInToKeep = existingRedirectUrisSignIn.filter(
(uri) => !redirectUrisSignInToRemove.includes(uri)
);
const redirectUrisSignOutToKeep = existingRedirectUrisSignOut.filter(
(uri) => !redirectUrisSignOutToRemove.includes(uri)
);
await updateUserPoolClient(
props,
redirectUrisSignInToKeep,
redirectUrisSignOutToKeep,
existingUserPoolClient
);
}
async function doNewUpdate(
props: Props,
redirectUrisSignIn: string[],
redirectUrisSignOut: string[]
) {
// Get existing callback URL's
const existingUserPoolClient = await getUserPoolClient(props);
const existingRedirectUrisSignIn = existingUserPoolClient?.CallbackURLs || [];
const existingRedirectUrisSignOut = existingUserPoolClient?.LogoutURLs || [];
// Add new callback url's
const redirectUrisSignInToSet = [
...existingRedirectUrisSignIn,
...redirectUrisSignIn,
];
const redirectUrisSignOutToSet = [
...existingRedirectUrisSignOut,
...redirectUrisSignOut,
];
await updateUserPoolClient(
props,
redirectUrisSignInToSet,
redirectUrisSignOutToSet,
existingUserPoolClient
);
}
interface Props {
UserPoolArn: string;
UserPoolClientId: string;
OAuthScopes: string[];
CloudFrontDistributionDomainName: string;
RedirectPathSignIn: string;
RedirectPathSignOut: string;
AlternateDomainNames: string[];
}
function getRedirectUris(props: Props) {
const redirectDomains = [
props.CloudFrontDistributionDomainName,
...props.AlternateDomainNames,
].filter((domain) => !!domain);
const redirectUrisSignIn = redirectDomains.map(
(domain) => `https://${domain}${props.RedirectPathSignIn}`
);
const redirectUrisSignOut = redirectDomains.map(
(domain) => `https://${domain}${props.RedirectPathSignOut}`
);
return { redirectUrisSignIn, redirectUrisSignOut };
}
async function updateCognitoUserPoolClient(
requestType: "Create" | "Update" | "Delete",
currentProps: Props,
oldProps?: Props,
physicalResourceId?: string
) {
const currentUris = getRedirectUris(currentProps);
if (requestType === "Create") {
await doNewUpdate(
currentProps,
currentUris.redirectUrisSignIn,
currentUris.redirectUrisSignOut
);
} else if (requestType === "Update") {
if (physicalResourceId === CUSTOM_RESOURCE_CURRENT_VERSION_NAME) {
const priorUris = getRedirectUris(oldProps!);
await undoPriorUpdate(
oldProps!,
priorUris.redirectUrisSignIn,
priorUris.redirectUrisSignOut
);
}
await doNewUpdate(
currentProps,
currentUris.redirectUrisSignIn,
currentUris.redirectUrisSignOut
);
} else if (requestType === "Delete") {
if (physicalResourceId === CUSTOM_RESOURCE_CURRENT_VERSION_NAME) {
await undoPriorUpdate(
currentProps,
currentUris.redirectUrisSignIn,
currentUris.redirectUrisSignOut
);
}
}
return {
RedirectUrisSignIn: currentUris.redirectUrisSignIn.join(","),
RedirectUrisSignOut: currentUris.redirectUrisSignOut.join(","),
};
}
export const handler: CloudFormationCustomResourceHandler = async (event) => {
console.log(JSON.stringify(event, undefined, 4));
const { ResourceProperties, RequestType } = event;
const { PhysicalResourceId } = event as
| CloudFormationCustomResourceDeleteEvent
| CloudFormationCustomResourceUpdateEvent;
const { OldResourceProperties } =
event as CloudFormationCustomResourceUpdateEvent;
let status = Status.SUCCESS;
let physicalResourceId: string | undefined;
let data: { [key: string]: any } | undefined;
let reason: string | undefined;
try {
data = await updateCognitoUserPoolClient(
RequestType,
ResourceProperties as unknown as Props,
OldResourceProperties as unknown as Props,
PhysicalResourceId
);
} catch (err) {
console.error(err);
status = Status.FAILED;
reason = err;
}
await sendCfnResponse({
event,
status,
data,
physicalResourceId,
reason,
});
};