-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper.js
371 lines (348 loc) · 9.88 KB
/
helper.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
'use strict'
const async = require('neo-async')
const jwt = require('jsonwebtoken')
const crypto = require('crypto')
const path = require('path')
const ejs = require('ejs')
const fs = require('fs')
const util = require('chimera-framework/lib/util.js')
const mongo = require('chimera-framework/lib/mongo.js')
const core = require('chimera-framework/lib/core.js')
module.exports = {
hashPassword,
getLoggedInAuth,
getLoggedOutAuth,
getWebConfig,
jwtMiddleware,
injectState,
mongoExecute,
getObjectFromJson,
getParsedNestedJson,
getNormalizedDocId,
getSubObject,
isAuthorized,
injectBaseLayout,
runChain,
loadEjs
}
function runChain (chain, ...args) {
let vars = {$: {runChain, helper: module.exports, cck: require('./cck.js')}}
let callback = args.pop()
core.executeChain(chain, args, vars, callback)
}
function loadEjs (fileName, data) {
let content = fs.readFileSync(fileName, 'utf8')
if (content.indexOf('<%') > -1) {
return ejs.render(content, data).trim()
}
return content
}
function renderContent (responseData, view, viewPath) {
if (fs.existsSync(view)) {
return loadEjs(view, responseData)
} else if (fs.existsSync(path.join(viewPath, view))) {
return loadEjs(path.join(viewPath, view), responseData)
} else {
return ejs.render(view, responseData)
}
}
function injectBaseLayout (state, callback) {
let cck = require('./cck.js')
let renderFieldTemplate = cck.renderFieldTemplate
let render = ejs.render
if (util.isRealObject(state.response.data)) {
state.response.data.renderFieldTemplate = renderFieldTemplate
state.response.data.render = render
state.response.data.isAuthorized = isAuthorized
}
if (util.isNullOrUndefined(state.response.view) || state.response.view === '') {
return callback(null, state)
}
let {response, config, request} = state
let responseData = response.data
let newResponseData
let actions = []
actions.push((next) => {
let content = renderContent(responseData, state.response.view, state.config.viewPath)
newResponseData = {content, partial: {}}
next()
})
for (let partialName in state.config.partial) {
actions.push((next) => {
let partialPath
if (util.isRealObject(state.response.partial) && state.response.partial[partialName]) {
partialPath = ejs.render(state.response.partial[partialName], state.config)
} else {
partialPath = state.config.partial[partialName]
}
if (fs.existsSync(partialPath)) {
newResponseData.partial[partialName] = loadEjs(partialPath, {request, response, renderFieldTemplate, render, config, isAuthorized})
} else {
newResponseData.partial[partialName] = ejs.render(partialPath, {request, response, renderFieldTemplate, render, config, isAuthorized})
}
next()
})
}
return async.parallel(actions, (error, result) => {
state.response.data = newResponseData
state.response.view = state.config.baseLayout
return callback(error, state)
})
}
function isAuthorized (auth, groups) {
if (util.isString(groups)) {
groups = [groups]
}
if (util.isNullOrUndefined(groups) || groups.length === 0) {
return false
}
if ('groups' in auth) {
return hasIntersectionOrEquals(auth.groups, groups)
}
return false
}
function hasIntersectionOrEquals (array1, array2) {
if (array1.length === 0 && array2.length === 0) {
return true
}
for (let element of array1) {
if (array2.indexOf(element) > -1) {
return true
}
}
return false
}
function getSubObject (obj, keys) {
let newObj = {}
if (util.isRealObject(obj)) {
for (let key of keys) {
if (key in obj) {
newObj[key] = obj[key]
}
}
}
return newObj
}
function getObjectFromJson (jsonString) {
try {
return JSON.parse(jsonString)
} catch (error) {
return {}
}
}
function getParsedNestedJson (obj) {
if (util.isString(obj)) {
try {
obj = JSON.parse(obj)
} catch (error) {
// do nothing
}
}
if (util.isRealObject(obj)) {
for (let key in obj) {
if (util.isString(obj[key])) {
try {
obj[key] = JSON.parse(obj[key])
} catch (error) {
// do nothing
}
} else {
obj[key] = getParsedNestedJson(obj[key])
}
}
}
return obj
}
function getNormalizedDocId (docId) {
if (util.isString(docId)) {
if (docId.length === 24) {
return docId
}
return util.getSlicedString(util.getStretchedString(docId, 24, '0'), 24)
}
return null
}
function mongoExecute (collectionName, fn, ...args) {
let webConfig = getWebConfig()
let mongoUrl = webConfig.mongoUrl
let dbConfig = util.isRealObject(collectionName) ? collectionName : {mongoUrl, collectionName}
if (!('mongoUrl' in dbConfig)) {
dbConfig.mongoUrl = mongoUrl
}
dbConfig.verbose = webConfig.verbose
mongo.execute(dbConfig, fn, ...args)
}
function injectState (state, callback) {
let cck = require('./cck.js')
let configDocs, routeDocs
let dbActions = [
(next) => {
mongoExecute('web_configs', 'find', {}, (error, docs) => {
configDocs = docs
next(error, docs)
})
},
(next) => {
mongoExecute('web_routes', 'find', {}, (error, docs) => {
routeDocs = docs
next(error, docs)
})
}
]
async.parallel(dbActions, (error, result) => {
if (error) {
return callback(error, state)
}
// render configuration from database
let configActions = []
for (let doc of configDocs) {
if (state.config.exceptionKeys.indexOf(doc.key) === -1) {
configActions.push((next) => {
state.config[doc.key] = renderConfigValue(doc, state.config)
next()
})
}
}
return async.parallel(configActions, (error, result) => {
if (error) {
return callback(error, state)
}
state.config.exceptionKeys = []
state.config.middlewares = []
state.config.vars = []
let routeActions = []
// render routes
for (let route in state.config.routes) {
routeActions.push((next) => {
route = renderRoute(route, state.config)
next()
})
}
// add rendered cck routes
let cckRoutes = cck.getRoutes()
for (let doc of cckRoutes) {
routeActions.push((next) => {
state.config.routes.push(renderRoute(doc, state.config))
next()
})
}
// add rendered db routes
for (let doc of routeDocs) {
routeActions.push((next) => {
state.config.routes.push(renderRoute(doc, state.config))
next()
})
}
return async.parallel(routeActions, (error, result) => {
// sort routes. If route.method is 'all', it should be at the end, otherwise, sort by route.route
state.config.routes.sort((a, b) => {
let aAll = a.method === 'all' ? 1 : 0
let bAll = b.method === 'all' ? 1 : 0
return (b.route.length - a.route.length) + (aAll - bAll)
})
callback(error, state)
})
})
})
}
function renderConfigValue (doc, webConfig) {
let value = doc.value
if (util.isRealObject(value) || util.isArray(value)) {
let str = JSON.stringify(value)
if (str.indexOf('<%') > -1) {
value = ejs.render(str, webConfig)
value = JSON.parse(value)
}
} else if (util.isString(value) && value.indexOf('<%') > -1) {
value = ejs.render(value, webConfig)
}
return value
}
function renderRoute (doc, config) {
let route = doc.route
let method = doc.method ? doc.method : 'all'
let chain = doc.chain && doc.chain.indexOf('<%') > -1 ? ejs.render(doc.chain, config) : doc.chain
if (!chain) { chain = 'ins: state' }
let groups = 'groups' in doc ? doc.groups : []
let routeObj = {route, method, chain, groups}
if (doc.view) {
routeObj.view = doc.view
}
return routeObj
}
function createRandomString (length = 16) {
return crypto.randomBytes(Math.ceil(length / 2))
.toString('hex')
.slice(0, length)
}
function hashPassword (password, salt = null, algorithm = 'sha512') {
if (util.isNullOrUndefined(salt)) {
salt = createRandomString(16)
}
let hmac = crypto.createHmac(algorithm, salt)
hmac.update(password)
let hashedPassword = hmac.digest('hex')
return {salt, hashedPassword}
}
function getLoggedInAuth (userDoc) {
let auth = util.getDeepCopiedObject(userDoc)
auth.id = auth._id
delete auth['_id']
delete auth['_history']
delete auth['_deleted']
delete auth['_muser']
delete auth['_mtime']
delete auth['password']
delete auth['salt']
delete auth['hashedPassword']
if (!util.isArray(auth.groups)) {
auth.groups = []
}
auth.groups.push('loggedIn')
return auth
}
function getLoggedOutAuth () {
return {
id: '000000000000000000000000',
username: null,
email: null,
groups: ['loggedOut']
}
}
function setCookieToken (webConfig, req, res) {
req.auth = getLoggedOutAuth()
if (req.cookies && req.cookies[webConfig.jwtTokenName]) {
if (!res.cookies) { res.cookies = {} }
res.cookies[webConfig.jwtTokenName] = jwt.sign({}, webConfig.jwtSecret)
}
}
function jwtMiddleware (req, res, next) {
let webConfig = getWebConfig()
let token
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
token = req.headers.authorization.split(' ')[1]
} else if (req.query && req.query[webConfig.jwtTokenName]) {
token = req.query[webConfig.jwtTokenName]
} else if (req.cookies && req.cookies[webConfig.jwtTokenName]) {
token = req.cookies[webConfig.jwtTokenName]
}
try {
if (!util.isNullOrUndefined(token)) {
req.auth = jwt.verify(token, webConfig.jwtSecret)
} else {
setCookieToken(webConfig, req, res)
}
} catch (error) {
setCookieToken(webConfig, req, res)
}
next()
}
function getWebConfig () {
let webConfig
try {
webConfig = require('./webConfig.js')
} catch (error) {
webConfig = require('./webConfig.default.js')
}
return webConfig
}