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 1
1
- 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', () => {
83
83
function mockEnqueueRequest (
84
84
data : unknown ,
85
85
contentType : string = 'application/json' ,
86
- context : { authorization : string } = { authorization : 'Bearer abc' }
86
+ context : { authorization ? : string } = { authorization : 'Bearer abc' }
87
87
) : ReturnType < typeof mockRequest > {
88
88
return mockRequest ( data , contentType , context ) ;
89
89
}
@@ -239,4 +239,24 @@ describe('onEnqueueHandler', () => {
239
239
expectedStatus : 204 ,
240
240
} ) ;
241
241
} ) ;
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
+ } ) ;
242
262
} ) ;
Original file line number Diff line number Diff line change @@ -117,20 +117,21 @@ export function onDispatchHandler<Req = any>(
117
117
throw new https . HttpsError ( 'invalid-argument' , 'Bad Request' ) ;
118
118
}
119
119
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 = {
130
131
uid : authToken . uid ,
131
132
token : authToken ,
132
- } ,
133
- } ;
133
+ } ;
134
+ }
134
135
135
136
const data : Req = https . decode ( req . body . data ) ;
136
137
if ( handler . length === 2 ) {
You can’t perform that action at this time.
0 commit comments