🔨」 fix(user-management): fix the fact the no fucking route was protected

This commit is contained in:
2025-10-23 15:49:35 +02:00
parent c4221d9463
commit 0c9f595047
19 changed files with 76 additions and 48 deletions

View File

@ -1,6 +1,12 @@
export async function dAvatar(request, reply, fastify, getUserInfo, getAvatarId, deleteAvatarId, deleteImage) { export async function dAvatar(request, reply, fastify, getUserInfo, getAvatarId, deleteAvatarId, deleteImage) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.cose(404).send({ error: "User does not exist" }); return reply.cose(404).send({ error: "User does not exist" });
} }

View File

@ -1,9 +1,9 @@
export async function dFriend(request, reply, fastify, getUserInfo, getFriend, deleteFriend) { export async function dFriend(request, reply, fastify, getUserInfo, getFriend, deleteFriend) {
try { try {
if (!request.user) {
return reply.code(400).send({ error: "Please specify a user" });
}
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,15 +1,12 @@
export async function dFriends(request, reply, fastify, getUserInfo, deleteFriends) { export async function dFriends(request, reply, fastify, getUserInfo, deleteFriends) {
try { try {
if (!request.user) {
return reply.code(400).send({ error: "Please specify a user" });
}
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
if (request.user !== 'admin' && request.user !== userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
deleteFriends.run(userId); deleteFriends.run(userId);
return reply.code(200).send({ msg: "Friends deleted successfully" }); return reply.code(200).send({ msg: "Friends deleted successfully" });
} catch (err) { } catch (err) {

View File

@ -1,15 +1,12 @@
export async function dMatchHistory(request, reply, fastify, getUserInfo, deleteMatchHistory, deleteStatsPong, deleteStatsTetris) { export async function dMatchHistory(request, reply, fastify, getUserInfo, deleteMatchHistory, deleteStatsPong, deleteStatsTetris) {
try { try {
if (!request.user) {
return reply.code(400).send({ error: "Please specify a user" });
}
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
if (request.user !== 'admin' && request.user !== userId) {
return reply.code(401).send({ error: "Unauthorized" });
}
const { game } = request.query; const { game } = request.query;
if (game !== 'pong' && game !== 'tetris') { if (game !== 'pong' && game !== 'tetris') {
return reply.code(400).send({ error: "Specified game does not exist" }); return reply.code(400).send({ error: "Specified game does not exist" });

View File

@ -1,22 +1,19 @@
export async function dMember(request, reply, fastify, getUserInfo, changeDisplayName) { export async function dMember(request, reply, fastify, getUserInfo, changeDisplayName) {
try { try {
if (!request.user) {
return reply.code(400).send({ error: "Please specify a user" });
}
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
const user = request.user; const user = request.user;
const member = request.params.member; const member = request.params.member;
if (user === 'admin' || user === request.params.userId) { if (member === 'displayName') {
if (member === 'displayName') { changeDisplayName.run("", request.params.userId);
changeDisplayName.run("", request.params.userId); return reply.code(200).send({ msg: "Display name deleted successfully" });
return reply.code(200).send({ msg: "Display name deleted successfully" });
}
return reply.code(400).send({ msg: "Member does not exist" })
} else { } else {
return reply.code(401).send({ error: 'You dont have the right to delete this' }); return reply.code(400).send({ msg: "Member does not exist" })
} }
} catch (err) { } catch (err) {
fastify.log.error(err); fastify.log.error(err);

View File

@ -1,6 +1,10 @@
export async function dUser(request, reply, fastify, getUserInfo, deleteMatchHistory, deleteFriends, deleteUser) { export async function dUser(request, reply, fastify, getUserInfo, deleteMatchHistory, deleteFriends, deleteUser) {
try { try {
if (!getUserInfo.get(request.params.userId)) { const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
deleteMatchHistory.run('pong', request.params.userId); deleteMatchHistory.run('pong', request.params.userId);

View File

@ -173,6 +173,7 @@ export default async function(fastify, options) {
if (jwt.user !== 'admin') { if (jwt.user !== 'admin') {
throw ('You lack administrator privileges'); throw ('You lack administrator privileges');
} }
request.user = jwt.user;
} catch (err) { } catch (err) {
reply.code(401).send({ error: 'Unauthorized' }); reply.code(401).send({ error: 'Unauthorized' });
} }

View File

@ -1,6 +1,9 @@
export async function gAvatar(request, reply, fastify, getUserInfo, getAvatarId, getImage) { export async function gAvatar(request, reply, fastify, getUserInfo, getAvatarId, getImage) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,6 +1,9 @@
export async function gFriends(request, reply, fastify, getUserInfo, getFriends) { export async function gFriends(request, reply, fastify, getUserInfo, getFriends) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,6 +1,9 @@
export async function gMatchHistory(request, reply, fastify, getUserInfo, getMatchHistory) { export async function gMatchHistory(request, reply, fastify, getUserInfo, getMatchHistory) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,9 +1,13 @@
export async function gNumberFriends(request, reply, fastify, getUserInfo, getNumberFriends) { export async function gNumberFriends(request, reply, fastify, getUserInfo, getNumberFriends) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
const row = getNumberFriends.get(userId); const row = getNumberFriends.get(userId);
return reply.code(200).send({ n_friends: row.n_friends }); return reply.code(200).send({ n_friends: row.n_friends });
} catch (err) { } catch (err) {

View File

@ -1,6 +1,9 @@
export async function gNumberMatches(request, reply, fastify, getUserInfo, getNumberMatches) { export async function gNumberMatches(request, reply, fastify, getUserInfo, getNumberMatches) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,11 +1,24 @@
export async function gUser(request, reply, fastify, getUserInfo) { export async function gUser(request, reply, fastify, getUserInfo) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
const userInfo = getUserInfo.get(userId); if (request.user !== userId && request.user !== 'admin') {
if (!userInfo) { return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
return reply.code(200).send({ username: userInfo.username, displayName: userInfo.displayName, pong: { wins: userInfo.pongWins, losses: userInfo.pongLosses }, tetris: { wins: userInfo.tetrisWins, losses: userInfo.tetrisLosses } }); return reply.code(200).send({
username: userInfo.username,
displayName: userInfo.displayName,
pong: {
wins: userInfo.pongWins,
losses: userInfo.pongLosses
},
tetris: {
wins: userInfo.tetrisWins,
losses: userInfo.tetrisLosses
}
});
} catch (err) { } catch (err) {
fastify.log.error(err); fastify.log.error(err);
return reply.code(500).send({ error: "Internal server error" }); return reply.code(500).send({ error: "Internal server error" });

View File

@ -8,6 +8,9 @@ import sharp from 'sharp';
export async function pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage) { export async function pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }

View File

@ -1,11 +1,8 @@
export async function pFriend(request, reply, fastify, getUserInfo, getFriend, addFriend) { export async function pFriend(request, reply, fastify, getUserInfo, getFriend, addFriend) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (!request.user) { if (request.user !== userId && request.user !== 'admin') {
return reply.code(400).send({ error: "Please specify a user" }); return reply.code(401).send({ error: 'Unauthorized' });
}
if (request.user !== 'admin' && request.user !== userId) {
return reply.code(401).send({ error: "Unauthorized" });
} }
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });

View File

@ -16,11 +16,11 @@ async function fetchSave(request, reply, userId, addMatch) {
export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris) { export async function pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (!request.user) { if (request.user !== userId && request.user !== 'admin') {
return reply.code(400).send({ error: "Please specify a user" }); return reply.code(401).send({ error: 'Unauthorized' });
} }
if (request.user !== 'admin' && request.user !== userId) { if (!getUserInfo.get(userId)) {
return reply.code(401).send({ error: "Unauthorized" }); return reply.code(404).send({ error: "User does not exist" });
} }
if (request.body.game !== 'pong' && request.body.game !== 'tetris') { if (request.body.game !== 'pong' && request.body.game !== 'tetris') {
return reply.code(400).send({ error: "Specified game does not exist" }); return reply.code(400).send({ error: "Specified game does not exist" });

View File

@ -1,10 +1,7 @@
export async function pUser(request, reply, fastify, getUserInfo, createUser) { export async function pUser(request, reply, fastify, getUserInfo, createUser) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (!request.user || !request.user.user) { if (request.user !== 'admin') {
return reply.code(400).send({ error: "Please specify a user" });
}
if (request.user.user !== 'admin') {
return reply.code(401).send({ error: "Unauthorized" }); return reply.code(401).send({ error: "Unauthorized" });
} }
if (getUserInfo.get(userId)) { if (getUserInfo.get(userId)) {

View File

@ -3,8 +3,11 @@ import sharp from 'sharp';
export async function uAvatar(request, reply, fastify, getUserInfo, setAvatarId, getAvatarId, deleteAvatarId, postImage, deleteImage) { export async function uAvatar(request, reply, fastify, getUserInfo, setAvatarId, getAvatarId, deleteAvatarId, postImage, deleteImage) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (request.user !== userId && request.user !== 'admin') {
return reply.code(401).send({ error: 'Unauthorized' });
}
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.cose(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });
} }
deleteAvatarId.run(userId); deleteAvatarId.run(userId);
const parts = request.parts(); const parts = request.parts();

View File

@ -1,11 +1,8 @@
export async function uMember(request, reply, fastify, getUserInfo, changeDisplayName, changeAvatarId) { export async function uMember(request, reply, fastify, getUserInfo, changeDisplayName, changeAvatarId) {
try { try {
const userId = request.params.userId; const userId = request.params.userId;
if (!request.user) { if (request.user !== userId && request.user !== 'admin') {
return reply.code(400).send({ error: "Please specify a user" }); return reply.code(401).send({ error: 'Unauthorized' });
}
if (request.user !== 'admin' && request.user !== userId) {
return reply.code(401).send({ error: "Unauthorized" });
} }
if (!getUserInfo.get(userId)) { if (!getUserInfo.get(userId)) {
return reply.code(404).send({ error: "User does not exist" }); return reply.code(404).send({ error: "User does not exist" });