Skip to main content

No project description provided

Project description

aboutcode.api_auth

Secured APIToken model and related APITokenAuthentication class.

Install

pip install aboutcode.api_auth

Define the APIToken model

In your main models.py module:

from aboutcode.api_auth import AbstractAPIToken

class APIToken(AbstractAPIToken):
    class Meta:
        verbose_name = "API Token"

Generate and apply schema migration:

$ ./manage.py makemigrations
$ ./manage.py migrate

Authenticator settings

Declare your APIToken model location in the API_TOKEN_MODEL setting:

API_TOKEN_MODEL = "your_app.APIToken"  # noqa: S105

Declare the APITokenAuthentication authentication class as one of the REST_FRAMEWORK.DEFAULT_AUTHENTICATION_CLASSES:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "aboutcode.api_auth.APITokenAuthentication",
    ),
}

Views (optional)

Base views are provided for generating and revoking API keys. They handle the token operations and redirect with a success message.

Subclass them in your app to add authentication requirements and configure the success URL and message:

from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy

from aboutcode.api_auth.views import BaseGenerateAPIKeyView
from aboutcode.api_auth.views import BaseRevokeAPIKeyView


class GenerateAPIKeyView(LoginRequiredMixin, BaseGenerateAPIKeyView):
    success_url = reverse_lazy("profile")
    success_message = (
        "Copy your API key now, it will not be shown again: <pre>{plain_key}</pre>"
    )


class RevokeAPIKeyView(LoginRequiredMixin, BaseRevokeAPIKeyView):
    success_url = reverse_lazy("profile")
    success_message = "API key revoked."

Wire them up in your urls.py:

from your_app.views import GenerateAPIKeyView
from your_app.views import RevokeAPIKeyView

urlpatterns = [
    ...
    path("profile/api_key/generate/", GenerateAPIKeyView.as_view(), name="generate-api-key"),
    path("profile/api_key/revoke/", RevokeAPIKeyView.as_view(), name="revoke-api-key"),
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aboutcode_api_auth-0.2.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aboutcode_api_auth-0.2.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file aboutcode_api_auth-0.2.0.tar.gz.

File metadata

  • Download URL: aboutcode_api_auth-0.2.0.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aboutcode_api_auth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5d3cbe19312db64f57f6ce3a63be11966d401e8c891e37290f4273b6893021b9
MD5 08a335aca2523e9f45c02a3805bf2480
BLAKE2b-256 957d97759f2b0008660ca05c7485f65824f2197edb246caa26879ba7d6c5c4c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for aboutcode_api_auth-0.2.0.tar.gz:

Publisher: publish-pypi-release-aboutcode-api-auth.yml on aboutcode-org/dejacode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aboutcode_api_auth-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aboutcode_api_auth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 871181671c26dd27f3ac10ae2d469f0b31489586b13d3f0d14561bc8b5f7eaca
MD5 292f4a3d65e8e6ff4287d1fd8f445c23
BLAKE2b-256 c61122fbf05ee0a7554d73f1ea26128c189c6b866f3f22bc460931dce5963663

See more details on using hashes here.

Provenance

The following attestation bundles were made for aboutcode_api_auth-0.2.0-py3-none-any.whl:

Publisher: publish-pypi-release-aboutcode-api-auth.yml on aboutcode-org/dejacode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page