@@ -2,7 +2,7 @@ import jwt from 'jsonwebtoken';
22import { Request , Response , NextFunction } from 'express' ;
33import { JwtPayload , VerifyErrors } from 'jsonwebtoken' ;
44import prisma from '../prisma/prisma' ;
5- import { NotFoundException } from './errors.utils' ;
5+ import { HttpException , NotFoundException } from './errors.utils' ;
66import { User , User_Secure_Settings , User_Settings } from '@prisma/client' ;
77
88const TOKEN_SECRET = process . env . TOKEN_SECRET || 'i<3security' ;
@@ -33,17 +33,9 @@ export const requireJwtProd = (req: Request, res: Response, next: NextFunction)
3333 ) {
3434 next ( ) ;
3535 } else if (
36- req . path === '/tasks/sendTaskDeadlineSlackNotifications' // task deadline notification endpoint
36+ req . path . startsWith ( '/deadline-notifications' ) // task deadline notification endpoint
3737 ) {
38- const { authorization } = req . headers ;
39- const { NOTIFICATION_ENDPOINT_SECRET } = process . env ;
40-
41- if ( ! authorization ) return res . status ( 401 ) . json ( { message : 'Authentication Failed: Secret not found!' } ) ;
42-
43- if ( authorization !== NOTIFICATION_ENDPOINT_SECRET )
44- return res . status ( 401 ) . json ( { message : 'Authentication Failed: Invalid secret!' } ) ;
45-
46- next ( ) ;
38+ notificationEndpointAuth ( req , res , next ) ;
4739 } else {
4840 const { token } = req . cookies ;
4941
@@ -72,17 +64,9 @@ export const requireJwtDev = (req: Request, res: Response, next: NextFunction) =
7264 ) {
7365 next ( ) ;
7466 } else if (
75- req . path === '/tasks/sendTaskDeadlineSlackNotifications' // task deadline notification endpoint
67+ req . path . startsWith ( '/deadline-notifications' ) // task deadline notification endpoint
7668 ) {
77- const { authorization } = req . headers ;
78- const { NOTIFICATION_ENDPOINT_SECRET } = process . env ;
79-
80- if ( ! authorization ) return res . status ( 401 ) . json ( { message : 'Authentication Failed: Secret not found!' } ) ;
81-
82- if ( authorization !== NOTIFICATION_ENDPOINT_SECRET )
83- return res . status ( 401 ) . json ( { message : 'Authentication Failed: Invalid secret!' } ) ;
84-
85- next ( ) ;
69+ notificationEndpointAuth ( req , res , next ) ;
8670 } else {
8771 const devUserId = req . headers . authorization ;
8872
@@ -94,6 +78,20 @@ export const requireJwtDev = (req: Request, res: Response, next: NextFunction) =
9478 }
9579} ;
9680
81+ const notificationEndpointAuth = ( req : Request , res : Response , next : NextFunction ) => {
82+ const { authorization } = req . headers ;
83+ const { NOTIFICATION_ENDPOINT_SECRET } = process . env ;
84+
85+ if ( ! NOTIFICATION_ENDPOINT_SECRET ) throw new HttpException ( 500 , 'Notification endpoint secret not found!' ) ;
86+
87+ if ( ! authorization ) return res . status ( 401 ) . json ( { message : 'Authentication Failed: Secret not found!' } ) ;
88+
89+ if ( authorization !== NOTIFICATION_ENDPOINT_SECRET )
90+ return res . status ( 401 ) . json ( { message : 'Authentication Failed: Invalid secret!' } ) ;
91+
92+ next ( ) ;
93+ } ;
94+
9795/**
9896 * get the user making the request.
9997 * @param res - we use the response because that's where we stored the userId data during jwt validation
0 commit comments