Skip to main content

Django GCP REST Auth

https://img.shields.io/pypi/v/dj_gcp_rest_auth.svg Gitlab pipeline status Documentation Status

Provides a way to determine if an incoming request is coming from a GCP service account that you have authorized. You can also use this module to as a way of wrapping an identity token which on its own has no access claims. The wrapped token is an authenticated GCP token and the inner token specifies the Django user.

For instance, an authenticated user requests an identity token using the /identity/ endpoint. This token on its own can not be used to access services. The user then gives this identity token to an IoT device that can obtain its own GCP Token. When the IoT device wants to make a request on behalf of the authenticated user, it adds the X-GCP-IDTOKEN header.

In another example, you may be using Google Functions from a pub/sub model to make a request to an API endpoint on your Django server. In this case your Google Function will have a default service account and you can obtain an id-token within your Google Function. An id-token is a JWT token that contains various claims. You will need to get an id-token that contains the email claim. You can pass this token into your request using the Authorization header. This module can then be used to verify the token and make sure that the identity of the user belongs a list of allowed service accounts that you configure.

Installation

Install dj_gcp_rest_auth from pip

$ pip install dj_gcp_rest_auth

Update your top level settings.py

GCP_REST_AUTH = {
    'IDENTITY_TOKEN_LIFETIME_DAYS': 7,
    'ALLOWED_SERVICE_ACCOUNTS': ['11111122222-compute@developer.gserviceaccount.com']
}

IDENTITY_TOKEN_LIFETIME_DAYS is optional and if specified specify the lifetime of an identity token in days. Identity tokens can be generated from this package.

ALLOWED_SERVICE_ACCOUNTS is a list of regex patterns representing the service account emails that are allowed to use your API.

In your views, set

from dj_gcp_rest_auth.authentication import GCPTokenAuthentication

class MyView(GenericAPIView):
    authentication_classes = ( GCPTokenAuthentication, )

Optionally, in your urls.py, set

import dj_gcp_rest_auth

path('', include(dj_gcp_rest_auth.urls))

Optionally, you can use the IsGCPUser custom permission. This permission is useful to indicate that the GCP Token is valid and from an authorized service account.

from dj_gcp_rest_auth.permissions import IsGCPUser

Obtaining an id-token

There are several ways to obtain an id-token from your Google service (Compute, GAE, Cloud Run, Function, etc.).

The following method only depends on curl and makes use of the internal meta data to retrieve the id-token

curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=arbitrary&format=full"

Authorization

Once you obtain an id-token, your GCP service can authenticate with Django by passing your id-token with the Authorization header as shown in the following request

curl -H "X-GCP-IDTOKEN: <user-id-token>" -H "Authorization: Bearer <my-id-token>" http://localhost:8000/protected/resource

The user-id-token is obtained from the /identity/ endpoint.

Endpoints

This package can be used to expose an endpoint to generate an identity token for the authenticated user

GET /identity

Metadata

Release files for dj-gcp-rest-auth 0.6.0

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

Source distribution (sdist)

Source distribution for dj-gcp-rest-auth 0.6.0
File Size Uploaded Provenance Actions
dj_gcp_rest_auth-0.6.0.tar.gz 5.9 kB Without provenance Details for dj_gcp_rest_auth-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dj-gcp-rest-auth 0.6.0
File Interpreter ABI Platform Actions
dj_gcp_rest_auth-0.6.0-py3-none-any.whl Python 3 none any Details for dj_gcp_rest_auth-0.6.0-py3-none-any.whl

Total release size: 13.8 kB

Release files / dj_gcp_rest_auth-0.6.0.tar.gz

Download URL dj_gcp_rest_auth-0.6.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
90bca4367498b7fbde5f0bbc6a38d697e697c4153ecc35bed29e36b7e24d0ea9
BLAKE2b-256 checksum
How to use checksums
396088068d718e22340de6b1e74b01db6632e0f5c82e473765514b8a948fa66c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

Release files / dj_gcp_rest_auth-0.6.0-py3-none-any.whl

Download URL dj_gcp_rest_auth-0.6.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9758a135400045374f87b1f62e75f94dde1240a3f967d16853a8e1b3cd354bde
BLAKE2b-256 checksum
How to use checksums
b403728846b712694edd5ac4f6828186f556ebe02dc036fbe4e71901f7637ad4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

Security for dj-gcp-rest-auth 0.6.0

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.

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