Skip to main content

No project description provided

Project description

byu-jwt-python

A python JWT validator that does all the BYU specific stuff as well as handle caching well-known and cert fetching

Installation

pip install byu_jwt

API


Note: It is important to declare the handler at a global level. This allows the caching of the well-known data as well as using the cache-control headers on the certificates only re-fetching those when cache-control has timed out. Reinitializing the class object will negate any benefit of the caching


Instantiate the class and reuse the object to utilize caching:

import byu_jwt
byujwt = byu_jwt.JWT_Handler()

Check only if JWT is valid

assert byujwt.is_valid(jwt_to_validate)

Decode JWT and Check validity

try:
    jwt = byujwt.decode(jwt_to_validate)
    return f"Hello, {jwt['preferredFirstName']}"
except byu_jwt.exceptions.JWTVerifyError as ex_info:
    return "Invalid JWT"
except byu_jwt.exceptions.JWTHandlerError as ex_info:
    return "Error attempting to verify the jwt"

JWT Header Names

BYU's API Manager creates an HTTP header that contains a signed JWT. The names of the designed BYU signed headers can be referenced here for lookup convenience.

BYU_JWT_HEADER_CURRENT

The property containing the name of the HTTP header that contains the BYU signed JWT sent directly from BYU's API Manager.

Value is X-JWT-Assertion.

Example

current_jwt_header = byu_jwt.JWT_HEADER

BYU_JWT_HEADER_ORIGINAL

The property containing the name of the HTTP header that contains the BYU signed JWT forwarded on from a service that received the BYU signed JWT sent directly from BYU's API Manager.

Value is X-JWT-Assertion-Original.

Example

original_jwt_header = byu_jwt.JWT_HEADER_ORIGINAL

Example Python Lambda function that makes use of caching

import byu_jwt

byujwt = byu_jwt.JWT_Handler()

def handler(event, context):
    jwt_to_decode = event['headers'][byu_jwt.JWT_HEADER]
    try:
        jwt = byujwt.decode(jwt_to_validate)
        return {'statusCode': 200, 'body': f'Hello, {jwt["preferredFirstName"]}'}
    except byu_jwt.exceptions.JWTVerifyError as ex_info:
        return {'statusCode': 403, 'body': "Invalid JWT"}
    except byu_jwt.exceptions.JWTHandlerError as ex_info:
        return {'statusCode': 500, 'body': "Error attempting to verify the jwt"}

Example Decoded JWT Structure

{
  "iss": "https://api.byu.edu",
  "exp": 1545425710,
  "byu": {
    "client": {
      "byuId": "",
      "claimSource": "",
      "netId": "",
      "personId": "",
      "preferredFirstName": "",
      "prefix": "",
      "restOfName": "",
      "sortName": "",
      "subscriberNetId": "",
      "suffix": "",
      "surname": "",
      "surnamePosition": ""
    },
    "resourceOwner": {
      "byuId": "",
      "netId": "",
      "personId": "",
      "preferredFirstName": "",
      "prefix": "",
      "restOfName": "",
      "sortName": "",
      "suffix": "",
      "surname": "",
      "surnamePosition": ""
    }
  },
  "wso2": {
    "apiContext": "",
    "application": {
      "id": "",
      "name": "",
      "tier": ""
    },
    "clientId": "",
    "endUser": "",
    "endUserTenantId": "",
    "keyType": "",
    "subscriber": "",
    "tier": "",
    "userType": "",
    "version": ""
  }
}

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

byu_jwt-2.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

byu_jwt-2.0.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file byu_jwt-2.0.0.tar.gz.

File metadata

  • Download URL: byu_jwt-2.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.1 Linux/4.14.77-70.59.amzn1.x86_64

File hashes

Hashes for byu_jwt-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4799712bad572f9c34b9b57432920aac94def3f316cffedc21bfebb9c122bf10
MD5 854f3a3d96555bb9c12d2ba676e9cdcc
BLAKE2b-256 3e4a64df03b787c51f908583438a8925861102afeadbaaed4cf179365bbbca9a

See more details on using hashes here.

File details

Details for the file byu_jwt-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: byu_jwt-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.1 Linux/4.14.77-70.59.amzn1.x86_64

File hashes

Hashes for byu_jwt-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d025861c1eb0d9976ecc1ba01b1ed9d67bb261410c52952cd22ee1cb97a67d6
MD5 c2dc4f25f61dbbabf90e56e95b4d18b0
BLAKE2b-256 84789d57eed23cf71e45704e2e9ba52db846ef39fd0def641c0a7839b5428592

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