Skip to main content

Python JWT implementation

Project description

Cherry JWT

Warning
An simple implementation of JWT for educational purposes.
Please do not use this in a real application.

Installation

pip install cryptography starkbank-ecdsa cherry_jwt

Usage

Creating a JWT with HS256 (mac)

from cherry_jwt.jwt import JWT

jwt = JWT(
    claims={
        'message': 'hello world'
    }, 
    algorithm='HS256', 
    secret='secret'
).encode()

Verifying a JWT with HS256

from cherry_jwt.verifier import JWTVerifier
from cherry_jwt.exceptions import JWTVerificationException

jwt_verifier = JWTVerifier(algorithm='HS256', key='secret', claims_validator={
    'message': 'hello world'
})

try:
    jwt_verifier.verify(jwt)
except JWTVerificationException as e:
    #handle rejection
    print(e)

Creating a JWT with ECDSA

from cherry_jwt.jwt import JWT

 #can accept key as PEM or DER
private_key_pem = get_private_key_pem()
jwt = JWT(
    claims={
        'message': 'hello world'
    }, 
    algorithm='ECDSA', 
    secret=private_key_pem,
    format='PEM'
).encode()

Verifying a JWT with ECDSA

from cherry_jwt.verifier import JWTVerifier
from cherry_jwt.exceptions import JWTVerificationException

jwt_verifier = JWTVerifier(
    algorithm='ECDSA', 
    key=public_key_pem, 
    claims_validator={
        'message': 'hello world'
    }),
    format='PEM'

try:
    jwt_verifier.verify(jwt)
except JWTVerificationException as e:
    #handle rejection
    print(e)

Set header

jwt = JWT(
    claims={
        'message': 'hello world'
    }, 
    algorithm='HS256', 
    secret='secret'
).set_header_val('FOO', 'BAR').encode()

Convenience methods for common claims

jwt_verifier = JWTVerifier(algorithm='HS256', key='secret', claims_validator={
    'message': 'hello world'
}).check_aud_is('xxx').check_iss_is('yyy').check_sub_is('zzz')

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

cherry_jwt-0.0.2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

cherry_jwt-0.0.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file cherry_jwt-0.0.2.tar.gz.

File metadata

  • Download URL: cherry_jwt-0.0.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for cherry_jwt-0.0.2.tar.gz
Algorithm Hash digest
SHA256 533924126d0c2ec5696c120f44fca8ae3ca294a96a6d99697d75d84ce6c5e5f9
MD5 528e74a231d61102c525507b32345969
BLAKE2b-256 98f2101806728433780bb5415d2b4357e3088200f3343352a022f7e09e91ed29

See more details on using hashes here.

File details

Details for the file cherry_jwt-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cherry_jwt-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for cherry_jwt-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 58b04a1b4e67b08702772e022258bf79133e2bf7b5d0f601a324556efac49b53
MD5 06fdd6bb797dea0a9a17bd931d62c462
BLAKE2b-256 1d2282ef94f2a9e77e1ec1f9eafe0be994e5d0ba2b323247db3ca002a0de6d2a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page