Skip to content

Commit 5d8e1c5

Browse files
committed
1.0.0
0 parents  commit 5d8e1c5

File tree

2,303 files changed

+244176
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,303 files changed

+244176
-0
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AUTH0_CLIENT_ID=J2Yxx9kSZ4llxOtbMjpiAGGM2P30EdQE
2+
AUTH0_DOMAIN=jrbsystem.auth0.com
3+
AUTH0_SECRED_KEY=P-mJNkdG8KD-M4tllmtaueG4w2vTmg0Q0__O6oqGQGds97PinUQTFsJEnCt3Eldy
4+
LOCAL_JWT_SECRET=WTQYAYYXBBAANKSNCLKFNLVLJDBVLJDS

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index.html

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Socket.io - token based authentiction
2+
3+
## Modules
4+
/module/mainSocketLocalToken.js - token based authentiction on local generated token
5+
6+
## Installation
7+
8+
9+
1. Install the all necesary modules and packages in project directory
10+
11+
```
12+
npm install
13+
```
14+
15+
16+
17+
18+
2. Start the server from the root directory of project
19+
20+
21+
```
22+
npm start
23+
```
24+
25+
3. Visit http://localhost:3000

database/db.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Users=[
2+
{ "id": 1, "firstName": "Greg", "lastName": "Kroczek", "email": "[email protected]", "password": "123456", "auth0_user_id": "google-oauth2|113980109958968827572" },
3+
{ "id": 2, "firstName": "Peter", "lastName": "mortsen", "email": "[email protected]", "password": "asasf", "auth0_user_id": "google-oauth2|13434010995896886847" },
4+
{ "id": 3, "firstName": "Anna", "lastName": "Kroczek", "email": "[email protected]", "password": "1wefewf", "auth0_user_id": "google-oauth2|138975983745878457395" },
5+
{ "id": 4, "firstName": "Andrew", "lastName": "McGregor", "email": "[email protected]", "password": "wqdewfwf", "auth0_user_id": "google-oauth2|423846783467326472837" },
6+
{ "id": 5, "firstName": "Jodie", "lastName": "Trauss", "email": "[email protected]", "password": "wefewf", "auth0_user_id": "google-oauth2|237489231748974385754" },
7+
{ "id": 12, "firstName": "Greg", "lastName": "Kroczek", "email": "[email protected]", "password": "123456", "auth0_user_id": "google-oauth2|113980109958968827572" },
8+
9+
];
10+
11+
module.exports=Users;

modules/lib/message.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
exports.broadcast = function(data){
3+
try{
4+
5+
if (!security.verifyConnection(socket,data)) return;
6+
7+
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);
11+
}
12+
);
13+
14+
}catch(ex){
15+
console.log('socket.io: Error during sending message to clients ');
16+
}
17+
}
18+
19+
20+
exports.sendError = function(socket, message) {
21+
22+
return socket.emit('error', {
23+
message: message
24+
});
25+
26+
27+
};

modules/lib/security.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
const _ = require('lodash'),
2+
jwt = require('jsonwebtoken');
3+
4+
5+
6+
const func = (socket,data) => {
7+
return new Promise((resolve, reject) => {
8+
9+
verifyConnection(socket,data);
10+
});
11+
};
12+
13+
exports.verifyConnection = func;
14+
15+
const verifyConnection = function(socket, data) {
16+
17+
var token = data && data.token || null;
18+
19+
if(!token || !clients[socket.id])
20+
{
21+
console.log('Unauthorized access: access denied');
22+
message.sendError(socket,'Invalid email or password');
23+
//reject(false);
24+
return false;
25+
}
26+
27+
if (token)
28+
{
29+
30+
jwt.verify(token, jwtSecret, function(err, decoded)
31+
{
32+
if (err)
33+
{
34+
35+
console.log(err);
36+
message.sendError(socket,'Invalid email or password');
37+
//reject(false);
38+
return false;
39+
}
40+
41+
42+
let userID = decoded.id;
43+
44+
let user = _.find(users,{id:userID});
45+
console.log('founded user',user);
46+
if(!user) {
47+
console.log('!user:false');
48+
message.sendError(socket, 'Invalid email or password');
49+
//reject(false);
50+
return false;
51+
}
52+
else {
53+
console.log('user:true');
54+
//reject(true);
55+
return true; }
56+
57+
});
58+
}
59+
60+
};
61+
62+
63+
const func2 = (socket,data) => {
64+
return new Promise((resolve, reject) => {
65+
66+
verifyConnectionAuth0(socket,data);
67+
68+
});
69+
};
70+
71+
72+
73+
exports.verifyConnection = func2;
74+
75+
exports.verifyConnectionAuth0 = function(socket, data) {
76+
77+
78+
var token = data && data.token || null;
79+
80+
81+
if(!token || !clients[socket.id])
82+
{
83+
console.log('Unauthorized access: access denied');
84+
message.sendError(socket,'Invalid email or password');
85+
//reject(false);
86+
return false;
87+
}
88+
89+
if (token)
90+
{
91+
92+
jwt.verify(token, jwtSecret, function(err, decoded)
93+
{
94+
if (err)
95+
{
96+
97+
console.log(err);
98+
message.sendError(socket,'Invalid email or password');
99+
//reject(false);
100+
return false;
101+
}
102+
103+
104+
let auth0UserID = decoded.sub;
105+
106+
107+
108+
let user = _.find(users,{auth0_user_id:auth0UserID});
109+
110+
if(!user) {
111+
message.sendError(socket, 'Invalid email or password');
112+
//reject(false);
113+
return false;
114+
}
115+
else {
116+
//reject(true);
117+
return true;
118+
}
119+
120+
});
121+
};
122+
123+
};
124+
125+
exports.authenticateUser = function authenticateUser (socket, data) {
126+
127+
const {email,password}=data;
128+
129+
130+
if(!email || !password) return message.sendError(socket,'Invalid email or password');
131+
132+
const user = _.find(users,{email:email,password:password});
133+
134+
135+
if(!user) {
136+
return message.sendError(socket,'Invalid email or password');
137+
}
138+
else
139+
{
140+
return loginUser(socket, user);
141+
}
142+
143+
};
144+
145+
146+
const loginUser = function loginUser(socket, user) {
147+
148+
var profile = {
149+
firstName: user.firstName,
150+
lastName: user.lastName,
151+
email: user.email,
152+
id: user.id
153+
};
154+
155+
156+
//expiresIn: expressed in seconds or a string describing a time span zeit/ms. Eg: 60, "2 days", "10h", "7d"
157+
var token = jwt.sign(profile, jwtSecret, {
158+
expiresIn: 360
159+
});
160+
161+
var data = {profile: profile, token:token};
162+
socket.emit('login.success', data);
163+
164+
clients[socket.id]=data;
165+
//clients[token]=data;
166+
console.log('User logged to socket.io. Number of connections',NumberOfConnections(),' user=',profile);
167+
168+
169+
return;
170+
171+
172+
};
173+
174+
175+
176+
177+

modules/lib/user.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const _ = require('lodash');
2+
3+
exports.getUser = function (socket, data) {
4+
5+
/*
6+
security.verifyConnection(socket,data).then(function(result) {
7+
console.log('Access allowed to getUser()');
8+
if(!data.id) return socket.emit('user.success', null);
9+
const user = _.find(users,{id:data.id});
10+
var profile = (user)? {
11+
firstName: user.firstName,
12+
lastName: user.lastName,
13+
email: user.email,
14+
id: user.id
15+
} : null;
16+
17+
return socket.emit('user.success', profile);
18+
}, function(err) {
19+
console.log('Access denied to getUser()'); return;
20+
});
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);
29+
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);
39+
40+
};

modules/mainSocketLocalToken.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/*
3+
Local generated token
4+
*/
5+
6+
const user = require('./lib/user'),
7+
socketio = require('socket.io');
8+
9+
let clients = {};
10+
11+
12+
global.clients = clients;
13+
global.users = require('../database/db.js');
14+
global.message = require('./lib/message');
15+
global.security = require('./lib/security');
16+
global.jwtSecret = process.env.LOCAL_JWT_SECRET;
17+
global.NumberOfConnections = function() {
18+
return Object.keys(clients).length;
19+
}
20+
21+
22+
23+
exports.connect = function(server){
24+
25+
var io = socketio.listen(server);
26+
27+
28+
io.on('connection', function (socket) {
29+
30+
console.log('socket.io: new connection, current number of connections:',NumberOfConnections());
31+
//Get User
32+
socket.on('getUser', function (data) {
33+
console.log('socket.io: call getUser');
34+
user.getUser(socket, data);
35+
});
36+
37+
socket.on('message', function (data) {
38+
message.broadcast(data);
39+
});
40+
41+
// Login
42+
socket.on('login', function (data) {
43+
security.authenticateUser(socket, data);
44+
});
45+
46+
// Logout
47+
socket.on('logout', function (token) {
48+
clients[socket.id] && delete clients[socket.id];
49+
console.log('close connection, number of connections:',NumberOfConnections());
50+
51+
});
52+
53+
54+
55+
socket.on('disconnect', function () {
56+
clients[socket.id] && delete clients[socket.id];
57+
console.log('close connection, number of connections:',NumberOfConnections());
58+
59+
60+
});
61+
});
62+
63+
io.on('end', function() {
64+
65+
console.log('socket.io: terminate server socket.io', NumberOfConnections());
66+
67+
});
68+
69+
70+
71+
72+
73+
74+
}
75+
76+
77+
78+

node_modules/.bin/mime

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)