Skip to content

Commit

Permalink
fix: check ban status outside of discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
CaramelKat committed Jul 30, 2024
1 parent 6bee19d commit 9cb644e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import express from 'express';
import xmlbuilder from 'xmlbuilder';
import moment from 'moment';
import { z } from 'zod';
import { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc';
import { getEndpoint } from '@/database';
import { getEndpoint, getUserSettings } from '@/database';
import { getUserAccountData, getValueFromHeaders, decodeParamPack, getPIDFromServiceToken } from '@/util';
import { HydratedEndpointDocument } from '@/types/mongoose/endpoint';

Expand Down Expand Up @@ -86,13 +87,6 @@ async function auth(request: express.Request, response: express.Response, next:
return serverError(response, discovery);
}

// TODO - This is temp, testing something. Will be removed in the future
if (request.path !== '/v1/endpoint') {
if (user.serverAccessLevel !== 'test' && user.serverAccessLevel !== 'dev') {
return badAuth(response, 16, 'BAD_TOKEN');
}
}

// * This is a false positive from ESLint.
// * Since this middleware is only ever called
// * per every request instance
Expand All @@ -101,6 +95,29 @@ async function auth(request: express.Request, response: express.Response, next:
// eslint-disable-next-line require-atomic-updates
request.paramPack = paramPackData;

const userSettings = await getUserSettings(request.pid);

if (!userSettings) {
return badAuth(response, 18, 'BAD_PARAM');
}

if (moment(userSettings.ban_lift_date) <= moment() && userSettings.account_status !== 3) {
userSettings.account_status = 0;
await userSettings.save();
}
// This includes ban checks for both Juxt specifically and the account server, ideally this should be squashed
// assuming we support more gradual bans on PNID's
if (userSettings.account_status < 0 || userSettings.account_status > 1 || user.accessLevel < 0) {
if (userSettings.account_status === 2 && request.method === 'GET') {
return next();
} else if (userSettings.account_status === 2) {
return badAuth(response, 8, 'PNID_POST_BAN');
} else {
return badAuth(response, 7, 'PNID_PERM_BAN');
}

}

return next();
}

Expand Down
5 changes: 5 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export function getPIDFromServiceToken(token: string): number {
return 0;
}

// * Check if the token is expired
if (unpackedToken.expire_time < Date.now()) {
return 0;
}

return unpackedToken.pid;
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 9cb644e

Please sign in to comment.