11import {
22 ERROR_WHILE_CREATING_REQUEST ,
33 IMPERSONATION_LOG_TYPE ,
4+ INVALID_ACTION_PARAM ,
45 LOG_ACTION ,
56 REQUEST_DOES_NOT_EXIST ,
67 REQUEST_LOG_TYPE ,
@@ -11,7 +12,7 @@ import { createImpersonationRequest, getImpersonationRequestById, updateImperson
1112import { fetchUser } from "../models/users" ;
1213import { addLog } from "./logService" ;
1314import { User } from "../typeDefinitions/users" ;
14- import { NotFound , Forbidden } from "http-errors" ;
15+ import { NotFound , Forbidden , BadRequest } from "http-errors" ;
1516import { CreateImpersonationRequestServiceBody , ImpersonationRequest , ImpersonationSessionServiceBody , UpdateImpersonationRequestDataResponse } from "../types/impersonationRequest" ;
1617import { Timestamp } from "firebase-admin/firestore" ;
1718import config from "config" ;
@@ -158,7 +159,7 @@ export const stopImpersonationService = async (
158159 throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
159160 }
160161 if ( impersonationRequest . impersonatedUserId !== body . userId ) {
161- throw new Forbidden ( "You are not authorized for this action " ) ;
162+ throw new Forbidden ( "You are not allowed for this operation at the moment " ) ;
162163 }
163164
164165 const newBody = { endedAt : Timestamp . now ( ) } ;
@@ -207,37 +208,48 @@ export const generateImpersonationTokenService = async (
207208 requestId : string ,
208209 action : string
209210) : Promise < { name : string , value : string , options : object } > => {
210- try {
211+ try {
211212 const request = await getImpersonationRequestById ( requestId ) ;
212213 if ( ! request ) {
213214 throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
214215 }
215- const impersonatedUserId = request . impersonatedUserId ;
216- const userId = request . userId ;
217- const cookieName = config . get ( "userToken.cookieName" ) as string ;
218- const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) ;
216+
217+ const { userId, impersonatedUserId } = request ;
218+ const cookieName = config . get < string > ( "userToken.cookieName" ) ;
219+ const rdsUiUrl = new URL ( config . get < string > ( "services.rdsUi.baseUrl" ) ) ;
220+ const ttlInSeconds = Number ( config . get ( "userToken.ttl" ) ) ;
221+
219222 let token : string ;
220- if ( action === "START" ) {
221- token = await authService . generateImpersonationAuthToken ( { userId, impersonatedUserId } ) ;
222- } else if ( action === "STOP" ) {
223- token = await authService . generateAuthToken ( { userId } ) ;
224- } else {
225- throw new Forbidden ( "Action can be only START/STOP" ) ;
223+
224+ switch ( action ) {
225+ case "START" :
226+ token = await authService . generateImpersonationAuthToken ( { userId, impersonatedUserId } ) ;
227+ break ;
228+
229+ case "STOP" :
230+ token = await authService . generateAuthToken ( { userId } ) ;
231+ break ;
232+
233+ default :
234+ throw new BadRequest ( INVALID_ACTION_PARAM ) ;
226235 }
227236
228237 return {
229238 name : cookieName ,
230239 value : token ,
231240 options : {
232241 domain : rdsUiUrl . hostname ,
233- expires : new Date ( Date . now ( ) + Number ( config . get ( "userToken.ttl" ) ) * 1000 ) ,
242+ expires : new Date ( Date . now ( ) + ttlInSeconds * 1000 ) ,
234243 httpOnly : true ,
235244 secure : true ,
236245 sameSite : "lax" ,
237- }
246+ } ,
238247 } ;
239248 } catch ( error ) {
240- logger . error ( "Error while generating impersonation token" , error ) ;
249+ logger . error (
250+ `Error generating impersonation token for requestId=${ requestId } , action=${ action } ` ,
251+ error
252+ ) ;
241253 throw error ;
242254 }
243255} ;
0 commit comments