forked from thestackshack/serverless-contact-us-form
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
84 lines (79 loc) · 3.54 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
var https = require('https');
var querystring = require('querystring');
var AWS = require("aws-sdk");
exports.handler = function (event, context, callback) {
// Validate the recaptcha
var input_data = JSON.parse(event.body);
var postData = querystring.stringify({
'secret': process.env.ReCaptchaSecret,
'response': input_data['g-recaptcha-response']
});
var options = {
hostname: 'www.google.com',
port: 443,
path: '/recaptcha/api/siteverify',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
var captchaResponse = JSON.parse(chunk);
if (captchaResponse.success) {
var sns = new AWS.SNS();
delete input_data['g-recaptcha-response'];
var message = "";
Object.keys(input_data).forEach(function(key) {
message += key+':\n';
message += '\t'+input_data[key]+'\n\n';
});
var params = {
Message: message,
Subject: process.env.Subject,
TopicArn: process.env.ContactUsSNSTopic
};
sns.publish(params, function (err, response) {
callback(null, {
statusCode: '200',
headers: {
"Access-Control-Allow-Methods" : "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Headers" : "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify(response)
});
});
} else {
callback(null, {
statusCode: '500',
headers: {
"Access-Control-Allow-Methods" : "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Headers" : "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({message:'Invalid recaptcha'})
});
}
});
});
req.on('error', function(e) {
callback(null, {
statusCode: '500',
headers: {
"Access-Control-Allow-Methods" : "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Headers" : "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({message:e.message})
});
});
// write data to request body
req.write(postData);
req.end();
};