Skip to content

Commit 1e5e8d0

Browse files
committed
Move auth URL to machine configuration for now so it can be used with existing server setup
1 parent ec4286f commit 1e5e8d0

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/murfey/server/api/auth.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
import secrets
45
import time
56
from logging import getLogger
@@ -18,7 +19,7 @@
1819

1920
from murfey.server import sanitise
2021
from murfey.server.murfey_db import murfey_db, url
21-
from murfey.util.config import get_security_config
22+
from murfey.util.config import get_machine_config, get_security_config
2223
from murfey.util.db import MurfeyUser as User
2324
from murfey.util.db import Session as MurfeySession
2425

@@ -63,6 +64,12 @@ async def __call__(self, request: Request):
6364

6465
# Set up variables used for authentication
6566
security_config = get_security_config()
67+
machine_config = get_machine_config()
68+
auth_url = (
69+
machine_config[os.getenv("BEAMLINE", "")].auth_url
70+
if machine_config.get(os.getenv("BEAMLINE", ""))
71+
else ""
72+
)
6673
ALGORITHM = security_config.auth_algorithm or "HS256"
6774
SECRET_KEY = security_config.auth_key or secrets.token_hex(32)
6875
if security_config.auth_type == "password":
@@ -156,7 +163,7 @@ def password_token_validation(token: str):
156163

157164
async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
158165
try:
159-
if security_config.auth_url:
166+
if auth_url:
160167
headers = (
161168
{}
162169
if security_config.auth_type == "cookie"
@@ -169,7 +176,7 @@ async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
169176
)
170177
async with aiohttp.ClientSession(cookies=cookies) as session:
171178
async with session.get(
172-
f"{security_config.auth_url}/validate_token",
179+
f"{auth_url}/validate_token",
173180
headers=headers,
174181
) as response:
175182
success = response.status == 200
@@ -218,13 +225,13 @@ class Token(BaseModel):
218225

219226

220227
def create_access_token(data: dict, token: str = "") -> str:
221-
if security_config.auth_url and data.get("session"):
228+
if auth_url and data.get("session"):
222229
session_id = data["session"]
223230
if not isinstance(session_id, int) and session_id > 0:
224231
# check the session ID is alphanumeric for security
225232
raise ValueError("Session ID was invalid (not alphanumeric)")
226233
minted_token_response = requests.get(
227-
f"{security_config.auth_url}/sessions/{sanitise(str(session_id))}/token",
234+
f"{auth_url}/sessions/{sanitise(str(session_id))}/token",
228235
headers={"Authorization": f"Bearer {token}"},
229236
)
230237
if minted_token_response.status_code != 200:
@@ -250,13 +257,13 @@ def create_access_token(data: dict, token: str = "") -> str:
250257
async def generate_token(
251258
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
252259
) -> Token:
253-
if security_config.auth_url:
260+
if auth_url:
254261
data = aiohttp.FormData()
255262
data.add_field("username", form_data.username)
256263
data.add_field("password", form_data.password)
257264
async with aiohttp.ClientSession() as session:
258265
async with session.post(
259-
f"{security_config.auth_url}/token",
266+
f"{auth_url}/token",
260267
data=data,
261268
) as response:
262269
validated = response.status == 200
@@ -270,7 +277,7 @@ async def generate_token(
270277
detail="Incorrect username or password",
271278
headers={"WWW-Authenticate": "Bearer"},
272279
)
273-
if not security_config.auth_url:
280+
if not auth_url:
274281
access_token = create_access_token(
275282
data={"user": form_data.username},
276283
)

src/murfey/util/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class MachineConfig(BaseModel):
6565
murfey_url: str = "http://localhost:8000"
6666

6767
security_configuration_path: Optional[Path] = None
68+
auth_url: str = ""
6869

6970

7071
def from_file(config_file_path: Path, instrument: str = "") -> Dict[str, MachineConfig]:
@@ -85,7 +86,6 @@ class Security(BaseModel):
8586
sqlalchemy_pooling: bool = True
8687
allow_origins: List[str] = ["*"]
8788
session_validation: str = ""
88-
auth_url: str = ""
8989
session_token_timeout: Optional[int] = None
9090
auth_type: Literal["password", "cookie"] = "password"
9191
cookie_key: str = ""

0 commit comments

Comments
 (0)