1
0
Fork 0
forked from Fuji/Fuji

Refactor authentication functions and move them to a new utils module

This commit is contained in:
Maarceeli 2025-02-22 13:19:40 +01:00
parent 20db69ef4d
commit b17be83791
2 changed files with 18 additions and 18 deletions

View file

@ -6,6 +6,7 @@ import keyring
import threading import threading
import flet as ft import flet as ft
from i18n import * from i18n import *
from utils import *
from pages.home import * from pages.home import *
from pathlib import Path from pathlib import Path
from pages.exams import * from pages.exams import *
@ -18,13 +19,6 @@ from pages.attendance import *
from sdk.src.interfaces.prometheus.context import * from sdk.src.interfaces.prometheus.context import *
from sdk.src.interfaces.prometheus.interface import * from sdk.src.interfaces.prometheus.interface import *
def saveauth(service, username, data, chunk_size=1000):
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
keyring.set_password(service, f"{username}_count", str(len(chunks)))
for i, chunk in enumerate(chunks):
keyring.set_password(service, f"{username}_{i}", chunk)
def sync(): def sync():
auth_context_raw = loadauth("Fuji", "Auth Context") auth_context_raw = loadauth("Fuji", "Auth Context")
auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw) auth_context = PrometheusAuthContext.model_validate_json(auth_context_raw)
@ -34,11 +28,11 @@ def sync():
student_context=None, student_context=None,
) )
try: try:
interface.login() interface.login()
except NoLoggedInException: except NoLoggedInException:
print("nologgedinexception") print("NoLoggedInException, or in other words, vulcan shitted itself.")
students = interface.get_students() students = interface.get_students()
@ -49,15 +43,6 @@ def sync():
auth_context = interface.get_auth_context() auth_context = interface.get_auth_context()
jsoncontext = auth_context.model_dump_json() jsoncontext = auth_context.model_dump_json()
saveauth("Fuji", "Auth Context", jsoncontext) saveauth("Fuji", "Auth Context", jsoncontext)
def loadauth(service, username):
try:
count = int(keyring.get_password(service, f"{username}_count"))
return "".join(keyring.get_password(service, f"{username}_{i}") or "" for i in range(count))
except (TypeError, ValueError):
return None
def main(page: ft.Page): def main(page: ft.Page):
# Page settings # Page settings

15
src/utils.py Normal file
View file

@ -0,0 +1,15 @@
import keyring
def saveauth(service, username, data, chunk_size=1000):
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
keyring.set_password(service, f"{username}_count", str(len(chunks)))
for i, chunk in enumerate(chunks):
keyring.set_password(service, f"{username}_{i}", chunk)
def loadauth(service, username):
try:
count = int(keyring.get_password(service, f"{username}_count"))
return "".join(keyring.get_password(service, f"{username}_{i}") or "" for i in range(count))
except (TypeError, ValueError):
return None