File tree Expand file tree Collapse file tree 5 files changed +32
-33
lines changed
autogpt_libs/autogpt_libs/auth Expand file tree Collapse file tree 5 files changed +32
-33
lines changed Original file line number Diff line number Diff line change 1
1
import fastapi
2
2
3
3
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
5
6
6
7
7
8
def requires_user (payload : dict = fastapi .Depends (auth_middleware )) -> User :
@@ -16,8 +17,12 @@ def requires_admin_user(
16
17
17
18
def verify_user (payload : dict | None , admin_only : bool ) -> User :
18
19
if not payload :
20
+ if Settings .ENABLE_AUTH :
21
+ raise fastapi .HTTPException (
22
+ status_code = 401 , detail = "Authorization header is missing"
23
+ )
19
24
# 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" }
21
26
22
27
user_id = payload .get ("sub" )
23
28
Original file line number Diff line number Diff line change 1
1
from dataclasses import dataclass
2
2
3
+ DEFAULT_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
4
+ DEFAULT_EMAIL = "default@example.com"
5
+
3
6
4
7
# Using dataclass here to avoid adding dependency on pydantic
5
8
@dataclass (frozen = True )
Original file line number Diff line number Diff line change 1
1
import logging
2
2
from typing import Optional , cast
3
3
4
+ from autogpt_libs .auth .models import DEFAULT_USER_ID
4
5
from autogpt_libs .supabase_integration_credentials_store .types import (
5
6
UserIntegrations ,
6
7
UserMetadata ,
15
16
16
17
logger = logging .getLogger (__name__ )
17
18
18
- DEFAULT_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
19
- DEFAULT_EMAIL = "default@example.com"
20
-
21
19
22
20
async def get_or_create_user (user_data : dict ) -> User :
23
21
user_id = user_data .get ("sub" )
Original file line number Diff line number Diff line change 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
3
4
4
- from backend .data .user import DEFAULT_USER_ID
5
5
from backend .util .settings import Settings
6
6
7
7
settings = Settings ()
8
8
9
9
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
Original file line number Diff line number Diff line change @@ -53,24 +53,24 @@ async def event_broadcaster(manager: ConnectionManager):
53
53
54
54
55
55
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 ""
61
63
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" )
71
69
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 ""
74
74
75
75
76
76
async def handle_subscribe (
You can’t perform that action at this time.
0 commit comments