Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import base64 |
| 2 | import hashlib |
| 3 | import hmac |
Ankush Menat | b05287d | 2022-01-27 20:08:05 +0530 | [diff] [blame] | 4 | from urllib.parse import urlparse |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 10 | def validate_webhooks_request(doctype, hmac_key, secret_key="secret"): |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 11 | def innerfn(fn): |
| 12 | settings = frappe.get_doc(doctype) |
| 13 | |
| 14 | if frappe.request and settings and settings.get(secret_key) and not frappe.flags.in_test: |
| 15 | sig = base64.b64encode( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 16 | hmac.new( |
| 17 | settings.get(secret_key).encode("utf8"), frappe.request.data, hashlib.sha256 |
| 18 | ).digest() |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 19 | ) |
| 20 | |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 21 | if frappe.request.data and sig != bytes(frappe.get_request_header(hmac_key).encode()): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | frappe.throw(_("Unverified Webhook Data")) |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 23 | frappe.set_user(settings.modified_by) |
| 24 | |
| 25 | return fn |
| 26 | |
| 27 | return innerfn |
| 28 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 29 | |
Ankush Menat | e28165e | 2021-05-07 20:27:51 +0530 | [diff] [blame] | 30 | def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 31 | endpoint = f"erpnext.erpnext_integrations.connectors.{connector_name}.{method}" |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 32 | |
| 33 | if exclude_uri: |
| 34 | return endpoint |
| 35 | |
| 36 | try: |
| 37 | url = frappe.request.url |
| 38 | except RuntimeError: |
hrwx | 5aa8743 | 2019-09-13 17:04:01 +0000 | [diff] [blame] | 39 | url = "http://localhost:8000" |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 40 | |
Ankush Menat | e28165e | 2021-05-07 20:27:51 +0530 | [diff] [blame] | 41 | url_data = urlparse(url) |
| 42 | scheme = "https" if force_https else url_data.scheme |
| 43 | netloc = url_data.netloc |
| 44 | |
| 45 | server_url = f"{scheme}://{netloc}/api/method/{endpoint}" |
Saurabh | d60c0f2 | 2018-05-16 11:33:47 +0530 | [diff] [blame] | 46 | |
hrwx | 5aa8743 | 2019-09-13 17:04:01 +0000 | [diff] [blame] | 47 | return server_url |
Mangesh-Khairnar | 27f81e0 | 2020-09-15 14:38:06 +0530 | [diff] [blame] | 48 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 50 | def get_tracking_url(carrier, tracking_number): |
| 51 | # Return the formatted Tracking URL. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | tracking_url = "" |
| 53 | url_reference = frappe.get_value("Parcel Service", carrier, "url_reference") |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 54 | if url_reference: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | tracking_url = frappe.render_template(url_reference, {"tracking_number": tracking_number}) |
jbienesdev | 1c9410e | 2020-07-13 16:25:09 +0800 | [diff] [blame] | 56 | return tracking_url |