Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "north-mcp-python-sdk"
version = "0.4.2"
version = "0.4.3"
description = "Add your description here"
readme = "README.md"
authors = [{ name = "Raphael Cristal", email = "raphael@cohere.com" }]
Expand Down
17 changes: 11 additions & 6 deletions src/north_mcp_python_sdk/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def _has_x_north_headers(self, conn: HTTPConnection) -> bool:
for header in [
"X-North-ID-Token",
"X-North-Connector-Tokens",
"X-North-User-Email",
]
)

Expand Down Expand Up @@ -341,24 +342,26 @@ def _create_authenticated_user(
)

async def _authenticate_x_north_headers(
self, conn: HTTPConnection
self, conn: HTTPConnection, *, require_id_token: bool = True
) -> tuple[AuthCredentials, BaseUser]:
"""Authenticate using new X-North headers."""
self.logger.debug("Using X-North headers for authentication")

user_id_token = conn.headers.get("X-North-ID-Token")
user_email_header = conn.headers.get("X-North-User-Email")

if not user_id_token:
self.logger.debug("No X-North-ID-Token header present")
raise AuthenticationError("no authentication headers present")

token_email = self._process_user_id_token(user_id_token)
if require_id_token:
raise AuthenticationError("no authentication headers present")
token_email = None
else:
token_email = self._process_user_id_token(user_id_token)

self.logger.debug("X-North authentication successful")

# Additional Headers
connector_tokens_header = conn.headers.get("X-North-Connector-Tokens")
user_email_header = conn.headers.get("X-North-User-Email")

# Parse connector tokens (Base64 URL-safe encoded JSON)
connector_access_tokens = {}
Expand Down Expand Up @@ -455,7 +458,9 @@ async def authenticate(
self.logger.debug(
"No auth configured, but X-North headers are present; parsing request context without enforcing authentication"
)
return await self._authenticate_x_north_headers(conn)
return await self._authenticate_x_north_headers(
conn, require_id_token=False
)

if conn.headers.get("Authorization"):
self.logger.debug(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_x_north_auth_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ async def test_x_north_user_email_header_fallback():
assert user.access_token.claims["email"] == "fallback@company.com"


@pytest.mark.asyncio
async def test_x_north_connector_tokens_without_id_token_open_auth():
"""Test connector tokens can provide context without an ID token in open auth mode."""
backend = NorthAuthBackend()

connector_tokens_json = json.dumps({"github": "token123"})
connector_tokens_b64 = (
base64.urlsafe_b64encode(connector_tokens_json.encode())
.decode()
.rstrip("=")
)
headers = {
"X-North-Connector-Tokens": connector_tokens_b64,
"X-North-User-Email": "fallback@company.com",
}
conn = create_mock_connection(headers)

auth_response = await backend.authenticate(conn)
if auth_response is None:
raise ValueError("Authentication response is None")
_, user = auth_response

assert isinstance(user, AuthenticatedUser)
assert user.access_token.token == ""
assert user.access_token.claims["email"] == "fallback@company.com"
assert user.access_token.claims["connector_access_tokens"] == {
"github": "token123"
}


@pytest.mark.asyncio
async def test_x_north_user_email_header_not_override_token_email():
"""Test that ID token email takes precedence over X-North-User-Email."""
Expand Down
24 changes: 24 additions & 0 deletions tests/test_x_north_trusted_issuers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ async def test_trusted_issuers_require_auth_headers():
await backend.authenticate(conn)


@pytest.mark.asyncio
async def test_trusted_issuers_reject_connector_tokens_without_id_token():
backend = NorthAuthBackend(
trusted_issuers=["https://example.okta.com"],
)
connector_tokens_json = json.dumps({"github": "token123"})
connector_tokens_b64 = (
base64.urlsafe_b64encode(connector_tokens_json.encode())
.decode()
.rstrip("=")
)
conn = create_mock_connection(
{
"X-North-Connector-Tokens": connector_tokens_b64,
"X-North-User-Email": "fallback@company.com",
}
)

with pytest.raises(
AuthenticationError, match="no authentication headers present"
):
await backend.authenticate(conn)


@pytest.mark.asyncio
async def test_x_north_headers_trusted_issuers_missing_issuer():
"""Test X-North headers reject tokens missing issuer when trusted issuers configured."""
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading