Add config/setting support for authentication via browser cookies

This commit is contained in:
Fabian 2024-02-29 21:08:55 +01:00
parent 4aee555605
commit aad11b9ea1
2 changed files with 94 additions and 16 deletions

View File

@ -1,4 +1,5 @@
from configparser import ConfigParser
from enum import StrEnum
from io import StringIO
from pathlib import Path
@ -17,9 +18,23 @@ def dequotify(string):
return string
class AuthMethods(StrEnum):
TOKEN = "token"
COOKIES = "cookies"
class Browsers(StrEnum):
AUTO = "auto"
FIREFOX = "firefox"
CHROME = "chrome"
CHROMIUM = "chromium"
EDGE = "edge"
OPERA = "opera"
class Config:
def __init__(self):
self._configparser = ConfigParser()
self._configparser = ConfigParser(interpolation=None)
config_dir_path = Path(QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.AppConfigLocation))
self.config_path = config_dir_path / "fime.conf"
if self.config_path.exists():
@ -47,7 +62,27 @@ class Config:
self._configparser["DEFAULT"]["jira_url"] = f'"{value}"'
@property
def jira_token(self):
def auth_mode(self) -> AuthMethods:
if self.jira_token:
fallback = AuthMethods.TOKEN.value
else:
fallback = AuthMethods.COOKIES.value
return dequotify(self._configparser.get("DEFAULT", "auth_mode", fallback=fallback)).lower()
@auth_mode.setter
def auth_mode(self, value: AuthMethods):
self._configparser["DEFAULT"]["auth_mode"] = f'"{value.value}"'
@property
def cookie_source(self) -> Browsers:
return dequotify(self._configparser.get("DEFAULT", "cookie_source", fallback=Browsers.AUTO.value)).lower()
@cookie_source.setter
def cookie_source(self, value: Browsers):
self._configparser["DEFAULT"]["cookie_source"] = f'"{value.value}"'
@property
def jira_token(self) -> str:
return dequotify(self._configparser.get("DEFAULT", "jira_token", fallback=""))
@jira_token.setter

View File

@ -5,8 +5,14 @@ try:
except ImportError:
from PySide2 import QtCore, QtGui, QtWidgets
from fime.config import Config
from fime.config import Config, AuthMethods, Browsers
AUTH_METHODS_PRESENTATION = {
AuthMethods.TOKEN: "Jira Access Token",
AuthMethods.COOKIES: "Browser Cookies",
}
AUTH_METHODS_VALUE = {v: k for k, v in AUTH_METHODS_PRESENTATION.items()}
class Settings(QtWidgets.QDialog):
def __init__(self, config: Config, parent, *args, **kwargs):
@ -27,34 +33,51 @@ class Settings(QtWidgets.QDialog):
self.jira_url_edit = QtWidgets.QLineEdit()
settings_layout.addWidget(self.jira_url_edit, 0, 1)
jira_token_label = QtWidgets.QLabel()
jira_token_label.setText("Jira Personal Access Token<br/> see <a href='https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html#UsingPersonalAccessTokens-CreatingPATsinapplication'>here</a> for how to get one")
jira_token_label.setTextFormat(QtCore.Qt.RichText)
jira_token_label.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
jira_token_label.setOpenExternalLinks(True)
settings_layout.addWidget(jira_token_label, 1, 0)
auth_method_label = QtWidgets.QLabel()
auth_method_label.setText("Authentication method")
settings_layout.addWidget(auth_method_label, 1, 0)
self.auth_method_combo_box = QtWidgets.QComboBox()
for am in AuthMethods:
self.auth_method_combo_box.addItem(AUTH_METHODS_PRESENTATION[am])
self.auth_method_combo_box.currentTextChanged.connect(self._auth_method_update)
settings_layout.addWidget(self.auth_method_combo_box, 1, 1, QtCore.Qt.AlignRight)
self.jira_token_label = QtWidgets.QLabel()
self.jira_token_label.setText("Jira Personal Access Token<br/> see <a href='https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html#UsingPersonalAccessTokens-CreatingPATsinapplication'>here</a> for how to get one")
self.jira_token_label.setTextFormat(QtCore.Qt.RichText)
self.jira_token_label.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
self.jira_token_label.setOpenExternalLinks(True)
settings_layout.addWidget(self.jira_token_label, 2, 0)
self.jira_token_edit = QtWidgets.QLineEdit()
settings_layout.addWidget(self.jira_token_edit, 1, 1)
settings_layout.addWidget(self.jira_token_edit, 2, 1)
self.cookie_source_label = QtWidgets.QLabel()
self.cookie_source_label.setText("Cookie source browser")
settings_layout.addWidget(self.cookie_source_label, 2, 0)
self.cookie_source_combo_box = QtWidgets.QComboBox()
for b in Browsers:
self.cookie_source_combo_box.addItem(b.capitalize())
settings_layout.addWidget(self.cookie_source_combo_box, 2, 1, QtCore.Qt.AlignRight)
tray_theme_label = QtWidgets.QLabel()
tray_theme_label.setText("Tray theme")
settings_layout.addWidget(tray_theme_label, 2, 0)
settings_layout.addWidget(tray_theme_label, 3, 0)
self.tray_theme_combo_box = QtWidgets.QComboBox()
self.tray_theme_combo_box.addItem("Light")
self.tray_theme_combo_box.addItem("Dark")
settings_layout.addWidget(self.tray_theme_combo_box, 2, 1, QtCore.Qt.AlignRight)
settings_layout.addWidget(self.tray_theme_combo_box, 3, 1, QtCore.Qt.AlignRight)
flip_menu_label = QtWidgets.QLabel()
flip_menu_label.setText("Flip menu")
settings_layout.addWidget(flip_menu_label, 3, 0)
settings_layout.addWidget(flip_menu_label, 4, 0)
self.flip_menu_check_box = QtWidgets.QCheckBox()
settings_layout.addWidget(self.flip_menu_check_box, 3, 1, QtCore.Qt.AlignRight)
settings_layout.addWidget(self.flip_menu_check_box, 4, 1, QtCore.Qt.AlignRight)
import_auto_change_task_label = QtWidgets.QLabel()
import_auto_change_task_label.setText("Automatically select task as active task\nafter task import")
settings_layout.addWidget(import_auto_change_task_label, 4, 0)
settings_layout.addWidget(import_auto_change_task_label, 5, 0)
self.import_auto_change_task_check_box = QtWidgets.QCheckBox()
settings_layout.addWidget(self.import_auto_change_task_check_box, 4, 1, QtCore.Qt.AlignRight)
settings_layout.addWidget(self.import_auto_change_task_check_box, 5, 1, QtCore.Qt.AlignRight)
self.ok_button = QtWidgets.QPushButton()
self.ok_button.setText("OK")
@ -77,19 +100,39 @@ class Settings(QtWidgets.QDialog):
def showEvent(self, _):
self.jira_url_edit.setText(self.config.jira_url)
self.auth_method_combo_box.setCurrentText(AUTH_METHODS_PRESENTATION[self.config.auth_mode])
self.jira_token_edit.setText(self.config.jira_token)
self.cookie_source_combo_box.setCurrentText(self.config.cookie_source.capitalize())
self.tray_theme_combo_box.setCurrentText(self.config.tray_theme.capitalize())
self.flip_menu_check_box.setChecked(self.config.flip_menu)
self.import_auto_change_task_check_box.setChecked(self.config.import_auto_change_task)
self._auth_method_update()
def _accepted(self):
self.config.jira_url = self.jira_url_edit.text()
self.config.auth_mode = AUTH_METHODS_VALUE[self.auth_method_combo_box.currentText()]
self.config.jira_token = self.jira_token_edit.text()
self.config.cookie_source = Browsers(self.cookie_source_combo_box.currentText().lower())
self.config.tray_theme = self.tray_theme_combo_box.currentText()
self.config.flip_menu = self.flip_menu_check_box.isChecked()
self.config.import_auto_change_task = self.import_auto_change_task_check_box.isChecked()
self.config.save()
def _auth_method_update(self):
match AUTH_METHODS_VALUE[self.auth_method_combo_box.currentText()]:
case AuthMethods.TOKEN:
self.jira_token_label.show()
self.jira_token_edit.show()
self.cookie_source_label.hide()
self.cookie_source_combo_box.hide()
case AuthMethods.COOKIES:
self.jira_token_label.hide()
self.jira_token_edit.hide()
self.cookie_source_label.show()
self.cookie_source_combo_box.show()
case _:
raise AssertionError("unhandled auth method")
# only for dev/debug
if __name__ == "__main__":