Skip to content

Commit ae7eb4b

Browse files
committed
Add admin auth
1 parent 26983b4 commit ae7eb4b

4 files changed

Lines changed: 77 additions & 12 deletions

File tree

admin.html

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,50 @@
44
<title>Admin page</title>
55
</head>
66
<body>
7-
<ul id='queueContainer'>
8-
</ul>
9-
Mark current team's response as : <br/>
10-
<button onclick="correctAnswer()">Correct</button>
11-
<button onclick="incorrectAnswer()">Incorrect</button>
7+
8+
<div id="login">
9+
Please enter the administrator password:<br />
10+
<form id="loginForm">
11+
<input id="loginPassword" type="password">
12+
<input type="submit">
13+
</form>
14+
</div>
15+
16+
<div id="authenticated">
17+
<ul id='queueContainer'>
18+
</ul>
19+
Mark current team's response as : <br/>
20+
<button onclick="correctAnswer()">Correct</button>
21+
<button onclick="incorrectAnswer()">Incorrect</button>
22+
</div>
23+
24+
1225
<script src="/socket.io/socket.io.js"></script>
1326
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
1427
<script>
15-
var socket = io('/admin');
16-
socket.on('queue', function (queue) {
17-
var html = queue.map(function (team) {
18-
return '<li>Team ' + team + '</li>';
19-
}).join('');
20-
$('#queueContainer').html(html)
28+
var socket, token;
29+
$('#authenticated').hide();
30+
$('#loginForm').submit(function() {
31+
$.post('/adminAuth', {
32+
password: $('#loginPassword').val()
33+
}, function(tk) {
34+
token = tk;
35+
socket = io('/admin', {
36+
query: "token=" + token
37+
});
38+
39+
socket.on('connect', function() {
40+
$('#login').hide();
41+
$('#authenticated').show();
42+
socket.on('queue', function (queue) {
43+
var html = queue.map(function (team) {
44+
return '<li>Team ' + team + '</li>';
45+
}).join('');
46+
$('#queueContainer').html(html)
47+
})
48+
});
49+
})
50+
return false;
2151
})
2252
function correctAnswer() {
2353
socket.emit('correctAnswer')

config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
'ADMIN_PASSWORD' : 'admin123',
3+
'BUZZER_PASSWORD': 'password',
4+
'PRIVATE_KEY' : 'THIS_IS_MY_UNCRACKABLE_PRIVATE_KEY'
5+
}

index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var app = require('express')();
2-
var http = require('http').Server(app);
2+
var bodyParser = require('body-parser');
3+
var http = require('http').createServer(app);
34
var io = require('socket.io')(http);
5+
var jwt = require('jsonwebtoken');
6+
const config = require('./config')
47

58
var queue = [];
69
var incorrectTeams = [];
@@ -10,14 +13,39 @@ function getTeamById(id) {
1013
return teams.indexOf(id) + 1;
1114
}
1215

16+
app.use(bodyParser.json())
17+
app.use(bodyParser.urlencoded({ extended: true }))
18+
1319
app.get('/', function(req, res){
1420
res.sendFile(__dirname + '/index.html');
1521
});
1622

23+
app.get('/queue', function(req, res){
24+
res.send(queue)
25+
});
26+
1727
app.get('/admin', function(req, res){
1828
res.sendFile(__dirname + '/admin.html');
1929
});
2030

31+
app.post('/adminAuth', function (req, res) {
32+
var password = req.body.password;
33+
if (password == config.ADMIN_PASSWORD) {
34+
var token = jwt.sign({}, config.PRIVATE_KEY, {
35+
expiresIn: '5h',
36+
subject: 'admin'
37+
});
38+
res.send(token)
39+
} else res.status(401).send()
40+
})
41+
42+
io.of('/admin').use(function(socket, next) {
43+
var token = socket.request._query.token;
44+
if (jwt.verify(token, config.PRIVATE_KEY, {subject: 'admin'}))
45+
next();
46+
else next(new Error("not authorized"))
47+
});
48+
2149
io.of('/admin').on('connection', function(socket) {
2250
socket.emit('queue', queue)
2351
socket.on('correctAnswer', function () {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"author": "Megh Parikh <[email protected]> (http://meghprkh.github.io)",
1010
"license": "ISC",
1111
"dependencies": {
12+
"body-parser": "^1.15.0",
1213
"express": "^4.13.4",
14+
"jsonwebtoken": "^5.7.0",
1315
"socket.io": "^1.4.5"
1416
},
1517
"devDependencies": {},

0 commit comments

Comments
 (0)