Skip to content

Commit

Permalink
Fix filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
cimnine committed Sep 13, 2021
1 parent 21c0b86 commit de1876a
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions yktotp/totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def totp_group(ctx: Context, device_serial: Optional[str], password: Optional[st
@click.option('+i/-i', '--ignore-case/--no-ignore-case', help="Whether to ignore casing when filtering.")
@click.argument('query', required=False, type=str)
@click.pass_obj
def codes(obj: dict[str, Union[OathSession, tuple[YkmanDevice, DeviceInfo]]], exact: Optional[bool], ignore_case: Optional[bool], query: Optional[str]):
def codes(obj: dict[str, Union[OathSession, tuple[YkmanDevice, DeviceInfo]]], exact: Optional[bool],
ignore_case: Optional[bool], query: Optional[str]):
"""
Generates TOTP codes.
Expand All @@ -80,33 +81,37 @@ def codes(obj: dict[str, Union[OathSession, tuple[YkmanDevice, DeviceInfo]]], ex
echo(f"Unable to generate codes with YubiKey {info.serial}: {err}")
exit(1)

totp_codes_list: list[Tuple[Credential, Optional[Code]]] = list(totp_codes.items())

if query:
if not exact and ignore_case:
totp_codes = [(cred, totp_code)
for (cred, totp_code)
in totp_codes
if query.lower() in cred.printable_key.lower()]
totp_codes_list = [(cred, totp_code)
for (cred, totp_code)
in totp_codes_list
if query.lower() in (cred.issuer + cred.name).lower()]
if not exact and not ignore_case:
totp_codes = [(cred, totp_code)
for (cred, totp_code)
in totp_codes
if query in cred.printable_key]
totp_codes_list = [(cred, totp_code)
for (cred, totp_code)
in totp_codes_list
if query in cred.issuer + cred.name]
if exact and ignore_case:
totp_codes = [(cred, totp_code)
for (cred, totp_code)
in totp_codes
if query.lower() == cred.printable_key.lower()]
totp_codes_list = [(cred, totp_code)
for (cred, totp_code)
in totp_codes_list
if query.lower() == (cred.issuer + cred.name).lower()]
if exact and not ignore_case:
totp_codes = [(cred, totp_code)
for (cred, totp_code)
in totp_codes
if query == cred.printable_key]
totp_codes_list = [(cred, totp_code)
for (cred, totp_code)
in totp_codes_list
if query == cred.issuer + cred.name]

totp_codes_list: list[Tuple[Credential, Optional[Code]]] = list(totp_codes.items())
totp_codes_list.sort(key=lambda t: t[0].issuer+t[0].name)
totp_codes_list.sort(key=lambda t: t[0].issuer + t[0].name)
for cc in totp_codes_list:
cred, code = cc
echo(f"{cred.issuer} ({cred.name}): {code.value}")
if cred.issuer:
echo(f"{cred.issuer} ({cred.name}): {code.value}")
else:
echo(f"{cred.name}: {code.value}")


@totp_group.command(name='list')
Expand Down

0 comments on commit de1876a

Please sign in to comment.