Skip to content

Commit

Permalink
fix: req field for auth, logic to allow access to project
Browse files Browse the repository at this point in the history
  • Loading branch information
anibalsolon committed Jan 18, 2024
1 parent 480fd45 commit 47be17c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 4 additions & 2 deletions api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as fs from 'fs';
import * as jwt from 'jsonwebtoken';
import * as config from './config';

const pubkey = fs.readFileSync(__dirname + '/auth.pub', 'ascii').trim();
const pubkey = config.authentication ? fs.readFileSync(__dirname + '/auth.pub', 'ascii').trim() : null;
const ezbidsPrivateKey = fs.readFileSync(`${__dirname}/ezbids.key`, 'ascii').trim();
const ezbidsPublicKey = fs.readFileSync(`${__dirname}/ezbids.pub`, 'ascii').trim();

export const validateWithJWTConfig = (options?: Params) => {
if (config.authentication) {
return expressjwt({
Expand All @@ -15,9 +16,10 @@ export const validateWithJWTConfig = (options?: Params) => {
});
} else {
return (req: any, res: any, next: any) => {
req.user = {
req.auth = {
sub: 0,
};

next();
};
}
Expand Down
3 changes: 1 addition & 2 deletions api/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ router.get('/health', (req, res) => {
* Session: $ref: '#/components/schemas/Session'
*/
router.post('/session', validateWithJWTConfig(), (req: Request, res: express.Response, next) => {
if (!req.auth.sub) res.sendStatus(HTTP_STATUS.BAD_REQUEST);

req.body.status = 'created';
req.body.request_headers = req.headers;

const session = new models.Session({
...req.body,
ownerId: req.auth.sub,
Expand Down
25 changes: 16 additions & 9 deletions api/controllers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextFunction, Response } from 'express';
import { Request } from 'express-jwt';
import { ISession, Session } from './models';
import { Types, Document } from 'mongoose';
import * as config from './config';

export enum HTTP_STATUS {
OK = 200,
Expand All @@ -28,24 +29,30 @@ export const validateUserCanAccessSession = (onlyOwnerCanAccess: boolean) => {
const sessionId = req.params.session_id;
const userId = req.auth.sub as unknown as number;

if (!sessionId || !userId) {
return res.status(HTTP_STATUS.BAD_REQUEST).json({ err: 'No sessionId or userId found' });
if (!sessionId) {
return res.status(HTTP_STATUS.BAD_REQUEST).json({ err: 'No sessionId found' });
}

if (config.authentication && !userId) {
return res.status(HTTP_STATUS.BAD_REQUEST).json({ err: 'No userId found' });
}

return Session.findById(sessionId)
.then((session) => {
if (!session)
return res
.status(HTTP_STATUS.BAD_REQUEST)
.status(HTTP_STATUS.NOT_FOUND)
.json({ err: 'Could not find session with ID: ' + sessionId });

const isOwner = userId === (session.ownerId || '');
const isInAllowedUserList = session.allowedUsers.some((allowedUser) => allowedUser === userId);
if (config.authentication) {
const isOwner = userId === (session.ownerId || '');
const isInAllowedUserList = session.allowedUsers.some((allowedUser) => allowedUser === userId);

if (onlyOwnerCanAccess && !isOwner) {
return res.status(HTTP_STATUS.UNAUTHORIZED).json({ err: 'unauthorized' });
} else if (!onlyOwnerCanAccess && !isOwner && !isInAllowedUserList) {
return res.status(HTTP_STATUS.UNAUTHORIZED).json({ err: 'unauthorized' });
if (onlyOwnerCanAccess && !isOwner) {
return res.status(HTTP_STATUS.UNAUTHORIZED).json({ err: 'unauthorized' });
} else if (!onlyOwnerCanAccess && !isOwner && !isInAllowedUserList) {
return res.status(HTTP_STATUS.UNAUTHORIZED).json({ err: 'unauthorized' });
}
}

req.ezBIDS = {
Expand Down

0 comments on commit 47be17c

Please sign in to comment.