Skip to main content

Generate tokens for use in the Adzooma platform.

Project description

rocket_token

rocket_token is a simple Python package that allows:

  1. Encrypting/Decrypt a dictionary.
  2. Generating an encrypted token to be sent to the Adzooma API.

Installing rocket_token And Supported Versions

rocket_token is available on PyPI:

$ pip install rocket_token

rocket_token officially supports Python 3.8+.

Encrypting/Decrypting A Dictionary

Encrypting a dictionary is accomplished using the encrypt_dictionary method of RocketToken.

import os

from rocket_token import RocketToken

my_dictionary = {"one": 1, "two": [], "three": {"sub1": 1, "sub2": 2}}

# Generate Private and Public key files in a directory called 'keys'
RocketToken.generate_key_pair(path="keys")

# Instantiate RocketToken object with private and public keys loaded
rocket = RocketToken.rocket_token_from_path(private_path=os.path.join("keys", "id_rsa"),
                                    public_path=os.path.join("keys", "id_rsa.pub"))

# Generate an encrypted token
token = rocket.generate_web_token(my_dictionary)

# Decode token
token_dict = rocket.decode_token(token)

print(token)
"Bearer kgYEkR9t57Jx/xcWwkTP03Y...7Wy3jzkdmVUea7dVO/fC5="
print(token_dict)
{'one': 1, 'two': [], 'three': {'sub1': 1, 'sub2': 2}}

Generating An API Token

Encrypting a dictionary is accomplished using the generate_web_token method of RocketToken.

Required keyword arguments are:

1. path: (str) Path to the requested resource.
2. exp: (int) Expiry time of request in minutes.
3. method: (str) A valid HTTP request method.

Followed by an arbitrary number of keyword arguments.

import os

from rocket_token import RocketToken

# Generate Private and Public key files in a directory called 'keys'
RocketToken.generate_key_pair(path="keys")

# Instantiate RocketToken object with private and public keys loaded
rocket = RocketToken.rocket_token_from_path(private_path=os.path.join("keys", "id_rsa"),
                                    public_path=os.path.join("keys", "id_rsa.pub"))

# Generate an encrypted token
token = rocket.generate_web_token(path="/reports/campaign",
                         exp=10, 
                         method="GET", 
                         customer_id=3)

# Decode token
token_dict = rocket.decode_token(token)

print(token)
"Bearer kgYEkR9t57Jx/xcWwkTP03Y...7Wy3jzkdmVUea7dVO/fC5="
print(token_dict)
{'path': '/reports/campaign', 'expiry': 10, 'expiry_date': '2021-06-25T10:02:36.556318', 'method': 'GET', 'customer_id': 3}

Command Line Interface

The above functionality can also be accessed through a command line interface.

CLI: Generate Public And Private Key Files

>>> rt_new_keys keys
Key-pair saved to `keys`

CLI: Generate A Token

>>> rt_web_token C:\Users\kmpla\Documents\dev\adzooma\rocket_token\keys\id_rsa.pub /campaign post 15
Token: Bearer Eb1L8xc5cUMXV52PCXX7woFRtSZXORHSp2ncd1M...7Usx0Q1m3RijNa7k=

Developer Mode

Users can put the rocket token library into developer mode by setting the environment variable ROCKET_DEVELOPER_MODE equal to true. In developer mode the RocketTokens library uses hard-coded public and private key's to remove the need to store key files locally or modify the environment during development. THIS VAR SHOULD NEVER BE SET IN PRODUCTION.

Developer Mode - CLI

The CLI also comes with a command to support developer mode so no keyfile has to be specified to generate a web token.

rt_dev_web_token /campaign POST 15 -p customer_id=5

Unit testing and coverage

To run unit tests users must have pytest installled and have cloned the repository from the repo. Once installed the command pytest can be ran from the root directory of the project to run all unit tests.

Checking Coverage

To check the unit test coverage users must have pytest and coverage installed and have cloned the repository from the repo. Once they're installed you can check the coverage by running coverage run --source=./src -m pytest. The results can then be seen by running coverage report

Using cover-gutters

Cover-gutters is a vs-code extension that allows you to see your line level coverage for your code. In order to use this you need pytest and pytest-cov installed. You can then generate the coverage file required for it to work by running pytest ./tests --cov-report xml:cov.xml --cov ./src/rocket_token from the root of the project. To view the coverage in your source code you can then press Ctrl + Shift + 7.

Uploading the package

  1. python -m pip install --upgrade twine
  2. python -m pip install --upgrade build
  3. python -m build
  4. Check your package contains the expected files: tar tzf dist/rocket_token-2.0.0.tar.gz Use twine to check package description will render correctly on PyPI: twine check dist/*
  5. twine upload dist/*

Converting a key into a string

base64.b64encode(key_string.encode('utf-8')).decode("utf-8")

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

rocket_token-3.0.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

rocket_token-3.0.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file rocket_token-3.0.0.tar.gz.

File metadata

  • Download URL: rocket_token-3.0.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.1

File hashes

Hashes for rocket_token-3.0.0.tar.gz
Algorithm Hash digest
SHA256 1ae59d28560de9fb293ae0cfaf3008ba236911a43a2f91981cba09eaf88afe76
MD5 d313f794a4d28ed3c1258cdc90130c45
BLAKE2b-256 64edefb19c5295acae2dfd1eee468a8076c93c3e9015c78f6fe994ae30bb2b83

See more details on using hashes here.

File details

Details for the file rocket_token-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: rocket_token-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.1

File hashes

Hashes for rocket_token-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3025bd6ce09683c316865efff2c82b89122f23e874cfe1dfc355c4e7bbcdb9cf
MD5 c786f8dd0edeaf6d9ac59bc006e68eaf
BLAKE2b-256 afbad6833652a4d1c8ed9014eb43ac07159e07c1872d34571f00ec0e3b73c1b8

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