Skip to content

Commit

Permalink
reverting res.locals and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lelemm committed Nov 11, 2024
1 parent 3111644 commit 2a49e21
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion jest.global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const setSessionUser = (userId, token = 'valid-token') => {

try {
const db = getAccountDb();
const session = db.get('SELECT token FROM sessions WHERE token = ?', [
const session = db.first('SELECT token FROM sessions WHERE token = ?', [
token,
]);
if (!session) {
Expand Down
18 changes: 9 additions & 9 deletions src/app-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ app.get('/users/', validateSessionMiddleware, (req, res) => {
});

app.post('/users', validateSessionMiddleware, async (req, res) => {
if (!isAdmin(res.locals.session.user_id)) {
if (!isAdmin(res.locals.user_id)) {
res.status(403).send({
status: 'error',
reason: 'forbidden',
Expand Down Expand Up @@ -89,7 +89,7 @@ app.post('/users', validateSessionMiddleware, async (req, res) => {
});

app.patch('/users', validateSessionMiddleware, async (req, res) => {
if (!isAdmin(res.locals.session.user_id)) {
if (!isAdmin(res.locals.user_id)) {
res.status(403).send({
status: 'error',
reason: 'forbidden',
Expand Down Expand Up @@ -141,7 +141,7 @@ app.patch('/users', validateSessionMiddleware, async (req, res) => {
});

app.delete('/users', validateSessionMiddleware, async (req, res) => {
if (!isAdmin(res.locals.session.user_id)) {
if (!isAdmin(res.locals.user_id)) {
res.status(403).send({
status: 'error',
reason: 'forbidden',
Expand Down Expand Up @@ -191,8 +191,8 @@ app.get('/access', validateSessionMiddleware, (req, res) => {

const accesses = UserService.getUserAccess(
fileId,
res.locals.session.user_id,
isAdmin(res.locals.session.user_id),
res.locals.user_id,
isAdmin(res.locals.user_id),
);

res.json(accesses);
Expand Down Expand Up @@ -305,12 +305,12 @@ app.get('/access/users', validateSessionMiddleware, async (req, res) => {

const { granted } = UserService.checkFilePermission(
fileId,
res.locals.session.user_id,
res.locals.user_id,
) || {
granted: 0,
};

if (granted === 0 && !isAdmin(res.locals.session.user_id)) {
if (granted === 0 && !isAdmin(res.locals.user_id)) {
res.status(400).send({
status: 'error',
reason: 'file-denied',
Expand Down Expand Up @@ -341,12 +341,12 @@ app.post(

const { granted } = UserService.checkFilePermission(
newUserOwner.fileId,
res.locals.session.user_id,
res.locals.user_id,
) || {
granted: 0,
};

if (granted === 0 && !isAdmin(res.locals.session.user_id)) {
if (granted === 0 && !isAdmin(res.locals.user_id)) {
res.status(400).send({
status: 'error',
reason: 'file-denied',
Expand Down
4 changes: 2 additions & 2 deletions src/app-openid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ app.use(requestLoggerMiddleware);
export { app as handlers };

app.post('/enable', validateSessionMiddleware, async (req, res) => {
if (!isAdmin(res.locals.session.user_id)) {
if (!isAdmin(res.locals.user_id)) {
res.status(403).send({
status: 'error',
reason: 'forbidden',
Expand All @@ -37,7 +37,7 @@ app.post('/enable', validateSessionMiddleware, async (req, res) => {
});

app.post('/disable', validateSessionMiddleware, async (req, res) => {
if (!isAdmin(res.locals.session.user_id)) {
if (!isAdmin(res.locals.user_id)) {
res.status(403).send({
status: 'error',
reason: 'forbidden',
Expand Down
2 changes: 1 addition & 1 deletion src/app-secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.post('/', async (req, res) => {
const { name, value } = req.body;

if (method === 'openid') {
let canSaveSecrets = isAdmin(res.locals.session.user_id);
let canSaveSecrets = isAdmin(res.locals.user_id);

if (!canSaveSecrets) {
res.status(403).send({
Expand Down
8 changes: 5 additions & 3 deletions src/app-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import {
} from './app-sync/validation.js';

const app = express();
app.use(validateSessionMiddleware);
app.use(errorMiddleware);
app.use(requestLoggerMiddleware);
app.use(express.raw({ type: 'application/actual-sync' }));
app.use(express.raw({ type: 'application/encrypted-file' }));
app.use(express.json());

app.use(validateSessionMiddleware);
export { app as handlers };

const OK_RESPONSE = { status: 'ok' };
Expand Down Expand Up @@ -113,6 +113,8 @@ app.post('/sync', async (req, res) => {
});

app.post('/user-get-key', (req, res) => {
if (!res.locals) return;

let { fileId } = req.body;

const filesService = new FilesService(getAccountDb());
Expand Down Expand Up @@ -247,7 +249,7 @@ app.post('/upload-user-file', async (req, res) => {
name: name,
encryptMeta: encryptMeta,
owner:
res.locals.session.user_id ||
res.locals.user_id ||
(() => {
throw new Error('User ID is required for file creation');
})(),
Expand Down Expand Up @@ -310,7 +312,7 @@ app.post('/update-user-filename', (req, res) => {

app.get('/list-user-files', (req, res) => {
const fileService = new FilesService(getAccountDb());
const rows = fileService.find({ userId: res.locals.session.user_id });
const rows = fileService.find({ userId: res.locals.user_id });
res.send({
status: 'ok',
data: rows.map((row) => ({
Expand Down
12 changes: 1 addition & 11 deletions src/util/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,13 @@ async function errorMiddleware(err, req, res, next) {
* @param {import('express').Response} res
* @param {import('express').NextFunction} next
*/
/**
* Middleware to validate session and attach it to response locals
* @param {import('express').Request} req
* @param {import('express').Response} res
* @param {import('express').NextFunction} next
*/
const validateSessionMiddleware = async (req, res, next) => {
let session = await validateSession(req, res);
if (!session) {
res.status(401).json({
status: 'error',
reason: 'invalid-session',
});
return;
}

res.locals.session = session;
res.locals = session;
next();
};

Expand Down

0 comments on commit 2a49e21

Please sign in to comment.