add timestamps to management requests

This commit is contained in:
2024-04-11 19:06:53 +01:00
parent 11ecfe7cb0
commit 53af0e1087
4 changed files with 59 additions and 18 deletions

View File

@@ -8,6 +8,7 @@ import pathlib
import os
import json
import sys
import time
def get_args():
@@ -51,20 +52,34 @@ def main(args):
if args.command == "delete" and args.value != "confirm":
print("--value not set to 'confirm'... delete operation may fail")
r = requests.post(f"{'http' if args.http else 'https'}://{server}",
data = {
'Command': args.command,
'Shortlink': args.shortlink,
'Value': args.value,
},
headers = {
'Signature': 'SUS-SIGNATURE-' + hmac.new(
secret.encode("UTF-8"),
(args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"),
hashlib.sha256
).hexdigest()
}
)
# accoring to python documentation (https://docs.python.org/3/library/time.html#time.time)
# this function does not explicitly have to use unix time, and implementation is dependent
# platform.
# most platforms (windows, unix) will probably give unix time though.
#
# the server side (main.go file) does explicitly use unix time (time.Now().UnixNano()) to get
# this number, but hopefully there should be no issues on most platforms.
timestamp = str(time.time_ns())
data = {
'Command': args.command,
'Shortlink': args.shortlink,
'Value': args.value,
'Timestamp': timestamp,
}
headers = {
'Signature': 'SUS-SIGNATURE-' + hmac.new(
secret.encode("UTF-8"),
(timestamp + ":" + args.command + ":" + args.shortlink + ":" + args.value).encode("UTF-8"),
hashlib.sha256
).hexdigest()
}
print(f"{data=}")
print(f"{headers=}")
r = requests.post(f"{'http' if args.http else 'https'}://{server}", data=data, headers=headers)
print(r, file=sys.stderr)
print(r.content.decode().strip())
return 0