Skip to content

Instantly share code, notes, and snippets.

@cathay4t
Created February 21, 2020 09:18
Show Gist options
  • Save cathay4t/bb646c0860f93e49ba0ffef8f0aaacde to your computer and use it in GitHub Desktop.
Save cathay4t/bb646c0860f93e49ba0ffef8f0aaacde to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import sys
import toml
import pyotp
from pathlib import Path
CONF_PATH = f"{Path.home()}/.config/token.conf"
KEY = "key"
DEFAULT = "default"
def get_key(cfg, name):
return cfg.get(name, {}).get(KEY)
def get_token(key):
return "{:06}".format(int(pyotp.TOTP(key).now()))
def main():
cfg = toml.load(CONF_PATH)
name = sys.argv[1] if len(sys.argv) > 1 else DEFAULT
key = get_key(cfg, name)
if not key:
print(f"No key found in config {CONF_PATH}")
exit(1)
print(get_token(key))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment