Generate tokens for use in the Adzooma platform.
Project description
rocket_token
rocket_token is a simple Python package that allows a user to encrypt a dictionary or generate an encrypted API token using an RSA public-key algorithm.
- Encrypting/Decrypt a dictionary.
- 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.load_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.encrypt_dictionary(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 new_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.load_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.new_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_keys -h
usage: rt_keys dir
Generate Public and Private key files.
optional arguments:
-h, --help show this help message and exit
-d dir Location to save private/public key files
A Simple Example
>>> rt_keys -d keys
Public and Private key pair saved to: keys
CLI: Encrypt A Dictionary
>>> rt_encrypt -h
usage: rt_encrypt -f <path>/id_rsa.pub -o parameter=value
Encrypt a dictionary.
optional arguments:
-h, --help show this help message and exit
-f --file Filepath to the public key file.
-o ... Arbitrary number of keyword arguments <keyword>=<value>
A Simple Example
>>> rt_encrypt -f keys/id_rsa.pub -o key1=value1 key2=value2
Token: Bearer Eb1L8xc5cUMXV52PCXX7woFRtSZXORHSp2ncd1M...7Usx0Q1m3RijNa7k=
CLI: Generate A Token
>>> rt_token -h
usage: rt_token -f -p -e -m -o parameter=value
Generate a new token.
optional arguments:
-h, --help show this help message and exit
-f --file Filepath to the public key file.
-p --path Path to the requested resource e.g. /google-
reporting/report/campaign
-e --exp Expiry time in minutes.
-m --method Method used in the request must be in [GET, POST].
-o ... Arbitrary number of keyword arguments <keyword>=<value>
A Simple Example
>>> rt_token -f keys/id_rsa.pub -p report/campaign -e 5 -m GET -o key1=value1 key2=value2
Token: Bearer Eb1L8xc5cUMXV52PCXX7woFRtSZXORHSp2ncd1M...7Usx0Q1m3RijNa7k=
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rocket_token-1.0.0.tar.gz.
File metadata
- Download URL: rocket_token-1.0.0.tar.gz
- Upload date:
- Size: 8.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.1 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a8051bc7eb7aa8c01eeb32dd334df631aa748f5c357fadcf490bc32a2364dfa
|
|
| MD5 |
ca18a9e0e039e493ed12879814fcc30e
|
|
| BLAKE2b-256 |
89055c40106135911dfe97176bcc9fa42b7531bcc8104dfee934c28f5e674789
|
File details
Details for the file rocket_token-1.0.0-py3-none-any.whl.
File metadata
- Download URL: rocket_token-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.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.1 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a75b7fd878021b5de814a47d8dde8a4a0b5dd9a50eeab1085c6d8d7c1149ea2b
|
|
| MD5 |
4e850afd5daaa51fff56f08da291d5df
|
|
| BLAKE2b-256 |
bad422e342247c40aec9c3cd16ebc8629787cf47c1a0dd6703a01145281bc458
|