pythonbeid 1.0.0__py3-none-any.whl → 1.0.1__py3-none-any.whl

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.
pythonbeid/__init__.py CHANGED
@@ -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",
pythonbeid/card_reader.py CHANGED
@@ -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
pythonbeid/exceptions.py CHANGED
@@ -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`) |
@@ -1,8 +1,8 @@
1
- pythonbeid/__init__.py,sha256=blxIJjtfcEG5BAIwInLVGJY4H5bT3erMssZyIY4w9Bs,1755
1
+ pythonbeid/__init__.py,sha256=McnwkIOYaO7wvmq9LUE2gZeFtRPBJJzp7vjBz2m8gCs,1797
2
2
  pythonbeid/__main__.py,sha256=k1ocEWawweo1qCJWNFAAvyxz3tcY13dzvCenHszij30,48
3
- pythonbeid/card_reader.py,sha256=qdpez_gfDf__DaB1iJzVgdP3Htyk1amEkO-NTX2zkKs,32892
3
+ pythonbeid/card_reader.py,sha256=uVXp8HiSQxJzmlBIHhgpbVmMN8dD99vsfDDmGYOhKbY,34264
4
4
  pythonbeid/cli.py,sha256=BA-Zx50ZJ0-TfOcUSPBqyOLF4vCAT5LroGH67wJ3EYc,5527
5
- pythonbeid/exceptions.py,sha256=pEndy6ciiqFNSh3WeNHzEE0llIa-KHT8bpLvBxMnUfA,1937
5
+ pythonbeid/exceptions.py,sha256=pynKnyMV92LtjNsdQjoxGjS0_zr7KDIYJo2PG0t33EE,2225
6
6
  pythonbeid/integrity.py,sha256=x1IvB_8NDD5azKT4tvw-SfZ4oKS3nsiOyIZ3qx6gX8M,16660
7
7
  pythonbeid/models.py,sha256=iSS_ywPThDjdiZKIfVWOGTd9_3i5CsbcTgIsYxBOVn8,20617
8
8
  pythonbeid/parser.py,sha256=d_CRUJP6B1td4BbdpK6REY00s4l5qZwPCDWElmhVk5g,16127
@@ -11,9 +11,9 @@ pythonbeid/trust/belgium_root_ca2.pem,sha256=2CqkJxMhInhCUUyVJIbnBBQqm3og7hqc0wx
11
11
  pythonbeid/trust/belgium_root_ca3.pem,sha256=3AAr5yeQ5Z42UDVtcQwHexk9TNCfg28gIn2dZ34YlAg,1988
12
12
  pythonbeid/trust/belgium_root_ca4.pem,sha256=lumT5v9x4256Mtf7XVW9DeBaCXnT5IWPrr2kWfo_pf8,1988
13
13
  pythonbeid/trust/belgium_root_ca6.pem,sha256=E92NB3hYd3BM0bPn2M9qTe_CaHmFABTniM-p-4B8oCw,1241
14
- pythonbeid-1.0.0.dist-info/licenses/LICENSE,sha256=VOOj3rL3m3LyHxJOvzfNxWbRhDeBfzne6GKYwypfvxA,1069
15
- pythonbeid-1.0.0.dist-info/METADATA,sha256=NoL1pSUvw3N_Xt46tH_E2BpIXcduArc8i04U1Ep8dAo,12539
16
- pythonbeid-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
17
- pythonbeid-1.0.0.dist-info/entry_points.txt,sha256=Z3MJzsElAc6cLLuxOogzl0SKWywH5o8F7Fwny_tBOLs,51
18
- pythonbeid-1.0.0.dist-info/top_level.txt,sha256=-Uw6t8lKuiCAYujzrtgkiyiNdngHathGf-Nh0EVwHc4,11
19
- pythonbeid-1.0.0.dist-info/RECORD,,
14
+ pythonbeid-1.0.1.dist-info/licenses/LICENSE,sha256=VOOj3rL3m3LyHxJOvzfNxWbRhDeBfzne6GKYwypfvxA,1069
15
+ pythonbeid-1.0.1.dist-info/METADATA,sha256=gi64i0f1JQBh3oMDSv9wnkTsD_21cs1NIhQnZms1ROQ,12648
16
+ pythonbeid-1.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
17
+ pythonbeid-1.0.1.dist-info/entry_points.txt,sha256=Z3MJzsElAc6cLLuxOogzl0SKWywH5o8F7Fwny_tBOLs,51
18
+ pythonbeid-1.0.1.dist-info/top_level.txt,sha256=-Uw6t8lKuiCAYujzrtgkiyiNdngHathGf-Nh0EVwHc4,11
19
+ pythonbeid-1.0.1.dist-info/RECORD,,