A Python SDK for handling FormSG webhooks.
Project description
FormSG Python SDK
This SDK provides convenient utilities for verifying FormSG webhooks and decrypting submissions in Python and Flask or Django.
Installation
pip install formsg
Usage
The SDK provides two main utility functions for handling FormSG webhook:
-
webhook_uri: str, signature_header: str, signature_expiry_seconds: float = 60) -> Mapping[str, Any]
verifies that the incoming webhook's signature is valid based on the FormSG production public key. It raises anacl.exceptions.BadSignatureError
if the signature is invalid. The signature header is usually found in theX-FormSG-Signature
header. Details on how the signature is constructed can be found here. -
decrypt_content(body: Mapping[str, Any], secret_key: str) -> Mapping[str, Any]
will decrypt the encrypted content using the given Base-64 encoded secret key.body
is expected to be a dictionary-like object. -
decrypt_attachment(body: Mapping[str, Any], field_id: str, secret_key: str) -> bytes
will download and decrypt the encrypted attachment for afield
and using the given Base-64 encoded secret key.body
is expected to be a dictionary-like object.
For convenience, the SDK implements a decrypt_django_request
and decrypt_flask_request
which returns the decrypted FormSG content from a Django/Flask request object directly.
Example with Flask
from formsg.flask import decrypt_flask_request
from flask import Flask
from flask import jsonify
from flask import request
app = Flask(__name__)
@app.route('/formsg_webhook', methods=['POST'])
def formsg_webhook():
decrypted = decrypt_flask_request(
request,
secret_key='xxx',
webhook_uri='https://xxx.ngrok.io/formsg_webhook', # we use ngrok to test our webhooks locally
)
return jsonify(decrypted)
#end def
if __name__ == '__main__':
app.run(debug=True)
#end if
Contributions
If you find any issues or would like to contribute improvements, please feel free to raise them in this repository directly.
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
File details
Details for the file formsg-0.1.8.tar.gz
.
File metadata
- Download URL: formsg-0.1.8.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8beee423bedc34636e23fddbc55723b9448adec1b1d131b6ca264609fa5c7930 |
|
MD5 | f93af9e7156be8f153ad5b30a45735a5 |
|
BLAKE2b-256 | 96821e3861f2e74c29cf5aa7d98a7580bd1f10f9f55744973c087ba053ce5383 |
File details
Details for the file formsg-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: formsg-0.1.8-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42b908ae8a94ac658cf95c824e96a82a366e5ae5e52f3fe3e476161bab0a1cbc |
|
MD5 | 78f99247ae46f4eee1c2d4323008d5af |
|
BLAKE2b-256 | 8001408156822d2b02630b5784c9cbab5787875bbb1914cde9f08c64a1119f54 |