pythonbeid 1.0.0__tar.gz → 1.0.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.
Files changed (38) hide show
  1. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/CHANGELOG.md +10 -0
  2. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/PKG-INFO +2 -1
  3. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/README.md +1 -0
  4. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pyproject.toml +1 -1
  5. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/__init__.py +2 -0
  6. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/card_reader.py +36 -0
  7. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/exceptions.py +9 -0
  8. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/PKG-INFO +2 -1
  9. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_card_reader.py +77 -0
  10. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/CONTRIBUTING.md +0 -0
  11. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/LICENSE +0 -0
  12. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/MANIFEST.in +0 -0
  13. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/SECURITY.md +0 -0
  14. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/__main__.py +0 -0
  15. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/cli.py +0 -0
  16. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/integrity.py +0 -0
  17. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/models.py +0 -0
  18. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/parser.py +0 -0
  19. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/py.typed +0 -0
  20. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/trust/belgium_root_ca2.pem +0 -0
  21. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/trust/belgium_root_ca3.pem +0 -0
  22. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/trust/belgium_root_ca4.pem +0 -0
  23. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid/trust/belgium_root_ca6.pem +0 -0
  24. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/SOURCES.txt +0 -0
  25. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/dependency_links.txt +0 -0
  26. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/entry_points.txt +0 -0
  27. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/requires.txt +0 -0
  28. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/pythonbeid.egg-info/top_level.txt +0 -0
  29. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/setup.cfg +0 -0
  30. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/__init__.py +0 -0
  31. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/builders.py +0 -0
  32. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/conftest.py +0 -0
  33. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/fake_card.py +0 -0
  34. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_cli.py +0 -0
  35. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_hardware.py +0 -0
  36. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_integrity.py +0 -0
  37. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_models.py +0 -0
  38. {pythonbeid-1.0.0 → pythonbeid-1.0.1}/tests/test_parser.py +0 -0
@@ -4,6 +4,15 @@ All notable changes to this project are documented here. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
5
  [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [1.0.1] - 2026-11-25
8
+
9
+ ### Fixed
10
+
11
+ - A card held exclusively by another application (for instance the eID
12
+ middleware during a browser authentication) was reported as missing.
13
+ The connection is now retried for one second, then fails with the new
14
+ `CardInUseError`, which says so.
15
+
7
16
  ## [1.0.0] - 2026-11-25
8
17
 
9
18
  ### Added
@@ -94,6 +103,7 @@ All notable changes to this project are documented here. The format follows
94
103
 
95
104
  - First release on PyPI: identity, address and photo reading.
96
105
 
106
+ [1.0.1]: https://github.com/Lapin-Blanc/pythonbeid/compare/v1.0.0...v1.0.1
97
107
  [1.0.0]: https://github.com/Lapin-Blanc/pythonbeid/compare/v0.3.0...v1.0.0
98
108
  [0.3.0]: https://github.com/Lapin-Blanc/pythonbeid/releases/tag/v0.3.0
99
109
  [0.2.0]: https://pypi.org/project/pythonbeid/0.2.0/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pythonbeid
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: Read and verify Belgian eID cards (identity, address, photo) through PC/SC
5
5
  Author-email: Fabien Toune <fabien.toune@gmail.com>
6
6
  License-Expression: MIT
@@ -228,6 +228,7 @@ All exceptions derive from `pythonbeid.BeidError`:
228
228
  | `PCSCServiceError` | the PC/SC service is not running (subclass of `NoReaderError`) |
229
229
  | `NoCardError` | no card in the reader, or the card was removed |
230
230
  | `UnsupportedCardError` | the card is not a Belgian eID card, or lacks a feature |
231
+ | `CardInUseError` | another application holds the card exclusively (subclass of `CardCommunicationError`) |
231
232
  | `CardCommunicationError` | transport failure |
232
233
  | `APDUError` | the card refused a command (`.sw` holds the status word) |
233
234
  | `CardDataError` | card content cannot be decoded (also a `ValueError`) |
@@ -181,6 +181,7 @@ All exceptions derive from `pythonbeid.BeidError`:
181
181
  | `PCSCServiceError` | the PC/SC service is not running (subclass of `NoReaderError`) |
182
182
  | `NoCardError` | no card in the reader, or the card was removed |
183
183
  | `UnsupportedCardError` | the card is not a Belgian eID card, or lacks a feature |
184
+ | `CardInUseError` | another application holds the card exclusively (subclass of `CardCommunicationError`) |
184
185
  | `CardCommunicationError` | transport failure |
185
186
  | `APDUError` | the card refused a command (`.sw` holds the status word) |
186
187
  | `CardDataError` | card content cannot be decoded (also a `ValueError`) |
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pythonbeid"
7
- version = "1.0.0"
7
+ version = "1.0.1"
8
8
  description = "Read and verify Belgian eID cards (identity, address, photo) through PC/SC"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -18,6 +18,7 @@ from .exceptions import (
18
18
  BeidError,
19
19
  CardCommunicationError,
20
20
  CardDataError,
21
+ CardInUseError,
21
22
  NoCardError,
22
23
  NoReaderError,
23
24
  PCSCServiceError,
@@ -57,6 +58,7 @@ __all__ = [
57
58
  "CardCommunicationError",
58
59
  "CardDataError",
59
60
  "CardFile",
61
+ "CardInUseError",
60
62
  "CardInfo",
61
63
  "CardReader",
62
64
  "DocumentType",
@@ -39,6 +39,7 @@ from smartcard.Exceptions import (
39
39
  from smartcard.pcsc.PCSCExceptions import BaseSCardException
40
40
  from smartcard.scard import (
41
41
  SCARD_E_NO_SMARTCARD,
42
+ SCARD_E_SHARING_VIOLATION,
42
43
  SCARD_LEAVE_CARD,
43
44
  SCARD_S_SUCCESS,
44
45
  SCARD_W_REMOVED_CARD,
@@ -54,6 +55,7 @@ from .exceptions import (
54
55
  BeidError,
55
56
  CardCommunicationError,
56
57
  CardDataError,
58
+ CardInUseError,
57
59
  NoCardError,
58
60
  NoReaderError,
59
61
  PCSCServiceError,
@@ -79,6 +81,8 @@ _MAX_6C_RETRIES = 3
79
81
  _MAX_GET_RESPONSE = 64
80
82
  _CHALLENGE_SIZE = 48 # SHA-384 digest size, as eid-viewer does
81
83
  _WAIT_STEP = 1.0 # seconds between two scans of the readers while waiting
84
+ _SHARING_ATTEMPTS = 3 # eid-mw also retries a card held by another process
85
+ _SHARING_DELAY = 0.5
82
86
 
83
87
  _SW_OK = 0x9000
84
88
  _SW_EOF_WARNING = 0x6282 # end of file reached before reading Le bytes
@@ -103,6 +107,7 @@ _CARD_ABSENT_CODES = frozenset(
103
107
  code & _UINT32 for code in (SCARD_W_REMOVED_CARD, SCARD_E_NO_SMARTCARD)
104
108
  )
105
109
  _CARD_RESET_CODE = SCARD_W_RESET_CARD & _UINT32
110
+ _SHARING_VIOLATION_CODE = SCARD_E_SHARING_VIOLATION & _UINT32
106
111
 
107
112
 
108
113
  class _Response(NamedTuple):
@@ -239,6 +244,7 @@ class CardReader:
239
244
  self._retry_delay = 0.0
240
245
  self._connection: Any = None
241
246
  self._card_info: CardInfo | None = None
247
+ self._in_use: CardInUseError | None = None
242
248
 
243
249
  available = _list_pcsc_readers()
244
250
  if not available:
@@ -286,6 +292,7 @@ class CardReader:
286
292
  if self._try_candidates(candidates, explicit=explicit):
287
293
  return
288
294
  if timeout is None:
295
+ self._raise_in_use()
289
296
  names = ", ".join(f"'{item}'" for item in candidates)
290
297
  msg = f"No Belgian eID card in reader {names}"
291
298
  raise NoCardError(msg)
@@ -293,18 +300,30 @@ class CardReader:
293
300
  while True:
294
301
  remaining = deadline - time.monotonic()
295
302
  if remaining <= 0:
303
+ self._raise_in_use()
296
304
  msg = f"No Belgian eID card inserted within {timeout:g} s"
297
305
  raise NoCardError(msg)
298
306
  self._wait_for_insertion(candidates, min(remaining, _WAIT_STEP))
299
307
  if self._try_candidates(candidates, explicit=explicit):
300
308
  return
301
309
 
310
+ def _raise_in_use(self) -> None:
311
+ """Report a card held by another application rather than "no card"."""
312
+ if self._in_use is not None:
313
+ raise self._in_use
314
+
302
315
  def _try_candidates(self, candidates: list[Any], *, explicit: bool) -> bool:
316
+ self._in_use = None
303
317
  for candidate in candidates:
304
318
  try:
305
319
  self._connect(candidate)
306
320
  except NoCardError:
307
321
  continue
322
+ except CardInUseError as exc:
323
+ if explicit:
324
+ raise
325
+ self._in_use = exc
326
+ continue
308
327
  except BeidError:
309
328
  if explicit:
310
329
  raise
@@ -314,6 +333,17 @@ class CardReader:
314
333
  return False
315
334
 
316
335
  def _connect(self, reader: Any) -> None:
336
+ for attempt in range(1, _SHARING_ATTEMPTS + 1):
337
+ try:
338
+ self._connect_once(reader)
339
+ except CardInUseError:
340
+ if attempt == _SHARING_ATTEMPTS:
341
+ raise
342
+ time.sleep(_SHARING_DELAY)
343
+ else:
344
+ return
345
+
346
+ def _connect_once(self, reader: Any) -> None:
317
347
  try:
318
348
  connection = _wrap_connection(reader.createConnection())
319
349
  connection.connect(disposition=SCARD_LEAVE_CARD)
@@ -324,6 +354,12 @@ class CardReader:
324
354
  if _hresult(exc) in _CARD_ABSENT_CODES:
325
355
  msg = f"No card in reader '{reader}'"
326
356
  raise NoCardError(msg) from exc
357
+ if _hresult(exc) == _SHARING_VIOLATION_CODE:
358
+ msg = (
359
+ f"The card in '{reader}' is used exclusively by another "
360
+ "application (eID middleware, browser...); close it or retry"
361
+ )
362
+ raise CardInUseError(msg) from exc
327
363
  msg = f"Connection error on '{reader}': {exc}"
328
364
  raise CardCommunicationError(msg) from exc
329
365
  self._connection = connection
@@ -11,6 +11,7 @@ __all__ = [
11
11
  "BeidError",
12
12
  "CardCommunicationError",
13
13
  "CardDataError",
14
+ "CardInUseError",
14
15
  "NoCardError",
15
16
  "NoReaderError",
16
17
  "PCSCServiceError",
@@ -42,6 +43,14 @@ class CardCommunicationError(BeidError):
42
43
  """Raised on a failure while talking to the card."""
43
44
 
44
45
 
46
+ class CardInUseError(CardCommunicationError):
47
+ """Raised when another application holds the card exclusively.
48
+
49
+ Typical causes: the eID middleware or a browser in the middle of an
50
+ authentication. Closing that application or retrying later helps.
51
+ """
52
+
53
+
45
54
  class APDUError(CardCommunicationError):
46
55
  """Raised when the card answers a command with an unexpected status word.
47
56
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pythonbeid
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: Read and verify Belgian eID cards (identity, address, photo) through PC/SC
5
5
  Author-email: Fabien Toune <fabien.toune@gmail.com>
6
6
  License-Expression: MIT
@@ -228,6 +228,7 @@ All exceptions derive from `pythonbeid.BeidError`:
228
228
  | `PCSCServiceError` | the PC/SC service is not running (subclass of `NoReaderError`) |
229
229
  | `NoCardError` | no card in the reader, or the card was removed |
230
230
  | `UnsupportedCardError` | the card is not a Belgian eID card, or lacks a feature |
231
+ | `CardInUseError` | another application holds the card exclusively (subclass of `CardCommunicationError`) |
231
232
  | `CardCommunicationError` | transport failure |
232
233
  | `APDUError` | the card refused a command (`.sw` holds the status word) |
233
234
  | `CardDataError` | card content cannot be decoded (also a `ValueError`) |
@@ -23,6 +23,7 @@ from pythonbeid import (
23
23
  CardCommunicationError,
24
24
  CardDataError,
25
25
  CardFile,
26
+ CardInUseError,
26
27
  CardReader,
27
28
  NoCardError,
28
29
  NoReaderError,
@@ -1010,3 +1011,79 @@ class TestReviewFindings:
1010
1011
  )
1011
1012
  assert not report.is_valid
1012
1013
  assert report.chip_authenticated is None
1014
+
1015
+
1016
+ class TestCardInUse:
1017
+ """A card held exclusively by another application (SCARD_E_SHARING_VIOLATION)."""
1018
+
1019
+ @staticmethod
1020
+ def _busy() -> CardConnectionException:
1021
+ return CardConnectionException(
1022
+ "in use", hresult=scard.SCARD_E_SHARING_VIOLATION
1023
+ )
1024
+
1025
+ def test_retried_until_released(
1026
+ self, install_readers: Any, card17: FakeCard, no_sleep: list[float]
1027
+ ) -> None:
1028
+ attempts = {"count": 0}
1029
+ original = card17.connect
1030
+
1031
+ def connect(*args: Any, **kwargs: Any) -> None:
1032
+ attempts["count"] += 1
1033
+ if attempts["count"] < 3:
1034
+ raise self._busy()
1035
+ original(*args, **kwargs)
1036
+
1037
+ card17.connect = connect # type: ignore[method-assign]
1038
+ install_readers(card17)
1039
+ with CardReader() as reader:
1040
+ assert reader.read_identity().last_name == "Specimen"
1041
+ assert attempts["count"] == 3
1042
+ assert no_sleep == [0.5, 0.5]
1043
+
1044
+ def test_explicit_reader(
1045
+ self, install_readers: Any, card17: FakeCard, no_sleep: list[float]
1046
+ ) -> None:
1047
+ card17.connect_error = self._busy()
1048
+ install_readers(("ACS ACR38U 0", card17))
1049
+ with pytest.raises(CardInUseError, match="used exclusively by another"):
1050
+ CardReader(0)
1051
+ assert len(no_sleep) == 2
1052
+
1053
+ def test_automatic_selection_reports_it(
1054
+ self, install_readers: Any, card17: FakeCard, no_sleep: list[float]
1055
+ ) -> None:
1056
+ card17.connect_error = self._busy()
1057
+ install_readers(("Empty", None), ("Busy", card17))
1058
+ with pytest.raises(CardInUseError, match="'Busy'"):
1059
+ CardReader()
1060
+
1061
+ def test_automatic_selection_prefers_a_free_card(
1062
+ self,
1063
+ install_readers: Any,
1064
+ card17: FakeCard,
1065
+ card18: FakeCard,
1066
+ no_sleep: list[float],
1067
+ ) -> None:
1068
+ card17.connect_error = self._busy()
1069
+ install_readers(("Busy", card17), ("Free", card18))
1070
+ with CardReader() as reader:
1071
+ assert reader.reader_name == "Free"
1072
+
1073
+ def test_reported_after_waiting(
1074
+ self,
1075
+ install_readers: Any,
1076
+ card17: FakeCard,
1077
+ no_sleep: list[float],
1078
+ monkeypatch: pytest.MonkeyPatch,
1079
+ ) -> None:
1080
+ card17.connect_error = self._busy()
1081
+ install_readers(("Busy", card17))
1082
+ TestWaitForCard._clock(monkeypatch, 0.0, 0.0, 5.0)
1083
+ monkeypatch.setattr(
1084
+ card_reader_module,
1085
+ "CardRequest",
1086
+ TestWaitForCard._request(TestWaitForCard._timeout),
1087
+ )
1088
+ with pytest.raises(CardInUseError):
1089
+ CardReader(timeout=2)
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