Skip to main content

Simple python authorization

This module was created to provide a simple authorization method to be used in service to service authorization.

How to use

Important note about the credentials backend: At this moment we do not provide a backend to store credentials. We receive a dict containing all the valid credentials following this format: {"app_key":["app_name","secret"]}

Create a middleware in your API that receives the Authorization header and passes it to the package to ensue that the token is valid. In the above example we have a simple endpoint that validates the authentication based on a local dictionary as a credentials backend. Ideally you will implement a credential repository and pass the valid credentials to the module.

from http import HTTPStatus
from flask import request, jsonify, Flask
from python_authorization import Authorization

app = Flask(__name__)

@app.route('/')
def dummy_endpoint():
    authorization_header = request.headers.get('Authorization')

    py_auth = Authorization(authorization_header, valid_credentials_dict={'valid_app_key1': ['valid_app_name', 'valid_secret']})

    # This method will only validate the format of the token
    is_valid = py_auth.validate_token_format()

    if not is_valid:
        return (jsonify({'Message': 'Invalid authorization token format'}), HTTPStatus.UNAUTHORIZED)

    # This method will asume that the token is in a valid format and will try to validate it 
    is_authorized = py_auth.validate_token()

    if not is_authorized:
        return (jsonify({'Message': 'Invalid authorization token'}), HTTPStatus.UNAUTHORIZED)

    return (jsonify({'Message': 'You are in!'}), HTTPStatus.OK)

app.run('0.0.0.0', port=5000)

You can also use this module to authenticate at a service using the Credentials class.

from python_authorization import Credentials

credentials_factory = Credentials('valid_app_name', 'valid_app_key1', 'valid_secret')

token = credentials_factory.build_token()

print(f'This is my token: {token}')

Release files for python-authorization 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-authorization 1.0.4
File Size Uploaded
python_authorization-1.0.4.tar.gz 4.2 kB Details

Release files / python_authorization-1.0.4.tar.gz

Download URL python_authorization-1.0.4.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
80816afbe217adbb4ee602c91a90ea9a863884e31dbe40df0497c5a2c600d77c
BLAKE2b-256 checksum
How to use checksums
46a5b7466db8a79b6938d7b1249fad0bc0d00ebf1dfe3bbf6fe8beaf323bce8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.61.2 importlib-metadata/4.10.1 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.0.4 This release

1 release file

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page