refactor(security): sockets

This commit is contained in:
cha0s 2021-01-19 11:08:58 -06:00
parent 73b483f7e9
commit c2a9287a52
8 changed files with 21 additions and 18 deletions

View File

@ -38,16 +38,16 @@
'@latus/redis/session': {}
'@latus/repl': {}
'@latus/socket': {
packets.decorate: [
'@latus/governor',
],
}
'@latus/socket/server': {
authenticate: [
'@latus/user/session',
'@latus/user/passport',
],
connect: [
'@latus/socket',
],
packets.decorate: [
'@latus/governor',
]
connect: [],
}
'@latus/user/local': {}
'@latus/user/models': {}

View File

@ -57,10 +57,10 @@ export default {
}
};
},
'@latus/socket/authenticate': (latus) => {
'@latus/socket/server/authenticate': (latus) => {
const {config: {'@latus/governor/server': {socket}}} = latus;
const limiter = createLimiter(latus, {
keyPrefix: '@latus/socket/authenticate',
keyPrefix: '@latus/socket/server/authenticate',
...socket,
});
return async (socket, next) => {

View File

@ -33,7 +33,7 @@ export default {
'@latus/user/session': (latus) => ({
store: new RedisStore({client: createClient(latus)}),
}),
'@latus/socket/connect': ({req}, {config: {'%redisAdapter': adapter}}) => {
'@latus/socket/server/connect': ({req}, {config: {'%redisAdapter': adapter}}) => {
adapterIntoReq('sockets', adapter, req);
},
'@latus/socket/server': (latus) => {

View File

@ -12,8 +12,7 @@ export {default as packets} from './packet/packets';
export default {
hooks: {
'@latus/core/config': () => ({
authenticate: [],
connect: [],
'packets.decorate': [],
}),
'@latus/core/starting': (latus) => {
// eslint-disable-next-line no-param-reassign

View File

@ -3,6 +3,10 @@ import Sockets from './sockets';
export default {
hooks: {
'@latus/core/config': () => ({
authenticate: [],
connect: [],
}),
'@latus/http/server/up': async (httpServer, latus) => {
latus.set('%sockets', new Sockets(httpServer, latus));
},

View File

@ -26,7 +26,7 @@ export default class SocketServer {
headers: socket.request.headers,
}, app.get('trust proxy fn')),
});
latus.invokeMiddleware('@latus/socket/authenticate', socket, next);
latus.invokeMiddleware('@latus/socket/server/authenticate', socket, next);
});
latus.invoke('@latus/socket/server.io', this.io);
this.io.on('connect', this.onConnect);
@ -47,7 +47,7 @@ export default class SocketServer {
const {req} = serverSocket;
req.latus = this.latus;
req.adapter = this.io.of('/').adapter;
this.latus.invokeSequential('@latus/socket/connect', serverSocket);
this.latus.invokeSequential('@latus/socket/server/connect', serverSocket);
}
static send(latus, nsp, packetOrDehydrated) {

View File

@ -60,10 +60,10 @@ export default {
);
},
}),
'@latus/socket/authenticate': (latus) => (socket, next) => {
debug('@latus/socket/authenticate: passport.initialize()');
'@latus/socket/server/authenticate': (latus) => (socket, next) => {
debug('@latus/socket/server/authenticate: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => {
debug('@latus/socket/authenticate: passport.session()');
debug('@latus/socket/server/authenticate: passport.session()');
passport.session()(socket.handshake, undefined, async () => {
/* eslint-disable no-param-reassign */
if (!socket.handshake.user) {

View File

@ -33,8 +33,8 @@ export default {
});
};
},
'@latus/socket/authenticate': (latus) => (socket, next) => {
debug('@latus/socket/authenticate: session()');
'@latus/socket/server/authenticate': (latus) => (socket, next) => {
debug('@latus/socket/server/authenticate: session()');
latus.config['%session'](socket.handshake, {}, next);
},
},