@@ -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' ;
@@ -32,6 +32,10 @@ export const requireJwtProd = (req: Request, res: Response, next: NextFunction)
3232 req . method === 'OPTIONS' // this is a pre-flight request and those don't send cookies
3333 ) {
3434 next ( ) ;
35+ } else if (
36+ req . path . startsWith ( '/deadline-notifications' ) // task deadline notification endpoint
37+ ) {
38+ notificationEndpointAuth ( req , res , next ) ;
3539 } else {
3640 const { token } = req . cookies ;
3741
@@ -59,6 +63,10 @@ export const requireJwtDev = (req: Request, res: Response, next: NextFunction) =
5963 req . path === '/users' // dev login needs the list of users to log in
6064 ) {
6165 next ( ) ;
66+ } else if (
67+ req . path . startsWith ( '/deadline-notifications' ) // task deadline notification endpoint
68+ ) {
69+ notificationEndpointAuth ( req , res , next ) ;
6270 } else {
6371 const devUserId = req . headers . authorization ;
6472
@@ -70,6 +78,20 @@ export const requireJwtDev = (req: Request, res: Response, next: NextFunction) =
7078 }
7179} ;
7280
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+
7395/**
7496 * get the user making the request.
7597 * @param res - we use the response because that's where we stored the userId data during jwt validation
@@ -85,6 +107,9 @@ export const getCurrentUser = async (res: Response): Promise<User> => {
85107
86108export type UserWithSettings = User & {
87109 userSettings : User_Settings | null ;
110+ } ;
111+
112+ export type UserWithSecureSettings = UserWithSettings & {
88113 userSecureSettings : User_Secure_Settings | null ;
89114} ;
90115
@@ -94,7 +119,7 @@ export type UserWithSettings = User & {
94119 * @returns the user with their user settings
95120 * @throws if no user with the userId exists
96121 */
97- export const getCurrentUserWithUserSettings = async ( res : Response ) : Promise < UserWithSettings > => {
122+ export const getCurrentUserWithUserSettings = async ( res : Response ) : Promise < UserWithSecureSettings > => {
98123 const { userId } = res . locals ;
99124 const user = await prisma . user . findUnique ( {
100125 where : { userId } ,
0 commit comments