forked from OpenAF/roJob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rojob.py.sample2
61 lines (49 loc) · 1.86 KB
/
rojob.py.sample2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# vvv CHANGE vvv THE URL AND APIKEY FOR YOUR CASE
URL = "http://127.0.0.1:8787"
APIKEY = "..."
# ^^^ CHANGE ^^^
# ---------------------------
import sys, os, base64, datetime, time, hashlib, hmac, json, binascii, re, math, httplib, urllib
# Sign with hmacSHA256
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
# Sign with hmacSHA256 but returning in hex
def signx(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).hexdigest()
# Sign data with a given aURI for the auth field
def signAuth(aURI, data):
dateStamp = str(int(math.floor(time.time() / (60*2))))
#print(dateStamp)
kDateStamp = sign("roJob" + APIKEY, (dateStamp).encode('utf-8'))
#print("roJob" + APIKEY)
kURI = sign(kDateStamp, (aURI).encode('utf-8'))
#print((aURI).encode('utf-8'))
kData = sign(kURI, hashlib.sha256(re.sub(r'[\n \t\r]+$', '', data)).hexdigest().encode('utf-8'))
#print(re.sub(r'\n+$', '', data))
#print(hashlib.sha256(re.sub(r'\n+$', '', data)).hexdigest().encode('utf-8'))
#print(binascii.hexlify(kData))
return binascii.hexlify(kData)
# Check the arguments
# -------------------
c = 0
restParams = { }
for arg in sys.argv:
if (c > 1):
tmpArgs = arg.split("=")
restParams[tmpArgs[0]] = tmpArgs[1]
c += 1
# Get the ojob file
# -----------------
with open(sys.argv[1], 'r') as file:
fdata = file.read()
# Prepare the parameters and request header
# -----------------------------------------
restParams = urllib.urlencode(restParams)
PARAMS = { "auth": signAuth("/ojob?" + restParams, fdata) }
# Make the call
# -------------
conn = httplib.HTTPConnection(re.sub(r'^[^/]+//', "", URL))
conn.request('POST', "/ojob?" + restParams, fdata, PARAMS)
# Print the result
# ----------------
print(conn.getresponse().read())