Skip to content

Commit 2428b0d

Browse files
committed
5511: Change API-KEY permission to allow admin users only
1 parent 8920bf2 commit 2428b0d

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

backend/open_webui/routers/auths.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,7 @@ async def update_ldap_config(
10351035

10361036
# create api key
10371037
@router.post("/api_key", response_model=ApiKey)
1038-
async def generate_api_key(request: Request, user=Depends(get_current_user)):
1039-
if not request.app.state.config.ENABLE_API_KEY:
1040-
raise HTTPException(
1041-
status.HTTP_403_FORBIDDEN,
1042-
detail=ERROR_MESSAGES.API_KEY_CREATION_NOT_ALLOWED,
1043-
)
1044-
1038+
async def generate_api_key(request: Request, user=Depends(get_admin_user)):
10451039
api_key = create_api_key()
10461040
success = Users.update_user_api_key_by_id(user.id, api_key)
10471041

@@ -1055,14 +1049,14 @@ async def generate_api_key(request: Request, user=Depends(get_current_user)):
10551049

10561050
# delete api key
10571051
@router.delete("/api_key", response_model=bool)
1058-
async def delete_api_key(user=Depends(get_current_user)):
1052+
async def delete_api_key(user=Depends(get_admin_user)):
10591053
success = Users.update_user_api_key_by_id(user.id, None)
10601054
return success
10611055

10621056

10631057
# get api key
10641058
@router.get("/api_key", response_model=ApiKey)
1065-
async def get_api_key(user=Depends(get_current_user)):
1059+
async def get_api_key(user=Depends(get_admin_user)):
10661060
api_key = Users.get_user_api_key_by_id(user.id)
10671061
if api_key:
10681062
return {

backend/open_webui/utils/auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ def get_current_user(
225225

226226
# auth by api key
227227
if token.startswith("sk-"):
228-
if not request.state.enable_api_key:
228+
# Load user to check for the admin role below
229+
user = get_current_user_by_api_key(token)
230+
231+
if user.role != "admin" and not request.state.enable_api_key:
229232
raise HTTPException(
230233
status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
231234
)
@@ -248,8 +251,6 @@ def get_current_user(
248251
status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
249252
)
250253

251-
user = get_current_user_by_api_key(token)
252-
253254
# Add user info to current span
254255
current_span = trace.get_current_span()
255256
if current_span:

0 commit comments

Comments
 (0)