Package: google-cloud-functions-tools
Google Cloud Functions ToolsA set of utility functions are provided, especially developed for Google Cloud Functions (might work on other serverless platforms too). The functions planned are mostly to be decorators.To install viapip, run:pipinstallgoogle-cloud-functions-toolsCORSAdds CORS headers to your function. Responds toOPTIONSrequest by sending CORS headers and without triggering the cloud function. Provided that Google Cloud Funtions use Flask for underlying logic, the return types must be compatible with Flask's.For example:fromgoogle_cloud_functions_toolsimportcors@corsdefrun(request):return'Response text'sets the following headers by default:Access-Control-Allow-Origin: '*'
Access-Control-Allow-Methods: 'POST'
Access-Control-Allow-Headers: ''
Access-Control-Max-Age: 3600However, these default values might change at any time, therefore it is better to specify headers explicitly:fromgoogle_cloud_functions_toolsimportcors@cors(origin='sample_origin',methods='GET',headers='Content-Type',max_age=9600)defrun(request):return'Response text',200sets the following headers:Access-Control-Allow-Origin: 'sample_origin'
Access-Control-Allow-Methods: 'GET'
Access-Control-Allow-Headers: 'Content-Type'
Access-Control-Max-Age: 9600The method warns ifAccess-Control-Allow-Originheader is set to'*':UserWarning: Setting Access-Control-Allow-Origin header to '*' is discouraged. It should not be used in production environments.Verify Firebase ID TokenAuthenticates Firebase sessions in cloud functions usingfirebase_admin'sauth.verify_id_tokenfunction. A request is required to provideAuthorization: Bearer <token>HTTP header.If authentication fails, an HTTP 401 Unauthorized error is returned without running the actual function. If an Authorization header is not supplied, an HTTP 400 Bad Request is returned immediately. In case of successful authorization, token information is sent to the actual function as a second argument (after request object).fromgoogle_cloud_functions_toolsimportverify_firebase_id_token@verify_firebase_id_tokendefrun(request,token_info):returntoken_infoapp_nameapp_nameargument passes app name tofirebase_adminlibrary. The default value is[DEFAULT].fromgoogle_cloud_functions_toolsimportverify_firebase_id_token@verify_firebase_id_token(app_name='[DEFAULT]')defrun(request,token_info):returntoken_infologSettinglogargument toTruelogs user information to cloud function logs.fromgoogle_cloud_functions_toolsimportverify_firebase_id_token@verify_firebase_id_token(log=True)defrun(request,token_info):returntoken_infologs:Authenticated for name="Someone", email="someone@example.com"limit_email_domain_toSupplylimit_email_domain_toargument to limit cloud function operation to users having a specifig email address domain only. Note that this is only a simple string check without any RegEx support.fromgoogle_cloud_functions_toolsimportverify_firebase_id_token@verify_firebase_id_token(limit_email_domain_to='example.com')defrun(request,token_info):returntoken_infoIf the users token does not provide an email address, HTTP 401 Unauthorized is returned with the following error message:Email domain authentication is active but the token did not provide email information.Users having different email addresses other than the allowed one get HTTP 401 Unauthorized with the following error message:Only users with valid example.com email addresses can perform this operation.ContibutingPull requests and issues are welcomed. New functions are planned to be added in time when they are needed.