#!/usr/bin/env python3 ''' This script will perform a simple query to the DarkOwl Vision Test API. A succesfull call will print {'answer': 'hoot'} ''' import requests import datetime import hmac import hashlib import base64 publicKey = 'PUBLIC_KEY_HERE' privateKey = 'PRIVATE_KEY_HERE' def main(): perform_query() def perform_query(): host = 'api.darkowl.com' endpoint = '/api/v1/test' url = f'https://{host}{endpoint}' date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') auth = generate_auth_header(endpoint, 'GET', privateKey, publicKey, date) headers = {'Authorization': auth, 'X-VISION-DATE': date, 'Accept': 'application/json'} r = requests.get(url, headers=headers) if r.status_code == 200: print(r.json()) else: print(r.content) exit(1) def generate_auth_header(abs_path, http_method, private_key, public_key, time_stamp): string2hash = http_method + abs_path + time_stamp bkey = bytes(source=private_key, encoding='UTF-8') bpayload = bytes(source=string2hash, encoding='UTF-8') hmacsha1 = hmac.new(bkey, bpayload, hashlib.sha1).digest() base64encoded = base64.b64encode(hmacsha1).decode('UTF-8') auth_header = f'OWL {public_key}:{base64encoded}' return auth_header if __name__ == '__main__': main()