-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy pathprocess.js
142 lines (113 loc) · 3.73 KB
/
process.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
'use strict'
const path = require('path')
const config = require(path.join(__dirname, '/../config'))
const errorHandler = require('../lib/ErrorHandler')
const reCaptcha = require('express-recaptcha')
const Staticman = require('../lib/Staticman')
const universalAnalytics = require('universal-analytics')
function checkRecaptcha (staticman, req) {
return new Promise((resolve, reject) => {
staticman.getSiteConfig().then(siteConfig => {
if (!siteConfig.get('reCaptcha.enabled')) {
return resolve(false)
}
const reCaptchaOptions = req.body.options && req.body.options.reCaptcha
if (!reCaptchaOptions || !reCaptchaOptions.siteKey || !reCaptchaOptions.secret) {
return reject(errorHandler('RECAPTCHA_MISSING_CREDENTIALS'))
}
let decryptedSecret
try {
decryptedSecret = staticman.decrypt(reCaptchaOptions.secret)
} catch (err) {
return reject(errorHandler('RECAPTCHA_CONFIG_MISMATCH'))
}
if (
reCaptchaOptions.siteKey !== siteConfig.get('reCaptcha.siteKey') ||
decryptedSecret !== siteConfig.get('reCaptcha.secret')
) {
return reject(errorHandler('RECAPTCHA_CONFIG_MISMATCH'))
}
reCaptcha.init(reCaptchaOptions.siteKey, decryptedSecret)
reCaptcha.verify(req, err => {
if (err) {
return reject(errorHandler(err))
}
return resolve(true)
})
}).catch(err => reject(err))
})
}
function createConfigObject (apiVersion, property) {
let remoteConfig = {}
if (apiVersion === '1') {
remoteConfig.file = '_config.yml'
remoteConfig.path = 'staticman'
} else {
remoteConfig.file = 'staticman.yml'
remoteConfig.path = property || ''
}
return remoteConfig
}
function process (staticman, req, res) {
const ua = config.get('analytics.uaTrackingId')
? universalAnalytics(config.get('analytics.uaTrackingId'))
: null
const fields = req.query.fields || req.body.fields
const options = req.query.options || req.body.options || {}
return staticman.processEntry(fields, options).then(data => {
sendResponse(res, {
redirect: data.redirect,
fields: data.fields
})
if (ua) {
ua.event('Entries', 'New entry').send()
}
})
}
function sendResponse (res, data) {
const error = data && data.err
const statusCode = error ? 500 : 200
if (!error && data.redirect) {
return res.redirect(data.redirect)
}
if (error && data.redirectError) {
return res.redirect(data.redirectError)
}
let payload = {
success: !error
}
if (error && error._smErrorCode) {
const errorCode = errorHandler.getInstance().getErrorCode(error._smErrorCode)
const errorMessage = errorHandler.getInstance().getMessage(error._smErrorCode)
if (errorMessage) {
payload.message = errorMessage
}
if (error.data) {
payload.data = error.data
}
if (error) {
payload.rawError = error
}
payload.errorCode = errorCode
} else {
payload.fields = data.fields
}
res.status(statusCode).send(payload)
}
module.exports = (req, res, next) => {
const staticman = new Staticman(req.params)
staticman.setConfigPath()
staticman.setIp(req.headers['x-forwarded-for'] || req.connection.remoteAddress)
staticman.setUserAgent(req.headers['user-agent'])
return checkRecaptcha(staticman, req)
.then(usedRecaptcha => process(staticman, req, res))
.catch(err => sendResponse(res, {
err,
redirect: req.body.options && req.body.options.redirect,
redirectError: req.body.options && req.body.options.redirectError
}))
}
module.exports.checkRecaptcha = checkRecaptcha
module.exports.createConfigObject = createConfigObject
module.exports.process = process
module.exports.sendResponse = sendResponse