formsg-python-sdk
This is the Python version of the Form Javascript SDK, which provides utility functions for verifying FormSG webhooks, as well as decrypting form submissions.
Note that this library is still in beta.
Requirements
Python 3.6+
Quickstart
pip install formsg-python-sdk
import formsg
# accepts STAGING or PRODUCTION, determines whether to use staging or production public signing keys
sdk = formsg.FormSdk("PRODUCTION")
# Your form's secret key downloaded from FormSG upon form creation
FORM_SECRET_KEY = "YOUR-SECRET-KEY"
# This is where your domain is hosted, and should match
# the URI supplied to FormSG in the form dashboard
YOUR_WEBHOOK_URI = "your-webhoook-uri"
# decryption without attachments
# if `verifiedContent` exists as a key in `encrypted_payload`, the return object will include a verified key
decrypted = sdk.crypto.decrypt(FORM_SECRET_KEY, encrypted_payload)
# webhook authentication
HEADER_RESP = "req.header.'x-formsg-signature'"
sdk.webhooks.authenticate(header=HEADER_RESP, uri=YOUR_WEBHOOK_URI)
# decryption with attachments
decrypted_with_attachments = sdk.crypto.decrypt_attachments(FORM_SECRET_KEY, encrypted_payload)
Refer to the example app if you're running a flask server.
End-to-end Encryption
Format of Submission Response
| Key | Type | Description |
|---|---|---|
| formId | str | Unique form identifier. |
| submissionId | str | Unique response identifier, displayed as 'Response ID' to form respondents |
| encryptedContent | str | The encrypted submission in base64. |
| created | str | Creation timestamp. |
| attachmentDownloadUrls | Mapping[str, str] | (Optional) Records containing field IDs and URLs where encrypted uploaded attachments can be downloaded. |
Format of Decrypted Submissions
crypto.decrypt(form_secret_key: str, decrypt_params: DecryptParams)
takes in decrypt_params as the second argument, and returns an an object with
the shape
{
responses: FormField[]
verified?: NotRequired[Mapping[str, Any]]
}
The decryptParams.encryptedContent field decrypts into a list of FormField objects, which will be assigned to the responses key of the returned object.
Furthermore, if decryptParams.verifiedContent exists, the function will
decrypt and open the signed decrypted content with the package's own
signingPublicKey in
constants.py.
The resulting decrypted verifiedContent will be assigned to the verified key
of the returned object.
NOTE
If any errors occur, either from the failure to decrypt eitherencryptedContentorverifiedContent, or the failure to authenticate the decrypted signed message inverifiedContent,Nonewill be returned.
Note that due to end-to-end encryption, FormSG servers are unable to verify the data format.
However, the decrypt function exposed by this library validates the decrypted content and will return None if the
decrypted content does not fit the schema displayed below.
| Key | Type | Description |
|---|---|---|
| question | str | The question listed on the form |
| answer | str | The submitter's answer to the question on form. Either this key or answerArray must exist. |
| answerArray | List[str] | The submitter's answer to the question on form. Either this key or answer must exist. |
| fieldType | str | The type of field for the question. |
| _id | str | A unique identifier of the form field. WARNING: Changes when new fields are created/removed in the form. |
The full schema can be viewed in
validate.ts.
If the decrypted content is the correct shape, then:
- the decrypted content (from
decryptParams.encryptedContent) will be set as the value of theresponseskey. - if
decryptParams.verifiedContentexists, then an attempt to decrypted the verified content will be called, and the result set as the value ofverifiedkey. There is no shape validation for the decrypted verified content. If the verification fails,Noneis returned, even ifdecryptParams.encryptedContentwas successfully decrypted.
Metadata
Release files for formsg-python-sdk 0.1.13
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-python-sdk-0.1.13.tar.gz | 11.9 kB | Without provenance | Details for formsg-python-sdk-0.1.13.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| formsg_python_sdk-0.1.13-py3-none-any.whl | Python 3 | none | any | Details for formsg_python_sdk-0.1.13-py3-none-any.whl |
Total release size: 25.6 kB
Release files / formsg-python-sdk-0.1.13.tar.gz
| Download URL | formsg-python-sdk-0.1.13.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb5adc6c515a08996a039c2e92de221bfaa42cf6086b68e86dcf470d39aab59f
|
|
BLAKE2b-256 checksum How to use checksums |
707f8fab306ad4826382eb28939ef19b8957b9af7e3a46370a637276f3a4338b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0
|
Release files / formsg_python_sdk-0.1.13-py3-none-any.whl
| Download URL | formsg_python_sdk-0.1.13-py3-none-any.whl |
|---|---|
| Size | 13.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a054448fd3759ae03994a6ceb1f759e49c81d11b0334b9323cca33d9553fd65a
|
|
BLAKE2b-256 checksum How to use checksums |
76e174a5a442676fff2c3cb1146ab5734aca2aba487092588eb4d25ed821c92c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0
|
Security for formsg-python-sdk 0.1.13
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.