-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloudFormationCognitoUserPoolIdentityProvider.js
84 lines (74 loc) · 3.71 KB
/
CloudFormationCognitoUserPoolIdentityProvider.js
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
const AWS = require('aws-sdk');
exports.handler = async (event) => {
try {
var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
switch (event.RequestType) {
case 'Create':
console.info(`CFE-Cognito-UserPoolFederation ${event.RequestType} - IN PROGRESS`);
await cognitoIdentityServiceProvider.createIdentityProvider({
UserPoolId: event.ResourceProperties.UserPoolId,
ProviderName: event.ResourceProperties.ProviderName,
ProviderType: event.ResourceProperties.ProviderType,
ProviderDetails: event.ResourceProperties.ProviderDetails,
AttributeMapping: event.ResourceProperties.AttributeMapping,
}).promise();
break;
case 'Update':
console.info(`CFE-Cognito-UserPoolFederation ${event.RequestType} - IN PROGRESS`);
await deleteIdentityProvider(cognitoIdentityServiceProvider,
event.OldResourceProperties.UserPoolId,
event.OldResourceProperties.ProviderName);
await cognitoIdentityServiceProvider.createIdentityProvider({
UserPoolId: event.ResourceProperties.UserPoolId,
ProviderName: event.ResourceProperties.ProviderName,
ProviderType: event.ResourceProperties.ProviderType,
ProviderDetails: event.ResourceProperties.ProviderDetails,
AttributeMapping: event.ResourceProperties.AttributeMapping
}).promise();
break;
case 'Delete':
console.info(`CFE-Cognito-UserPoolFederation ${event.RequestType} - IN PROGRESS`);
await deleteIdentityProvider(cognitoIdentityServiceProvider,
event.ResourceProperties.UserPoolId,
event.ResourceProperties.ProviderName);
break;
}
await sendCloudFormationResponse(event, 'SUCCESS');
console.info(`CFE-Cognito-UserPoolFederation ${event.RequestType} - SUCCESS`);
} catch (error) {
console.error(`CFE-Cognito-UserPoolFederation ${event.RequestType} - FAILED:`, error);
await sendCloudFormationResponse(event, 'FAILED', event);
}
}
async function deleteIdentityProvider(cognitoIdentityServiceProvider, userPoolId, providerName) {
var response = await cognitoIdentityServiceProvider.describeIdentityProvider({
UserPoolId: userPoolId,
ProviderName: providerName
}).promise();
if (response.IdentityProvider.UserPoolId) {
await cognitoIdentityServiceProvider.deleteIdentityProvider({
UserPoolId: response.IdentityProvider.UserPoolId,
ProviderName: providerName
}).promise();
}
}
async function sendCloudFormationResponse(event, responseStatus, responseData) {
var params = {
FunctionName: 'CloudFormationSendResponse',
InvocationType: 'RequestResponse',
Payload: JSON.stringify({
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
ResponseURL: event.ResponseURL,
ResponseStatus: responseStatus,
ResponseData: responseData
})
};
var lambda = new AWS.Lambda();
var response = await lambda.invoke(params).promise();
if (response.FunctionError) {
var responseError = JSON.parse(response.Payload);
throw new Error(responseError.errorMessage);
}
}