apsig 0.3.1__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: apsig
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Signature implementation used in ActivityPub.
5
5
  Author-Email: AmaseCocoa <amase.cocoa@gmail.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "apsig"
3
- version = "0.3.1"
3
+ version = "0.3.2"
4
4
  description = "Signature implementation used in ActivityPub."
5
5
  authors = [
6
6
  { name = "AmaseCocoa", email = "amase.cocoa@gmail.com" },
@@ -10,11 +10,11 @@ from urllib.parse import urlparse
10
10
  class draftVerifier:
11
11
  def _generate_digest(body: bytes | str):
12
12
  digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
13
- digest.update(body.encode("utf-8") if not isinstance(body, bytes) else body)
13
+ digest.update(body.encode("utf-8") if isinstance(body, str) else body)
14
14
  hash_bytes = digest.finalize()
15
15
  return "SHA-256=" + base64.b64encode(hash_bytes).decode("utf-8")
16
16
 
17
-
17
+ @staticmethod
18
18
  def verify(public_pem: str, method: str, url: str, headers: dict, body: bytes=b"") -> tuple[bool, str]:
19
19
  """Verifies the digital signature of an HTTP request.
20
20
 
@@ -33,7 +33,9 @@ class draftVerifier:
33
33
  Raises:
34
34
  ValueError: If the signature header is missing or if the algorithm is unsupported.
35
35
  """
36
- signature_header = headers.get("signature")
36
+ case_insensitive_headers = {key.lower(): value for key, value in headers.items()}
37
+
38
+ signature_header = case_insensitive_headers.get("signature")
37
39
  if not signature_header:
38
40
  return False, "Signature header is missing"
39
41
 
@@ -43,7 +45,7 @@ class draftVerifier:
43
45
  signature_parts[key.strip()] = value.strip().strip('"')
44
46
 
45
47
  signature = base64.b64decode(signature_parts["signature"])
46
- #keyId = signature_parts["keyId"]
48
+ keyId = signature_parts["keyId"]
47
49
  algorithm = signature_parts["algorithm"]
48
50
 
49
51
  if algorithm != "rsa-sha256":
@@ -74,7 +76,7 @@ class draftVerifier:
74
76
  return False, "Invalid signature"
75
77
 
76
78
  expected_digest = draftVerifier._generate_digest(body)
77
- if headers.get("digest") != expected_digest:
79
+ if case_insensitive_headers.get("digest") != expected_digest:
78
80
  return False, "Digest mismatch"
79
81
 
80
82
  date_header = headers.get("date")
@@ -33,7 +33,7 @@ class TestSignatureFunctions(unittest.TestCase):
33
33
 
34
34
  signed_headers = draftSigner.sign(self.private_key, method, url, headers, "https://example.com/users/johndoe#main-key", body)
35
35
 
36
- is_valid, message = draftVerifier.verify(self.public_pem, method, url, signed_headers, body.encode("utf-8"))
36
+ is_valid, message = draftVerifier.verify(public_pem=self.public_pem, method=method, url=url, headers=signed_headers, body=body.encode("utf-8"))
37
37
 
38
38
  self.assertTrue(is_valid)
39
39
  self.assertEqual(message, "Signature is valid")
@@ -44,7 +44,7 @@ class TestSignatureFunctions(unittest.TestCase):
44
44
  headers = {
45
45
  "Content-Type": "application/json",
46
46
  "Date": "Wed, 21 Oct 2015 07:28:00 GMT",
47
- "signature": 'keyId="your-key-id",algorithm="rsa-sha256",headers="(request-target) Content-Type Date",signature="invalid_signature"'
47
+ "Signature": 'keyId="your-key-id",algorithm="rsa-sha256",headers="(request-target) Content-Type Date",signature="invalid_signature"'
48
48
  }
49
49
  body = '{"key": "value"}'
50
50
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes