altcha 2.1.0__tar.gz → 2.2.0__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.
- {altcha-2.1.0/altcha.egg-info → altcha-2.2.0}/PKG-INFO +1 -1
- {altcha-2.1.0 → altcha-2.2.0}/altcha/__init__.py +1 -1
- {altcha-2.1.0 → altcha-2.2.0}/altcha/v1.py +15 -15
- {altcha-2.1.0 → altcha-2.2.0}/altcha/v2.py +9 -7
- {altcha-2.1.0 → altcha-2.2.0/altcha.egg-info}/PKG-INFO +1 -1
- {altcha-2.1.0 → altcha-2.2.0}/setup.py +8 -3
- {altcha-2.1.0 → altcha-2.2.0}/tests/test_altcha.py +46 -6
- {altcha-2.1.0 → altcha-2.2.0}/tests/test_altcha_v2.py +7 -9
- {altcha-2.1.0 → altcha-2.2.0}/LICENSE +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/MANIFEST.in +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/README.md +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/altcha/altcha.py +12 -12
- {altcha-2.1.0 → altcha-2.2.0}/altcha/py.typed +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/altcha.egg-info/SOURCES.txt +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/altcha.egg-info/dependency_links.txt +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/altcha.egg-info/top_level.txt +0 -0
- {altcha-2.1.0 → altcha-2.2.0}/setup.cfg +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# ruff: noqa: F401
|
|
1
|
+
# ruff: noqa: F401, I001 -- imports are grouped by API version, not sorted
|
|
2
2
|
# ---------------------------------------------------------------------------
|
|
3
3
|
# V2 API (main / unprefixed)
|
|
4
4
|
# ---------------------------------------------------------------------------
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import base64
|
|
4
|
+
import datetime
|
|
3
5
|
import hashlib
|
|
4
6
|
import hmac
|
|
5
|
-
import base64
|
|
6
7
|
import json
|
|
7
8
|
import secrets
|
|
8
9
|
import time
|
|
9
10
|
import urllib.parse
|
|
10
11
|
from typing import Literal, TypedDict, cast, overload
|
|
11
|
-
import datetime
|
|
12
12
|
|
|
13
13
|
# Define algorithms
|
|
14
14
|
SHA1: Literal["SHA-1"] = "SHA-1"
|
|
@@ -153,7 +153,7 @@ class Payload:
|
|
|
153
153
|
return base64.b64encode(json.dumps(self.to_dict()).encode()).decode()
|
|
154
154
|
|
|
155
155
|
@classmethod
|
|
156
|
-
def from_dict(cls, data: PayloadType) ->
|
|
156
|
+
def from_dict(cls, data: PayloadType) -> Payload:
|
|
157
157
|
"""Create a Payload from a dictionary."""
|
|
158
158
|
return cls(
|
|
159
159
|
algorithm=cast(AlgoType, data["algorithm"]),
|
|
@@ -164,7 +164,7 @@ class Payload:
|
|
|
164
164
|
)
|
|
165
165
|
|
|
166
166
|
@classmethod
|
|
167
|
-
def from_base64(cls, encoded: str) ->
|
|
167
|
+
def from_base64(cls, encoded: str) -> Payload:
|
|
168
168
|
"""Create a Payload from a base64 encoded JSON string."""
|
|
169
169
|
data = cast(PayloadType, json.loads(base64.b64decode(encoded).decode()))
|
|
170
170
|
return cls.from_dict(data)
|
|
@@ -561,17 +561,18 @@ def verify_solution(
|
|
|
561
561
|
except ValueError: # Guard against malformed expires
|
|
562
562
|
return False, "Altcha payload expired"
|
|
563
563
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
)
|
|
570
|
-
|
|
564
|
+
# Hash the submitted salt verbatim (plus the splicing delimiter). Re-parsing
|
|
565
|
+
# and re-serializing its params would collapse duplicate keys differently
|
|
566
|
+
# from the expiry check above, letting a forged salt pass as unexpired.
|
|
567
|
+
algorithm = cast(AlgoType, p["algorithm"])
|
|
568
|
+
salt = cast(str, p["salt"])
|
|
569
|
+
if not salt.endswith("&"):
|
|
570
|
+
salt += "&"
|
|
571
|
+
expected_challenge = hash_hex(algorithm, (salt + str(p["number"])).encode())
|
|
572
|
+
expected_signature = hmac_hex(algorithm, expected_challenge.encode(), hmac_key)
|
|
571
573
|
|
|
572
574
|
return (
|
|
573
|
-
expected_challenge
|
|
574
|
-
and expected_challenge.signature == p["signature"]
|
|
575
|
+
expected_challenge == p["challenge"] and expected_signature == p["signature"]
|
|
575
576
|
), None
|
|
576
577
|
|
|
577
578
|
|
|
@@ -770,8 +771,7 @@ def solve_challenge(
|
|
|
770
771
|
max_number = 1000000
|
|
771
772
|
challenge_str = challenge
|
|
772
773
|
|
|
773
|
-
|
|
774
|
-
start = 0
|
|
774
|
+
start = max(start, 0)
|
|
775
775
|
|
|
776
776
|
start_time = time.time()
|
|
777
777
|
for n in range(start, max_number + 1):
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import base64
|
|
4
|
+
import datetime
|
|
3
5
|
import hashlib
|
|
4
6
|
import hmac as _hmac_module
|
|
5
|
-
import base64
|
|
6
7
|
import json
|
|
7
8
|
import re
|
|
8
9
|
import secrets
|
|
@@ -12,7 +13,6 @@ import urllib.error
|
|
|
12
13
|
import urllib.parse
|
|
13
14
|
import urllib.request
|
|
14
15
|
from typing import Callable, Literal
|
|
15
|
-
import datetime
|
|
16
16
|
|
|
17
17
|
# ---------------------------------------------------------------------------
|
|
18
18
|
# Types
|
|
@@ -368,8 +368,10 @@ def derive_key_scrypt(
|
|
|
368
368
|
|
|
369
369
|
|
|
370
370
|
try:
|
|
371
|
-
from argon2.low_level import
|
|
372
|
-
|
|
371
|
+
from argon2.low_level import ( # type: ignore
|
|
372
|
+
Type as _Argon2Type,
|
|
373
|
+
hash_secret_raw as _argon2_hash_secret_raw,
|
|
374
|
+
)
|
|
373
375
|
|
|
374
376
|
def derive_key_argon2id(
|
|
375
377
|
parameters: ChallengeParameters, salt: bytes, password: bytes
|
|
@@ -806,7 +808,7 @@ def parse_verification_data(
|
|
|
806
808
|
else:
|
|
807
809
|
result[key] = value.strip()
|
|
808
810
|
return result
|
|
809
|
-
except
|
|
811
|
+
except (ValueError, TypeError, AttributeError):
|
|
810
812
|
return None
|
|
811
813
|
|
|
812
814
|
|
|
@@ -985,7 +987,7 @@ def verify_server(
|
|
|
985
987
|
for attempt in range(attempts):
|
|
986
988
|
try:
|
|
987
989
|
status, resp_body = post(url, data, req_headers, timeout)
|
|
988
|
-
except Exception as e:
|
|
990
|
+
except Exception as e: # noqa: BLE001 -- custom transports may raise anything
|
|
989
991
|
if attempt >= attempts - 1:
|
|
990
992
|
return VerifyServerResult(
|
|
991
993
|
verified=False, reason=str(e) or type(e).__name__
|
|
@@ -996,7 +998,7 @@ def verify_server(
|
|
|
996
998
|
if status == 400:
|
|
997
999
|
try:
|
|
998
1000
|
err_data = json.loads(resp_body.decode())
|
|
999
|
-
except
|
|
1001
|
+
except ValueError:
|
|
1000
1002
|
err_data = None
|
|
1001
1003
|
reason = (err_data or {}).get("error") or f"HTTP_{status}"
|
|
1002
1004
|
return VerifyServerResult(verified=False, reason=reason)
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
from
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from setuptools import ( # type: ignore[import-not-found, import-untyped, unused-ignore]
|
|
4
|
+
find_packages,
|
|
5
|
+
setup,
|
|
6
|
+
)
|
|
2
7
|
|
|
3
8
|
setup(
|
|
4
9
|
name="altcha",
|
|
5
|
-
version="2.
|
|
10
|
+
version="2.2.0",
|
|
6
11
|
description="A library for creating and verifying challenges for ALTCHA.",
|
|
7
|
-
long_description=
|
|
12
|
+
long_description=Path("README.md").read_text(encoding="utf-8"),
|
|
8
13
|
long_description_content_type="text/markdown",
|
|
9
14
|
author="Daniel Regeci",
|
|
10
15
|
author_email="536331+ovx@users.noreply.github.com",
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import base64
|
|
1
2
|
import datetime
|
|
2
3
|
import hashlib
|
|
3
4
|
import hmac
|
|
5
|
+
import json
|
|
4
6
|
import time
|
|
5
7
|
import unittest
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
+
from collections.abc import Callable
|
|
9
|
+
|
|
8
10
|
from altcha.altcha import (
|
|
9
11
|
ChallengeOptions,
|
|
10
12
|
Payload,
|
|
@@ -277,6 +279,44 @@ class TestALTCHA(unittest.TestCase):
|
|
|
277
279
|
result, _ = verify_solution(payload_encoded, self.hmac_key, check_expires=False)
|
|
278
280
|
self.assertFalse(result)
|
|
279
281
|
|
|
282
|
+
def _expired_payload_with_salt(
|
|
283
|
+
self, salt_for: Callable[[str, str], str]
|
|
284
|
+
) -> Payload:
|
|
285
|
+
options = ChallengeOptions(
|
|
286
|
+
algorithm="SHA-256",
|
|
287
|
+
max_number=1000,
|
|
288
|
+
hmac_key=self.hmac_key,
|
|
289
|
+
expires=datetime.datetime.now(datetime.timezone.utc)
|
|
290
|
+
- datetime.timedelta(minutes=1),
|
|
291
|
+
number=123,
|
|
292
|
+
)
|
|
293
|
+
challenge = create_challenge(options)
|
|
294
|
+
base, query = challenge.salt.split("?", 1)
|
|
295
|
+
original = query.split("expires=")[1].rstrip("&")
|
|
296
|
+
return Payload(
|
|
297
|
+
algorithm="SHA-256",
|
|
298
|
+
challenge=challenge.challenge,
|
|
299
|
+
number=123,
|
|
300
|
+
salt=salt_for(base, original),
|
|
301
|
+
signature=challenge.signature,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def test_verify_solution_duplicate_expires_rejected(self):
|
|
305
|
+
future = str(int(time.time()) + 3600)
|
|
306
|
+
payload = self._expired_payload_with_salt(
|
|
307
|
+
lambda base, orig: f"{base}?expires={future}&expires={orig}&"
|
|
308
|
+
)
|
|
309
|
+
result, _ = verify_solution(payload, self.hmac_key, check_expires=True)
|
|
310
|
+
self.assertFalse(result)
|
|
311
|
+
|
|
312
|
+
def test_verify_solution_reencoded_salt_rejected(self):
|
|
313
|
+
# A percent-encoded variant of the issued salt must not verify.
|
|
314
|
+
payload = self._expired_payload_with_salt(
|
|
315
|
+
lambda base, orig: f"{base}?expires=%{ord(orig[0]):02X}{orig[1:]}&"
|
|
316
|
+
)
|
|
317
|
+
result, _ = verify_solution(payload, self.hmac_key, check_expires=False)
|
|
318
|
+
self.assertFalse(result)
|
|
319
|
+
|
|
280
320
|
def test_valid_signature(self):
|
|
281
321
|
expire_time = int(time.time()) + 600 # 10 minutes from now
|
|
282
322
|
verification_data = (
|
|
@@ -400,13 +440,13 @@ class TestALTCHA(unittest.TestCase):
|
|
|
400
440
|
self.assertEqual(solution.number, 100, "Solution be 100")
|
|
401
441
|
|
|
402
442
|
def test_hash_hex(self):
|
|
403
|
-
result = hash_hex("SHA-256", "testdata"
|
|
404
|
-
self.assertEqual(result, hashlib.sha256("testdata"
|
|
443
|
+
result = hash_hex("SHA-256", b"testdata")
|
|
444
|
+
self.assertEqual(result, hashlib.sha256(b"testdata").hexdigest())
|
|
405
445
|
|
|
406
446
|
def test_hmac_hex(self):
|
|
407
|
-
result = hmac_hex("SHA-256", "testdata"
|
|
447
|
+
result = hmac_hex("SHA-256", b"testdata", self.hmac_key)
|
|
408
448
|
expected = hmac.new(
|
|
409
|
-
self.hmac_key.encode(), "testdata"
|
|
449
|
+
self.hmac_key.encode(), b"testdata", hashlib.sha256
|
|
410
450
|
).hexdigest()
|
|
411
451
|
self.assertEqual(result, expected)
|
|
412
452
|
|
|
@@ -3,6 +3,7 @@ import json
|
|
|
3
3
|
import struct
|
|
4
4
|
import unittest
|
|
5
5
|
import unittest.mock
|
|
6
|
+
from typing import cast
|
|
6
7
|
|
|
7
8
|
from altcha.v2 import (
|
|
8
9
|
DEFAULT_HMAC_ALGORITHM,
|
|
@@ -26,7 +27,6 @@ from altcha.v2 import (
|
|
|
26
27
|
verify_solution,
|
|
27
28
|
)
|
|
28
29
|
|
|
29
|
-
|
|
30
30
|
HMAC_KEY = "test-secret"
|
|
31
31
|
|
|
32
32
|
|
|
@@ -424,14 +424,12 @@ class TestParseVerificationData(unittest.TestCase):
|
|
|
424
424
|
assert result is not None
|
|
425
425
|
self.assertEqual(result["classification"], "GOOD")
|
|
426
426
|
|
|
427
|
-
def
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
)
|
|
434
|
-
self.assertIsNotNone(result) # empty string is valid (empty dict)
|
|
427
|
+
def test_empty_string_returns_empty_dict(self):
|
|
428
|
+
self.assertEqual(parse_verification_data(""), {})
|
|
429
|
+
|
|
430
|
+
def test_unparseable_input_returns_none(self):
|
|
431
|
+
# bytes parse into bytes pairs, which the str-based coercion rejects
|
|
432
|
+
self.assertIsNone(parse_verification_data(cast(str, b"verified=true")))
|
|
435
433
|
|
|
436
434
|
|
|
437
435
|
class TestVerifyServerSignature(unittest.TestCase):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -6,27 +6,27 @@ from ``altcha.altcha`` continues to work unchanged.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from .v1 import (
|
|
9
|
+
DEFAULT_ALGORITHM as DEFAULT_ALGORITHM,
|
|
10
|
+
DEFAULT_MAX_NUMBER as DEFAULT_MAX_NUMBER,
|
|
11
|
+
DEFAULT_SALT_LENGTH as DEFAULT_SALT_LENGTH,
|
|
12
|
+
SHA1 as SHA1,
|
|
13
|
+
SHA256 as SHA256,
|
|
14
|
+
SHA512 as SHA512,
|
|
9
15
|
AlgoType as AlgoType,
|
|
10
|
-
ChallengeOptions as ChallengeOptions,
|
|
11
16
|
Challenge as Challenge,
|
|
17
|
+
ChallengeOptions as ChallengeOptions,
|
|
12
18
|
Payload as Payload,
|
|
13
19
|
PayloadType as PayloadType,
|
|
14
20
|
ServerSignaturePayload as ServerSignaturePayload,
|
|
15
21
|
ServerSignatureVerificationData as ServerSignatureVerificationData,
|
|
16
22
|
Solution as Solution,
|
|
17
|
-
DEFAULT_MAX_NUMBER as DEFAULT_MAX_NUMBER,
|
|
18
|
-
DEFAULT_SALT_LENGTH as DEFAULT_SALT_LENGTH,
|
|
19
|
-
DEFAULT_ALGORITHM as DEFAULT_ALGORITHM,
|
|
20
|
-
SHA1 as SHA1,
|
|
21
|
-
SHA256 as SHA256,
|
|
22
|
-
SHA512 as SHA512,
|
|
23
|
-
hash_hex as hash_hex,
|
|
24
|
-
hash_algorithm as hash_algorithm,
|
|
25
|
-
hmac_hex as hmac_hex,
|
|
26
23
|
create_challenge as create_challenge,
|
|
27
|
-
verify_solution as verify_solution,
|
|
28
24
|
extract_params as extract_params,
|
|
25
|
+
hash_algorithm as hash_algorithm,
|
|
26
|
+
hash_hex as hash_hex,
|
|
27
|
+
hmac_hex as hmac_hex,
|
|
28
|
+
solve_challenge as solve_challenge,
|
|
29
29
|
verify_fields_hash as verify_fields_hash,
|
|
30
30
|
verify_server_signature as verify_server_signature,
|
|
31
|
-
|
|
31
|
+
verify_solution as verify_solution,
|
|
32
32
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|