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.BadSignatureErrorif the signature is invalid. The signature header is usually found in theX-FormSG-Signatureheader. 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.bodyis expected to be a dictionary-like object. -
decrypt_attachment(body: Mapping[str, Any], field_id: str, secret_key: str) -> byteswill download and decrypt the encrypted attachment for afieldand using the given Base-64 encoded secret key.bodyis 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.
Metadata
Release files for formsg 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| formsg-0.1.8.tar.gz | 4.4 kB | Without provenance | Details for formsg-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| formsg-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for formsg-0.1.8-py3-none-any.whl |
Total release size: 9.9 kB
Release files / formsg-0.1.8.tar.gz
| Download URL | formsg-0.1.8.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8beee423bedc34636e23fddbc55723b9448adec1b1d131b6ca264609fa5c7930
|
|
BLAKE2b-256 checksum How to use checksums |
96821e3861f2e74c29cf5aa7d98a7580bd1f10f9f55744973c087ba053ce5383
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure
|
Release files / formsg-0.1.8-py3-none-any.whl
| Download URL | formsg-0.1.8-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42b908ae8a94ac658cf95c824e96a82a366e5ae5e52f3fe3e476161bab0a1cbc
|
|
BLAKE2b-256 checksum How to use checksums |
8001408156822d2b02630b5784c9cbab5787875bbb1914cde9f08c64a1119f54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1039-azure
|
Security for formsg 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.