File tree Expand file tree Collapse file tree 3 files changed +35
-13
lines changed Expand file tree Collapse file tree 3 files changed +35
-13
lines changed Original file line number Diff line number Diff line change 11- Adds RTDB Triggers for v2 functions (#1127 )
2+ - Fixes bug where emulated task queue function required auth header (#1154 )
Original file line number Diff line number Diff line change @@ -83,7 +83,7 @@ describe('onEnqueueHandler', () => {
8383 function mockEnqueueRequest (
8484 data : unknown ,
8585 contentType : string = 'application/json' ,
86- context : { authorization : string } = { authorization : 'Bearer abc' }
86+ context : { authorization ? : string } = { authorization : 'Bearer abc' }
8787 ) : ReturnType < typeof mockRequest > {
8888 return mockRequest ( data , contentType , context ) ;
8989 }
@@ -239,4 +239,24 @@ describe('onEnqueueHandler', () => {
239239 expectedStatus : 204 ,
240240 } ) ;
241241 } ) ;
242+
243+ it ( 'should skip auth in emulated environment' , async ( ) => {
244+ const restore = process . env . FUNCTIONS_EMULATOR ;
245+ process . env . FUNCTIONS_EMULATOR = 'true' ;
246+
247+ await runTaskTest ( {
248+ httpRequest : mockEnqueueRequest ( null , 'application/json' , { } ) ,
249+ expectedData : null ,
250+ taskFunction : ( data , context ) => {
251+ expect ( context . auth ) . to . be . undefined ;
252+ return null ;
253+ } ,
254+ taskFunction2 : ( request ) => {
255+ expect ( request . auth ) . to . be . undefined ;
256+ } ,
257+ expectedStatus : 204 ,
258+ } ) ;
259+
260+ process . env . FUNCTIONS_EMULATOR = restore ;
261+ } ) ;
242262} ) ;
Original file line number Diff line number Diff line change @@ -117,20 +117,21 @@ export function onDispatchHandler<Req = any>(
117117 throw new https . HttpsError ( 'invalid-argument' , 'Bad Request' ) ;
118118 }
119119
120- const authHeader = req . header ( 'Authorization' ) || '' ;
121- const token = authHeader . match ( / ^ B e a r e r ( .* ) $ / ) ?. [ 1 ] ;
122- // Note: this should never happen since task queue functions are guarded by IAM.
123- if ( ! token ) {
124- throw new https . HttpsError ( 'unauthenticated' , 'Unauthenticated' ) ;
125- }
126- // We skip authenticating the token since tq functions are guarded by IAM.
127- const authToken = await https . unsafeDecodeIdToken ( token ) ;
128- const context : TaskContext = {
129- auth : {
120+ const context : TaskContext = { } ;
121+ if ( ! process . env . FUNCTIONS_EMULATOR ) {
122+ const authHeader = req . header ( 'Authorization' ) || '' ;
123+ const token = authHeader . match ( / ^ B e a r e r ( .* ) $ / ) ?. [ 1 ] ;
124+ // Note: this should never happen since task queue functions are guarded by IAM.
125+ if ( ! token ) {
126+ throw new https . HttpsError ( 'unauthenticated' , 'Unauthenticated' ) ;
127+ }
128+ // We skip authenticating the token since tq functions are guarded by IAM.
129+ const authToken = await https . unsafeDecodeIdToken ( token ) ;
130+ context . auth = {
130131 uid : authToken . uid ,
131132 token : authToken ,
132- } ,
133- } ;
133+ } ;
134+ }
134135
135136 const data : Req = https . decode ( req . body . data ) ;
136137 if ( handler . length === 2 ) {
You can’t perform that action at this time.
0 commit comments