#!/usr/bin/env python3

import requests
import base64
import hashlib
import hmac
from datetime import datetime

publicKey = 'PUBLIC_KEY_HERE'
privateKey = 'PRIVATE_KEY_HERE'


def main():

	### Email addresses from a given domain that have a password in the record
	payload = {
	    "searchText": "",
	    "find": {"emailDomain": ["domain.com"]},
	    "filter": {"has": ["password"]},
	    "options": {}
	}

	r = perform_query(payload)

	print(r)
	

def perform_query(payload):
    host = 'api.darkowl.com'
    endpoint = '/api/v1/leaks/search'

    #Generate url string
    url = f'https://{host}{endpoint}'
    headers = gen_headers()

    r = requests.post(url, headers=headers, json=payload)
    if r.status_code == 200:
        return r.json()
    else:
        print(r.content)
        exit(1)


def gen_headers(verb='POST', endpoint='/api/v1/leaks/search'):

    date = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

    string2hash = verb + endpoint + date
    bkey = bytes(source=privateKey, 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 {publicKey}:{base64encoded}'

    headers = {'Authorization': auth_header, 'X-VISION-DATE': date, 'content-type': 'application/json'}
    return headers


if __name__ == '__main__':
    main()	