Created
September 9, 2021 08:40
-
-
Save cathay4t/373f78e681b439f708db9ca47e456a42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| """ | |
| To use this script, you create ~/.config/token.conf with stuff like: | |
| [default] | |
| # comments | |
| key = "SOME_32_CHARS" | |
| [github] | |
| # comments | |
| key = "SOME_32_CHARS" | |
| """ | |
| 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