apsig 0.5.0__tar.gz → 0.5.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.5.0
3
+ Version: 0.5.2
4
4
  Summary: Signature implementation used in ActivityPub.
5
5
  Author-Email: AmaseCocoa <amase.cocoa@gmail.com>
6
6
  License: MIT
@@ -31,7 +31,7 @@ classifiers = [
31
31
  ]
32
32
  requires-python = ">=3.10"
33
33
  readme = "README.md"
34
- version = "0.5.0"
34
+ version = "0.5.2"
35
35
 
36
36
  [project.license]
37
37
  text = "MIT"
@@ -0,0 +1 @@
1
+ __version__ = '0.5.2'
@@ -1,5 +1,7 @@
1
1
  from typing import Any
2
2
  from typing_extensions import deprecated
3
+ import json
4
+
3
5
  from cryptography.hazmat.primitives.asymmetric import padding, rsa
4
6
  from cryptography.hazmat.primitives import hashes
5
7
 
@@ -17,7 +19,7 @@ class draftSigner:
17
19
  return signer.sign()
18
20
 
19
21
  class Signer:
20
- def __init__(self, headers: dict[Any, Any], private_key: rsa.RSAPrivateKey, method: str, url: str, key_id: str, body: bytes=b"") -> None:
22
+ def __init__(self, headers: dict[Any, Any], private_key: rsa.RSAPrivateKey, method: str, url: str, key_id: str, body: bytes | dict=b"") -> None:
21
23
  """Signs an HTTP request with a digital signature.
22
24
 
23
25
  Args:
@@ -45,12 +47,16 @@ class Signer:
45
47
  self.method = method
46
48
  self.url = url
47
49
  self.key_id = key_id
48
- self.body = body
50
+ if isinstance(body, dict):
51
+ self.body = json.dumps(body, separators=(',', ':')).encode("utf-8")
52
+ else:
53
+ self.body = body
49
54
 
50
55
  if not self.headers.get("Host"):
51
56
  self.headers["Host"] = self.parsed_url.netloc
52
57
 
53
- self.__generate_digest(self.body)
58
+ if method.upper() != "GET":
59
+ self.__generate_digest(self.body)
54
60
 
55
61
  def __generate_sign_header(self, signature: str):
56
62
  self.headers["Signature"] = signature
@@ -1,12 +1,13 @@
1
1
  from typing import Union
2
2
  import base64
3
3
  from urllib.parse import urlparse
4
+ import json
4
5
 
5
6
  import pytz
6
7
  from cryptography.exceptions import InvalidSignature
7
8
  from cryptography.hazmat.backends import default_backend
8
9
  from cryptography.hazmat.primitives import hashes, serialization
9
- from cryptography.hazmat.primitives.asymmetric import padding
10
+ from cryptography.hazmat.primitives.asymmetric import padding, rsa
10
11
  from pyfill import datetime
11
12
  from typing_extensions import deprecated
12
13
 
@@ -20,7 +21,7 @@ class draftVerifier:
20
21
  "apsig.draft.verify.draftVerifier is deprecated; use apsig.draft.verify.Verifier instead. This will be removed in apsig 1.0."
21
22
  )
22
23
  def verify(
23
- public_pem: str, method: str, url: str, headers: dict, body: bytes = b""
24
+ public_pem: str, method: str, url: str, headers: dict, body: bytes = None
24
25
  ) -> tuple[bool, str]:
25
26
  """Verifies the digital signature of an HTTP request.
26
27
 
@@ -51,7 +52,7 @@ class draftVerifier:
51
52
 
52
53
  class Verifier:
53
54
  def __init__(
54
- self, public_pem: str, method: str, url: str, headers: dict, body: bytes = b""
55
+ self, public_pem: Union[rsa.RSAPublicKey, str], method: str, url: str, headers: dict, body: bytes | dict = None
55
56
  ) -> None:
56
57
  """
57
58
  Args:
@@ -61,12 +62,20 @@ class Verifier:
61
62
  headers (dict): A dictionary of HTTP headers, including the signature and other relevant information.
62
63
  body (bytes, optional): The request body. Defaults to an empty byte string.
63
64
  """
64
- self.public_pem = public_pem
65
+ if isinstance(public_pem, str) or isinstance(public_pem, bytes):
66
+ self.public_key = serialization.load_pem_public_key(
67
+ public_pem.encode("utf-8") if isinstance(public_pem, str) else public_pem, backend=default_backend()
68
+ )
69
+ else:
70
+ self.public_key = public_pem
65
71
  self.method = method
66
72
  self.url = url
67
73
  self.headers_raw = headers
68
74
  self.headers = {key.lower(): value for key, value in headers.items()}
69
- self.body = body
75
+ if isinstance(body, dict):
76
+ self.body = json.dumps(body, separators=(',', ':')).encode("utf-8")
77
+ else:
78
+ self.body = body
70
79
 
71
80
  def __decode_sign(self, signature):
72
81
  return base64.standard_b64decode(signature)
@@ -120,12 +129,8 @@ class Verifier:
120
129
  signature_headers, headers=signed_headers
121
130
  ).encode("utf-8")
122
131
 
123
- public_key = serialization.load_pem_public_key(
124
- self.public_pem.encode("utf-8"), backend=default_backend()
125
- )
126
-
127
132
  try:
128
- public_key.verify(
133
+ self.public_key.verify(
129
134
  signature, signature_string, padding.PKCS1v15(), hashes.SHA256()
130
135
  )
131
136
  except InvalidSignature:
@@ -135,13 +140,14 @@ class Verifier:
135
140
  )
136
141
  return None
137
142
 
138
- expected_digest = calculate_digest(self.body)
139
- if headers.get("digest") != expected_digest:
140
- if raise_on_fail:
141
- raise VerificationFailed(
142
- "Digest mismatch"
143
- )
144
- return None
143
+ if self.body:
144
+ expected_digest = calculate_digest(self.body)
145
+ if headers.get("digest") != expected_digest:
146
+ if raise_on_fail:
147
+ raise VerificationFailed(
148
+ "Digest mismatch"
149
+ )
150
+ return None
145
151
 
146
152
  date_header = headers.get("date")
147
153
  if date_header:
@@ -55,6 +55,37 @@ class TestSignatureFunctions(unittest.TestCase):
55
55
  self.assertIsInstance(result, str)
56
56
  self.assertEqual(result, "https://example.com/users/johndoe#main-key")
57
57
 
58
+ def test_create_and_verify_signature_method_get(self):
59
+ date = email.utils.formatdate(usegmt=True)
60
+
61
+ method = "GET"
62
+ url = "https://example.com/api/resource"
63
+ headers = {
64
+ "Content-Type": "application/json",
65
+ "Date": date,
66
+ }
67
+
68
+ signer = Signer(
69
+ headers=headers,
70
+ private_key=self.private_key,
71
+ method=method,
72
+ url=url,
73
+ key_id="https://example.com/users/johndoe#main-key",
74
+ )
75
+
76
+ signed_headers = signer.sign()
77
+ verifier = Verifier(
78
+ public_pem=self.public_pem,
79
+ method=method,
80
+ url=url,
81
+ headers=signed_headers,
82
+ )
83
+
84
+ result = verifier.verify(raise_on_fail=True)
85
+
86
+ self.assertIsInstance(result, str)
87
+ self.assertEqual(result, "https://example.com/users/johndoe#main-key")
88
+
58
89
  def test_too_far_date(self):
59
90
  method = "POST"
60
91
  url = "https://example.com/api/resource"
@@ -1 +0,0 @@
1
- __version__ = '0.5.0'
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
File without changes
File without changes