Skip to content

Commit 35988da

Browse files
authored
Forward the request headers from the frontend to the auth server (#728)
Forwards the headers the backend server receives from the frontend to the authentication server. This provides the authentication server with the information it needs to validate sessions locally without needing to query the authentication endpoint every time.
1 parent 63d5c4b commit 35988da

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/murfey/server/api/auth.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import aiohttp
1010
import requests
11-
from fastapi import APIRouter, Depends, HTTPException, status
11+
from fastapi import APIRouter, Depends, HTTPException, Request, status
1212
from fastapi.security import (
1313
APIKeyCookie,
1414
OAuth2PasswordBearer,
@@ -84,18 +84,22 @@ def check_user(username: str) -> bool:
8484
return username in [u.username for u in users]
8585

8686

87-
async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
87+
async def validate_token(
88+
token: Annotated[str, Depends(oauth2_scheme)],
89+
request: Request,
90+
):
8891
"""
8992
Used by the backend routers to validate requests coming in from frontend.
9093
"""
9194
try:
9295
# Validate using auth URL if provided; will error if invalid
9396
if auth_url:
94-
headers = (
95-
{}
96-
if security_config.auth_type == "cookie"
97-
else {"Authorization": f"Bearer {token}"}
98-
)
97+
# Extract and forward headers as-is
98+
headers = dict(request.headers)
99+
# Update/add authorization header if authenticating using password
100+
if security_config.auth_type == "password":
101+
headers["authorization"] = f"Bearer {token}"
102+
# Forward the cookie along if authenticating using cookie
99103
cookies = (
100104
{security_config.cookie_key: token}
101105
if security_config.auth_type == "cookie"

0 commit comments

Comments
 (0)