25
25
import firebase_functions .core as _core
26
26
from functions_framework import logging as _logging
27
27
28
- from firebase_functions .options import HttpsOptions
28
+ from firebase_functions .options import HttpsOptions , _GLOBAL_OPTIONS
29
29
from flask import Request , Response , make_response as _make_response , jsonify as _jsonify
30
30
from flask_cors import cross_origin as _cross_origin
31
31
@@ -346,7 +346,8 @@ class CallableRequest(_typing.Generic[_core.T]):
346
346
_C2 = _typing .Callable [[CallableRequest [_typing .Any ]], _typing .Any ]
347
347
348
348
349
- def _on_call_handler (func : _C2 , request : Request ) -> Response :
349
+ def _on_call_handler (func : _C2 , request : Request ,
350
+ enforce_app_check : bool ) -> Response :
350
351
try :
351
352
if not _util .valid_on_call_request (request ):
352
353
_logging .error ("Invalid request, unable to process." )
@@ -362,23 +363,22 @@ def _on_call_handler(func: _C2, request: Request) -> Response:
362
363
raise HttpsError (FunctionsErrorCode .UNAUTHENTICATED ,
363
364
"Unauthenticated" )
364
365
365
- # TODO support for `allowInvalidAppCheckToken`
366
- if token_status . app == _util .OnCallTokenState .INVALID :
366
+ if enforce_app_check and token_status . app in (
367
+ _util .OnCallTokenState .MISSING , _util . OnCallTokenState . INVALID ) :
367
368
raise HttpsError (FunctionsErrorCode .UNAUTHENTICATED ,
368
369
"Unauthenticated" )
369
-
370
- if token_status .auth_token is not None :
370
+ if token_status .app == _util .OnCallTokenState .VALID and token_status .app_token is not None :
371
371
context = _dataclasses .replace (
372
372
context ,
373
- auth = AuthData (token_status .auth_token [ "uid " ],
374
- token_status .auth_token ),
373
+ app = AppCheckData (token_status .app_token [ "sub " ],
374
+ token_status .app_token ),
375
375
)
376
376
377
- if token_status .app_token is not None :
377
+ if token_status .auth_token is not None :
378
378
context = _dataclasses .replace (
379
379
context ,
380
- app = AppCheckData (token_status .app_token [ "sub " ],
381
- token_status .app_token ),
380
+ auth = AuthData (token_status .auth_token [ "uid " ],
381
+ token_status .auth_token ),
382
382
)
383
383
384
384
instance_id = request .headers .get ("Firebase-Instance-ID-Token" )
@@ -474,13 +474,26 @@ def on_call_inner_decorator(func: _C2):
474
474
if options .cors is not None and options .cors .cors_origins is not None :
475
475
origins = options .cors .cors_origins
476
476
477
+ # Default to False.
478
+ enforce_app_check = False
479
+ # If the global option is set, use that.
480
+ if options .enforce_app_check is None and _GLOBAL_OPTIONS .enforce_app_check is not None :
481
+ enforce_app_check = _GLOBAL_OPTIONS .enforce_app_check
482
+ # If the global option is not set, use the local option.
483
+ elif options .enforce_app_check is not None :
484
+ enforce_app_check = options .enforce_app_check
485
+
477
486
@_cross_origin (
478
487
methods = "POST" ,
479
488
origins = origins ,
480
489
)
481
490
@_functools .wraps (func )
482
491
def on_call_wrapped (request : Request ):
483
- return _on_call_handler (func , request )
492
+ return _on_call_handler (
493
+ func ,
494
+ request ,
495
+ enforce_app_check ,
496
+ )
484
497
485
498
_util .set_func_endpoint_attr (
486
499
on_call_wrapped ,
0 commit comments