Skip to main content

NATS JWT tokens signed using NKeys for the Python3 ecosystem

Project description

NATS jwt lib for python

Python's library for generating JWT tokens for NATS server.

⚠️ Warning ⚠️

This library is not well-tested and is in the development stage.

The Author(s) is not a developer of the NATS, so may not understand zen of the NATS.

Notes

Scope level description
Code ℹ️ This library was inspired and based on official NATS's go library.
Code ℹ️ Author tried to save structure of code that GoLang version has, but it is not one-to-one due to languages specs.
Code ℹ️ In this library there is snippets.py that is targeting to make creation of accounts and users easier.
Tests ⚠️ Tests not covering all code.
Documentation ℹ️ NATS has powerful documentation for JWT. Recommended for reading.

Code Example

Code examples are using snippets.py which is not part of the go library.

Create Operator from seed

from nats_jwt.v2.snippets import Operator
from nats_jwt.v2.account_claims import Export
from nats_jwt.nkeys_ext import nkeys2
import nkeys

# create raw seed - 32 'random' bytes
raw_seed: bytes = nkeys2.create_seed()

# create a new seed for operator. This seed now would look in base64 like:
# SO...
op_seed: bytes = nkeys2.encode_seed(nkeys.PREFIX_BYTE_OPERATOR, raw_seed)

# Tip: Also operator, account and user seeds can be created via prepared functions
# Note 1: those functions are returning nkeys.KeyPair objects (ed25519 generated keys)
# Note 2: You can extract seed from KeyPair object by calling seed() method
#
# nkeys2.create_operator_pair()
# nkeys2.create_account_pair()
# nkeys2.create_user_pair()

# now we can create an abstraction above this seed for operator operations
op = Operator(seed=op_seed)

# `create_account` will create new seed, KeyPair, AccountClaims with issuer set to operator's public key
# also, `Account` snippet object has signer key pair as object attribute (`_skp`) and when jwt generation
# is done jwt automatically is signed by this key pair (and `iat` is also set to current time).
ac = op.create_account("my_account")

ac.claims.name = "rewrite_name"
ac.claims.nats.exports.append(Export("my_export", "MY.CUSTOM.SUBJECT.>"))

# JWT for any snippet is generated by calling `jwt` property-method
jwt: str = ac.jwt

# now we can verify this jwt by calling `verify` operator method
if op.verify(jwt):
    print("account JWT is valid")
else:
    # should not happen :D
    print("account JWT is invalid")

us = ac.create_user("my_user")
if ac.verify(us.jwt):
    print("user JWT is valid")
else:
    # should not happen :D
    print("user JWT is invalid")

LICENSE

This library is licensed under the same LICENSE as the NATS's go library

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

nats_jwt-0.2.2.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

nats_jwt-0.2.2-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file nats_jwt-0.2.2.tar.gz.

File metadata

  • Download URL: nats_jwt-0.2.2.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-1020-azure

File hashes

Hashes for nats_jwt-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7ac5c9acfdc9b4e0727060b88b963cec498d8a2cecc68959f4651059414db334
MD5 cb829f72d58bbf4758d08d6b66027c78
BLAKE2b-256 a0260c0afe6c50b6e420bde49e248c81504c6dd38be639849a5800bbcea87c9a

See more details on using hashes here.

File details

Details for the file nats_jwt-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: nats_jwt-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-1020-azure

File hashes

Hashes for nats_jwt-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fda5e6783d49ded71574b17df022bc5bdd6d0d2a120d294a01f3ddcb30663c84
MD5 04ea8b87aa9ad2536c33f71959382250
BLAKE2b-256 7834b2ee01dd2aff3c60af7b5fa6c332fa1efa7aa872cfb452bfcc8d84c7dbbd

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