Branca Tokens for Python
Authenticated and encrypted API tokens using modern crypto.
What?
Branca is a secure easy to use token format which makes it hard to shoot yourself in the foot. It uses IETF XChaCha20-Poly1305 AEAD symmetric encryption to create encrypted and tamperproof tokens. Payload itself is an arbitrary sequence of bytes. You can use for example a JSON object, plain text string or even binary data serialized by MessagePack or Protocol Buffers.
Although not a design goal, it is possible to use Branca as an alternative to JWT.
Install
Install the library using pip. Note that you also must have libsodium installed.
$ brew install libsodium
$ pip install pybranca
Usage
The payload of the token can be anything, like a simple string.
import secrets
from branca import Branca
key = secrets.token_bytes(32)
branca = Branca(key)
token = branca.encode("Hello world!")
payload = branca.decode(token)
print(token)
print(payload)
# 87xqn4ACMhqDZvoNuO0pXykuDlCwRz4Vg7LS3klfHpTiOUw1ramOqfWoaA6bvsGwOQ49MDFOERU0T
# b'Hello world!'
For more complicated data structures JSON is an usual choice.
import json
import secrets
from branca import Branca
key = secrets.token_bytes(32)
branca = Branca(key)
string = json.dumps({"scope" : ["read", "write", "delete"]})
token = branca.encode(string)
payload = branca.decode(token)
print(token)
print(payload)
print(json.loads(payload))
# 6AlLJaBIFpXbwKTFsI3xXsk4se8YsdEKOtxYwtYDQHpoqabwZzmxAUS99BLxBJpmfJqnJ9VvzJYO1FXfsX78d0YsvTe43opYbUPgUao0EGV5qBli
# b'{"scope": ["read", "write", "delete"]}'
# {'scope': ['read', 'write', 'delete']}
By using MessagePack you can have more compact tokens.
import msgpack
from branca import Branca
key = secrets.token_bytes(32)
branca = Branca(key)
packed = msgpack.dumps({"scope" : ["read", "write", "delete"]})
token = branca.encode(packed)
payload = branca.decode(token)
print(token)
print(payload)
print(msgpack.loads(payload, raw=False))
# 3iJOQqw5CWjCRRDnsd7Jh4dfsyf7a4qbuEO0uT8MBEvnMVaR8rOW4dFKBVFKKgxZkVlNchGJSIgPdHtHIM4rF4mZYsriTE37
# b'\x81\xa5scope\x93\xa4read\xa5write\xa6delete'
# {'scope': ['read', 'write', 'delete']}
License
The MIT License (MIT). Please see License File for more information.
Metadata
Release files for pybranca 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pybranca-0.5.0.tar.gz | 5.9 kB | Without provenance | Details for pybranca-0.5.0.tar.gz |
Release files / pybranca-0.5.0.tar.gz
| Download URL | pybranca-0.5.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8cf0edcd1d9bab225ff3518aac227d04053deb1cb69cad76e2ed60f427cf70d2
|
|
BLAKE2b-256 checksum How to use checksums |
1af4ab54cfbb0d16a52c5c1f54e94948de16effc0183ee068370ed9b1d10b307
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
|
Security for pybranca 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.