Skip to content

Commit 4fc974f

Browse files
committed
1.0.0
1 parent a029079 commit 4fc974f

File tree

4 files changed

+53
-54
lines changed

4 files changed

+53
-54
lines changed

modules/lib/message.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11

2-
exports.broadcast = function(data){
3-
try{
2+
exports.broadcast = function(socket,data){
43

5-
if (!security.verifyConnection(socket,data)) return;
6-
4+
5+
security.verifyConnection(socket,data).then(function(result) {
6+
7+
console.log('Access allowed to broadcast()');
8+
try{
79
var obj = {org:data.org,topic:data.topic,message:data.message};
8-
console.log('socket.io broadcast:',obj);
9-
clients.forEach(function(client) {
10-
client.emit('message', obj);
10+
console.log('socket.io broadcast:',obj);
11+
clients.forEach(function(client) {
12+
client.emit('message', obj);
13+
}
14+
);
15+
}catch(ex){
16+
console.log('socket.io: Error during sending message to clients ');
1117
}
12-
);
1318

14-
}catch(ex){
15-
console.log('socket.io: Error during sending message to clients ');
16-
}
19+
}, function(err) {
20+
console.log('Access denied to broadcast()'); return;
21+
});
22+
23+
1724
}
1825

1926

modules/lib/security.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ const verifyConnection = function(socket, data) {
1616

1717
var token = data && data.token || null;
1818

19-
if(!token || !clients[socket.id])
19+
if(!socket || socket==='undefined' || !clients[socket.id] || !token)
2020
{
2121
console.log('Unauthorized access: access denied');
2222
message.sendError(socket,'Invalid email or password');
23-
//reject(false);
24-
return false;
23+
24+
socket.disconnect();
25+
reject(false);
2526
}
2627

2728
if (token)
@@ -34,8 +35,9 @@ const verifyConnection = function(socket, data) {
3435

3536
console.log(err);
3637
message.sendError(socket,'Invalid email or password');
37-
//reject(false);
38-
return false;
38+
39+
socket.disconnect();
40+
reject(false);
3941
}
4042

4143

@@ -46,13 +48,13 @@ const verifyConnection = function(socket, data) {
4648
if(!user) {
4749
console.log('!user:false');
4850
message.sendError(socket, 'Invalid email or password');
49-
//reject(false);
50-
return false;
51+
52+
socket.disconnect();
53+
reject(false);
5154
}
5255
else {
5356
console.log('user:true');
54-
//reject(true);
55-
return true; }
57+
resolve(true);}
5658

5759
});
5860
}
@@ -64,26 +66,27 @@ const func2 = (socket,data) => {
6466
return new Promise((resolve, reject) => {
6567

6668
verifyConnectionAuth0(socket,data);
67-
69+
6870
});
6971
};
7072

7173

7274

73-
exports.verifyConnection = func2;
75+
exports.verifyConnectionAuth0 = func2;
7476

7577
exports.verifyConnectionAuth0 = function(socket, data) {
7678

7779

7880
var token = data && data.token || null;
7981

8082

81-
if(!token || !clients[socket.id])
83+
if(!socket || socket==='undefined' || !clients[socket.id] || !token)
8284
{
8385
console.log('Unauthorized access: access denied');
8486
message.sendError(socket,'Invalid email or password');
85-
//reject(false);
86-
return false;
87+
88+
socket.disconnect();
89+
reject(false);
8790
}
8891

8992
if (token)
@@ -93,11 +96,11 @@ exports.verifyConnectionAuth0 = function(socket, data) {
9396
{
9497
if (err)
9598
{
96-
97-
console.log(err);
99+
console.log(err);
98100
message.sendError(socket,'Invalid email or password');
99-
//reject(false);
100-
return false;
101+
102+
socket.disconnect();
103+
reject(false);
101104
}
102105

103106

@@ -109,12 +112,13 @@ exports.verifyConnectionAuth0 = function(socket, data) {
109112

110113
if(!user) {
111114
message.sendError(socket, 'Invalid email or password');
112-
//reject(false);
113-
return false;
115+
116+
socket.disconnect();
117+
reject(false);
114118
}
115119
else {
116-
//reject(true);
117-
return true;
120+
121+
resolve(true);
118122
}
119123

120124
});
@@ -163,7 +167,8 @@ const loginUser = function loginUser(socket, user) {
163167

164168
clients[socket.id]=data;
165169
//clients[token]=data;
166-
console.log('User logged to socket.io. Number of connections',NumberOfConnections(),' user=',profile);
170+
console.log('User logged to socket.io. number of authorized connections:',NumberOfConnections());
171+
console.log('User profile logged to socket.io=',profile);
167172

168173

169174
return;

modules/lib/user.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ const _ = require('lodash');
22

33
exports.getUser = function (socket, data) {
44

5-
/*
5+
66
security.verifyConnection(socket,data).then(function(result) {
7-
console.log('Access allowed to getUser()');
7+
8+
console.log('Access allowed to getUser()');
89
if(!data.id) return socket.emit('user.success', null);
10+
911
const user = _.find(users,{id:data.id});
12+
1013
var profile = (user)? {
1114
firstName: user.firstName,
1215
lastName: user.lastName,
@@ -15,26 +18,10 @@ security.verifyConnection(socket,data).then(function(result) {
1518
} : null;
1619

1720
return socket.emit('user.success', profile);
21+
1822
}, function(err) {
1923
console.log('Access denied to getUser()'); return;
2024
});
21-
*/
22-
23-
// Emit an error if the token is invalid
24-
if (!security.verifyConnection(socket,data)) { console.log('Access denied to getUser()'); return; }
25-
26-
console.log('Access allowed to getUser()');
27-
28-
if(!data.id) return socket.emit('user.success', null);
2925

30-
const user = _.find(users,{id:data.id});
31-
var profile = (user)? {
32-
firstName: user.firstName,
33-
lastName: user.lastName,
34-
email: user.email,
35-
id: user.id
36-
} : null;
37-
38-
return socket.emit('user.success', profile);
3926

4027
};

modules/mainSocketLocalToken.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports.connect = function(server){
3535
});
3636

3737
socket.on('message', function (data) {
38-
message.broadcast(data);
38+
message.broadcast(socket,data);
3939
});
4040

4141
// Login

0 commit comments

Comments
 (0)