Skip to main content

Knoxth uses Knox tokens to provide token-level authorization management for DRF viewsets

Project description



Knoxth - Auth for Knox

A authorization module for django built on top of DRF and Knox.

pipeline status GitHub coverage report

IntroductionInstallationHow To UseDocumentationLicense

Introduction

Knoxth uses Knox tokens to provide token-level authorization management for DRF viewsets.

Django Rest Frameowork is an amazingly simple to use and easy frameowork to write REST APIs in Django. With Knox, you can secure your API with Access Tokens. Yet, there is a gap left by Knox, the need for context-specific authentication system built on top of knox.

Users will want to customize the "scope" of each token they create. This way, users may create different tokens for separate usecases.

Let's take an example. Let us say you are developinga rest api that lets it's create and manage TODO lists. Now, you have a viewset that handles all CRUD operations, but you want to provide a way for users to manage access to their todo lists on a token level. So, a user may want to create a token, with which they can only read TODO lists, but not modify them.

This is where knoxth comes in. Knoxth implements contextual authorization of DRF viewsets on token level, using Knox Tokens.

Installation

Knox should be installed with pip

1. Install knoxth

pip install knoxth

For pipenv projects, it can also be installed as such

pipenv install knoxth

After installing knoxth, you will need to setup knoxth to work with your existing project. Before seting up knoxth, make sure you have rest_framework and knox setup and ready to go.

2. Add to INSTALLED_APPS

Add rest_framework, knox and knoxth to your INSTALLED_APPS, and add rest_framework.authtoken if you removed it while setting up knox.

INSTALLED_APPS = (
  ...
  'rest_framework',
  'rest_framework.authtoken',
  'knox',
  'knoxth',
  ...
)

3. Setup default Authentication class

Make knox's TokenAuthentication your default authentication class for django-rest-framework:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',),
    ...
}

4. Include knoxth URLS

Knoxth provides a url config ready with its four default views routed.

This can easily be included in your url config:

urlpatterns = [
  #...snip...
  url(r'api/auth/', include('knoxth.urls'))
  #...snip...
]

Note It is important to use the string syntax and not try to import knoxth.urls, as the reference to the User model will cause the app to fail at import time.

The views would then acessible as:

Endpoint Description
/api/auth/authorize Authorize username & password. Return authorization code.
/api/auth/login Accept authorization code and return access token
/api/auth/logout Logout the user and delete the access token and authorization code.
/api/auth/logoutall Same as logout, but logs out of all running sessions.

they can also be looked up by name:

reverse('knoxth_login')
reverse('knoxth_logout')
reverse('knoxth_logoutall')
reverse('knoxth_authorize')

5. Migrate

Apply the migrations for the models

python manage.py migrate

How To Use

Refer to our documentation for details, or follow our getting started guide.

License

GNU GPL V3


GitHub @ayys  ·  Twitter @habuayush

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

knoxth-0.0.3.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

knoxth-0.0.3-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file knoxth-0.0.3.tar.gz.

File metadata

  • Download URL: knoxth-0.0.3.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for knoxth-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e45b5b90dc52c7f5f91bb04cbcf3cda5e8f578e0bedf85d8d21d17918fcf175d
MD5 3bade1b4f8720ed71bcce4ba8b58d2b4
BLAKE2b-256 981f399bd6a039f87fc79faf1ec548443f7768b36404a2b6227d14aea5ad1f71

See more details on using hashes here.

File details

Details for the file knoxth-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: knoxth-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for knoxth-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9c2269b637ffe25365bba447b77e863e3652f70ae0d795d5a8967ca6c945bc2d
MD5 7aafb7b1e2a51121a80b430b7f2d969a
BLAKE2b-256 ab494992ff84b45569371695d9111f36c95ec964d426683a2e13e03b5c370716

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