Skip to content

Latest commit

 

History

History
95 lines (63 loc) · 2.81 KB

README.rst

File metadata and controls

95 lines (63 loc) · 2.81 KB

jwt_helper

https://app.codacy.com/project/badge/Grade/bfc97f9667f64c4a9f7d26f937393260

Introduction

jwt_helper` aims to be a simple to use wrapper around pyjwt with some predefined functions to make life easier.

The most important classes of this library are

  • JWTEncoder - A simple Helper class to create JWTs
  • Issuer - A class which holds Informations about an Entity that signs JWTs
  • JWTValidator - A simple class containing multiple `Issuer`s which get used to validate JWTs

Examples

Here are some Examples to show different Features of the jwt_helper package

Creating a JWTEncoder to create JWTs

from jwt_helper import JWTEncoder, SignMethod

jwt_encoder = JWTEncoder(
    issuer="test_issuer", signmethod=SignMethod.HS512, secret="super_secret_hmac_key"
)

Creating a JWT with that JWTEncoder

jwt = jwt_encoder.create_jwt(
    headers={"generic_jwt_header_item": "test123"},
    body={"username": "test123"},
    valid_minutes=10,
)

Creating a JWTValidator with multiple known JWT Issuers

from jwt_helper import JWTValidator, Issuer, SignMethod

validator = JWTValidator()
validator.add_issuer(
    Issuer(
        name="test_issuer",
        secret="super_secret_hmac_key",
        methods=[SignMethod.HS256, SignMethod.HS384, SignMethod.HS512],
    )
)

validator.add_issuer(
    Issuer(
        name="another_jwt_issuer",
        secret="another secret hmac key",
        methods=[SignMethod.HS256],
    )
)

Verifying the generated JWT with the validator

if not validator.verify_jwt(jwt):
    print("JWT is invalid")

jwt_content = validator.get_jwt_as_dict(jwt)