foxkey 0.2.0__tar.gz → 0.2.1__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,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: foxkey
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Secure, dependency-light TOTP 2FA manager with zero footguns.
5
5
  License-Expression: MIT
6
6
  Project-URL: Source, https://github.com/foxhackerzdevs/foxkey
7
- Requires-Python: >=3.8
7
+ Requires-Python: >=3.9
8
8
  Description-Content-Type: text/markdown
9
9
  License-File: LICENSE
10
- Requires-Dist: cryptography>=42.0.0
10
+ Requires-Dist: cryptography>=44.0.0
11
11
  Dynamic: license-file
12
12
 
13
13
  # 🦊 FoxKey — Secure TOTP Manager
@@ -12,7 +12,7 @@ import binascii
12
12
  from core.storage import Vault
13
13
  from core.totp import generate_totp
14
14
 
15
- VERSION = "0.2.0" # keep in sync with pyproject.toml's [project].version
15
+ VERSION = "0.2.1" # keep in sync with pyproject.toml's [project].version
16
16
 
17
17
  def sanitize_and_validate_secret(secret_input: str) -> dict:
18
18
  """Returns {'secret': str, 'algorithm': str, 'digits': int, 'period': int}."""
@@ -3,6 +3,7 @@ import json
3
3
  import os
4
4
  import struct
5
5
  import shutil
6
+ import tempfile
6
7
  from pathlib import Path
7
8
  from cryptography.exceptions import InvalidTag
8
9
  from .crypto import derive_key, encrypt, decrypt, SALT_SIZE
@@ -59,7 +60,25 @@ class Vault:
59
60
  encrypted_data = encrypt(raw_json, key)
60
61
 
61
62
  VAULT_PATH.parent.mkdir(parents=True, exist_ok=True)
62
- VAULT_PATH.write_bytes(salt + header_len_bytes + header_bytes + encrypted_data)
63
+ if os.name == "posix":
64
+ os.chmod(VAULT_PATH.parent, 0o700)
65
+
66
+ # Write through a symlinked vault (for example one kept in a synced folder) rather
67
+ # than replacing the link with a regular file.
68
+ destination = Path(os.path.realpath(VAULT_PATH))
69
+ fd, tmp_path = tempfile.mkstemp(dir=destination.parent, prefix=".vault.", suffix=".tmp")
70
+ try:
71
+ with os.fdopen(fd, "wb") as f:
72
+ f.write(salt + header_len_bytes + header_bytes + encrypted_data)
73
+ f.flush()
74
+ os.fsync(f.fileno())
75
+ if os.name == "posix":
76
+ os.chmod(tmp_path, 0o600)
77
+ os.replace(tmp_path, destination)
78
+ except BaseException:
79
+ if os.path.exists(tmp_path):
80
+ os.unlink(tmp_path)
81
+ raise
63
82
 
64
83
  def export_backup(self, backup_path: Path):
65
84
  """Simple secure export - just copy the encrypted vault."""
@@ -13,7 +13,7 @@ _ALGORITHMS = {
13
13
 
14
14
  def generate_totp(secret: str, time_step: int = 30, digits: int = 6, algorithm: str = "SHA1") -> str:
15
15
  """Generates an RFC 6238 TOTP token with clean base32 padding."""
16
- clean_secret = secret.upper().replace(' ', '')
16
+ clean_secret = secret.upper().replace(' ', '').replace('-', '')
17
17
  missing_padding = len(clean_secret) % 8
18
18
  if missing_padding:
19
19
  clean_secret += '=' * (8 - missing_padding)
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: foxkey
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Secure, dependency-light TOTP 2FA manager with zero footguns.
5
5
  License-Expression: MIT
6
6
  Project-URL: Source, https://github.com/foxhackerzdevs/foxkey
7
- Requires-Python: >=3.8
7
+ Requires-Python: >=3.9
8
8
  Description-Content-Type: text/markdown
9
9
  License-File: LICENSE
10
- Requires-Dist: cryptography>=42.0.0
10
+ Requires-Dist: cryptography>=44.0.0
11
11
  Dynamic: license-file
12
12
 
13
13
  # 🦊 FoxKey — Secure TOTP Manager
@@ -12,4 +12,5 @@ foxkey.egg-info/SOURCES.txt
12
12
  foxkey.egg-info/dependency_links.txt
13
13
  foxkey.egg-info/entry_points.txt
14
14
  foxkey.egg-info/requires.txt
15
- foxkey.egg-info/top_level.txt
15
+ foxkey.egg-info/top_level.txt
16
+ tests/test_core.py
@@ -0,0 +1 @@
1
+ cryptography>=44.0.0
@@ -1,16 +1,16 @@
1
1
  [build-system]
2
- requires = ["setuptools>=61.0"]
2
+ requires = ["setuptools>=77.0.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "foxkey"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  description = "Secure, dependency-light TOTP 2FA manager with zero footguns."
9
9
  readme = "README.md"
10
- requires-python = ">=3.8"
10
+ requires-python = ">=3.9"
11
11
  license = "MIT"
12
12
  dependencies = [
13
- "cryptography>=42.0.0",
13
+ "cryptography>=44.0.0",
14
14
  ]
15
15
 
16
16
  [project.scripts]
@@ -0,0 +1,139 @@
1
+ """Tests for foxkey's TOTP and vault code. Run with: python -m unittest discover -s tests"""
2
+ import base64
3
+ import os
4
+ import stat
5
+ import struct
6
+ import sys
7
+ import tempfile
8
+ import unittest
9
+ from pathlib import Path
10
+ from unittest import mock
11
+
12
+ sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
13
+
14
+ from core import storage, totp # noqa: E402
15
+
16
+ SECRETS = {
17
+ "SHA1": b"12345678901234567890",
18
+ "SHA256": b"12345678901234567890123456789012",
19
+ "SHA512": b"1234567890123456789012345678901234567890123456789012345678901234",
20
+ }
21
+ # RFC 6238 appendix B: time -> (SHA1, SHA256, SHA512), 8 digits.
22
+ RFC6238 = {
23
+ 59: ("94287082", "46119246", "90693936"),
24
+ 1111111109: ("07081804", "68084774", "25091201"),
25
+ 1111111111: ("14050471", "67062674", "99943326"),
26
+ 1234567890: ("89005924", "91819424", "93441116"),
27
+ 2000000000: ("69279037", "90698825", "38618901"),
28
+ 20000000000: ("65353130", "77737706", "47863826"),
29
+ }
30
+ B32 = base64.b32encode(SECRETS["SHA1"]).decode()
31
+ POSIX = os.name == "posix"
32
+
33
+
34
+ class TotpTests(unittest.TestCase):
35
+ def test_rfc4226_hotp_vectors(self):
36
+ expected = ["755224", "287082", "359152", "969429", "338314",
37
+ "254676", "287922", "162583", "399871", "520489"]
38
+ got = [totp.hotp(SECRETS["SHA1"], counter, 6) for counter in range(10)]
39
+ self.assertEqual(got, expected)
40
+
41
+ def test_rfc6238_totp_vectors(self):
42
+ for moment, expected in RFC6238.items():
43
+ for algorithm, want in zip(("SHA1", "SHA256", "SHA512"), expected):
44
+ secret = base64.b32encode(SECRETS[algorithm]).decode()
45
+ with mock.patch.object(totp.time, "time", return_value=moment):
46
+ self.assertEqual(totp.generate_totp(secret, 30, 8, algorithm), want,
47
+ f"{algorithm} at {moment}")
48
+
49
+ def test_secret_with_hyphens_and_spaces_matches_plain_secret(self):
50
+ grouped = "-".join(B32[i:i + 4] for i in range(0, len(B32), 4))
51
+ spaced = " ".join(B32[i:i + 4] for i in range(0, len(B32), 4))
52
+ with mock.patch.object(totp.time, "time", return_value=59):
53
+ plain = totp.generate_totp(B32, 30, 8)
54
+ self.assertEqual(totp.generate_totp(grouped, 30, 8), plain)
55
+ self.assertEqual(totp.generate_totp(spaced, 30, 8), plain)
56
+ self.assertEqual(totp.generate_totp(grouped.lower(), 30, 8), plain)
57
+
58
+ def test_unsupported_algorithm_is_rejected(self):
59
+ with self.assertRaises(ValueError):
60
+ totp.hotp(b"key", 0, 6, "MD5")
61
+
62
+
63
+ class VaultTests(unittest.TestCase):
64
+ def setUp(self):
65
+ self.tmp = tempfile.TemporaryDirectory()
66
+ self.addCleanup(self.tmp.cleanup)
67
+ self.dir = Path(self.tmp.name) / ".foxkey"
68
+ self.path = self.dir / "vault.enc"
69
+ patcher = mock.patch.object(storage, "VAULT_PATH", self.path)
70
+ patcher.start()
71
+ self.addCleanup(patcher.stop)
72
+ self.vault = storage.Vault()
73
+ self.vault.entries = {"github": {"secret": B32}, "aws": {"secret": B32, "digits": 8}}
74
+
75
+ def test_round_trip_and_wrong_password(self):
76
+ self.vault.save("correct horse")
77
+ loaded = storage.Vault()
78
+ self.assertTrue(loaded.load("correct horse"))
79
+ self.assertEqual(loaded.entries, self.vault.entries)
80
+ self.assertFalse(storage.Vault().load("wrong"))
81
+
82
+ def test_every_save_uses_a_fresh_salt_and_ciphertext(self):
83
+ self.vault.save("pw")
84
+ first = self.path.read_bytes()
85
+ self.vault.save("pw")
86
+ second = self.path.read_bytes()
87
+ self.assertNotEqual(first[:16], second[:16])
88
+ self.assertNotEqual(first, second)
89
+
90
+ @unittest.skipUnless(POSIX, "POSIX permissions")
91
+ def test_permissions_are_tightened(self):
92
+ self.dir.mkdir()
93
+ os.chmod(self.dir, 0o755)
94
+ self.vault.save("pw")
95
+ self.assertEqual(stat.S_IMODE(os.stat(self.path).st_mode), 0o600)
96
+ self.assertEqual(stat.S_IMODE(os.stat(self.dir).st_mode), 0o700)
97
+
98
+ def test_failed_save_leaves_the_existing_vault_intact(self):
99
+ self.vault.save("pw")
100
+ before = self.path.read_bytes()
101
+ self.vault.entries["new"] = {"secret": B32}
102
+ for name in ("fsync", "replace"):
103
+ with mock.patch.object(os, name, side_effect=OSError("injected")):
104
+ with self.assertRaises(OSError):
105
+ self.vault.save("pw")
106
+ self.assertEqual(self.path.read_bytes(), before, name)
107
+ self.assertEqual([p.name for p in self.dir.iterdir()], ["vault.enc"], name)
108
+ self.assertTrue(storage.Vault().load("pw"))
109
+
110
+ @unittest.skipUnless(POSIX, "symlinks")
111
+ def test_a_symlinked_vault_stays_a_symlink_and_the_target_is_updated(self):
112
+ self.dir.mkdir()
113
+ target = Path(self.tmp.name) / "synced" / "vault.enc"
114
+ target.parent.mkdir()
115
+ self.path.symlink_to(target)
116
+ self.vault.save("pw")
117
+ self.assertTrue(self.path.is_symlink())
118
+ self.assertTrue(target.exists())
119
+ reloaded = storage.Vault()
120
+ self.assertTrue(reloaded.load("pw"))
121
+ self.assertEqual(reloaded.entries, self.vault.entries)
122
+
123
+ def test_tampered_ciphertext_is_rejected(self):
124
+ self.vault.save("pw")
125
+ data = bytearray(self.path.read_bytes())
126
+ data[-1] ^= 1
127
+ self.path.write_bytes(bytes(data))
128
+ self.assertFalse(storage.Vault().load("pw"))
129
+
130
+ def test_corrupt_files_raise_a_clean_error(self):
131
+ self.dir.mkdir()
132
+ for data in (b"abc", bytes(16) + struct.pack(">I", 10 ** 6) + b"x"):
133
+ self.path.write_bytes(data)
134
+ with self.assertRaises(ValueError):
135
+ storage.Vault().load("pw")
136
+
137
+
138
+ if __name__ == "__main__":
139
+ unittest.main()
@@ -1 +0,0 @@
1
- cryptography>=42.0.0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes