-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathserve-header-auth.js
More file actions
80 lines (74 loc) · 2.67 KB
/
Copy pathserve-header-auth.js
File metadata and controls
80 lines (74 loc) · 2.67 KB
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
/**
* This file is part of the Elephant.io package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright ElephantIO
* @copyright Wisembly
* @license http://www.opensource.org/licenses/MIT-License MIT License
*/
const ExampleServer = require('./serve');
/**
* An example of authorization using request header server.
*/
class HeaderAuthServer extends ExampleServer {
tokens = {}
users = {}
initialize() {
this.namespace = 'header-auth';
}
handle() {
if (typeof this.nsp.use === 'function') {
this.nsp.use((socket, next) => {
const auth = socket.request.headers.authorization;
const user = socket.request.headers.user;
if (auth && user) {
const token = auth.replace('Bearer ', '');
this.log('auth token', token);
// do some security check with token
// ...
// store token and bind with specific socket id
if (!this.tokens[token] && !this.users[token]) {
this.tokens[token] = socket.id;
this.users[token] = user;
}
next();
} else{
next(new Error('no authorization header'));
}
});
}
this.nsp.on('connection', socket => {
let nb;
this.log('connected: %s', socket.id);
socket
.on('disconnect', () => {
this.log('disconnected: %s', socket.id);
})
.on('message', message => {
++nb;
let reply;
this.log('message < %s', message);
if (!message['token']) {
reply = 'Token is missed';
}
if (!this.tokens[message['token']]) {
reply = 'Token is invalid';
}
const user = this.users[message['token']];
if (!user) {
reply = 'Sorry. I don\'t remember you.';
} else if (message['message'].indexOf('remember') !== -1) {
reply = 'I remember you, ' + user;
} else {
reply = 'I am fine, ' + user;
}
this.log('message > %s', reply);
socket.emit('message', reply);
});
});
return true;
}
}
module.exports = HeaderAuthServer;