Skip to main content

A small class that helps with authentication and CORS for HTTP-triggered cloud functions written in Python and hosted on GCP

Please note: This is not an officially supported Google product.

If you are using OAuth to authenticate to a Cloud Function written in Python and hosted on GCP, and your client is running in a browser, this is for you. If not, you probably won't find this useful.

This class does three things:

  • Responds correctly and configurably to the HTTP OPTIONS method used by browsers to do pre-flight checks as part of CORS
  • Retrieves an OAuth2 token supplied in the Authorization HTTP header, validates it, and then fetches the information encoded by the token
  • Gives you back either a valid token, or a response to send back to the user in case a valid token can't be found

Installation

You can install the latest version of this package using pip:

python3 -m pip install --user gcloud-flask-oauth-cors

It's hosted on pypi: https://pypi.org/project/gcloud-flask-oauth-cors/

Use

If you don't have one, create an OAuth client ID and pass it as an environment variable to your cloud function. Then, you can use the following:

import gcloud_flask_oauth_cors as oauth

def my_function_name(request):
    auth = oauth.Auth(os.getenv("OAUTH_CLIENT_ID"))
    id_info = auth.get_id_info(request)
    if id_info is None:
        # If we were called with the HTTP OPTIONS method, this will return the relevant CORS headers.
        # If another HTTP method was used and we can't authenticate, this will return a 401 (Unauthorized)
        return auth.get_response()

    # Do something with the id_info, for example:
    print(id_info["sub"])

On the client side, you can use Google Sign-in. Make sure you pass your id_token in any requests like this:

let xhr = new XMLHttpRequest();
xhr.setRequestHeader("Authorization", "Bearer " + id_token);

Metadata

Release files for gcloud-flask-oauth-cors 0.1.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 gcloud-flask-oauth-cors 0.1.1
File Size Uploaded Provenance Actions
gcloud-flask-oauth-cors-0.1.1.tar.gz 3.2 kB Without provenance Details for gcloud-flask-oauth-cors-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gcloud-flask-oauth-cors 0.1.1
File Interpreter ABI Platform Actions
gcloud_flask_oauth_cors-0.1.1-py3-none-any.whl Python 3 none any Details for gcloud_flask_oauth_cors-0.1.1-py3-none-any.whl

Total release size: 11.4 kB

Release files / gcloud-flask-oauth-cors-0.1.1.tar.gz

Download URL gcloud-flask-oauth-cors-0.1.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
afa1a18c5cfc2eedc3e438bc7f48aece600f34f4adae922182ea59094c3a55b7
BLAKE2b-256 checksum
How to use checksums
61192d7f3d5c079bc4ad80a20f59ee580f213c17a55d3d9dd1c53a31caa99c1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / gcloud_flask_oauth_cors-0.1.1-py3-none-any.whl

Download URL gcloud_flask_oauth_cors-0.1.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54244aae0cd03229801c74e993223949eb053373b0f53849f00b110408be43e2
BLAKE2b-256 checksum
How to use checksums
516a9dc480642386f4f25eeeb6147cda503a6572e0825cffe64168a336747b34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Security for gcloud-flask-oauth-cors 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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