Skip to content

bad-microservices/jwt_helper

Repository files navigation

jwt_helper

image

image

image

image

image

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)

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published