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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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