1
0
Fork 0
mirror of synced 2024-06-29 03:31:06 +12:00
Rare/rare/components/dialogs/login/browser_login.py

94 lines
3.4 KiB
Python
Raw Normal View History

import json
from logging import getLogger
2021-10-11 09:08:31 +13:00
from typing import Tuple
2021-05-29 06:34:34 +12:00
from PyQt5.QtCore import pyqtSignal, QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QFrame, qApp
2021-10-11 09:08:31 +13:00
from legendary.core import LegendaryCore
2022-06-24 03:05:04 +12:00
from legendary.utils import webview_login
2021-05-29 06:34:34 +12:00
from rare.ui.components.dialogs.login.browser_login import Ui_BrowserLogin
from rare.utils.extra_widgets import IndicatorLineEdit
from rare.utils.misc import icon
logger = getLogger("BrowserLogin")
2021-02-18 22:22:15 +13:00
class BrowserLogin(QFrame):
success = pyqtSignal()
changed = pyqtSignal()
2022-06-24 03:05:04 +12:00
login_url = (
"https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fid%2Fapi%2Fredirect"
)
2021-02-18 22:22:15 +13:00
2021-05-29 06:34:34 +12:00
def __init__(self, core: LegendaryCore, parent=None):
super(BrowserLogin, self).__init__(parent=parent)
self.setFrameStyle(self.StyledPanel)
self.ui = Ui_BrowserLogin()
self.ui.setupUi(self)
2021-05-29 06:34:34 +12:00
self.core = core
self.sid_edit = IndicatorLineEdit(
2022-03-15 11:29:54 +13:00
placeholder=self.tr("Insert SID here"), edit_func=self.text_changed, parent=self
)
self.ui.link_text.setText(self.login_url)
self.ui.copy_button.setIcon(icon("mdi.content-copy", "fa.copy"))
self.ui.copy_button.clicked.connect(self.copy_link)
2022-01-23 13:03:56 +13:00
self.ui.sid_layout.addWidget(self.sid_edit)
self.ui.open_button.clicked.connect(self.open_browser)
self.sid_edit.textChanged.connect(self.changed.emit)
2022-01-23 13:03:56 +13:00
def copy_link(self):
clipboard = qApp.clipboard()
2022-01-23 13:03:56 +13:00
clipboard.setText(self.login_url)
self.ui.status_label.setText(self.tr("Copied to clipboard"))
2022-01-23 13:03:56 +13:00
def is_valid(self):
return self.sid_edit.is_valid
@staticmethod
def text_changed(text) -> Tuple[bool, str, str]:
if text:
text = text.strip()
if text.startswith("{") and text.endswith("}"):
try:
text = json.loads(text).get("sid")
except json.JSONDecodeError:
return False, text, IndicatorLineEdit.reasons.wrong_format
elif '"' in text:
text = text.strip('"')
return len(text) == 32, text, IndicatorLineEdit.reasons.wrong_format
else:
return False, text, ""
2021-05-29 06:34:34 +12:00
def do_login(self):
self.ui.status_label.setText(self.tr("Logging in..."))
2021-05-29 06:34:34 +12:00
sid = self.sid_edit.text()
try:
token = self.core.auth_sid(sid)
if self.core.auth_code(token):
2022-06-24 03:05:04 +12:00
logger.info(f"Successfully logged in as {self.core.lgd.userdata['displayName']}")
2021-05-29 06:34:34 +12:00
self.success.emit()
else:
self.ui.status_label.setText(self.tr("Login failed."))
2021-05-29 06:34:34 +12:00
logger.warning("Failed to login through browser")
except Exception as e:
logger.warning(e)
def open_browser(self):
if webview_login.webview_available is False:
2022-06-24 03:05:04 +12:00
logger.warning("You don't have webengine installed, " "you will need to manually copy the SID.")
QDesktopServices.openUrl(QUrl(self.login_url))
else:
if webview_login.do_webview_login(
2022-06-24 03:05:04 +12:00
callback_sid=self.core.auth_sid, callback_code=self.core.auth_code
):
logger.info("Successfully logged in as " f"{self.core.lgd.userdata['displayName']}")
self.success.emit()
else:
logger.warning("Failed to login through browser.")