Skip to content

Commit 55f1f69

Browse files
committed
5511: Change API-KEY permission to allow admin users only
1 parent 9ae06a3 commit 55f1f69

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
@@ -228,7 +228,10 @@ def get_current_user(
228228

229229
# auth by api key
230230
if token.startswith("sk-"):
231-
if not request.state.enable_api_key:
231+
# Load user to check for the admin role below
232+
user = get_current_user_by_api_key(token)
233+
234+
if user.role != "admin" and not request.state.enable_api_key:
232235
raise HTTPException(
233236
status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
234237
)
@@ -251,8 +254,6 @@ def get_current_user(
251254
status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.API_KEY_NOT_ALLOWED
252255
)
253256

254-
user = get_current_user_by_api_key(token)
255-
256257
# Add user info to current span
257258
current_span = trace.get_current_span()
258259
if current_span:

0 commit comments

Comments
 (0)