Skip to main content

A package for django rest framework to handle clerk authentication. It includes a auth middleware, permission and a wrapper around all the user related apis.

Project description

Clerk Django

"Clerk Django" is a Python library that offers middleware and permissions for authenticating requests when your authentication process is managed by Clerk.

Installation

Use the package manager pip to install clerk_django.

pip install clerk-django

Usage

The first step will be to set your CLERK_SECRET_KEY and CLERK_PEM_PUBLIC_KEY in your environment variables. Then go to your settings.py file and set the ALLOWED_PARTIES config.

ALLOWED_PARTIES = ["http://localhost:5173"]

To use the ClerkAuthMiddleware, add clerk_django.middlewares.clerk.ClerkAuthMiddleware to your middlewares.

MIDDLEWARE = [
    # other middlewares
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'clerk_django.middlewares.clerk.ClerkAuthMiddleware'
]

Once you have added the middleware, you can access the user from the request using request.clerk_user. clerk_user has 3 properties.

  • is_authenticated - It is true/false based on if the user is authenticated or not.
  • id - Clerk user id. This key will be available only if the user is authenticated.
  • decoded_token - This is the value after decoding the token which might contain useful user info based on how you have configured your JWTTemplates in Clerk. This key will be available only if the user is authenticated.
from rest_framework import viewsets
from rest_framework.response import Response
from permissions.clerk import ClerkAuthenticated

class ExampleViewset(viewsets.ViewSet):
    permission_classes = [ClerkAuthenticated]

    def list(self, request):
        user_id = request.clerk_user.get('id')
        return Response()

There is a wrapper around the User related apis. All the different function can be found in clerk backend sdk and clerk backend apis

from clerk_django.client import ClerkClient

#Set your CLERK_SECRET_KEY and CLERK_PEM_PUBLIC_KEY in your environment variables.

cc = ClerkClient()

user_details = cc.users.getUser(user_id=user_id)

#Check the clerk documentation for the list of query params.
user_list = cc.users.getUserList({
               "email_address" : ["reachsahilverma@gmail.com"]
            })

I have added all the functions mentioned in the Clerk Backend SDK - User. You just need to use user_id instead of userId. Also in case of verifyPassword, just pass the user_id and password directly. In all the other functions you can pass the params as required and mentioned in the documentation.

res = cc.users.verifyPassword(user_id,password)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

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

clerk_django-1.0.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

clerk_django-1.0.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file clerk_django-1.0.3.tar.gz.

File metadata

  • Download URL: clerk_django-1.0.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.9

File hashes

Hashes for clerk_django-1.0.3.tar.gz
Algorithm Hash digest
SHA256 3be54a3ab66514dfa148b85c0499b098c6f035054d7987f8a460be648aa76b84
MD5 5f6b3d5ba56fdda61f8dd3d62e149140
BLAKE2b-256 33530cfe88f1a1ae9d3cfdbfc09605a20864e66ceeca1f6ff79196d2ec025060

See more details on using hashes here.

File details

Details for the file clerk_django-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for clerk_django-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ea262a364216bfd7125d2485ff4b8a6da3adf97006e622594edb37d3c5e0bf6e
MD5 d4ce9742789ee699cd6f3338cb58826f
BLAKE2b-256 2a0cbb2d7007c037dc63f5b72333fefb08bfba2ac0bac15acc4822ef2bf1669e

See more details on using hashes here.

Supported by

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