|
25 | 25 |
|
26 | 26 | _C = _typing.Callable[[CallableRequest[_typing.Any]], _typing.Any]
|
27 | 27 |
|
| 28 | +def _on_call_handler(func: _C2, |
| 29 | + request: Request, |
| 30 | + enforce_app_check: bool, |
| 31 | + verify_token: bool = True) -> Response: |
| 32 | + try: |
| 33 | + if not _util.valid_on_call_request(request): |
| 34 | + _logging.error("Invalid request, unable to process.") |
| 35 | + raise HttpsError(FunctionsErrorCode.INVALID_ARGUMENT, "Bad Request") |
| 36 | + context: CallableRequest = CallableRequest( |
| 37 | + raw_request=request, |
| 38 | + data=_json.loads(request.data)["data"], |
| 39 | + ) |
| 40 | + |
| 41 | + token_status = _util.on_call_check_tokens(request, |
| 42 | + verify_token=verify_token) |
| 43 | + |
| 44 | + if token_status.auth == _util.OnCallTokenState.INVALID: |
| 45 | + raise HttpsError(FunctionsErrorCode.UNAUTHENTICATED, |
| 46 | + "Unauthenticated") |
| 47 | + |
| 48 | + if enforce_app_check and token_status.app in ( |
| 49 | + _util.OnCallTokenState.MISSING, _util.OnCallTokenState.INVALID): |
| 50 | + raise HttpsError(FunctionsErrorCode.UNAUTHENTICATED, |
| 51 | + "Unauthenticated") |
| 52 | + if token_status.app == _util.OnCallTokenState.VALID and token_status.app_token is not None: |
| 53 | + context = _dataclasses.replace( |
| 54 | + context, |
| 55 | + app=AppCheckData(token_status.app_token["sub"], |
| 56 | + token_status.app_token), |
| 57 | + ) |
| 58 | + |
| 59 | + if token_status.auth_token is not None: |
| 60 | + context = _dataclasses.replace( |
| 61 | + context, |
| 62 | + auth=AuthData( |
| 63 | + token_status.auth_token["uid"] |
| 64 | + if "uid" in token_status.auth_token else None, |
| 65 | + token_status.auth_token), |
| 66 | + ) |
| 67 | + |
| 68 | + instance_id = request.headers.get("Firebase-Instance-ID-Token") |
| 69 | + if instance_id is not None: |
| 70 | + # Validating the token requires an http request, so we don't do it. |
| 71 | + # If the user wants to use it for something, it will be validated then. |
| 72 | + # Currently, the only real use case for this token is for sending |
| 73 | + # pushes with FCM. In that case, the FCM APIs will validate the token. |
| 74 | + context = _dataclasses.replace( |
| 75 | + context, |
| 76 | + instance_id_token=request.headers.get( |
| 77 | + "Firebase-Instance-ID-Token"), |
| 78 | + ) |
| 79 | + result = _core._with_init(func)(context) |
| 80 | + return _jsonify(result=result) |
| 81 | + # Disable broad exceptions lint since we want to handle all exceptions here |
| 82 | + # and wrap as an HttpsError. |
| 83 | + # pylint: disable=broad-except |
| 84 | + except Exception as err: |
| 85 | + if not isinstance(err, HttpsError): |
| 86 | + _logging.error("Unhandled error: %s", err) |
| 87 | + err = HttpsError(FunctionsErrorCode.INTERNAL, "INTERNAL") |
| 88 | + status = err._http_error_code.status |
| 89 | + return _make_response(_jsonify(error=err._as_dict()), status) |
| 90 | + |
28 | 91 |
|
29 | 92 | @_util.copy_func_kwargs(_options.TaskQueueOptions)
|
30 | 93 | def on_task_dispatched(**kwargs) -> _typing.Callable[[_C], Response]:
|
|
0 commit comments