-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (168 loc) · 7.45 KB
/
index.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
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
import {
AdminEnableUserCommand,
AdminGetUserCommand,
ResendConfirmationCodeCommand,
SignUpCommand,
CognitoIdentityProviderClient
} from "@aws-sdk/client-cognito-identity-provider";
import { SendEmailCommand, SESClient } from "@aws-sdk/client-ses";
import {
SQSClient,
SendMessageCommand
} from "@aws-sdk/client-sqs";
const AWS_REGION = process.env.REGION;
const CLIENT_ID = process.env.CLIENT_ID;
const USERPOOL_ID = process.env.USERPOOL_ID;
const ENV = process.env.ENV;
const cognitoClient = new CognitoIdentityProviderClient({ region: AWS_REGION });
const sesClient = new SESClient({ region: AWS_REGION });
const sqsClient = new SQSClient({ region: AWS_REGION });
const URLS = {
createAppUserQueue: `https://sqs.us-west-1.amazonaws.com/213993515054/CreateAppUser-${ENV}`,
adminDisableUserCommandQueue: `https://sqs.us-west-1.amazonaws.com/213993515054/AdminDisableUserCommand-${ENV}`
}
/**
* @type {import('@types/aws-lambda').APIGatewayProxyHandler}
*/
export const handler = async (event) => {
const { requestContext, body } = event;
const { http } = requestContext;
const { sourceIp, userAgent } = http;
const { firstName, lastName, email, password, consentsToTermsAndConditions } = JSON.parse(body);
const trimmedEmail = email.trim().toLowerCase();
let cognitoUser
// create cognito user
try {
const signupCommand = new SignUpCommand({
ClientId: CLIENT_ID,
Username: trimmedEmail,
Password: password
})
cognitoUser = await cognitoClient.send(signupCommand);
} catch (error) {
console.error(`creating cognito user: ${error.message}`)
if (error.name === "UsernameExistsException") {
// NOTE: there are two *likely* scenarios here:
// 1. user didn't confirm their account first time through sign up process
// 2. someone is trying to create an account that already exists (maybe malicious, maybe not)
const adminGetUserCommand = new AdminGetUserCommand({
Username: email,
UserPoolId: USERPOOL_ID
})
const cognitoUser = await cognitoClient.send(adminGetUserCommand);
if (cognitoUser.Enabled) {
// send email saying someone tried to create account but account already exists
const electronicMail = new SendEmailCommand({
Destination: {
CcAddresses: [],
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: "Someone is trying to register an account at EasyLegal.app with your email address."
},
Text: {
Charset: "UTF-8",
Data: "Someone is trying to register an account at EasyLegal.app with your email address.",
},
},
Subject: {
Charset: "UTF-8",
Data: "EasyLegal.app Alert",
},
},
Source: "[email protected]",
ReplyToAddresses: []
})
try {
await sesClient.send(electronicMail)
} catch (error) {
console.error("SENDING MAIL:", error)
}
// return 202 response, don't compromise existing user's privacy by revealing publicly that account already exists
return {
statusCode: 202,
body: JSON.stringify({ message: "Account created" })
}
} else {
// enable user (so that you can resend confirmation code)
try {
const adminEnableUserCommand = new AdminEnableUserCommand({
UserPoolId: USERPOOL_ID,
Username: trimmedEmail
});
await cognitoClient.send(adminEnableUserCommand);
} catch (error) {
console.error("enabling cognito user:", error.message)
return { statusCode: 400 }
}
// resend confirmation code
const resendConfirmationCodeCommand = new ResendConfirmationCodeCommand({
ClientId: CLIENT_ID,
Username: email,
})
await cognitoClient.send(resendConfirmationCodeCommand)
// Send user info to AdminDisableUserCommand
try {
const adminDisableUserCommandInput = {
QueueUrl: URLS.adminDisableUserCommandQueue,
MessageBody: JSON.stringify({ email: trimmedEmail })
}
const adminDisableUserCommandCommand = new SendMessageCommand(adminDisableUserCommandInput)
await sqsClient.send(adminDisableUserCommandCommand)
} catch (error) {
console.error(`sending to AdminDisableUserCommand queue: ${error.message}`)
return { statusCode: 400 }
}
// pretend everything is fine
// NOTE: if there is any discrepancy between original data submitted and presently-submitted data,
// original data will be kept and presently-submitted data will be discarded. This MAY need to be changed.
return {
statusCode: 202,
body: JSON.stringify({ message: "Account created" })
}
}
}
return {
statusCode: 400,
body: JSON.stringify({
error: {
field: "submit",
errorMessage: "We are not accepting new customers at this time. Please try again later."
}
})
};
}
// Send user info to CreateAppUser
try {
// NOTE: JSON.stringfy can't handle ES6 object property shorthand -> define data object outside JSON.stringify
const data = { cognitoUser, sourceIp, userAgent, firstName, lastName, email, consentsToTermsAndConditions }
const createAppUserInput = {
QueueUrl: URLS.createAppUserQueue,
MessageBody: JSON.stringify(data)
}
const createAppUserCommand = new SendMessageCommand(createAppUserInput)
await sqsClient.send(createAppUserCommand)
} catch (error) {
console.error(`sending to createAppUser queue: ${error.message}`)
return { statusCode: 400 }
}
// Send user info to AdminDisableUserCommand
try {
const adminDisableUserCommandInput = {
QueueUrl: URLS.adminDisableUserCommandQueue,
MessageBody: JSON.stringify({ email: trimmedEmail })
}
const adminDisableUserCommandCommand = new SendMessageCommand(adminDisableUserCommandInput)
await sqsClient.send(adminDisableUserCommandCommand)
} catch (error) {
console.error(`sending to AdminDisableUserCommand queue: ${error.message}`)
return { statusCode: 400 }
}
return {
statusCode: 202,
body: JSON.stringify({ message: "Account being provisioned." })
};
};