|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Protocol |
| 4 | + |
| 5 | +from smartcard.pcsc.PCSCReader import PCSCReader |
| 6 | +from smartcard.System import readers as smartcard_readers |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from smartcard.CardConnection import CardConnection |
| 10 | + |
| 11 | + |
| 12 | +class PcscConnection(Protocol): |
| 13 | + """PC/SC connection interface used by the backend.""" |
| 14 | + |
| 15 | + def connect(self) -> None: |
| 16 | + """Connect to the card.""" |
| 17 | + |
| 18 | + def transmit(self, command: list[int]) -> tuple[list[int], int, int]: |
| 19 | + """Transmit a command APDU.""" |
| 20 | + |
| 21 | + |
| 22 | +class PcscReader(Protocol): |
| 23 | + """PC/SC reader interface used by the backend.""" |
| 24 | + |
| 25 | + name: str |
| 26 | + |
| 27 | + def create_connection(self) -> PcscConnection: |
| 28 | + """Create a PC/SC connection.""" |
| 29 | + |
| 30 | + |
| 31 | +class SmartcardPcscConnection: |
| 32 | + """Adapter for pyscard connection objects.""" |
| 33 | + |
| 34 | + def __init__(self, connection: CardConnection) -> None: |
| 35 | + self._connection = connection |
| 36 | + |
| 37 | + def connect(self) -> None: |
| 38 | + """Connect to the card.""" |
| 39 | + self._connection.connect() |
| 40 | + |
| 41 | + def transmit(self, command: list[int]) -> tuple[list[int], int, int]: |
| 42 | + """Transmit a command APDU.""" |
| 43 | + response, sw1, sw2 = self._connection.transmit(command) |
| 44 | + return list(response), sw1, sw2 |
| 45 | + |
| 46 | + |
| 47 | +class SmartcardPcscReader: |
| 48 | + """Adapter for pyscard PC/SC reader objects.""" |
| 49 | + |
| 50 | + def __init__(self, reader: PCSCReader) -> None: |
| 51 | + self._reader = reader |
| 52 | + |
| 53 | + @property |
| 54 | + def name(self) -> str: |
| 55 | + """Return the PC/SC reader name.""" |
| 56 | + return self._reader.name |
| 57 | + |
| 58 | + def create_connection(self) -> SmartcardPcscConnection: |
| 59 | + """Create a wrapped PC/SC connection.""" |
| 60 | + return SmartcardPcscConnection(self._reader.createConnection()) |
| 61 | + |
| 62 | + |
| 63 | +class PcscReaderProvider: |
| 64 | + """Discover and wrap pyscard PC/SC readers.""" |
| 65 | + |
| 66 | + class PcscReaderProviderError(Exception): |
| 67 | + """PC/SC reader provider error.""" |
| 68 | + |
| 69 | + class ReaderNotFoundError(PcscReaderProviderError): |
| 70 | + """Raised when a requested PC/SC reader is not found.""" |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def readers() -> list[PcscReader]: |
| 74 | + """Return available PC/SC readers.""" |
| 75 | + return [ |
| 76 | + SmartcardPcscReader(reader) |
| 77 | + for reader in smartcard_readers() |
| 78 | + if isinstance(reader, PCSCReader) |
| 79 | + ] |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def reader_names(cls) -> list[str]: |
| 83 | + """Return available PC/SC reader names.""" |
| 84 | + return [reader.name for reader in cls.readers()] |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def get(cls, name: str) -> PcscReader: |
| 88 | + """Return a named PC/SC reader.""" |
| 89 | + for reader in cls.readers(): |
| 90 | + if reader.name == name: |
| 91 | + return reader |
| 92 | + raise cls.ReaderNotFoundError |
0 commit comments