From 64c062caa236e7ea31eeda8aaa619919199d782d Mon Sep 17 00:00:00 2001 From: Maximilian Zettler Date: Mon, 13 Jun 2022 11:16:23 +0200 Subject: [PATCH] allow caching of password in memory --- timebot/gui.py | 40 ++++++++++++++++++++++++++++++++++------ timebot/timebot.py | 8 ++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/timebot/gui.py b/timebot/gui.py index cd4ca06..88e1aa4 100644 --- a/timebot/gui.py +++ b/timebot/gui.py @@ -1,15 +1,43 @@ import datetime import logging +from typing import Tuple from PyQt5 import QtGui from PyQt5.QtCore import QTimer, QObject, pyqtSignal, QThread, Qt -from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QMessageBox, QInputDialog, QLineEdit, QHBoxLayout +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QMessageBox, QLineEdit, \ + QHBoxLayout, QCheckBox, QDialogButtonBox, QFormLayout, QDialog from timebot.timebot import TimeBot, TimebotObtainPasswordError package_logger = logging.getLogger(__name__) +class MyPasswordDialog(QDialog): + def __init__(self, parent=None): + super().__init__(parent) + + self.setWindowTitle("Timebot Password") + + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) + self.buttonBox.accepted.connect(self.accept) + self.buttonBox.rejected.connect(self.reject) + + self.first = QLineEdit(self) + self.first.setEchoMode(QLineEdit.Password) + self.second = QCheckBox(self) + + self.layout = QFormLayout() + self.layout.addRow("Enter your password:", self.first) + self.layout.addRow("Cache password:", self.second) + self.layout.addWidget(self.buttonBox) + self.setLayout(self.layout) + + def get_password(self) -> Tuple[bool, str, bool]: + if self.exec(): + return True, self.first.text(), self.second.isChecked() + return False, "", False + + class HoursPresentLabelArea(QWidget): def __init__(self, parent=None): super().__init__(parent) @@ -66,8 +94,6 @@ class TimebotMainWindow(QWidget): self.error_msg.setIcon(QMessageBox.Critical) self.error_msg.finished.connect(self.error_msg_finished) - self.password_dialog = QInputDialog() - self.update_status_running = False self.update_status() @@ -98,16 +124,18 @@ class TimebotMainWindow(QWidget): self.update_status_started() def get_password(self, callback: callable = None): + if self.timebot.mobatime_api.password is not None: + return self.status_timer.stop() - password, ok = self.password_dialog.getText(self, "Timebot Password", 'Enter your password:', - QLineEdit.Password) + ok, password, keep = MyPasswordDialog(self).get_password() if ok: self.timebot.mobatime_api.password = password try: _ = self.timebot.mobatime_api.session except Exception as e: self.error_msg_show(str(e)) - self.timebot.mobatime_api.password = None + if not keep: + self.timebot.mobatime_api.password = None if callback is not None: callback() self.status_timer.start(self.status_timer_time) diff --git a/timebot/timebot.py b/timebot/timebot.py index 0e24a54..337f404 100644 --- a/timebot/timebot.py +++ b/timebot/timebot.py @@ -1,5 +1,6 @@ import datetime import getpass +import json import logging import pickle import sys @@ -102,10 +103,13 @@ class MobatimeApi: "username": self.user, "password": self.password, } - session.cookies.clear_session_cookies() + session.cookies.clear() + self.logger.debug("deleted session cookies: " + json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) + # This gives 200 even with wrong password + # 500 is given if you try to login with old session session.post(self.baseurl + "Account/LogOn", data=login_data, - timeout=2).raise_for_status() # This always gives 200 ... even with wrong password + timeout=2).raise_for_status() session.get(self.baseurl + "Employee/GetEmployeeList", timeout=2).raise_for_status() @staticmethod