|
| 1 | +"""One Time Pin Generator |
| 2 | +
|
| 3 | +* Requires: |
| 4 | + * pyotpi |
| 5 | + * keyring |
| 6 | + * xclip for click to copy to clipboard |
| 7 | + |
| 8 | +* Parameters(both required): |
| 9 | + * opt.service_name: service name where secret_key is stored in keyring |
| 10 | + * opt.user_name: username for keyring where secret_key is store |
| 11 | +
|
| 12 | +contributed by `theymightbetim <https://github.com/theymightbetim>` |
| 13 | +""" |
| 14 | + |
| 15 | +import core.module |
| 16 | +import core.widget |
| 17 | +import core.input |
| 18 | + |
| 19 | +import pyotp |
| 20 | +import keyring |
| 21 | + |
| 22 | + |
| 23 | +class Module(core.module.Module): |
| 24 | + |
| 25 | + def __init__(self, config, theme): |
| 26 | + super().__init__(config, theme, core.widget.Widget(self.full_text)) |
| 27 | + self.__username = self.parameter("user_name", "not set") |
| 28 | + self.__service_name = self.parameter("service_name", "not set") |
| 29 | + self.__key = keyring.get_password(self.__service_name, self.__username) |
| 30 | + self.__totp = pyotp.TOTP(self.__key) |
| 31 | + core.input.register( |
| 32 | + self, button=core.input.LEFT_MOUSE, cmd=self.copy_to_clipboard |
| 33 | + ) |
| 34 | + |
| 35 | + def full_text(self, widget): |
| 36 | + if self.__service_name == "not set": |
| 37 | + return "ConfigError: otp.service_name not set" |
| 38 | + if self.__username == "not set": |
| 39 | + return "ConfigError: otp.user_name not set" |
| 40 | + return self.__totp.now() |
| 41 | + |
| 42 | + def copy_to_clipboard(self, event): |
| 43 | + import subprocess |
| 44 | + |
| 45 | + mfa_code = str(self.__totp.now()) |
| 46 | + subprocess.Popen(["xclip", "-i"], stdin=subprocess.PIPE).communicate( |
| 47 | + mfa_code.encode("utf-8") |
| 48 | + ) |
0 commit comments