Skip to main content

Wrapper around JWT tokens and the Zope Component Architecture (ZCA).

Project description

https://travis-ci.com/gocept/gocept.webtoken.svg?branch=master https://coveralls.io/repos/github/gocept/gocept.webtoken/badge.svg

This library helps you using JWT tokens with the Zope Component Architecture (ZCA).

This package is compatible with Python version 2.7, 3.6 up to 3.8.

Copyright (c) 2015-2020 gocept gmbh & co kg

All Rights Reserved.

This software is subject to the provisions of the Zope Public License, Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. THIS SOFTWARE IS PROVIDED “AS IS” AND ANY AND ALL EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.

Installation

This package requires cryptography, which needs some install attention. Please refer to its install documentation for further information.

Usage

The CryptographicKey utility

gocept.webtoken uses a global utility of the class gocept.webtoken.CryptographicKeys, which provides cryptographic keys for different purposes. It loads a set of public and private keys from disk. It takes the filesystem path to your key files and a list of key names:

>>> import gocept.webtoken
>>> import pkg_resources
>>> path_to_keys = pkg_resources.resource_filename(
...     'gocept.webtoken', 'testing/keys')
>>> keys = gocept.webtoken.CryptographicKeys(
...     path_to_keys, ['key1'])

For each of the names, a private key file of the same name and a public key file (with a .pub suffix) must reside inside the keys_dir.

The utility needs to be registered at the ZCA, either via a zcml file or via:

>>> import zope.component
>>> zope.component.provideUtility(keys)

Creating a token

Create a signed web token with the function create_web_token. You will need the private key name, which was registered at the CryptographycKey utility. It is referenced by its name and the suffix -private:

>>> expires_in = 300  # The token is valid for 300 seconds
>>> payload = {'your': 'data'}
>>> result = gocept.webtoken.create_web_token(
...     'key1-private', 'issuer', 'subject', expires_in, payload)
>>> sorted(result.keys())
['data', 'token']

The token is available under the key token, while the data encoded in the token is placed under the key data.

Creating a Bearer Authorization header

You can create an Bearer Authorization header either from a token_dict as returned by create_web_token or from a token directly:

>>> gocept.webtoken.create_authorization_header(b'<TOKEN>')
('Authorization', 'Bearer <TOKEN>')

Extracting a token from a Bearer Authorization header

Extract the token from a dict containing the headers of you request or from the value of the HTTP Authorization header itself:

>>> request_headers = dict(Authorization='Bearer <TOKEN>')
>>> b'<TOKEN>' == gocept.webtoken.extract_token(request_headers)
True

Decoding a token

Decode a signed web token with the function decode_web_token. You will need the public key name, which was registered at the CryptographycKey utility. It is referenced by its name and the suffix -public:

>>> result = gocept.webtoken.decode_web_token(
...     result['token'], 'key1-public', 'subject')

Note that the subject must match the subject given when the token was created.

The result contains all data encoded in the token. You can find the payload under the key data:

>>> {'your': 'data'} == result['data']
True

gocept.webtoken

3.1.post1 (2020-04-08)

  • Drop faulty python_requires in setup.py.

3.1 (2020-04-08)

  • Migrate to Github.

  • Test with Python 3.8 and PyPy3.

  • Stop testing with Python 3.5.

3.0 (2018-11-14)

  • Change license from ZPL to MIT.

  • Add support for Python 3.7.

  • Drop support for Python 3.4.

  • Make subject check optional as some systems like Keycloak use a random uuid as the subject which is unknown for the decoder.

  • Add audience parameter which is required to decode tokens generated by Keycloak.

2.0 (2018-01-08)

  • Drop support for Python 3.3 but add it for 3.6.

  • Make setup.py compatible with newer setuptools versions by no longer using absolute paths.

1.2.1 (2015-10-08)

  • Fix extract_token to accept any collections.Mapping derived object.

1.2 (2015-10-08)

  • Added helper functions to create a Bearer Authorization header and extract a token from it.

  • Officially support Python 3.5.

1.1 (2015-10-01)

  • Shortened imports for CryptographicKeys, create_web_token and decode_web_token, which are now importable directly from gocept.webtoken.

  • Added documentation.

1.0 (2015-10-01)

  • Add support for Python 3.3 and 3.4.

  • Initial release, extracted from internally used package.

Download files

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

Source Distribution

gocept.webtoken-3.1.post1.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

gocept.webtoken-3.1.post1-py2.py3-none-any.whl (17.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gocept.webtoken-3.1.post1.tar.gz.

File metadata

  • Download URL: gocept.webtoken-3.1.post1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/None requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.7

File hashes

Hashes for gocept.webtoken-3.1.post1.tar.gz
Algorithm Hash digest
SHA256 f0f517d6b77d89220aa2634eb296aa1867f07d9fa39e0e65c14d2a7cd93eb849
MD5 22ea58e23d4df73684deb218e3eb4b22
BLAKE2b-256 ea6acb0e00a24f0f21c80e0e9c1b18046db98aa1a83f290bb181b1a5692c8f60

See more details on using hashes here.

File details

Details for the file gocept.webtoken-3.1.post1-py2.py3-none-any.whl.

File metadata

  • Download URL: gocept.webtoken-3.1.post1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/None requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.7

File hashes

Hashes for gocept.webtoken-3.1.post1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3c0512128bf3ad852fef5c6245cbf01b7635d7635c951c8be62412c12f2dbfe4
MD5 f7b869c15dfcf5dc8cb60643d6cbbb94
BLAKE2b-256 fa9233003a2d4ac1fb7e8ac4d0a0f3f6b7f1df5fa5b221243d9c8955f13ad43d

See more details on using hashes here.

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