Make HTTP authentication extendable

This commit is contained in:
Fabian 2024-02-29 21:35:51 +01:00
parent aad11b9ea1
commit ebd938484d
3 changed files with 25 additions and 42 deletions

View File

@ -8,6 +8,8 @@ from queue import Queue, Empty
from loguru import logger from loguru import logger
from requests import Response from requests import Response
from fime.util import add_auth
try: try:
from PySide6 import QtCore, QtWidgets from PySide6 import QtCore, QtWidgets
except ImportError: except ImportError:
@ -30,6 +32,8 @@ class TaskCompleter(QtWidgets.QCompleter):
self.setFilterMode(QtCore.Qt.MatchFlag.MatchContains) self.setFilterMode(QtCore.Qt.MatchFlag.MatchContains)
self.setCaseSensitivity(QtCore.Qt.CaseInsensitive) self.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.session = FuturesSession() self.session = FuturesSession()
self.session.headers["Accept"] = "application/json"
add_auth(config, self.session)
self.config = config self.config = config
self.picker_url = None self.picker_url = None
self.search_url = None self.search_url = None
@ -81,7 +85,7 @@ class TaskCompleter(QtWidgets.QCompleter):
def update_picker(self, text): def update_picker(self, text):
self.text = text self.text = text
if self.text == self.currentCompletion(): if self.text == self.currentCompletion():
# do not update, after auto completion was used # do not update, after auto-completion was used
return return
if self.escalate: if self.escalate:
self.update_search() self.update_search()
@ -93,10 +97,6 @@ class TaskCompleter(QtWidgets.QCompleter):
params={ params={
"query": self.text "query": self.text
}, },
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
) )
with self.rif_counter_lock: with self.rif_counter_lock:
self.rif_counter += 1 self.rif_counter += 1
@ -133,10 +133,6 @@ class TaskCompleter(QtWidgets.QCompleter):
"maxResults": 10, "maxResults": 10,
"fields": "key,summary", "fields": "key,summary",
}, },
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
) )
with self.rif_counter_lock: with self.rif_counter_lock:
self.rif_counter += 1 self.rif_counter += 1
@ -162,13 +158,7 @@ class TaskCompleter(QtWidgets.QCompleter):
stripped = self.text.strip() stripped = self.text.strip()
if not self.issue_key_regex.match(stripped): if not self.issue_key_regex.match(stripped):
return return
future = self.session.get( future = self.session.get(self.issue_url_tmpl.format(stripped.upper()))
self.issue_url_tmpl.format(stripped.upper()),
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
)
with self.rif_counter_lock: with self.rif_counter_lock:
self.rif_counter += 1 self.rif_counter += 1
future.add_done_callback(partial(self.issue_response_callback, self.text)) future.add_done_callback(partial(self.issue_response_callback, self.text))

View File

@ -1,6 +1,9 @@
import enum import enum
from loguru import logger from loguru import logger
from requests import Session
from fime.config import Config, AuthMethods
try: try:
from PySide6 import QtCore, QtGui, QtWidgets from PySide6 import QtCore, QtGui, QtWidgets
@ -51,3 +54,13 @@ class Status(enum.Enum):
PROGRESS = enum.auto() PROGRESS = enum.auto()
OK = enum.auto() OK = enum.auto()
ERROR = enum.auto() ERROR = enum.auto()
def add_auth(config: Config, session: Session):
match config.auth_mode:
case AuthMethods.TOKEN:
session.headers["Authorization"] = f"Bearer {config.jira_token}"
case AuthMethods.COOKIES:
raise NotImplemented
case _:
raise AssertionError("Unknown auth method")

View File

@ -12,7 +12,7 @@ from requests_futures.sessions import FuturesSession
from fime.config import Config from fime.config import Config
from fime.exceptions import FimeException from fime.exceptions import FimeException
from fime.util import Status from fime.util import Status, add_auth
class WorklogRest: class WorklogRest:
@ -23,6 +23,8 @@ class WorklogRest:
self.worklog_url = os.path.join(config.jira_url, "rest/api/2/issue/{}/worklog") self.worklog_url = os.path.join(config.jira_url, "rest/api/2/issue/{}/worklog")
self.worklog_update_url = os.path.join(config.jira_url, "rest/api/2/issue/{issue_key}/worklog/{worklog_id}") self.worklog_update_url = os.path.join(config.jira_url, "rest/api/2/issue/{issue_key}/worklog/{worklog_id}")
self.session = FuturesSession() self.session = FuturesSession()
self.session.headers["Accept"] = "application/json"
add_auth(config, self.session)
self._user = None self._user = None
self._user_future = self._req_user() self._user_future = self._req_user()
self._issue_state: Dict[str, Tuple[Status, str]] = dict() self._issue_state: Dict[str, Tuple[Status, str]] = dict()
@ -31,13 +33,7 @@ class WorklogRest:
self._issues_lock = Lock() self._issues_lock = Lock()
def _req_user(self): def _req_user(self):
future = self.session.get( future = self.session.get(self.user_url)
self.user_url,
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
)
future.add_done_callback(self._resp_user) future.add_done_callback(self._resp_user)
return future return future
@ -60,13 +56,7 @@ class WorklogRest:
return ret return ret
def _req_issue(self, issue_key: str, pdate: date): def _req_issue(self, issue_key: str, pdate: date):
future = self.session.get( future = self.session.get(self.issue_url.format(issue_key))
self.issue_url.format(issue_key),
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
)
future.add_done_callback(partial(self._resp_issue, issue_key, pdate)) future.add_done_callback(partial(self._resp_issue, issue_key, pdate))
def _resp_issue(self, issue_key: str, pdate: date, future: Future): def _resp_issue(self, issue_key: str, pdate: date, future: Future):
@ -79,13 +69,7 @@ class WorklogRest:
self._issue_state[issue_key] = (Status.ERROR, "Could not find specified issue") self._issue_state[issue_key] = (Status.ERROR, "Could not find specified issue")
def _req_worklog_check(self, issue_key: str, issue_title: str, pdate: date): def _req_worklog_check(self, issue_key: str, issue_title: str, pdate: date):
future = self.session.get( future = self.session.get(self.worklog_url.format(issue_key))
self.worklog_url.format(issue_key),
headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
},
)
future.add_done_callback(partial(self._resp_worklog_check, issue_key, issue_title, pdate)) future.add_done_callback(partial(self._resp_worklog_check, issue_key, issue_title, pdate))
def _resp_worklog_check(self, issue_key: str, issue_title: str, pdate: date, future: Future): def _resp_worklog_check(self, issue_key: str, issue_title: str, pdate: date, future: Future):
@ -144,8 +128,6 @@ class WorklogRest:
future = self.session.post( future = self.session.post(
self.worklog_url.format(issue_key), self.worklog_url.format(issue_key),
headers={ headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
json={ json={
@ -161,8 +143,6 @@ class WorklogRest:
future = self.session.put( future = self.session.put(
self.worklog_update_url.format(issue_key=issue_key, worklog_id=worklog_id), self.worklog_update_url.format(issue_key=issue_key, worklog_id=worklog_id),
headers={ headers={
"Authorization": f"Bearer {self.config.jira_token}",
"Accept": "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
json={ json={