-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflask.py
More file actions
35 lines (27 loc) · 1.11 KB
/
flask.py
File metadata and controls
35 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
from flask import Flask, request, Response
import formsg
from formsg.exceptions import WebhookAuthenticateException
app = Flask(__name__)
FORM_SECRET_KEY = "YOUR-SECRET-KEY"
YOUR_WEBHOOK_URI = "your-webhook-uri"
# accepts STAGING or PRODUCTION, determines whether to use staging or production public signing keys
sdk = formsg.FormSdk("PRODUCTION")
@app.route("/webhook", methods=["POST"])
def webhook_route():
posted_data = json.loads(request.data)
try:
sdk.webhooks.authenticate(
request.headers["X-FormSG-Signature"], YOUR_WEBHOOK_URI
)
except WebhookAuthenticateException as e:
print(e)
return Response("Unauthorized", 401)
# decryption without attachments
# if `verifiedContent` exists as a key in `posted_data['data']`, the return object will include a verified key
responses = sdk.crypto.decrypt(FORM_SECRET_KEY, posted_data["data"])
# decryption with attachments
responses_with_attachments = sdk.crypto.decrypt_with_attachments(
FORM_SECRET_KEY, posted_data["data"]
)
return Response(json.dumps({"message": "ok"}), 202)