-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
208 lines (192 loc) · 6.87 KB
/
handler.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const config = require('./config.json');
const captchaSecret = config['captchaSecret'];
const mailTo = config['sendMailTo'];
const request = require('request');
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
const ses = new AWS.SES();
function isURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name and extension
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?'+ // port
'(\\/[-a-z\\d%@_.~+&:]*)*'+ // path
'(\\?[;&a-z\\d%@_.,~+&:=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return pattern.test(str);
}
module.exports.processCommentRequest = (event, context, callback) => {
// response to the user always uses this template
var end = (resp) => {
const response = {
status: '200',
statusDescription: 'OK',
headers: {
vary: [{
key: 'Vary',
value: '*',
}],
'last-modified': [{
key: 'Last-Modified',
value: '2018-09-19',
}],
'Content-Type': [{
key: 'Content-Type',
value: 'application/json',
}],
},
body: JSON.stringify(resp),
};
callback(null, response);
}
console.log(JSON.stringify(event, null, 2));
console.log(JSON.stringify(context, null, 2));
var ddb = new AWS.DynamoDB({apiVersion: '2012-10-08'});
var time = Date.now();
// Creating a new comment
if (event.method == 'POST') {
var name;
var page;
var website;
var captcha;
var comment;
var params;
function sanitizeBodyData(){
var missing = new Array();
name = event.body['name'];
if (name === undefined || name === "" || name.length > 30) {
if( name.length > 30) end({'result': 'error', 'reason': `Name must be less than 30 chars`})
missing.push('name');
}
website = event.body['website'];
if (website === undefined || website === "") {
website = 'null';
}
if (website != 'null'){
if (!isURL(website)){
console.log("bad url: ", website)
missing.push('website');
}
}
page = event.body['article'];
if (page === undefined || page === "") {
missing.push('article');
}
comment = event.body['comment'];
if (comment === undefined || comment === "" ) {
if( comment.length > 500) end({'result': 'error', 'reason': `Comment must be less than 500 chars`})
missing.push('comment');
}
captcha = event.body['g-recaptcha-response'];
if(event.body['g-recaptcha-response'] === undefined || event.body['g-recaptcha-response'] === '' || event.body['g-recaptcha-response'] === null) {
missing.push('reCAPTCHA');
}
console.log(missing);
// If missing the name or comment, return an error
if (missing.length != 0) {
end({'result': 'error', 'reason': `missing fields: ${missing}`});
}
// Build the putitem object for ddb
params = {
TableName: config['dynamoTable'],
Item: {
'ts' : {N: time.toString()},
'name' : {S: name},
'comment': {S: comment},
'page': {S: page},
'website': {S: website},
},
ConditionExpression: 'attribute_not_exists(ts)'
};
}
function checkArticleNot404(){
return new Promise(function (resolve, reject){
request(`${config['baseUrl']}${page}`,function(error, response, body){
if (response.statusCode == 200){
resolve(true);
}else{
end({'result': 'error', 'reason': `blog does not exist`});
}
});
});
}
function checkCaptcha(){
return new Promise(function (resolve, reject) {
var url = "https://www.google.com/recaptcha/api/siteverify?secret=" + captchaSecret + "&response=" + captcha;
request(url,function(error,response,body) {
body = JSON.parse(body);
if (!error && response.statusCode == 200 && body.success == true){
resolve(true);
}else{
end({result: "error", reason: "Bad captcha"});
}
});
});
}
function sendMail(){
name = name.trim();
var subject = "New comment on blog " + page;
var message = "Comment from " + name + "\n\n" + comment.trim();
ses.sendEmail({
Destination: {
ToAddresses: [
'Alden Jenkins <'+ config['sendMailTo'] + '>'
]
},
Message: {
Body: {
Text: {
Data: message,
Charset: 'UTF-8'
}
},
Subject: {
Data: subject,
Charset: 'UTF-8'
}
},
Source: 'Comment bot 2.fun <' + config['sendMailTo'] + '>',
}, (err, data) => {
if (err) {
// email was not sent
console.log('Error Sending Email:', JSON.stringify(err, null, 2));
end({result: "error", reason: "couldnt send email to owner"});
}
});
}
(async () => {
await Promise.all([sanitizeBodyData(), checkArticleNot404(), checkCaptcha()]);
// Call DynamoDB to add the item to the table
sendMail();
ddb.putItem(params, function(err, data) {
if (err) {
//console.log("Error", err);
end({result: "error", reason: err});
} else {
//console.log("Success", data);
end({result: "ok", reason: data});
}
});
})()
// IT'S A GET, so get the comments
} else if (event.method == 'GET') {
// The query to send to ddb
var params = {
ExpressionAttributeValues: {
":v1": {
S: event.path.id
}
},
KeyConditionExpression: `page = :v1`,
TableName: config['dynamoTable']
};
ddb.query(params, function(err, data) {
if (err) {
end({result: "error", reason: err});
} else {
end(data);
}
});
}
};