allow caching of password in memory

master
Maximilian Zettler 3 years ago
parent fa3e79a1b3
commit 64c062caa2
  1. 40
      timebot/gui.py
  2. 8
      timebot/timebot.py

@ -1,15 +1,43 @@
import datetime import datetime
import logging import logging
from typing import Tuple
from PyQt5 import QtGui from PyQt5 import QtGui
from PyQt5.QtCore import QTimer, QObject, pyqtSignal, QThread, Qt 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 from timebot.timebot import TimeBot, TimebotObtainPasswordError
package_logger = logging.getLogger(__name__) 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): class HoursPresentLabelArea(QWidget):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
@ -66,8 +94,6 @@ class TimebotMainWindow(QWidget):
self.error_msg.setIcon(QMessageBox.Critical) self.error_msg.setIcon(QMessageBox.Critical)
self.error_msg.finished.connect(self.error_msg_finished) self.error_msg.finished.connect(self.error_msg_finished)
self.password_dialog = QInputDialog()
self.update_status_running = False self.update_status_running = False
self.update_status() self.update_status()
@ -98,16 +124,18 @@ class TimebotMainWindow(QWidget):
self.update_status_started() self.update_status_started()
def get_password(self, callback: callable = None): def get_password(self, callback: callable = None):
if self.timebot.mobatime_api.password is not None:
return
self.status_timer.stop() self.status_timer.stop()
password, ok = self.password_dialog.getText(self, "Timebot Password", 'Enter your password:', ok, password, keep = MyPasswordDialog(self).get_password()
QLineEdit.Password)
if ok: if ok:
self.timebot.mobatime_api.password = password self.timebot.mobatime_api.password = password
try: try:
_ = self.timebot.mobatime_api.session _ = self.timebot.mobatime_api.session
except Exception as e: except Exception as e:
self.error_msg_show(str(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: if callback is not None:
callback() callback()
self.status_timer.start(self.status_timer_time) self.status_timer.start(self.status_timer_time)

@ -1,5 +1,6 @@
import datetime import datetime
import getpass import getpass
import json
import logging import logging
import pickle import pickle
import sys import sys
@ -102,10 +103,13 @@ class MobatimeApi:
"username": self.user, "username": self.user,
"password": self.password, "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", session.post(self.baseurl + "Account/LogOn",
data=login_data, 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() session.get(self.baseurl + "Employee/GetEmployeeList", timeout=2).raise_for_status()
@staticmethod @staticmethod

Loading…
Cancel
Save