backend-manager 2.5.23 → 2.5.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fetch = require('wonderful-fetch');
|
|
3
|
+
const {get,set,merge} = require('lodash');
|
|
4
|
+
|
|
5
|
+
function Module() {
|
|
6
|
+
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
Module.prototype.main = function () {
|
|
10
|
+
const self = this;
|
|
11
|
+
const Manager = self.Manager;
|
|
12
|
+
const Api = self.Api;
|
|
13
|
+
const assistant = self.assistant;
|
|
14
|
+
const payload = self.payload;
|
|
15
|
+
|
|
16
|
+
return new Promise(async function(resolve, reject) {
|
|
17
|
+
payload.data.payload.id = payload.data.payload.id
|
|
18
|
+
payload.data.payload.email = payload.data.payload.email
|
|
19
|
+
|
|
20
|
+
const DEFAULT = {
|
|
21
|
+
spamFilter: {
|
|
22
|
+
ip: 3,
|
|
23
|
+
email: 3,
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!payload.data.payload.id) {
|
|
28
|
+
return reject(assistant.errorManager(`Parameter {id} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
29
|
+
} else if (!payload.data.payload.email) {
|
|
30
|
+
return reject(assistant.errorManager(`Parameter {email} is required.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let emailPayload
|
|
34
|
+
try {
|
|
35
|
+
emailPayload = merge({}, DEFAULT, require(path.join(__dirname, 'emails', `${payload.data.payload.id}.js`))());
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return reject(assistant.errorManager(`${payload.data.payload.id} is not a valid email ID.`, {code: 400, sentry: false, send: false, log: false}).error)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const storage = Manager.storage({temporary: true});
|
|
41
|
+
const ipPath = ['api:general:send-email', 'ips', assistant.request.ip];
|
|
42
|
+
const emailPath = ['api:general:send-email', 'emails', payload.data.payload.email];
|
|
43
|
+
|
|
44
|
+
const ipData = storage.get(ipPath).value() || {};
|
|
45
|
+
const emailData = storage.get(emailPath).value() || {};
|
|
46
|
+
|
|
47
|
+
ipData.count = (ipData.count || 0) + 1;
|
|
48
|
+
ipData.firstRequestTime = ipData.firstRequestTime ? ipData.firstRequestTime : new Date().toISOString();
|
|
49
|
+
ipData.lastRequestTime = new Date().toISOString();
|
|
50
|
+
|
|
51
|
+
emailData.count = (emailData.count || 0) + 1;
|
|
52
|
+
emailData.firstRequestTime = emailData.firstRequestTime ? emailData.firstRequestTime : new Date().toISOString();
|
|
53
|
+
emailData.lastRequestTime = new Date().toISOString();
|
|
54
|
+
|
|
55
|
+
storage.set(ipPath, ipData).write();
|
|
56
|
+
storage.set(emailPath, emailData).write();
|
|
57
|
+
|
|
58
|
+
assistant.log('Storage', storage.getState()['api:general:send-email']);
|
|
59
|
+
|
|
60
|
+
if (ipData.count >= emailPayload.spamFilter.ip || emailData.count >= emailPayload.spamFilter.email) {
|
|
61
|
+
self.assistant.errorManager(`Spam filter triggered ip=${ipData.count}, email=${emailData.count}`, {code: 429, sentry: false, send: false, log: true})
|
|
62
|
+
return resolve({data: {success: true}});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log('---emailPayload', emailPayload);
|
|
66
|
+
|
|
67
|
+
fetch('https://us-central1-itw-creative-works.cloudfunctions.net/wrapper', {
|
|
68
|
+
method: 'post',
|
|
69
|
+
timeout: 30000,
|
|
70
|
+
tries: 1,
|
|
71
|
+
response: 'json',
|
|
72
|
+
body: {
|
|
73
|
+
service: 'sendgrid',
|
|
74
|
+
command: `client/v4/zones/${doc.cloudflare.zone}/purge_cache`,
|
|
75
|
+
method: 'post',
|
|
76
|
+
body: {
|
|
77
|
+
purge_everything: true
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
.then(result => {
|
|
82
|
+
console.log('---result', result);
|
|
83
|
+
return resolve({
|
|
84
|
+
data: {
|
|
85
|
+
success: true,
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
})
|
|
89
|
+
.catch(e => {
|
|
90
|
+
return reject(assistant.errorManager(`Error sending email: ${e}`, {code: 500, sentry: true, send: false, log: false}).error)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// return resolve({data: {success: true}});
|
|
94
|
+
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
module.exports = Module;
|
|
101
|
+
|
|
@@ -69,7 +69,7 @@ Module.prototype.main = function () {
|
|
|
69
69
|
redirectUrl: payload.data.payload.redirect_uri,
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
assistant.log('OAuth2 payload', payload.data.payload);
|
|
72
|
+
assistant.log('OAuth2 payload', payload.data.payload, {environment: 'production'});
|
|
73
73
|
|
|
74
74
|
if (!payload.data.payload.provider) {
|
|
75
75
|
return reject(new Error(`The provider parameter is required.`));
|
|
@@ -101,6 +101,8 @@ Module.prototype.main = function () {
|
|
|
101
101
|
newUrl.searchParams.set('response_type', typeof payload.data.payload.response_type === 'undefined' ? 'code' : payload.data.payload.response_type)
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
assistant.log('OAuth2 newUrl', newUrl, {environment: 'production'});
|
|
105
|
+
|
|
104
106
|
await self.oauth2.buildUrl(payload.data.payload.state, newUrl)
|
|
105
107
|
.then(url => {
|
|
106
108
|
if (url) {
|
|
@@ -169,7 +171,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
|
|
|
169
171
|
return new Promise(async function(resolve, reject) {
|
|
170
172
|
const finalUrl = newUrl.toString();
|
|
171
173
|
|
|
172
|
-
assistant.log('Running processState_tokenize()', {environment: '
|
|
174
|
+
assistant.log('Running processState_tokenize()', {environment: 'production'});
|
|
173
175
|
|
|
174
176
|
const body = {
|
|
175
177
|
client_id: _.get(Manager.config, `oauth2.${payload.data.payload.provider}.client_id`),
|
|
@@ -180,7 +182,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
|
|
|
180
182
|
// scope: '',
|
|
181
183
|
};
|
|
182
184
|
|
|
183
|
-
assistant.log('body', body, {environment: '
|
|
185
|
+
assistant.log('body', body, {environment: 'production'});
|
|
184
186
|
|
|
185
187
|
const tokenizeResponse = await fetch(finalUrl, {
|
|
186
188
|
method: 'POST',
|
|
@@ -197,7 +199,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
|
|
|
197
199
|
.then(json => json)
|
|
198
200
|
.catch(e => e)
|
|
199
201
|
|
|
200
|
-
assistant.log('tokenizeResponse', tokenizeResponse, {environment: '
|
|
202
|
+
assistant.log('tokenizeResponse', tokenizeResponse, {environment: 'production'});
|
|
201
203
|
|
|
202
204
|
if (tokenizeResponse instanceof Error) {
|
|
203
205
|
return reject(tokenizeResponse);
|
|
@@ -208,7 +210,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
|
|
|
208
210
|
.then(identity => identity)
|
|
209
211
|
.catch(e => e);
|
|
210
212
|
|
|
211
|
-
assistant.log('verifiedIdentity', verifiedIdentity, {environment: '
|
|
213
|
+
assistant.log('verifiedIdentity', verifiedIdentity, {environment: 'production'});
|
|
212
214
|
|
|
213
215
|
if (verifiedIdentity instanceof Error) {
|
|
214
216
|
return reject(verifiedIdentity);
|
|
@@ -234,7 +236,7 @@ Module.prototype.processState_tokenize = function (newUrl) {
|
|
|
234
236
|
.then(r => r)
|
|
235
237
|
.catch(e => e)
|
|
236
238
|
|
|
237
|
-
assistant.log('storeResponse', storeResponse, {environment: '
|
|
239
|
+
assistant.log('storeResponse', storeResponse, {environment: 'production'});
|
|
238
240
|
|
|
239
241
|
if (storeResponse instanceof Error) {
|
|
240
242
|
return reject(storeResponse);
|