License as MIT
This commit is contained in:
parent
32c5240fea
commit
7767fa4b66
5 changed files with 123 additions and 27 deletions
34
.pre-commit-config.yaml
Normal file
34
.pre-commit-config.yaml
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
|
||||
#
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
default_language_version:
|
||||
python: python3
|
||||
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.5.0
|
||||
hooks:
|
||||
- id: check-yaml
|
||||
- id: end-of-file-fixer
|
||||
exclude: tests
|
||||
- id: trailing-whitespace
|
||||
exclude: tests
|
||||
- repo: https://github.com/codespell-project/codespell
|
||||
rev: v2.2.6
|
||||
hooks:
|
||||
- id: codespell
|
||||
args: [-w]
|
||||
- repo: https://github.com/fsfe/reuse-tool
|
||||
rev: v2.1.0
|
||||
hooks:
|
||||
- id: reuse
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.1.6
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff
|
||||
args: [ --fix, --preview ]
|
||||
# Run the formatter.
|
||||
- id: ruff-format
|
||||
9
LICENSES/MIT.txt
Normal file
9
LICENSES/MIT.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
10
LICENSES/Unlicense.txt
Normal file
10
LICENSES/Unlicense.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
||||
|
|
@ -1,3 +1,9 @@
|
|||
<!--
|
||||
SPDX-FileCopyrightText: 2023 Jeff Epler
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
-->
|
||||
|
||||
# textual-totp: TOTP (authenticator) application using Python & Textual
|
||||
|
||||
# Installation
|
||||
|
|
|
|||
85
ttotp.py
85
ttotp.py
|
|
@ -1,5 +1,9 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
# SPDX-FileCopyrightText: 2023 Jeff Epler
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import time
|
||||
|
|
@ -20,6 +24,7 @@ import pyotp
|
|||
import platformdirs
|
||||
import tomllib
|
||||
|
||||
|
||||
# Copied from pyotp with the issuer mismatch check removed
|
||||
def parse_uri(uri: str) -> pyotp.OTP:
|
||||
"""
|
||||
|
|
@ -66,7 +71,9 @@ def parse_uri(uri: str) -> pyotp.OTP:
|
|||
elif value == "SHA512":
|
||||
otp_data["digest"] = hashlib.sha512
|
||||
else:
|
||||
raise ValueError("Invalid value for algorithm, must be SHA1, SHA256 or SHA512")
|
||||
raise ValueError(
|
||||
"Invalid value for algorithm, must be SHA1, SHA256 or SHA512"
|
||||
)
|
||||
elif key == "digits":
|
||||
digits = int(value)
|
||||
if digits not in [6, 7, 8]:
|
||||
|
|
@ -90,8 +97,10 @@ def parse_uri(uri: str) -> pyotp.OTP:
|
|||
|
||||
raise ValueError("Not a supported OTP type")
|
||||
|
||||
|
||||
default_conffile = platformdirs.user_config_path("ttotp") / "settings.toml"
|
||||
|
||||
|
||||
class TOTPLabel(Label, can_focus=True):
|
||||
BINDINGS = [
|
||||
Binding("c", "copy", "Copy code", show=True),
|
||||
|
|
@ -102,18 +111,18 @@ class TOTPLabel(Label, can_focus=True):
|
|||
|
||||
@property
|
||||
def idx(self):
|
||||
return int(self.css_class.split('-')[1])
|
||||
return int(self.css_class.split("-")[1])
|
||||
|
||||
@property
|
||||
def css_class(self):
|
||||
for c in self.classes:
|
||||
if re.match('otp-[0-9]', c):
|
||||
if re.match("otp-[0-9]", c):
|
||||
return c
|
||||
return None
|
||||
|
||||
@property
|
||||
def related(self, arg=''):
|
||||
return self.screen.query(f'.{self.css_class}{arg}')
|
||||
def related(self, arg=""):
|
||||
return self.screen.query(f".{self.css_class}{arg}")
|
||||
|
||||
def related_remove_class(self, cls):
|
||||
for widget in self.related:
|
||||
|
|
@ -130,6 +139,7 @@ class TOTPLabel(Label, can_focus=True):
|
|||
def on_focus(self):
|
||||
self.related_add_class("otp-focused")
|
||||
|
||||
|
||||
@dataclass
|
||||
class TOTPData:
|
||||
totp: pyotp.TOTP
|
||||
|
|
@ -146,9 +156,9 @@ class TOTPData:
|
|||
self.value_widget.update("*" * self.totp.digits)
|
||||
self.progress_widget.progress = self.totp.interval - progress
|
||||
|
||||
class TTOTP(App[None]):
|
||||
|
||||
CSS = '''
|
||||
class TTOTP(App[None]):
|
||||
CSS = """
|
||||
VerticalScroll {
|
||||
layout: grid;
|
||||
grid-size: 2;
|
||||
|
|
@ -159,7 +169,7 @@ class TTOTP(App[None]):
|
|||
ProgressBar { column-span: 2; }
|
||||
Bar > .bar--bar { color: $success; }
|
||||
Bar { width: 1fr; }
|
||||
'''
|
||||
"""
|
||||
|
||||
def __init__(self, tokens):
|
||||
super().__init__()
|
||||
|
|
@ -171,13 +181,15 @@ class TTOTP(App[None]):
|
|||
|
||||
def on_mount(self):
|
||||
self.timer_func()
|
||||
self.timer = self.set_interval(.1, self.timer_func)
|
||||
self.clear_clipboard_timer = self.set_timer(30, self.clear_clipboard_func, pause=True)
|
||||
self.timer = self.set_interval(0.1, self.timer_func)
|
||||
self.clear_clipboard_timer = self.set_timer(
|
||||
30, self.clear_clipboard_func, pause=True
|
||||
)
|
||||
|
||||
def clear_clipboard_func(self):
|
||||
if pyperclip.paste() == self.copied:
|
||||
self.notify("Clipboard cleared", title="")
|
||||
pyperclip.copy('')
|
||||
pyperclip.copy("")
|
||||
|
||||
def timer_func(self):
|
||||
now = time.time()
|
||||
|
|
@ -189,9 +201,19 @@ class TTOTP(App[None]):
|
|||
with VerticalScroll() as v:
|
||||
v.can_focus = False
|
||||
for i, otp in enumerate(self.tokens):
|
||||
otp_name = TOTPLabel(f"{otp.name} / {otp.issuer}", classes=f"otp-name otp-name-{i} otp-{i}", expand=True)
|
||||
otp_value = Label("", classes=f"otp-value otp-value-{i} otp-{i}", expand=True)
|
||||
otp_progress = ProgressBar(classes=f"otp-progress otp-progress-{i} otp-{i}", show_percentage=False, show_eta=False)
|
||||
otp_name = TOTPLabel(
|
||||
f"{otp.name} / {otp.issuer}",
|
||||
classes=f"otp-name otp-name-{i} otp-{i}",
|
||||
expand=True,
|
||||
)
|
||||
otp_value = Label(
|
||||
"", classes=f"otp-value otp-value-{i} otp-{i}", expand=True
|
||||
)
|
||||
otp_progress = ProgressBar(
|
||||
classes=f"otp-progress otp-progress-{i} otp-{i}",
|
||||
show_percentage=False,
|
||||
show_eta=False,
|
||||
)
|
||||
|
||||
otp_progress.total = otp.interval
|
||||
otpdata = TOTPData(otp, otp_name, otp_value, otp_progress)
|
||||
|
|
@ -217,14 +239,25 @@ class TTOTP(App[None]):
|
|||
|
||||
self.notify("Code copied", title="")
|
||||
|
||||
@click.command
|
||||
@click.option("--config", type=pathlib.Path, default=default_conffile, help="Configuration file to use")
|
||||
@click.option("--profile", type=str, default=None, help="Profile to use within the configuration file")
|
||||
def main(config, profile):
|
||||
|
||||
@click.command
|
||||
@click.option(
|
||||
"--config",
|
||||
type=pathlib.Path,
|
||||
default=default_conffile,
|
||||
help="Configuration file to use",
|
||||
)
|
||||
@click.option(
|
||||
"--profile",
|
||||
type=str,
|
||||
default=None,
|
||||
help="Profile to use within the configuration file",
|
||||
)
|
||||
def main(config, profile):
|
||||
def config_hint():
|
||||
config.parent.mkdir(parents=True, exist_ok=True)
|
||||
print(f"""\
|
||||
print(
|
||||
f"""\
|
||||
You need to create the configuration file: {config}
|
||||
|
||||
It's a toml file which specifies a command to run to retrieve the list of OTPs.
|
||||
|
|
@ -238,7 +271,8 @@ You can have multiple profiles as configuration file sections:
|
|||
[work]
|
||||
otp-command = ['pass', 'totp-tokens-work']
|
||||
|
||||
""")
|
||||
"""
|
||||
)
|
||||
raise SystemExit(2)
|
||||
|
||||
if not config.exists():
|
||||
|
|
@ -252,21 +286,24 @@ You can have multiple profiles as configuration file sections:
|
|||
if profile:
|
||||
config_data = config_data[profile]
|
||||
|
||||
otp_command = config_data.get('otp-command')
|
||||
otp_command = config_data.get("otp-command")
|
||||
if otp_command is None:
|
||||
config_hint()
|
||||
|
||||
c = subprocess.check_output(otp_command, shell=isinstance(otp_command, str), text=True)
|
||||
c = subprocess.check_output(
|
||||
otp_command, shell=isinstance(otp_command, str), text=True
|
||||
)
|
||||
print(f"{c=!r}")
|
||||
|
||||
global tokens
|
||||
tokens = []
|
||||
for row in c.strip().split('\n'):
|
||||
for row in c.strip().split("\n"):
|
||||
if row.startswith("otpauth://"):
|
||||
print(f"parsing {row=!r}")
|
||||
tokens.append(parse_uri(row))
|
||||
|
||||
TTOTP(tokens).run()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue