Skip to content

Commit 041056e

Browse files
authored
TUI authentication fix (#604)
* Added 'allow_user_token' to security config to enable verification using the simpler 'user' token * Added messages when starting up Murfey TUI to describe why it fails to connect to server
1 parent 2a4bf84 commit 041056e

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/murfey/client/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,20 @@ def run():
275275
rich_handler.setLevel(logging.DEBUG if args.debug else logging.INFO)
276276

277277
# Set up websocket app and handler
278-
client_id = requests.get(
278+
client_id_response = requests.get(
279279
f"{murfey_url.geturl()}{url_path_for('session_control.router', 'new_client_id')}"
280-
).json()
280+
)
281+
if client_id_response.status_code == 401:
282+
exit(
283+
"This instrument is not authorised to run the TUI app; please use the "
284+
"Murfey web UI instead"
285+
)
286+
elif client_id_response.status_code != 200:
287+
exit(
288+
"Unable to establish connection to Murfey server: \n"
289+
f"{client_id_response.json()}"
290+
)
291+
client_id: dict = client_id_response.json()
281292
ws = murfey.client.websocket.WSApp(
282293
server=args.server,
283294
id=client_id["new_id"],

src/murfey/instrument_server/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import murfey
99
import murfey.client.update
10+
import murfey.client.websocket
1011
from murfey.client.customlogging import CustomHandler
1112
from murfey.util import LogFilter
1213
from murfey.util.client import read_config

src/murfey/server/api/auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,16 @@ async def validate_instrument_token(
175175
if expiry_time := decoded_data.get("expiry_time"):
176176
if expiry_time < time.time():
177177
raise JWTError
178+
# Check that the decoded session corresponds to the visit
178179
elif decoded_data.get("session") is not None:
179-
# Check that the decoded session corresponds to the visit
180180
if not validate_session_against_visit(
181181
decoded_data["session"], decoded_data["visit"]
182182
):
183183
raise JWTError
184+
# Verify 'user' token if enabled
185+
elif security_config.allow_user_token:
186+
if not decoded_data.get("user"):
187+
raise JWTError
184188
else:
185189
raise JWTError
186190
except JWTError:

src/murfey/util/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ class Security(BaseModel):
124124
ispyb_credentials: Optional[Path] = None
125125

126126
# Murfey server connection settings
127+
auth_url: str = ""
128+
auth_type: Literal["password", "cookie"] = "password"
127129
auth_algorithm: str = ""
128130
auth_key: str = ""
129-
auth_type: Literal["password", "cookie"] = "password"
130-
auth_url: str = ""
131-
instrument_auth_type: Literal["token", ""] = "token"
132-
instrument_auth_url: str = ""
133131
cookie_key: str = ""
132+
instrument_auth_url: str = ""
133+
instrument_auth_type: Literal["token", ""] = "token"
134+
allow_user_token: bool = False # TUI 'user' token support
134135
session_validation: str = ""
135136
session_token_timeout: Optional[int] = None
136137
allow_origins: list[str] = ["*"]

0 commit comments

Comments
 (0)