Skip to content

Commit f36d95a

Browse files
authored
fix(backend): Avoid falling back to default user unless ENABLED_AUTH is set to False (Significant-Gravitas#8691)
1 parent a660833 commit f36d95a

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed

autogpt_platform/autogpt_libs/autogpt_libs/auth/depends.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fastapi
22

33
from .middleware import auth_middleware
4-
from .models import User
4+
from .models import User, DEFAULT_USER_ID, DEFAULT_EMAIL
5+
from .config import Settings
56

67

78
def requires_user(payload: dict = fastapi.Depends(auth_middleware)) -> User:
@@ -16,8 +17,12 @@ def requires_admin_user(
1617

1718
def verify_user(payload: dict | None, admin_only: bool) -> User:
1819
if not payload:
20+
if Settings.ENABLE_AUTH:
21+
raise fastapi.HTTPException(
22+
status_code=401, detail="Authorization header is missing"
23+
)
1924
# This handles the case when authentication is disabled
20-
payload = {"sub": "3e53486c-cf57-477e-ba2a-cb02dc828e1a", "role": "admin"}
25+
payload = {"sub": DEFAULT_USER_ID, "role": "admin"}
2126

2227
user_id = payload.get("sub")
2328

autogpt_platform/autogpt_libs/autogpt_libs/auth/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from dataclasses import dataclass
22

3+
DEFAULT_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
4+
DEFAULT_EMAIL = "default@example.com"
5+
36

47
# Using dataclass here to avoid adding dependency on pydantic
58
@dataclass(frozen=True)

autogpt_platform/backend/backend/data/user.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from typing import Optional, cast
33

4+
from autogpt_libs.auth.models import DEFAULT_USER_ID
45
from autogpt_libs.supabase_integration_credentials_store.types import (
56
UserIntegrations,
67
UserMetadata,
@@ -15,9 +16,6 @@
1516

1617
logger = logging.getLogger(__name__)
1718

18-
DEFAULT_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
19-
DEFAULT_EMAIL = "default@example.com"
20-
2119

2220
async def get_or_create_user(user_data: dict) -> User:
2321
user_id = user_data.get("sub")
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
from autogpt_libs.auth.middleware import auth_middleware
2-
from fastapi import Depends, HTTPException
1+
from autogpt_libs.auth.depends import requires_user
2+
from autogpt_libs.auth.models import User
3+
from fastapi import Depends
34

4-
from backend.data.user import DEFAULT_USER_ID
55
from backend.util.settings import Settings
66

77
settings = Settings()
88

99

10-
def get_user_id(payload: dict = Depends(auth_middleware)) -> str:
11-
if not payload:
12-
# This handles the case when authentication is disabled
13-
return DEFAULT_USER_ID
14-
15-
user_id = payload.get("sub")
16-
if not user_id:
17-
raise HTTPException(status_code=401, detail="User ID not found in token")
18-
return user_id
10+
def get_user_id(user: User = Depends(requires_user)) -> str:
11+
return user.user_id

autogpt_platform/backend/backend/server/ws_api.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,24 @@ async def event_broadcaster(manager: ConnectionManager):
5353

5454

5555
async def authenticate_websocket(websocket: WebSocket) -> str:
56-
if settings.config.enable_auth:
57-
token = websocket.query_params.get("token")
58-
if not token:
59-
await websocket.close(code=4001, reason="Missing authentication token")
60-
return ""
56+
if not settings.config.enable_auth:
57+
return DEFAULT_USER_ID
58+
59+
token = websocket.query_params.get("token")
60+
if not token:
61+
await websocket.close(code=4001, reason="Missing authentication token")
62+
return ""
6163

62-
try:
63-
payload = parse_jwt_token(token)
64-
user_id = payload.get("sub")
65-
if not user_id:
66-
await websocket.close(code=4002, reason="Invalid token")
67-
return ""
68-
return user_id
69-
except ValueError:
70-
await websocket.close(code=4003, reason="Invalid token")
64+
try:
65+
payload = parse_jwt_token(token)
66+
user_id = payload.get("sub")
67+
if not user_id:
68+
await websocket.close(code=4002, reason="Invalid token")
7169
return ""
72-
else:
73-
return DEFAULT_USER_ID
70+
return user_id
71+
except ValueError:
72+
await websocket.close(code=4003, reason="Invalid token")
73+
return ""
7474

7575

7676
async def handle_subscribe(

0 commit comments

Comments
 (0)