Skip to content

Commit 0143aa0

Browse files
authored
Fix _auth_populate in GatewayRead (#473)
* Fix GatewayRead._populate_auth Signed-off-by: Madhav Kandukuri <[email protected]> * Fix doctest for _populate_auth Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent 48d944a commit 0143aa0

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

mcpgateway/schemas.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import json
2626
import logging
2727
import re
28-
from typing import Any, Dict, List, Literal, Optional, Union
28+
from typing import Any, Dict, List, Literal, Optional, Union, Self
2929

3030
# Third-Party
3131
from pydantic import AnyHttpUrl, BaseModel, ConfigDict, Field, field_serializer, field_validator, model_validator, ValidationInfo
@@ -1518,9 +1518,9 @@ class GatewayRead(BaseModelWithConfigDict):
15181518
slug: str = Field(None, description="Slug for gateway endpoint URL")
15191519

15201520
# This will be the main method to automatically populate fields
1521-
@classmethod
15221521
@model_validator(mode="after")
1523-
def _populate_auth(cls, values: Dict[str, Any]) -> Dict[str, Any]:
1522+
@classmethod
1523+
def _populate_auth(cls, values: Self) -> Dict[str, Any]:
15241524
"""Populate authentication fields based on auth_type and encoded auth_value.
15251525
15261526
This post-validation method decodes the stored authentication value and
@@ -1552,28 +1552,33 @@ def _populate_auth(cls, values: Dict[str, Any]) -> Dict[str, Any]:
15521552
15531553
Examples:
15541554
>>> # Basic auth example
1555-
>>> values = GatewayRead._populate_auth({
1556-
... 'auth_type': 'basic',
1557-
... 'auth_value': encode_auth({'username': 'admin', 'password': 'secret'})
1558-
... })
1555+
>>> string_bytes = "admin:secret".encode("utf-8")
1556+
>>> encoded_auth = base64.urlsafe_b64encode(string_bytes).decode("utf-8")
1557+
>>> values = GatewayRead.model_construct(
1558+
... auth_type="basic",
1559+
... auth_value=encode_auth({"Authorization": f"Basic {encoded_auth}"})
1560+
... )
1561+
>>> values = GatewayRead._populate_auth(values)
15591562
>>> values.auth_username
15601563
'admin'
15611564
>>> values.auth_password
15621565
'secret'
15631566
15641567
>>> # Bearer auth example
1565-
>>> values = GatewayRead._populate_auth({
1566-
... 'auth_type': 'bearer',
1567-
... 'auth_value': encode_auth({'Authorization': 'Bearer mytoken123'})
1568-
... })
1568+
>>> values = GatewayRead.model_construct(
1569+
... auth_type="bearer",
1570+
... auth_value=encode_auth({"Authorization": "Bearer mytoken123"})
1571+
... )
1572+
>>> values = GatewayRead._populate_auth(values)
15691573
>>> values.auth_token
15701574
'mytoken123'
15711575
15721576
>>> # Custom headers example
1573-
>>> values = GatewayRead._populate_auth({
1574-
... 'auth_type': 'authheaders',
1575-
... 'auth_value': encode_auth({'X-API-Key': 'abc123'})
1576-
... })
1577+
>>> values = GatewayRead.model_construct(
1578+
... auth_type='authheaders',
1579+
... auth_value=encode_auth({"X-API-Key": "abc123"})
1580+
... )
1581+
>>> values = GatewayRead._populate_auth(values)
15771582
>>> values.auth_header_key
15781583
'X-API-Key'
15791584
>>> values.auth_header_value
@@ -1583,8 +1588,9 @@ def _populate_auth(cls, values: Dict[str, Any]) -> Dict[str, Any]:
15831588
auth_value_encoded = values.auth_value
15841589
auth_value = decode_auth(auth_value_encoded)
15851590
if auth_type == "basic":
1586-
u = auth_value.get("username")
1587-
p = auth_value.get("password")
1591+
auth = auth_value.get("Authorization")
1592+
auth = auth.removeprefix("Basic ")
1593+
u, p = base64.urlsafe_b64decode(auth).decode("utf-8").split(":")
15881594
if not u or not p:
15891595
raise ValueError("basic auth requires both username and password")
15901596
values.auth_username, values.auth_password = u, p

0 commit comments

Comments
 (0)