Simple python authorization library.
Project description
# 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.
```python
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.
```python
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}')
```
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.
```python
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.
```python
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}')
```
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
Built Distribution
Close
Hashes for python_authorization-1.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d2cc0de45795c2e4bbab504879664543f16ec84d910c9ec78207e969aa14090 |
|
MD5 | 88da3f57edd2985d4c4f4272a59e004d |
|
BLAKE2b-256 | 3b2fa8a8ea1ecf04702c32816a6be04d70ec54c58940c77af1883c05c3369c32 |
Close
Hashes for python_authorization-1.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c220f3d8524dbef6c74dc9ac15ebc36f53b29ef09fe9ba98891599939956ee36 |
|
MD5 | 6c257dd88abde5ced5218249918e53a0 |
|
BLAKE2b-256 | f5e4a2f1c5d69c1c87a2e6ad5311823e7a923e3c6a5016f4f442117f8c826323 |