Skip to content

Commit

Permalink
Improve display_name of CAPABILITY
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Jun 5, 2024
1 parent bf4db5a commit 166a9c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 14 additions & 5 deletions ykman/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ def _configure_applications(
else:
reboot = False

if enable:
changes.append(f"Enable {enable.display_name}")
if disable:
changes.append(f"Disable {disable.display_name}")
if reboot:
changes.append("The YubiKey will reboot")

Expand Down Expand Up @@ -397,9 +393,14 @@ def usb(

if enable_all:
enable = info.supported_capabilities.get(TRANSPORT.USB)
changes.append("Enable all applications")
else:
enable = CAPABILITY(sum(enable))
if enable:
changes.append(f"Enable {enable.display_name}")
disable = CAPABILITY(sum(disable))
if disable:
changes.append(f"Disable {disable.display_name}")

if touch_eject:
config.device_flags = info.config.device_flags | DEVICE_FLAG.EJECT
Expand Down Expand Up @@ -466,21 +467,29 @@ def nfc(ctx, enable, disable, enable_all, disable_all, list_enabled, lock_code,

config = DeviceConfig({}, None, None, None)
info = ctx.obj["info"]
changes = []

nfc_supported = info.supported_capabilities.get(TRANSPORT.NFC)
if enable_all:
enable = nfc_supported
changes.append("Enable all applications")
else:
enable = CAPABILITY(sum(enable))
if enable:
changes.append(f"Enable {enable.display_name}")

if disable_all:
disable = nfc_supported
changes.append("Disable all applications")
else:
disable = CAPABILITY(sum(disable))
if disable:
changes.append(f"Disable {disable.display_name}")

_configure_applications(
ctx,
config,
[],
changes,
TRANSPORT.NFC,
enable,
disable,
Expand Down
16 changes: 12 additions & 4 deletions yubikit/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ def _from_aid(cls, aid: AID) -> "CAPABILITY":

@property
def display_name(self) -> str:
if self == 0:
return "None"
if f"{self:b}".count("1") > 1:
i = 1
names = []
while i < self:
if i & self:
names.append(CAPABILITY(i).display_name)
i <<= 1
return ", ".join(names)

if self == CAPABILITY.OTP:
return "Yubico OTP"
elif self == CAPABILITY.U2F:
Expand All @@ -110,10 +121,7 @@ def display_name(self) -> str:
return "OpenPGP"
elif self == CAPABILITY.HSMAUTH:
return "YubiHSM Auth"
# mypy bug?
return self.name or ", ".join(
c.display_name for c in CAPABILITY if c in self # type: ignore
)
return self.name or f"Unknown(0x{self:x})"

@property
def usb_interfaces(self) -> USB_INTERFACE:
Expand Down

0 comments on commit 166a9c6

Please sign in to comment.