Skip to content

Conversation

arnavdas88
Copy link

@arnavdas88 arnavdas88 commented Jul 28, 2025

Added Pythonic Interface for creating Account Claims and User claims as JWT.

Issue: Issue 717

Replicated the example at NATS by Example - Programmatic NKeys and JWTs in the test file tests/contrib/test_jwt.py.

Example Code Snippet

operatorKP = from_seed(b'SOALU7LPGJK2BDF7IHD7UZT6ZM23UMKYLGJLNN35QJSUI5BNR4DJRFH4R4')
accountKP = from_seed(b'SAALXUEDN2QR5KZDDSH5S4RIWAZDM7CVDG5HNJI2HS5LBVYFTLAQCOXZAU')
userKP = from_seed(b'SUALJTG5JNRQCQKFE652DV4XID522ALOHJNQVHKKDJNVGWHCLHOEXEROEM')

account_claims = Claims(
    name="my-account",
    jti="PBFES33GGIFZM6UGC7NY5ARHRBFVFU4UD7FS2WNLZH3KPGWFVEFQ",
    iat=1678973945,
    iss=operatorKP.public_key.decode(),
    sub=accountKP.public_key.decode(),
    nats=Account(
        limits=OperatorLimits(
            nats_limits=NatsLimits(data = -1, payload = -1, subs = -1),
            account_limits=AccountLimits(exports = -1, imports = -1, wildcards = True, conn = -1, leaf = -1),
            jetstream_limits=JetStreamLimits(disk_storage=-1, mem_storage=-1)
        ),
        default_permissions=Permissions(),
        generic_fields=GenericFields(version=2, type=Types.Account)
    )
)

account_claims.to_dict()

Some issues with the PR

  • Directory and File structure is a mess in the contrib/.
  • Test for Operator Claim is not written yet as the operator claims are not clear in NATS by Example - Programmatic NKeys and JWTs blog.
  • encode_user_claim() and encode_account_claim() is not yet mature and only exists in the test file.
  • The tests pass with the example shown in NATS by Example - Programmatic NKeys and JWTs, but the resultant jwt has not been tested with a nats-server.
  • Adding code documentation can be helpful.

Note : Directory and File structure is a mess in the contrib/ because following any other file structure was resulting in circular import error, and putting all the code in a single file was making it hard to read. Any suggestions / commits on how to have a better structure for the files in contrib/ directory are welcome!

@arnavdas88 arnavdas88 requested a review from GarraPeters August 18, 2025 15:11
@Jarema Jarema requested review from caspervonb and removed request for GarraPeters August 19, 2025 11:18
@arnavdas88
Copy link
Author

Some (foreign and unrelated to this PR) tests in test_js.py are failing. Can someone confirm this behaviour?

@caspervonb @GarraPeters @wallyqs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants