[refactor] build httpsig string
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
SouthFox 2023-06-09 11:07:02 +08:00
parent e9f8ca145b
commit 3f9e159eb1

View file

@ -4,9 +4,20 @@ Verify and build HTTP signatures
""" """
import base64 import base64
from dataclasses import dataclass
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5 from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from httpx import Headers
@dataclass
class SignedData:
method: str
path: str
signed_list: list
body_digest: str | None
headers: Headers
class HttpSignature: class HttpSignature:
@ -72,24 +83,21 @@ class HttpSignature:
@classmethod @classmethod
def build_signature_string( def build_signature_string(
cls, cls,
method : str, signed_data: SignedData,
path : str,
signed_headers : list,
body_digest : str | None,
headers,
) -> str: ) -> str:
""" """
Build signature string Build signature string
""" """
signed_string = [] signed_string = []
for signed_header in signed_headers: for signed_str in signed_data.signed_list:
if signed_header == "(request-target)": if signed_str == "(request-target)":
signed_string.append("(request-target): " signed_string.append("(request-target): "
+ method.lower() + ' ' + path) + signed_data.method.lower() + ' ' + signed_data.path)
elif signed_header == "digest" and body_digest: elif signed_str == "digest" and signed_data.body_digest:
signed_string.append("digest: " + body_digest) signed_string.append("digest: " + signed_data.body_digest)
else: else:
signed_string.append(signed_header + ": " signed_string.append(signed_str + ": "
+ headers[signed_header]) + signed_data.headers[signed_str])
return "\n".join(signed_string) return "\n".join(signed_string)