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.
- {apsig-0.5.0 → apsig-0.5.2}/PKG-INFO +1 -1
- {apsig-0.5.0 → apsig-0.5.2}/pyproject.toml +1 -1
- apsig-0.5.2/src/apsig/_version.py +1 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/draft/sign.py +9 -3
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/draft/verify.py +23 -17
- {apsig-0.5.0 → apsig-0.5.2}/tests/test_draft.py +31 -0
- apsig-0.5.0/src/apsig/_version.py +0 -1
- {apsig-0.5.0 → apsig-0.5.2}/COPYING.md +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/LICENSE +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/README.md +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/__init__.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/__polyfill/datetime.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/actor/keytools.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/draft/__init__.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/draft/tools.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/exceptions.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/ld_signature.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/proof/sign.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/proof/verify.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/src/apsig/tools.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/tests/__init__.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/tests/test_ld.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/tests/test_multibase_ed25519.py +0 -0
- {apsig-0.5.0 → apsig-0.5.2}/tests/test_proof.py +0 -0
|
@@ -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
|
-
|
|
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
|
-
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
if
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
File without changes
|
|
File without changes
|