Skip to main content

Google Cloud Functions Tools

A 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 via pip, run:

pip install google-cloud-functions-tools

CORS

Adds CORS headers to your function. Responds to OPTIONS request 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:

from google_cloud_functions_tools import cors

@cors
def run(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: 3600

However, these default values might change at any time, therefore it is better to specify headers explicitly:

from google_cloud_functions_tools import cors

@cors(origin='sample_origin',
      methods='GET',
      headers='Content-Type',
      max_age=9600)
def run(request):
    return 'Response text', 200

sets the following headers:

Access-Control-Allow-Origin: 'sample_origin'
Access-Control-Allow-Methods: 'GET'
Access-Control-Allow-Headers: 'Content-Type'
Access-Control-Max-Age: 9600

The method warns if Access-Control-Allow-Origin header is set to '*':

UserWarning: Setting Access-Control-Allow-Origin header to '*' is discouraged. It should not be used in production environments.

Verify Firebase ID Token

Authenticates Firebase sessions in cloud functions using firebase_admin's auth.verify_id_token function. A request is required to provide Authorization: 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).

from google_cloud_functions_tools import verify_firebase_id_token

@verify_firebase_id_token
def run(request, token_info):
    return token_info

app_name

app_name argument passes app name to firebase_admin library. The default value is [DEFAULT].

from google_cloud_functions_tools import verify_firebase_id_token

@verify_firebase_id_token(app_name='[DEFAULT]')
def run(request, token_info):
    return token_info

log

Setting log argument to True logs user information to cloud function logs.

from google_cloud_functions_tools import verify_firebase_id_token

@verify_firebase_id_token(log=True)
def run(request, token_info):
    return token_info

logs:

Authenticated for name="Someone", email="someone@example.com"

limit_email_domain_to

Supply limit_email_domain_to argument 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.

from google_cloud_functions_tools import verify_firebase_id_token

@verify_firebase_id_token(limit_email_domain_to='example.com')
def run(request, token_info):
    return token_info

If 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.

Contibuting

Pull requests and issues are welcomed. New functions are planned to be added in time when they are needed.

Metadata

Release files for google-cloud-functions-tools 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for google-cloud-functions-tools 0.2.1
File Size Uploaded
google_cloud_functions_tools-0.2.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for google-cloud-functions-tools 0.2.1
File Interpreter ABI Platform
google_cloud_functions_tools-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / google_cloud_functions_tools-0.2.1.tar.gz

Download URL google_cloud_functions_tools-0.2.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cf3a57afa02b4b4cc2f61c48e54b1e0dff82dee69f39c257b4fcd21fefcaa0b8
BLAKE2b-256 checksum
How to use checksums
c38ba9eeb27593f4004921e8bf6cdcfee63d5288c0bc9fa80c7c315b6b321286
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / google_cloud_functions_tools-0.2.1-py3-none-any.whl

Download URL google_cloud_functions_tools-0.2.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c25b0a7781b61fcaea4588f48c8f97460b07517419d4b52b6279ec000883eb2a
BLAKE2b-256 checksum
How to use checksums
affadd2cd50030cef1c6cd07f9a9cdb3e222017a27d3b1c83acbbc3e769a0df5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page