-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctf.js
271 lines (246 loc) · 8.56 KB
/
ctf.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
const passport = require("passport");
const Competition = require("./models/competition");
module.exports = class CTF {
constructor(config) {
this._before = {};
this._after = {};
// db config
require("./db")
.init(config.db_uri)
.then(
function () {
this.db = require("./db").db;
}.bind(this)
);
this.models = {};
this.models.User = require("./models/user");
this.models.Team = require("./models/team");
this.models.Competition = require("./models/competition");
this.models.Submission = require("./models/submission");
this.models.Challenge = require("./models/challenge");
this.models.Model = require("./models/model");
// passport config
this.passport = passport;
const LocalStrategy = require("passport-local").Strategy;
const JwtStrategy = require("passport-jwt").Strategy;
const User = this.models.User;
this.jwt_secret = config.jwt_secret;
passport.use(
new LocalStrategy({passReqToCallback: true}, function (
req,
username,
password,
done
) {
User.findOne({
username: username,
competition: req.competition,
}).then(function (user) {
if (user && user.authenticate(password)) {
return done(null, user);
} else {
return done(null, false);
}
});
})
);
passport.use(
new JwtStrategy(
{
jwtFromRequest: function (req) {
let token = null;
if (
req &&
req.headers &&
req.headers.authorization &&
req.headers.authorization.split(" ")[0] ===
"Token" &&
req.headers.authorization.split(" ")[1]
) {
token = req.headers.authorization.split(" ")[1];
}
return token;
},
secretOrKey: config.jwt_secret,
},
function (payload, done) {
const query = {id: payload.id};
if (payload.admin === true) {
query.admin = true;
}
if (payload.competition) {
query.competition = payload.competition;
}
User.findOne(query)
.then(function (user) {
if (user) {
return done(null, user);
} else {
return done(null, false);
}
})
.catch(function (err) {
return done(err, false);
});
}
)
);
// server config
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const router = express.Router();
let corsMiddleware;
if (config.cors_origin) {
corsMiddleware = cors({
origin: config.cors_origin,
allowedHeaders: ["Authorization", "Content-Type", "captcha"],
});
}
if (corsMiddleware) {
router.use(corsMiddleware);
router.options(corsMiddleware);
}
if (config.disable_cache) {
this.disableCache = true;
}
const usersRouter = require("./routes/users")(this);
const authRouter = require("./routes/auth")(this);
const teamsRouter = require("./routes/teams")(this);
const challengesRouter = require("./routes/challenges")(this);
const competitionsRouter = require("./routes/competitions")(this);
const adminRouter = require("./routes/admin")(this);
const selfRouter = require("./routes/self")(this);
router.use(
function (req, res, next) {
req.jwt_secret = this.jwt_secret;
next();
}.bind({jwt_secret: this.jwt_secret})
);
router.use(passport.initialize());
router.use(bodyParser.json());
router.use(
"/competitions/:competition/auth",
this._assignCompetition,
authRouter
);
router.use(
"/competitions/:competition/users",
this._assignCompetition,
usersRouter
);
router.use(
"/competitions/:competition/teams",
this._assignCompetition,
teamsRouter
);
router.use(
"/competitions/:competition/challenges",
this._assignCompetition,
challengesRouter
);
router.use("/competitions", competitionsRouter);
router.use(
"/admin",
function (req, res, next) {
req.competition = null;
next();
},
adminRouter
);
router.use("/self", selfRouter);
router.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).json({message: "internal_server_error"});
});
this.router = router;
}
async competitionStarted(req, res, next) {
passport.authenticate("jwt", {session: false}, async function (
err,
user
) {
if (err) {
return res.sendStatus(500);
}
if (req.competition) {
const competition = await Competition.findOne({
id: req.competition,
});
if (
+new Date(competition.start) > +new Date() &&
(!user || user.admin !== true)
) {
res.status(403).json({message: "competition_not_started"});
} else {
next();
}
} else {
res.status(403).json({message: "competition_not_started"});
}
})(req, res, next);
}
async _assignCompetition(req, res, next) {
let competition;
req.competition = req.params.competition;
if (!isNaN(req.competition)) {
competition = await Competition.findOne({id: req.competition});
} else {
competition = undefined;
}
if (competition) {
req.competition = parseInt(req.competition);
next();
} else {
res.status(404).json({message: "competition_not_found"});
}
}
addCompetitionRoute(path, ...middleware) {
this.router.use(
"/competitions/:competition" + path,
this._assignCompetition,
...middleware
);
}
addGlobalRoute(path, ...middleware) {
this.router.use(path, ...middleware);
}
before(event, func) {
if (this._before[event] === undefined) {
this._before[event] = [];
}
this._before[event].push(func);
}
after(event, func) {
if (this._after[event] === undefined) {
this._after[event] = [];
}
this._after[event].push(func);
}
unbindBefore(event, func) {
if (this._before[event] && this._before[event].indexOf(func) !== -1) {
this._before[event].splice(this._before[event].indexOf(func), 1);
}
}
unbindAfter(event, func) {
if (this._after[event] && this._after[event].indexOf(func) !== -1) {
this._after[event].splice(this._after[event].indexOf(func), 1);
}
}
async emitBefore(event, req, additionalProperties) {
if (this._before[event] === undefined) {
return;
}
for (let i = 0; i < this._before[event].length; i++) {
await this._before[event][i](req, additionalProperties);
}
}
async emitAfter(event, req, additionalProperties) {
if (this._after[event] === undefined) {
return;
}
for (let i = 0; i < this._after[event].length; i++) {
await this._after[event][i](req, additionalProperties);
}
}
};