Skip to content

Commit 6b83a81

Browse files
committed
Fix authorization headers for basic auth
Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent 24cce3b commit 6b83a81

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

mcpgateway/admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ async def admin_add_tool(
545545
"""
546546
logger.debug(f"User {user} is adding a new tool")
547547
form = await request.form()
548-
logger.info(f"Received form data: {dict(form)}")
548+
logger.debug(f"Received form data: {dict(form)}")
549549

550550
tool_data = {
551551
"name": form["name"],
@@ -563,10 +563,10 @@ async def admin_add_tool(
563563
"auth_header_key": form.get("auth_header_key", ""),
564564
"auth_header_value": form.get("auth_header_value", ""),
565565
}
566-
logger.info(f"Tool data built: {tool_data}")
566+
logger.debug(f"Tool data built: {tool_data}")
567567
try:
568568
tool = ToolCreate(**tool_data)
569-
logger.info(f"Validated tool data: {tool.dict()}")
569+
logger.debug(f"Validated tool data: {tool.dict()}")
570570
await tool_service.register_tool(db, tool)
571571
return JSONResponse(
572572
content={"message": "Tool registered successfully!", "success": True},

mcpgateway/schemas.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222
import json
23+
import base64
2324
import logging
2425
from datetime import datetime
2526
from typing import Any, Dict, List, Literal, Optional, Union
@@ -318,7 +319,8 @@ def assemble_auth(cls, values: Dict[str, Any]) -> Dict[str, Any]:
318319
auth_type = values.get("auth_type")
319320
if auth_type:
320321
if auth_type.lower() == "basic":
321-
encoded_auth = encode_auth({"username": values.get("auth_username", ""), "password": values.get("auth_password", "")})
322+
creds = base64.b64encode(f'{values.get("auth_username", "")}:{values.get("auth_password", "")}'.encode("utf-8")).decode()
323+
encoded_auth = encode_auth({"Authorization": f"Basic {creds}"})
322324
values["auth"] = {"auth_type": "basic", "auth_value": encoded_auth}
323325
elif auth_type.lower() == "bearer":
324326
encoded_auth = encode_auth({"Authorization": f"Bearer {values.get('auth_token', '')}"})
@@ -376,7 +378,8 @@ def assemble_auth(cls, values: Dict[str, Any]) -> Dict[str, Any]:
376378
auth_type = values.get("auth_type")
377379
if auth_type:
378380
if auth_type.lower() == "basic":
379-
encoded_auth = encode_auth({"username": values.get("auth_username", ""), "password": values.get("auth_password", "")})
381+
creds = base64.b64encode(f'{values.get("auth_username", "")}:{values.get("auth_password", "")}'.encode("utf-8")).decode()
382+
encoded_auth = encode_auth({"Authorization": f"Basic {creds}"})
380383
values["auth"] = {"auth_type": "basic", "auth_value": encoded_auth}
381384
elif auth_type.lower() == "bearer":
382385
encoded_auth = encode_auth({"Authorization": f"Bearer {values.get('auth_token', '')}"})
@@ -715,7 +718,8 @@ def _process_auth_fields(values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
715718
if not username or not password:
716719
raise ValueError("For 'basic' auth, both 'auth_username' and 'auth_password' must be provided.")
717720

718-
return encode_auth({"username": username, "password": password})
721+
creds = base64.b64encode(f'{username}:{password}'.encode("utf-8")).decode()
722+
return encode_auth({"Authorization": f"Basic {creds}"})
719723

720724
if auth_type == "bearer":
721725
# For bearer authentication, only token is required
@@ -824,7 +828,8 @@ def _process_auth_fields(values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
824828
if not username or not password:
825829
raise ValueError("For 'basic' auth, both 'auth_username' and 'auth_password' must be provided.")
826830

827-
return encode_auth({"username": username, "password": password})
831+
creds = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode()
832+
return encode_auth({"Authorization": f"Basic {creds}"})
828833

829834
if auth_type == "bearer":
830835
# For bearer authentication, only token is required

mcpgateway/services/tool_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616

1717
import asyncio
18+
import base64
1819
import json
1920
import logging
2021
import time
@@ -125,10 +126,12 @@ def _convert_tool_to_read(self, tool: DbTool) -> ToolRead:
125126

126127
decoded_auth_value = decode_auth(tool.auth_value)
127128
if tool.auth_type == "basic":
129+
decoded_bytes = base64.b64decode(decoded_auth_value["Authorization"].split("Basic ")[1])
130+
username, password = decoded_bytes.decode("utf-8").split(":")
128131
tool_dict["auth"] = {
129132
"auth_type": "basic",
130-
"username": decoded_auth_value["username"],
131-
"password": "********" if decoded_auth_value["password"] else None,
133+
"username": username,
134+
"password": "********" if password else None,
132135
}
133136
elif tool.auth_type == "bearer":
134137
tool_dict["auth"] = {

0 commit comments

Comments
 (0)