Skip to content

Commit 6807c17

Browse files
authored
Add type annotation to SignedHash model (#5)
* Add model ClassVar type annotation and bump to 0.5.1 * Format code with Black * Add pylint comments * Use str instead of ClassVar * Convert tests to work with httpx * Pin specific versions of fastapi, httpx, h11, pydantic
1 parent b292eaf commit 6807c17

File tree

8 files changed

+30
-22
lines changed

8 files changed

+30
-22
lines changed

authsign/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
""" authsign package """
22

3-
__version__ = "0.5.0"
3+
__version__ = "0.5.1"

authsign/acme_signer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def select_http01_chall(self, orderr):
8585
if isinstance(i.chall, challenges.HTTP01):
8686
return i
8787

88+
# pylint: disable=broad-exception-raised
8889
raise Exception("HTTP-01 challenge was not offered by the CA server.")
8990

9091
@contextmanager

authsign/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Config:
3030
class SignedHash(SignReq):
3131
"""Signed Hash of the SignReq, created by signer, ready for verification"""
3232

33-
version = "0.1.0"
33+
version: str = "0.1.0"
3434

3535
software: Optional[str]
3636

authsign/signer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Generate or load certs and handle signing
33
"""
4+
45
from pathlib import Path
56

67
import datetime
@@ -192,6 +193,7 @@ def __init__(
192193
self.update_signing_key_and_cert()
193194

194195
if not self.domain_signing:
196+
# pylint: disable=broad-exception-raised
195197
raise Exception("Could not load domain signing cert + keys")
196198

197199
self.timestampers = [Timestamper(**ts_data) for ts_data in timestamping]
@@ -311,6 +313,7 @@ def __call__(self, sign_req):
311313
timestamp - self.stamp_duration, timestamp, created
312314
)
313315
print(msg)
316+
# pylint: disable=broad-exception-raised
314317
raise Exception(msg)
315318

316319
return SignedHash(

authsign/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def format_date(date):
4444
@contextlib.contextmanager
4545
def open_file(filename_or_resource, mode):
4646
"""open file from either package or file system"""
47+
# pylint: disable=deprecated-method
4748
res = None
4849
if filename_or_resource.startswith("pkg://"):
4950
pkg, resource = filename_or_resource[6:].split("/", 1)

authsign/verifier.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" Verify signed responses api"""
22

3-
43
import base64
54
import traceback
65

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
cryptography
22
rfc3161ng
33
pem
4-
fastapi
4+
fastapi==0.110.1
55
uvicorn
66
pyyaml
77
acme
88
pyasn1
99
python-dateutil
10+
pydantic==1.10.13
11+
httpx>=0.22.0,<1.0
12+
h11<0.15,>=0.13

tests/test_authsign.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ def test_sign_invalid_token(domain, config_file):
134134
req = {"hash": "some_data", "created": now}
135135

136136
with TestClient(app) as client:
137-
resp = client.post("/sign", json=req)
137+
resp = client.request("POST", "/sign", json=req)
138138
assert resp.status_code == 403
139139

140-
resp = client.post(
140+
resp = client.request(
141+
"POST",
141142
"/sign",
142143
headers={
143144
"Authorization": "bearer " + base64.b64encode(b"abc").decode("ascii")
@@ -153,8 +154,8 @@ def test_sign_valid_token(domain, config_file):
153154

154155
global signed_hash
155156
with TestClient(app) as client:
156-
resp = client.post(
157-
"/sign", headers={"Authorization": "bearer " + auth_token}, json=req
157+
resp = client.request(
158+
"POST", "/sign", headers={"Authorization": "bearer " + auth_token}, json=req
158159
)
159160
signed_hash = resp.json()
160161
print(signed_hash)
@@ -189,8 +190,8 @@ def test_sign_valid_token_a_few_mins_ago(domain, config_file):
189190
req = {"hash": "some_data", "created": now}
190191

191192
with TestClient(app) as client:
192-
resp = client.post(
193-
"/sign", headers={"Authorization": "bearer " + auth_token}, json=req
193+
resp = client.request(
194+
"POST", "/sign", headers={"Authorization": "bearer " + auth_token}, json=req
194195
)
195196
assert resp.status_code == 200
196197

@@ -205,8 +206,8 @@ def test_sign_valid_token_wrong_date_too_early(domain, config_file):
205206

206207
global signed_hash
207208
with TestClient(app) as client:
208-
resp = client.post(
209-
"/sign", headers={"Authorization": "bearer " + auth_token}, json=req
209+
resp = client.request(
210+
"POST", "/sign", headers={"Authorization": "bearer " + auth_token}, json=req
210211
)
211212
assert resp.status_code == 400
212213

@@ -221,8 +222,8 @@ def test_sign_valid_token_bad_date_in_future(domain, config_file):
221222

222223
global signed_hash
223224
with TestClient(app) as client:
224-
resp = client.post(
225-
"/sign", headers={"Authorization": "bearer " + auth_token}, json=req
225+
resp = client.request(
226+
"POST", "/sign", headers={"Authorization": "bearer " + auth_token}, json=req
226227
)
227228
assert resp.status_code == 400
228229

@@ -231,31 +232,31 @@ def test_verify_invalid_missing(domain, config_file):
231232
with TestClient(app) as client:
232233
req = signed_hash.copy()
233234
req.pop("timeSignature", "")
234-
resp = client.post("/verify", json=req)
235+
resp = client.request("POST", "/verify", json=req)
235236
assert resp.status_code == 422
236237

237238

238239
def test_verify_invalid_hash(domain, config_file):
239240
with TestClient(app) as client:
240241
req = signed_hash.copy()
241242
req["hash"] = "other data"
242-
resp = client.post("/verify", json=req)
243+
resp = client.request("POST", "/verify", json=req)
243244
assert resp.status_code == 400
244245

245246

246247
def test_verify_wrong_cert(domain, config_file):
247248
with TestClient(app) as client:
248249
req = signed_hash.copy()
249250
req["timestampCert"] = req["domainCert"]
250-
resp = client.post("/verify", json=req)
251+
resp = client.request("POST", "/verify", json=req)
251252
assert resp.status_code == 400
252253

253254

254255
def test_verify_wrong_cross_signed_cert(domain, config_file):
255256
with TestClient(app) as client:
256257
req = signed_hash.copy()
257258
req["crossSignedCert"] = req["timestampCert"]
258-
resp = client.post("/verify", json=req)
259+
resp = client.request("POST", "/verify", json=req)
259260
assert resp.status_code == 400
260261

261262

@@ -264,7 +265,7 @@ def test_verify_invalid_bad_date(domain, config_file):
264265
# date to early
265266
req = signed_hash.copy()
266267
req["created"] = "abc"
267-
resp = client.post("/verify", json=req)
268+
resp = client.request("POST", "/verify", json=req)
268269
assert resp.status_code == 422
269270

270271

@@ -275,21 +276,21 @@ def test_verify_invalid_date_out_of_range(domain, config_file):
275276
req["created"] = format_date(
276277
datetime.datetime.utcnow() - datetime.timedelta(days=1)
277278
)
278-
resp = client.post("/verify", json=req)
279+
resp = client.request("POST", "/verify", json=req)
279280
assert resp.status_code == 400
280281

281282
# date to late
282283
req = signed_hash.copy()
283284
req["created"] = format_date(
284285
datetime.datetime.utcnow() + datetime.timedelta(days=1)
285286
)
286-
resp = client.post("/verify", json=req)
287+
resp = client.request("POST", "/verify", json=req)
287288
assert resp.status_code == 400
288289

289290

290291
def test_verify_valid(domain, config_file):
291292
with TestClient(app) as client:
292-
resp = client.post("/verify", json=signed_hash)
293+
resp = client.request("POST", "/verify", json=signed_hash)
293294
assert resp.status_code == 200
294295
res = resp.json()
295296
assert res["observer"] == domain

0 commit comments

Comments
 (0)