I want the user to input an account name so I can look for a matching token on storage. If the token exists I want to load a webview with the set cookie. This will aid in fast login when the user has multiple accounts. The cookie is getting set as def on_cookie_added is printing the token, but the page doesn't load with the user logged in.
Name: ACCESS_INFO
Value: 00000000%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The 0s are the fixed user Id from Twitch, the x are generated characters generated by Stream Raiders. From the tokens I checked, it always has 111 characters in total
Domain: .www.streamraiders.com
Path: /
Expires: Time in ISO 8601
Size: 122
HttpOnly: True
Secure: True
SameSite: Lax
Priority: Medium
Here's the code, very simple, gets user input and loads the webview. That is working properly.
import time
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl, QDateTime
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtWidgets import QApplication
from utils import constants
from utils.settings import open_file
name = input("Please insert the account name you want to access: ")
accounts = open_file(constants.py_accounts)
ACCESS_INFO = None
for account in accounts:
if account["name"] == name:
ACCESS_INFO = account["token"]
break
else:
print("Please insert the account you would like to access.")
sys.exit()
app = QApplication([])
view = QWebEngineView()
page = QWebEnginePage()
store = page.profile().cookieStore()
store.deleteAllCookies()
cookie = QNetworkCookie()
cookie.setName(b"ACCESS_INFO")
cookie.setValue(ACCESS_INFO.encode("utf-8"))
cookie.setDomain(".www.streamraiders.com")
cookie.setPath("/")
expiration_date = QDateTime.currentDateTime().addDays(5)
cookie.setExpirationDate(expiration_date)
cookie.setHttpOnly(True)
cookie.setSecure(True)
store.setCookie(cookie, QUrl("https://www.streamraiders.com"))
view.setPage(page)
def on_cookie_added(cookie):
cookie_name = cookie.name().data().decode()
cookie_value = cookie.value().data().decode()
if cookie_name == "ACCESS_INFO":
print(cookie_value)
time.sleep(5)
view.setUrl(view.url())
store.cookieAdded.connect(on_cookie_added)
url = QUrl("https://www.streamraiders.com")
view.setUrl(url)
view.show()
app.exec_()
I've already tried type the domain in different ways such as without the leading dot, with https, without the expiration date. I've already tried passing only the name and the value. Nothing worked.
Any help is appreciated.
setUrl()with the same url is completely pointless. You should at least usereload(). And you should also not use blocking functions liketime.sleep()in the main UI thread; if you want to delay a function call, useQTimer.singleShot().xyou used are placeholders or you actually did usex; in that case, it's obviously invalid: the server uses encrypted values in order to properly ensure that the authentication is valid (possibly with a server side expiration and a checksum), it obviously isn't sufficient to just set the user name in the cookie, otherwise there would be a huge security problem there.