Skip to content

Commit

Permalink
Merge pull request #14 from bergercookie/master
Browse files Browse the repository at this point in the history
Add show-only subcommand in CLI + Fish completion file
  • Loading branch information
Hugo Osvaldo Barrera authored Mar 10, 2020
2 parents d79223e + 3cc5133 commit cbd4e5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions contrib/totp-cli-completion.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# subcommands
complete -xc totp -n '__fish_use_subcommand' -a show -d 'Show the current TOTP token for a registered entry'
complete -xc totp -n '__fish_use_subcommand' -a add -d 'Add a new TOTP entry to the database'

complete -xc totp -n '__fish_seen_subcommand_from show' -a '-n --nocopy' -d 'Show TOKEN but don\'t copy to clipboard'

# 2FA codes
test -z "$PASSWORD_STORE_DIR"; and set PASSWORD_STORE_DIR "$HOME/.password-store"
complete -xc totp -a '(command ls $PASSWORD_STORE_DIR/2fa)' -d "2FA code"
6 changes: 4 additions & 2 deletions totp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def normalize_secret(secret):
return s


def generate_token(path, seconds=0):
def generate_token(path, seconds=0, to_clipboard=True):
"""Generate the TOTP token for the given path and the given time offset"""
import time
clock = time.time() + float(seconds)
Expand All @@ -145,7 +145,9 @@ def generate_token(path, seconds=0):
clock=clock)

print(token.decode())
copy_to_clipboard(token)

if to_clipboard:
copy_to_clipboard(token)


def parse_otpauth_uri(uri):
Expand Down
6 changes: 5 additions & 1 deletion totp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ def add_uri(path, uri):
metavar='SECONDS',
default=0,
help='offset the clock by the given number of seconds'),
argument(
'-n', '--nocopy',
action="store_true",
help='Do not copy the token, only show it.'),
argument(
'identifier',
help='the identifier by which the key can be found under the \'2fa\' folder'),
description='Show the current TOTP token for a registered entry.',
help='(default action) show the current TOTP token for a registered entry')
def _cmd_show(args):
totp.generate_token(args.identifier, seconds=args.offset_seconds)
totp.generate_token(args.identifier, seconds=args.offset_seconds, to_clipboard=not args.nocopy)

if __name__ == '__main__':
run()

0 comments on commit cbd4e5d

Please sign in to comment.