Skip to main content

Temporary Django REST Framework permission-scoped token

Project description

Build Status Requirements Status

rest_framework_tmp_scoped_token provides a Django REST Framework-compatible system to generate and validate signed authorization tokens. Generated tokens contain the ID of a user on whose behalf the token bearer authenticates, a white-list of HTTP verbs and API endpoints that the bearer is permitted to access, an max-lifespan of the token, and a note about the intended recipient.

Usage

Authorization

Add rest_framework_tmp_scoped_token.TokenAuth to the DEFAULT_AUTHENTICATION_CLASSES section of your REST_FRAMEWORK settings in settings.py:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        # ... Your other forms of auth
        'rest_framework_tmp_scoped_token.TokenAuth',
    )
}

To authenticate with an temporary permissions token, make a request with the token included in either:

  • the Authorization HTTP header with a TmpToken keyword:

    Authorization: TmpToken eyJzb21ldGhpbmctc2VjcmV0IjoiaG9wZSBub2JvZHkgc2VlcyB0aGlzIn0:1d47N6:woJG0EgLNDb0OjYQmCbsjniP-2Y
  • a TOKEN GET query parameter:

    /api/?TOKEN=eyJzb21ldGhpbmctc2VjcmV0IjoiaG9wZSBub2JvZHkgc2VlcyB0aGlzIn0:1d47N6:woJG0EgLNDb0OjYQmCbsjniP-2Y

If you would like to customize either the Authorization header keyword or the GET query parameter used, you can subclass the rest_framework_tmp_scoped_token.TokenAuth class and override the keyword or get_param values.

Token

To generate a token, use the rest_framework_tmp_scoped_token.TokenManager class. The token encompasses the following information:

  • user: User that will be authenticated by token.

  • endpoints: key:value pairs of HTTP methods and endpoint roots that token is authorized to access. The following values would authorize the token to make GET requests to any endpoints that begin with ‘api/v1/foo:

    {'GET': ['/api/v1/foo']}

    NOTE: This this token will not override any existing permissions for its associatted User within the system. It only adds further restrictions to the endpoints that can be accessed.

  • max_age: How long, in seconds, the token will be valid. By default, tokens will be valid for 1 hour. Non-expiring tokens are not supported.

  • recipient: (Optional) A textual description of the recipient for which this token was intended. No validation is done with this data, however it is appended to the request as a X-API-Token-Recipient header by the accompanying DRF authentication scheme. This is for tracking purposes

NOTE: The tokens are signed via Django’s `signing <https://docs.djangoproject.com/en/dev/topics/signing/>`__ facility. It is important to know that the tokens are not encrypted, they are simply signed. For this reason, you should not include any sensitive/secret information in the tokens. For an example, notice how easy it is to view the contents of a signed string:

In [1]: from django.core import signing

In [2]: t = signing.dumps({'something-secret': 'hope nobody sees this'})

In [3]: print(t)
eyJzb21ldGhpbmctc2VjcmV0IjoiaG9wZSBub2JvZHkgc2VlcyB0aGlzIn0:1d47N6:woJG0EgLNDb0OjYQmCbsjniP-2Y

In [4]: import base64

In [5]: def b64_decode(s):
   ...:     pad = b'=' * (-len(s) % 4)
   ...:     return base64.urlsafe_b64decode(s + pad)
   ...:

In [6]: print(b64_decode(t.encode('utf8')))
b'{"something-secret":"hope nobody sees this"}5w\x8e\xcd\xeb\n\t\x1bA ,\xd0\xdb\xd0\xe8\xd8B`\x9b\xb29\xe2?\xed\x98'

Contributing

Testing

pip install -r requirements-test.txt
./runtests

Deploying

pip install -r requirements-deploy.txt
python setup.py test clean build tag publish

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

drf-tmp-scoped-token-0.2.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distributions

drf_tmp_scoped_token-0.2.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

drf_tmp_scoped_token-0.2.0-py2-none-any.whl (11.5 kB view details)

Uploaded Python 2

File details

Details for the file drf-tmp-scoped-token-0.2.0.tar.gz.

File metadata

File hashes

Hashes for drf-tmp-scoped-token-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b20d9b76cb569e6f799a86bcad2cb4777712afe73c28ddcd48e269ad7383f3bc
MD5 3b830f75c3510ac363af84bb230247ad
BLAKE2b-256 a5e61c9a6a19cf045ebca3ee60ca33b16cbe716cfef62f7888c640ca41309c79

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for drf_tmp_scoped_token-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06a4a1637b9b16d5b85e870341dd8bdf4d614d3bfd68bdf93ba632f94ff35778
MD5 69a2763a41e0e102595f6d38d41a0c4b
BLAKE2b-256 7f2a067ec6b1c8b68487493e7eb61a425ecd19e4e0581d7bbdca2bda607c41e3

See more details on using hashes here.

Provenance

File details

Details for the file drf_tmp_scoped_token-0.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for drf_tmp_scoped_token-0.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 004a7cab87ed86f230af935eb923eef7583976162f2e746081e836a2d7a89035
MD5 02023dab103aa41c83e8fabb9aa43e86
BLAKE2b-256 16ae68e395e87d196ec4d316a6d93956f14a86413a91f4f98735246a5a40d14c

See more details on using hashes here.

Provenance

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