Include date in http signature

master
Bob Mottram 2019-08-15 10:08:18 +01:00
parent 6fbc778609
commit be1a061122
1 changed files with 3 additions and 2 deletions

View File

@ -14,6 +14,7 @@ from Crypto.Signature import pkcs1_15
from requests.auth import AuthBase from requests.auth import AuthBase
import base64 import base64
import json import json
from time import gmtime, strftime
def signPostHeaders(privateKeyPem: str, nickname: str, domain: str, \ def signPostHeaders(privateKeyPem: str, nickname: str, domain: str, \
port: int,path: str, \ port: int,path: str, \
@ -30,7 +31,7 @@ def signPostHeaders(privateKeyPem: str, nickname: str, domain: str, \
else: else:
bodyDigest = \ bodyDigest = \
base64.b64encode(SHA256.new(messageBodyJson.encode()).digest()) base64.b64encode(SHA256.new(messageBodyJson.encode()).digest())
headers = {'host': domain, 'digest': f'SHA-256={bodyDigest}'} headers = {'host': domain, 'date': strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()),'digest': f'SHA-256={bodyDigest}'}
privateKeyPem = RSA.import_key(privateKeyPem) privateKeyPem = RSA.import_key(privateKeyPem)
headers.update({ headers.update({
'(request-target)': f'post {path}', '(request-target)': f'post {path}',
@ -72,7 +73,7 @@ def createSignedHeader(privateKeyPem: str,nickname: str,domain: str,port: int, \
messageBodyJsonStr=json.dumps(messageBodyJson) messageBodyJsonStr=json.dumps(messageBodyJson)
bodyDigest = \ bodyDigest = \
base64.b64encode(SHA256.new(messageBodyJsonStr.encode()).digest()) base64.b64encode(SHA256.new(messageBodyJsonStr.encode()).digest())
headers = {'host': headerDomain, 'digest': f'SHA-256={bodyDigest}'} headers = {'host': headerDomain, 'date': strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()), 'digest': f'SHA-256={bodyDigest}'}
path='/inbox' path='/inbox'
signatureHeader = signPostHeaders(privateKeyPem, nickname, domain, port, \ signatureHeader = signPostHeaders(privateKeyPem, nickname, domain, port, \
path, httpPrefix, None) path, httpPrefix, None)