Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
show(client, data): WsResponse {
if (!data.userId) throw new WsException('Missing entry id.');
if (client.handshake.user.id !== data.userId) throw new WsException('Unable to find the user.');
const user = client.handshake.user;
return { event: 'showUser', data: user };
}
}
return (req, next) => {
const matches = req.url.match(/token=([^&].*)/);
req['token'] = matches && matches[1];
if (!req.token) {
throw new WsException('Missing token.');
}
return jwt.verify(req.token, 'secret', async (err, payload) => {
if (err) throw new WsException(err);
const user = await this.userService.findOne({ where: { email: payload.email }});
req.user = user;
return next(true);
});
}
}
async show(client, data): Promise> {
if (!data.entryId) throw new WsException('Missing entry id.');
if (!data.commentId) throw new WsException('Missing comment id.');
const comment = await this.commentService.findOne({
where: {
id: data.commentId,
entryId: data.entryId
}
});
return { event: 'showComment', data: comment };
}
}
async verifyWsConnection(client) {
try {
return jwt.verify(client.handshake.query.token, this.configService.secrets.secretKey);
} catch (e) {
client.disconnect();
throw new WsException('Unauthorized');
}
}
/**
return jwt.verify(req.token, 'secret', async (err, payload) => {
if (err) throw new WsException(err);
const user = await this.userService.findOne({ where: { email: payload.email }});
req.user = user;
return next(true);
});
}
handleSigningKeyError: (err, cb) => {
if (err instanceof SigningKeyNotFoundError) {
return cb(new WsException('This is bad: SigningKeyNotFoundError'));
}
return cb(err);
},
async index(client, data): Promise> {
if (!data.entryId) throw new WsException('Missing entry id.');
const comments = await this.commentService.findAll({
where: {entryId: data.entryId}
});
return { event: 'indexComment', data: comments };
}
async getOutOfDatePlugins(client, payload) {
try {
return await this.pluginsService.getOutOfDatePlugins();
} catch (e) {
return new WsException(e.message);
}
}
}
return (socket, next) => {
if (!socket.handshake.query.auth_token) {
throw new WsException('Missing token.');
}
return jwt.verify(
socket.handshake.query.auth_token,
'secret',
async (err, payload) => {
if (err) throw new WsException(err);
const user = await this.userService.findOne({
where: { email: payload.email }
});
socket.handshake.user = user;
return next();
}
);
};
async homebridgeVersionCheck(client, payload) {
try {
return await this.pluginsService.getHomebridgePackage();
} catch (e) {
return new WsException(e.message);
}
}