-
Notifications
You must be signed in to change notification settings - Fork 528
/
server.js
186 lines (156 loc) · 4.43 KB
/
server.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
const bodyParser = require('body-parser')
const config = require('./config')
const express = require('express')
const ExpressBrute = require('express-brute')
const GithubWebHook = require('express-github-webhook')
const objectPath = require('object-path')
class StaticmanAPI {
constructor () {
this.controllers = {
connect: require('./controllers/connect'),
encrypt: require('./controllers/encrypt'),
auth: require('./controllers/auth'),
handlePR: require('./controllers/handlePR'),
home: require('./controllers/home'),
process: require('./controllers/process')
}
this.server = express()
this.server.use(bodyParser.json())
this.server.use(bodyParser.urlencoded({
extended: true
// type: '*'
}))
this.initialiseWebhookHandler()
this.initialiseCORS()
this.initialiseBruteforceProtection()
this.initialiseRoutes()
}
initialiseBruteforceProtection () {
const store = new ExpressBrute.MemoryStore()
this.bruteforce = new ExpressBrute(store)
}
initialiseCORS () {
this.server.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
}
initialiseRoutes () {
// Route: connect
this.server.get(
'/v:version/connect/:username/:repository',
this.bruteforce.prevent,
this.requireApiVersion([1, 2]),
this.controllers.connect
)
// Route: process
this.server.post(
'/v:version/entry/:username/:repository/:branch',
this.bruteforce.prevent,
this.requireApiVersion([1, 2]),
this.requireParams(['fields']),
this.controllers.process
)
this.server.post(
'/v:version/entry/:username/:repository/:branch/:property',
this.bruteforce.prevent,
this.requireApiVersion([2]),
this.requireParams(['fields']),
this.controllers.process
)
this.server.post(
'/v:version/entry/:service/:username/:repository/:branch/:property',
this.bruteforce.prevent,
this.requireApiVersion([3]),
this.requireService(['github', 'gitlab']),
this.requireParams(['fields']),
this.controllers.process
)
// Route: encrypt
this.server.get(
'/v:version/encrypt/:text',
this.bruteforce.prevent,
this.requireApiVersion([2, 3]),
this.controllers.encrypt
)
// Route: oauth
this.server.get(
'/v:version/auth/:service/:username/:repository/:branch/:property',
this.bruteforce.prevent,
this.requireApiVersion([2, 3]),
this.requireService(['github', 'gitlab']),
this.controllers.auth
)
// Route: root
this.server.get(
'/',
this.controllers.home
)
}
initialiseWebhookHandler () {
const webhookHandler = GithubWebHook({
path: '/v1/webhook'
})
webhookHandler.on('pull_request', this.controllers.handlePR)
this.server.use(webhookHandler)
}
requireApiVersion (versions) {
return (req, res, next) => {
const versionMatch = versions.some(version => {
return version.toString() === req.params.version
})
if (!versionMatch) {
return res.status(400).send({
success: false,
errorCode: 'INVALID_VERSION'
})
}
return next()
}
}
requireService (services) {
return (req, res, next) => {
const serviceMatch = services.some(service => service === req.params.service)
if (!serviceMatch) {
return res.status(400).send({
success: false,
errorCode: 'INVALID_SERVICE'
})
}
return next()
}
}
requireParams (params) {
return function (req, res, next) {
let missingParams = []
params.forEach(param => {
if (
objectPath.get(req.query, param) === undefined &&
objectPath.get(req.body, param) === undefined
) {
missingParams.push(param)
}
})
if (missingParams.length) {
return res.status(500).send({
success: false,
errorCode: 'MISSING_PARAMS',
data: missingParams
})
}
return next()
}
}
start (callback) {
this.instance = this.server.listen(config.get('port'), () => {
if (typeof callback === 'function') {
callback(config.get('port'))
}
})
}
close () {
this.instance.close()
}
}
module.exports = StaticmanAPI