Skip to content

Commit a6fc956

Browse files
Use different auth procedures for different endpoints (#601)
* Allow different auth routes for instrument server and frontend routers * Split token auth mechanisms for two different groups of endpoints * Major refactor of 'murfey.server.api.auth', rearranging functions by purpose and splitting the authentication of instrument and frontend tokens into separate functions * Updated URL paths for 'auth' and 'clem' routers * Refactored 'murfey.server.api.file_manip' into 'file_io_instrument', 'file_io_frontend', and 'file_io_shared', as frontend and instrument both access the same functions; moved 'process_gain()' into 'file_io_shared' so that it can be called from both with proper validation * Updated route manifest and client-side URL lookups * Used FastAPI's 'APIKeyCookie' object for cookie authentication * Split session access validation function for instrument server and frontend into separate functions * Fixed logic for 'create_access_token' and 'generate_token' for handling authentication using either 'password' or 'cookie' * 'simple_token_validation()' should be using instrument server validation function instead * Created new annotated ints for type hinting in endpoints receiving requests from frontend and instrument server; updates the other server routers to use the newly created annotated ints --------- Co-authored-by: Eu Pin Tien <[email protected]>
1 parent d64446c commit a6fc956

25 files changed

+566
-370
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ GitHub = "https://github.com/DiamondLightSource/python-murfey"
9696
"murfey.spa_inject" = "murfey.cli.inject_spa_processing:run"
9797
"murfey.spa_ispyb_entries" = "murfey.cli.spa_ispyb_messages:run"
9898
"murfey.transfer" = "murfey.cli.transfer:run"
99-
[project.entry-points."murfey.auth.token_validation"]
100-
"password" = "murfey.server.api.auth:password_token_validation"
10199
[project.entry-points."murfey.config.extraction"]
102100
"murfey_machine" = "murfey.util.config:get_extended_machine_config"
103101
[project.entry-points."murfey.workflows"]

src/murfey/client/contexts/spa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def post_transfer(
567567
)
568568
if not environment.movie_counters.get(str(source)):
569569
movie_counts_get = capture_get(
570-
f"{environment.url.geturl()}{url_path_for('session_info.router', 'count_number_of_movies')}",
570+
f"{environment.url.geturl()}{url_path_for('session_control.router', 'count_number_of_movies')}",
571571
)
572572
if movie_counts_get is not None:
573573
environment.movie_counters[str(source)] = count(
@@ -581,7 +581,7 @@ def post_transfer(
581581
eer_fractionation_file = None
582582
if file_transferred_to.suffix == ".eer":
583583
response = capture_post(
584-
f"{str(environment.url.geturl())}{url_path_for('file_manip.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
584+
f"{str(environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
585585
json={
586586
"eer_path": str(file_transferred_to),
587587
"fractionation": environment.data_collection_parameters[

src/murfey/client/contexts/tomo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def _add_tilt(
317317
eer_fractionation_file = None
318318
if environment.data_collection_parameters.get("num_eer_frames"):
319319
response = requests.post(
320-
f"{str(environment.url.geturl())}{url_path_for('file_manip.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
320+
f"{str(environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
321321
json={
322322
"num_frames": environment.data_collection_parameters[
323323
"num_eer_frames"

src/murfey/client/multigrid_control.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _start_rsyncer(
251251
log.info(f"starting rsyncer: {source}")
252252
if transfer:
253253
# Always make sure the destination directory exists
254-
make_directory_url = f"{self.murfey_url}{url_path_for('file_manip.router', 'make_rsyncer_destination', session_id=self.session_id)}"
254+
make_directory_url = f"{self.murfey_url}{url_path_for('file_io_instrument.router', 'make_rsyncer_destination', session_id=self.session_id)}"
255255
capture_post(make_directory_url, json={"destination": destination})
256256
if self._environment:
257257
self._environment.default_destinations[source] = destination
@@ -437,7 +437,7 @@ def _start_dc(self, json, from_form: bool = False):
437437
log.info("Registering tomography processing parameters")
438438
if self._environment.data_collection_parameters.get("num_eer_frames"):
439439
eer_response = requests.post(
440-
f"{str(self._environment.url.geturl())}{url_path_for('file_manip.router', 'write_eer_fractionation_file', visit_name=self._environment.visit, session_id=self._environment.murfey_session)}",
440+
f"{str(self._environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=self._environment.visit, session_id=self._environment.murfey_session)}",
441441
json={
442442
"num_frames": self._environment.data_collection_parameters[
443443
"num_eer_frames"

src/murfey/client/tui/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _start_rsyncer(
209209
log.info(f"starting rsyncer: {source}")
210210
if transfer:
211211
# Always make sure the destination directory exists
212-
make_directory_url = f"{str(self._url.geturl())}{url_path_for('file_manip.router', 'make_rsyncer_destination', session_id=self._environment.murfey_session)}"
212+
make_directory_url = f"{str(self._url.geturl())}{url_path_for('file_io_instrument.router', 'make_rsyncer_destination', session_id=self._environment.murfey_session)}"
213213
capture_post(make_directory_url, json={"destination": destination})
214214
if self._environment:
215215
self._environment.default_destinations[source] = destination
@@ -488,7 +488,7 @@ def _start_dc(self, json, from_form: bool = False):
488488
log.info("Registering tomography processing parameters")
489489
if self.app._environment.data_collection_parameters.get("num_eer_frames"):
490490
eer_response = requests.post(
491-
f"{str(self.app._environment.url.geturl())}{url_path_for('file_manip.router', 'write_eer_fractionation_file', visit_name=self.app._environment.visit, session_id=self.app._environment.murfey_session)}",
491+
f"{str(self.app._environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=self.app._environment.visit, session_id=self.app._environment.murfey_session)}",
492492
json={
493493
"num_frames": self.app._environment.data_collection_parameters[
494494
"num_eer_frames"

src/murfey/client/tui/screens.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def determine_default_destination(
110110
_default = environment.destination_registry[source_name]
111111
else:
112112
suggested_path_response = capture_post(
113-
url=f"{str(environment.url.geturl())}{url_path_for('file_manip.router', 'suggest_path', visit_name=visit, session_id=environment.murfey_session)}",
113+
url=f"{str(environment.url.geturl())}{url_path_for('file_io_instrument.router', 'suggest_path', visit_name=visit, session_id=environment.murfey_session)}",
114114
json={
115115
"base_path": f"{destination}/{visit}/{mid_path.parent if include_mid_path else ''}/raw",
116116
"touch": touch,
@@ -906,7 +906,7 @@ def on_button_pressed(self, event):
906906
f"Gain reference file {posix_path(self._dir_tree._gain_reference)!r} was not successfully transferred to {visit_path}/processing"
907907
)
908908
process_gain_response = requests.post(
909-
url=f"{str(self.app._environment.url.geturl())}{url_path_for('file_manip.router', 'process_gain', session_id=self.app._environment.murfey_session)}",
909+
url=f"{str(self.app._environment.url.geturl())}{url_path_for('file_io_instrument.router', 'process_gain', session_id=self.app._environment.murfey_session)}",
910910
json={
911911
"gain_ref": str(self._dir_tree._gain_reference),
912912
"eer": bool(

src/murfey/instrument_server/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
def validate_session_token(
5252
session_id: int, token: Annotated[str, Depends(oauth2_scheme)]
5353
):
54+
"""
55+
Validates the token received from the backend server
56+
"""
5457
try:
5558
decoded_data = jwt.decode(
5659
token,
@@ -62,7 +65,7 @@ def validate_session_token(
6265
except JWTError:
6366
raise HTTPException(
6467
status_code=status.HTTP_401_UNAUTHORIZED,
65-
detail="Could not validate credentials",
68+
detail="Could not validate credentials from backend",
6669
headers={"WWW-Authenticate": "Bearer"},
6770
)
6871
return session_id

0 commit comments

Comments
 (0)