|
|
@@ -180,13 +180,14 @@ def sign_payload(payload, secret): # For TESTING purposes
|
|
|
|
|
|
|
|
|
def verify_gbhook_signature(payload, signature, secret):
|
|
|
+ current_app.logger.info(f'\n\nRecieved Signature: {signature}\n\n')
|
|
|
expected_signature = hmac.new(
|
|
|
secret.encode(),
|
|
|
payload.encode(),
|
|
|
hashlib.sha256
|
|
|
).hexdigest()
|
|
|
do_sig_check = hmac.compare_digest(signature, expected_signature)
|
|
|
- current_app.logger.info(f'\n\nExpected Signature: {expected_signature}\nTest output: {do_sig_check}\n\n')
|
|
|
+ current_app.logger.debug(f'\n\nExpected Signature: {expected_signature}\nResult: {do_sig_check}\n\n')
|
|
|
return do_sig_check
|
|
|
|
|
|
|
|
|
@@ -194,7 +195,13 @@ def verify_gbhook_signature(payload, signature, secret):
|
|
|
def gb_hook():
|
|
|
current_app.logger.info(f'----------------------------------------------------------------------------')
|
|
|
current_app.logger.info(f'GiveButter request')
|
|
|
- signature = request.headers.get('X-Givebutter-Signature')
|
|
|
+
|
|
|
+ headers = request.headers
|
|
|
+ current_app.logger.debug('Headers:')
|
|
|
+ for header, value in headers.items():
|
|
|
+ current_app.logger.debug(f'> {header}: {value}')
|
|
|
+
|
|
|
+ signature = request.headers.get('Signature')
|
|
|
gb_secret = current_app.config['GB_SETTINGS']['secret']
|
|
|
|
|
|
raw_data = request.get_data(as_text=True)
|
|
|
@@ -202,36 +209,42 @@ def gb_hook():
|
|
|
# signature = sign_payload(raw_data, gb_secret) # For TESTING purposes
|
|
|
|
|
|
event = request.json
|
|
|
- transaction = event['data'] # TESTING
|
|
|
- from_name = f'{transaction["first_name"]} {transaction["last_name"]}' # TESTING
|
|
|
- email = transaction['email'] # TESTING
|
|
|
- amount = transaction['amount'] # TESTING
|
|
|
- current_app.logger.info(f'\n\n{raw_data}\n\nSignature: {signature}\nFrom: {from_name}\nEmail: {email}\nAmount: {amount}\n\n') # TESTING
|
|
|
-
|
|
|
- if not verify_gbhook_signature(raw_data, signature, gb_secret):
|
|
|
- return jsonify({'error': 'Invalid signature'}), 401
|
|
|
- if current_app.config['GB_SETTINGS']['integration']:
|
|
|
- try:
|
|
|
- event_type = event['event']
|
|
|
-
|
|
|
- if event_type == 'transaction.succeeded':
|
|
|
- transaction = event['data']
|
|
|
- from_name = f'{transaction["first_name"]} {transaction["last_name"]}'
|
|
|
- email = transaction['email']
|
|
|
- amount = transaction['amount']
|
|
|
- donation_info = [True, from_name, email, amount, '']
|
|
|
- donation_points = current_app.config['GB_SETTINGS']['donation_points']
|
|
|
- if accept_donation(donation_info, donation_points, 'GiveButter'):
|
|
|
- current_app.logger.info(f'Donation processed.')
|
|
|
- else:
|
|
|
- current_app.logger.info(f'Unhandled event type: {event_type}')
|
|
|
- except Exception as pgberror:
|
|
|
- current_app.logger.error(f'General exception processing gbhook: {pgberror}')
|
|
|
- else:
|
|
|
- current_app.logger.error(f'GiveButter donation recieved, but GiveButter integration is turned off. Doing nothing.')
|
|
|
- return jsonify({'status': 'Failed. Not currently accepting GiveButter donations.'}), 400
|
|
|
|
|
|
- return jsonify({'received': True}), 200
|
|
|
+ # if not verify_gbhook_signature(raw_data, signature, gb_secret): # GB is not currently signing payloads. Just sending the signing secret
|
|
|
+ # return jsonify({'error': 'Invalid signature'}), 401 # Leaving this here in case they ever decide to sign it
|
|
|
+
|
|
|
+ if signature == gb_secret:
|
|
|
+ if current_app.config['GB_SETTINGS']['integration']:
|
|
|
+ try:
|
|
|
+ event_type = event['event']
|
|
|
+
|
|
|
+ if event_type == 'transaction.succeeded':
|
|
|
+ transaction = event['data']
|
|
|
+ from_name = f'{transaction["first_name"]} {transaction["last_name"]}'
|
|
|
+ email = transaction['email']
|
|
|
+ amount = transaction['amount']
|
|
|
+ current_app.logger.debug(f'From: {from_name}\nEmail: {email}\nAmount: {amount}\n\n')
|
|
|
+ donation_info = [True, from_name, email, amount, '']
|
|
|
+ donation_points = current_app.config['GB_SETTINGS']['donation_points']
|
|
|
+ points_for_donations = current_app.config['GB_SETTINGS']['donations']
|
|
|
+ if points_for_donations: # Are we giving points for donations?
|
|
|
+ if accept_donation(donation_info, donation_points, 'GiveButter'):
|
|
|
+ current_app.logger.info(f'Donation processed.')
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'Points for donations is disabled. Doing nothing.')
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'Unhandled event type: {event_type}')
|
|
|
+ except Exception as pgberror:
|
|
|
+ current_app.logger.error(f'General exception processing gbhook: {pgberror}')
|
|
|
+
|
|
|
+ else:
|
|
|
+ current_app.logger.error(f'GiveButter donation recieved, but GiveButter integration is turned off. Doing nothing.')
|
|
|
+ return jsonify({'status': 'Failed. Not currently accepting GiveButter donations.'}), 400
|
|
|
+
|
|
|
+ return jsonify({'status': 'Success'}), 200 # If signature matched
|
|
|
+
|
|
|
+ else:
|
|
|
+ return jsonify({'status': 'Signature invalid'}), 401 # If signature didn't match
|
|
|
|
|
|
|
|
|
@ocb.route('/checkFollows') # Polled by follower.html template to check for new followers
|