Skip to content

Instantly share code, notes, and snippets.

@manuelep
Last active March 15, 2018 16:56
Show Gist options
  • Save manuelep/b6f6c00b4dec5234ab97229199bb223d to your computer and use it in GitHub Desktop.
Save manuelep/b6f6c00b4dec5234ab97229199bb223d to your computer and use it in GitHub Desktop.
woocommerce webhook authemtication
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest, json
from woohook import *
class TestWoo(unittest.TestCase):
def test_authenticate(self):
secret = "!0E8maEcqODHMft6vrg*_lpR@dLe8v!<.Q(P!P*1G2;i)r_S{V"
signature = "8hrIw3LLKdLu0WL6S2lZSoyxLa7OlApcu7IHq2iEqEY="
body = u"{\"id\":206,\"name\":\"Prova gratuita\",\"slug\":\"free-trial\",\"permalink\":\"https:\\/\\/www.colouree.com\\/?post_type=product&p=206\",\"date_created\":\"2018-03-14T17:01:54\",\"date_created_gmt\":\"2018-03-14T17:01:54\",\"date_modified\":\"2018-03-15T15:33:20\",\"date_modified_gmt\":\"2018-03-15T15:33:20\",\"type\":\"simple\",\"status\":\"draft\",\"featured\":false,\"catalog_visibility\":\"visible\",\"description\":\"\",\"short_description\":\"\",\"sku\":\"\",\"price\":\"0\",\"regular_price\":\"0\",\"sale_price\":\"\",\"date_on_sale_from\":null,\"date_on_sale_from_gmt\":null,\"date_on_sale_to\":null,\"date_on_sale_to_gmt\":null,\"price_html\":\"<ins class=\\\"h2\\\"><span class=\\\"woocommerce-Price-amount amount\\\">0,00<span class=\\\"woocommerce-Price-currencySymbol\\\">€<\\/ins>\",\"on_sale\":false,\"purchasable\":true,\"total_sales\":0,\"virtual\":true,\"downloadable\":false,\"downloads\":[],\"download_limit\":-1,\"download_expiry\":-1,\"external_url\":\"\",\"button_text\":\"\",\"tax_status\":\"taxable\",\"tax_class\":\"\",\"manage_stock\":false,\"stock_quantity\":null,\"in_stock\":true,\"backorders\":\"no\",\"backorders_allowed\":false,\"backordered\":false,\"sold_individually\":false,\"weight\":\"\",\"dimensions\":{\"length\":\"\",\"width\":\"\",\"height\":\"\"},\"shipping_required\":false,\"shipping_taxable\":false,\"shipping_class\":\"\",\"shipping_class_id\":0,\"reviews_allowed\":false,\"average_rating\":\"0.00\",\"rating_count\":0,\"related_ids\":[155,153,143,151],\"upsell_ids\":[],\"cross_sell_ids\":[],\"parent_id\":0,\"purchase_note\":\"\",\"categories\":[{\"id\":15,\"name\":\"Uncategorized\",\"slug\":\"uncategorized\"}],\"tags\":[],\"images\":[{\"id\":0,\"date_created\":\"2018-03-15T15:33:29\",\"date_created_gmt\":\"2018-03-15T15:33:29\",\"date_modified\":\"2018-03-15T15:33:29\",\"date_modified_gmt\":\"2018-03-15T15:33:29\",\"src\":\"https:\\/\\/www.colouree.com\\/wp-content\\/plugins\\/woocommerce\\/assets\\/images\\/placeholder.png\",\"name\":\"Segnaposto\",\"alt\":\"Segnaposto\",\"position\":0}],\"attributes\":[{\"id\":1,\"name\":\"Numero massimo di progetti\",\"position\":0,\"visible\":true,\"variation\":false,\"options\":[\"7\",\"1000\"]}],\"default_attributes\":[],\"variations\":[],\"grouped_products\":[],\"menu_order\":0,\"meta_data\":[{\"id\":1263,\"key\":\"_vc_post_settings\",\"value\":{\"vc_grid_id\":[]}},{\"id\":1299,\"key\":\"_uncode_featured_media_display\",\"value\":\"carousel\"},{\"id\":1300,\"key\":\"_uncode_specific_menu_opaque\",\"value\":\"off\"},{\"id\":1301,\"key\":\"_uncode_specific_menu_no_shadow\",\"value\":\"off\"},{\"id\":1302,\"key\":\"_uncode_blocks_list\",\"value\":\"104\"},{\"id\":1303,\"key\":\"_uncode_revslider_list\",\"value\":\"HomeHeader\"},{\"id\":1304,\"key\":\"_uncode_header_full_width\",\"value\":\"on\"},{\"id\":1305,\"key\":\"_uncode_header_height\",\"value\":[\"50\",\"%\"]},{\"id\":1306,\"key\":\"_uncode_header_title\",\"value\":\"on\"},{\"id\":1307,\"key\":\"_uncode_header_title_custom\",\"value\":\"off\"},{\"id\":1308,\"key\":\"_uncode_header_style\",\"value\":\"dark\"},{\"id\":1309,\"key\":\"_uncode_header_content_width\",\"value\":\"off\"},{\"id\":1310,\"key\":\"_uncode_header_custom_width\",\"value\":\"100\"},{\"id\":1311,\"key\":\"_uncode_header_align\",\"value\":\"left\"},{\"id\":1312,\"key\":\"_uncode_header_position\",\"value\":\"header-center header-middle\"},{\"id\":1313,\"key\":\"_uncode_header_title_size\",\"value\":\"h1\"},{\"id\":1314,\"key\":\"_uncode_header_title_italic\",\"value\":\"off\"},{\"id\":1315,\"key\":\"_uncode_header_featured\",\"value\":\"on\"},{\"id\":1316,\"key\":\"_uncode_header_background\",\"value\":{\"background-color\":\"color-wayh\",\"background-repeat\":\"\",\"background-attachment\":\"\",\"background-position\":\"\",\"background-size\":\"\",\"background-image\":\"\"}},{\"id\":1317,\"key\":\"_uncode_header_parallax\",\"value\":\"off\"},{\"id\":1318,\"key\":\"_uncode_header_kburns\",\"value\":\"off\"},{\"id\":1319,\"key\":\"_uncode_header_overlay_color_alpha\",\"value\":\"100\"},{\"id\":1320,\"key\":\"_uncode_header_scroll_opacity\",\"value\":\"off\"},{\"id\":1321,\"key\":\"_uncode_header_scrolldown\",\"value\":\"off\"},{\"id\":1322,\"key\":\"_uncode_menu_no_padding\",\"value\":\"off\"},{\"id\":1323,\"key\":\"_uncode_menu_no_padding_mobile\",\"value\":\"off\"},{\"id\":1324,\"key\":\"_uncode_product_media_size\",\"value\":\"0\"},{\"id\":1325,\"key\":\"_uncode_specific_navigation_hide\",\"value\":\"off\"},{\"id\":1326,\"key\":\"_uncode_fullpage_type\",\"value\":\"curtain\"},{\"id\":1327,\"key\":\"_uncode_fullpage_opacity\",\"value\":\"off\"},{\"id\":1328,\"key\":\"_uncode_scroll_dots\",\"value\":\"off\"},{\"id\":1329,\"key\":\"_uncode_empty_dots\",\"value\":\"off\"},{\"id\":1330,\"key\":\"_uncode_scroll_history\",\"value\":\"off\"},{\"id\":1331,\"key\":\"_uncode_scroll_safe_padding\",\"value\":\"on\"},{\"id\":1332,\"key\":\"_uncode_scroll_additional_padding\",\"value\":\"0\"},{\"id\":1333,\"key\":\"_uncode_fullpage_mobile\",\"value\":\"off\"},{\"id\":1334,\"key\":\"slide_template\",\"value\":\"default\"},{\"id\":1335,\"key\":\"_yoast_wpseo_primary_product_cat\",\"value\":\"\"},{\"id\":1336,\"key\":\"_yoast_wpseo_content_score\",\"value\":\"30\"},{\"id\":1338,\"key\":\"_oembed_49ac4fe610da8430da7c28c657d21983\",\"value\":\"{{unknown}}\"}]}"
res = WooHook.check(body, signature, secret)
self.assertNone(res)
if __name__ == '__main__':
unittest.main()
# -*- coding: utf-8 -*-
import hmac, hashlib, base64
class AuthenticationError(Exception):
""" """
class WooHook(object):
"""docstring for WooHook."""
@staticmethod
def _auth_method_1_(body, signature, secret):
_secret = secret.encode('utf-8')
_body = body.encode('utf-8')
dig = hmac.new(secret, msg=_body, digestmod=hashlib.sha256).digest()
decoded = base64.b64encode(dig).decode('utf-8')
return (signature == decoded, decoded,)
@classmethod
def check(cls, *args, **kwargs):
check, result = cls._auth_method_1_(*args, **kwargs)
if not check:
raise AuthenticationError(result)
@manuelep
Copy link
Author

At the moment running the test the result is:

$ python -m test
E
======================================================================
ERROR: test_authenticate (__main__.TestWoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "[...]/woohook/test.py", line 16, in test_authenticate
    res = WooHook.check(body, signature, secret)
  File "woohook.py", line 23, in check
    raise AuthenticationError(result)
AuthenticationError: WNeVWlUGBX6pSusRngDavUWlck6eAhVpTRoTYBbJdYM=

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

What's wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment