Skip to main content

A simple library for securing Flask REST APIs with JWTs using decorators

Project description

IBM Flask JWT

This project provides a simple Python library for securing Flask APIs with JWT authentication.

Build

JSON Web Tokens

Secure endpoints are accessed by passing a JSON Web Token (JWT).

API Decorators

The follow Python decorators are available for use on Flask API endpoints.

  • private - Secures an API endpoint. Requests to the endpoint will return a 401 Unauthorized response unless a valid JWT is attached to the HTTP request. The JWT must be sent as a bearer token in the standard authorization header: Authorization: Bearer <token>.
  • public - This is a marker decorator to identify an endpoint as intentionally public.

Example

The following example shows how to secure a private endpoint for a simple API built with the Flask RESTful framework. In this example, requests to the resource will return a 401 Unauthorized response unless a valid JWT token is attached to the HTTP request.

from flask_restful import Resource
from ibm_flask_jwt.decorators import private


class PrivateApi(Resource):

    @private
    def get(self):
        return 'Success'

Configuration

The following environment variables are loaded by the library:

  • JWT_PUBLIC_KEY - (Required) RSA256 public key for JWT signature verification.

Development

Dependencies

Use Pipenv for managing dependencies. Install all dependencies with pipenv install --dev.

Testing

Run the unit tests with code coverage with pipenv run pytest --cov lib test.

Building

Run the build.py file to generate the setup.py file. This allows us to read the required dependencies from Pipfile.lock so they are available in the install_requires configuration field of setup.py.

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

ibm-flask-jwt-0.0.2.tar.gz (7.3 kB view details)

Uploaded Source

File details

Details for the file ibm-flask-jwt-0.0.2.tar.gz.

File metadata

  • Download URL: ibm-flask-jwt-0.0.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for ibm-flask-jwt-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b40cfc986086f46e3934555c80ba1e46f63974be435f18cfd8e63783d10178c3
MD5 4b589ef22e3d5affb78750ad013947a2
BLAKE2b-256 2602e465146a6269b72dd6efa181c43ec5f5af9b33ffe6cc8e2e196ccf30ccb9

See more details on using hashes here.

Supported by

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