Skip to content

Commit

Permalink
Merge pull request #123 from robin-nitrokey/nk3-path
Browse files Browse the repository at this point in the history
nk3: Don’t connect to other devices if --path is set
  • Loading branch information
robin-nitrokey authored Dec 10, 2021
2 parents 68a7f5b + 70f6af2 commit 172048f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
18 changes: 11 additions & 7 deletions pynitrokey/cli/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pynitrokey.helpers import local_critical, local_print
from pynitrokey.nk3 import list as list_nk3
from pynitrokey.nk3 import open as open_nk3
from pynitrokey.nk3.base import Nitrokey3Base
from pynitrokey.nk3.device import Nitrokey3Device

Expand All @@ -24,21 +25,24 @@ def __init__(self, path: Optional[str]) -> None:
self.path = path

def list(self) -> List[Nitrokey3Base]:
devices = []
for device in list_nk3():
if not self.path or self.path == device.path:
devices.append(device)
return devices
if self.path:
device = open_nk3(self.path)
if device:
return [device]
else:
return []
else:
return list_nk3()

def _select_unique(self, name: str, devices: List[T]) -> T:
if len(devices) == 0:
msg = f"No {name} device found"
if self.path:
msg += f" at path {self.path}"
raise click.ClickException(msg)
local_critical(msg)

if len(devices) > 1:
raise click.ClickException(
local_critical(
f"Multiple {name} devices found -- use the --path option to select one"
)

Expand Down
9 changes: 6 additions & 3 deletions pynitrokey/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from typing import List
from typing import List, Optional

from .base import Nitrokey3Base
from .device import Nitrokey3Device

VID_NITROKEY = 0x20A0
PID_NITROKEY3_DEVICE = 0x42B2
PID_NITROKEY3_BOOTLOADER = 0x42DD


def list() -> List[Nitrokey3Base]:
from .device import Nitrokey3Device

return [device for device in Nitrokey3Device.list()]


def open(path: str) -> Optional[Nitrokey3Base]:
return Nitrokey3Device.open(path)
23 changes: 20 additions & 3 deletions pynitrokey/nk3/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@

import enum
import logging
import sys
from enum import Enum
from typing import List, Optional

from fido2.hid import CtapHidDevice
from fido2.hid import CtapHidDevice, open_device

from . import PID_NITROKEY3_DEVICE, VID_NITROKEY
from .base import Nitrokey3Base

RNG_LEN = 57
UUID_LEN = 16
VERSION_LEN = 4

logger = logging.getLogger(__name__)


@enum.unique
class Command(Enum):
Expand Down Expand Up @@ -56,6 +58,8 @@ class Nitrokey3Device(Nitrokey3Base):
"""A Nitrokey 3 device running the firmware."""

def __init__(self, device: CtapHidDevice) -> None:
from . import PID_NITROKEY3_DEVICE, VID_NITROKEY

(vid, pid) = (device.descriptor.vid, device.descriptor.pid)
if (vid, pid) != (VID_NITROKEY, PID_NITROKEY3_DEVICE):
raise ValueError(
Expand All @@ -64,7 +68,7 @@ def __init__(self, device: CtapHidDevice) -> None:
)

self.device = device
self.logger = logging.getLogger(f"{__name__}.{device.descriptor.path}")
self.logger = logger.getChild(device.descriptor.path)

@property
def path(self) -> str:
Expand Down Expand Up @@ -129,3 +133,16 @@ def list() -> List["Nitrokey3Device"]:
# not a Nitrokey 3 device, skip
pass
return devices

@staticmethod
def open(path: str) -> Optional["Nitrokey3Device"]:
try:
device = open_device(path)
except Exception:
logger.warn(f"No CTAPHID device at path {path}", exc_info=sys.exc_info())
return None
try:
return Nitrokey3Device(device)
except ValueError:
logger.warn(f"No Nitrokey 3 device at path {path}", exc_info=sys.exc_info())
return None

0 comments on commit 172048f

Please sign in to comment.