AareGUI: Auth based on local user
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import os
|
||||
import jwt
|
||||
import requests
|
||||
|
||||
from aaredaqlib.models import TokenData
|
||||
|
||||
def auth(base_url: str | None) -> str:
|
||||
curr_user = os.getlogin()
|
||||
if base_url is None:
|
||||
token_data = TokenData(sub=curr_user,
|
||||
staff=True,
|
||||
session=15,
|
||||
pgroups=["p16371", "p22233"])
|
||||
return jwt.encode(token_data.model_dump(), "ABC123")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{base_url}/token",
|
||||
data={
|
||||
"username": curr_user,
|
||||
"password": ""
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
response_json = response.json()
|
||||
if "access_token" in response_json:
|
||||
return response_json["access_token"]
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"Authentication request failed: {e}")
|
||||
|
||||
return ""
|
||||
+12
-12
@@ -3,6 +3,7 @@ import sys
|
||||
from PySide6.QtCore import QCommandLineParser, QCommandLineOption
|
||||
from PySide6.QtWidgets import QApplication, QDialog
|
||||
|
||||
from aaregui.auth import auth
|
||||
from aaregui.main_window import MainWindow
|
||||
from aaregui.widgets.login import LoginDialog
|
||||
from aaredaqlib.beamline import MXBeamline, mx_beamline
|
||||
@@ -66,16 +67,15 @@ if __name__ == "__main__":
|
||||
default_image = parser.value(defaultImage)
|
||||
else:
|
||||
default_image = None
|
||||
|
||||
dialog = LoginDialog(base_url=base_url)
|
||||
|
||||
if dialog.exec() == QDialog.DialogCode.Accepted and dialog.token:
|
||||
win = MainWindow(base_url=base_url,
|
||||
token=dialog.token,
|
||||
default_image=default_image,
|
||||
zmq_addr=zmq_addr)
|
||||
win.show()
|
||||
sys.exit(app.exec())
|
||||
else:
|
||||
print("Cannot connect to AareDAQ server. Exiting.")
|
||||
try:
|
||||
token = auth(base_url)
|
||||
except Exception as e:
|
||||
print(f"Cannot connect to AareDAQ server. Exiting. {e}")
|
||||
sys.exit()
|
||||
|
||||
win = MainWindow(base_url=base_url,
|
||||
token=token,
|
||||
default_image=default_image,
|
||||
zmq_addr=zmq_addr)
|
||||
win.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
Reference in New Issue
Block a user