Provides easy integration of the HMAC signatures for your REST Flask Applications.
Project description
This module provides an authentication to Flask routes. The intended use case is for use with REST APIs. It’s simply designed to check that a client is entitled to access a particular route in a Flask application, based on the fact that it must possess a copy of the secret key.
Usage
Server
app = Flask(__name__)
app.config['HMAC_KEY'] = 's3cr3tk3y' # define the secret key in an app config
@app.route("/no_auth_view")
def no_auth_view():
return "no_auth_view"
@app.route("/hmac_auth_view")
@hmac.auth # decorate view
def hmac_auth_view():
return "hmac_auth_view"
Client
Call without payload
sig = hmac.make_hmac() # generate signature
response = requests.get(
'/hmac_auth_view',
headers={hmac.header: sig}
)
Call with payload
Request payload has to be used as a data for HMAC generation.
data = json.dumps({'foo': 'boo'})
sig = hmac.make_hmac(data) # generate signature
response = requests.post(
'/hmac_auth_view',
data=data,
headers={hmac.header: sig}
)
You can define custom errors overwriting abort method:
class MyHmac(Hmac):
def abort(self):
message = {'status': '403', 'message': 'not authorized'}
response = jsonify(message)
response.status_code = 403
return response
For HMAC auth of all views you can use Flask’s before_request:
@app.before_request
def before_request():
try:
hmac.validate_signature(request)
except HmacException:
return abort(400)
Generate signature for/from another application:
sig = make_hmac(self, data, key=another_app_key)
Change Log
0.1.1
Able to change secret key in make_hmac method
Method validate_signature created which can be used outside of Hmac class
Custom exceptions
0.0.1
Initial release including the core feature set
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
File details
Details for the file flaskhmac-0.1.1.tar.gz
.
File metadata
- Download URL: flaskhmac-0.1.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d787085c5263b59c2240132963a26bff8dd810d30cf1452de9d03c65dcd9e9e1 |
|
MD5 | e77990acbb8a0352e27835fbec180a36 |
|
BLAKE2b-256 | ea1162fb6189643ba006073a1998fbb4ad3ed9935d596c6e0153a3ac14d5642e |