-
Notifications
You must be signed in to change notification settings - Fork 23.6k
feat(core): add gateway OAuth bearer authentication #38805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John Kennedy (jkennedyvz)
wants to merge
4
commits into
cc/gateway-base-url
Choose a base branch
from
john/dcode-add-oauth-bearer-auth-mode-for-langsmith-gateway
base: cc/gateway-base-url
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
82001a8
feat(core): add gateway OAuth bearer authentication
jkennedyvz 8a871cb
fix(core): address gateway OAuth review feedback
jkennedyvz 44a6633
fix(openai): apply OAuth auth to proxy clients
jkennedyvz f230774
fix(core): update gateway OAuth test expectations
jkennedyvz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,10 @@ celerybeat.pid | |
| # Environments | ||
| .env | ||
| .envrc | ||
| *.pem | ||
| *.key | ||
| *.crt | ||
| credentials.json | ||
| .venv* | ||
| venv* | ||
| env/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """Authentication helpers for LangSmith Gateway clients.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import httpx | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import AsyncGenerator, Generator | ||
|
|
||
|
|
||
| class LangSmithGatewayOAuth(httpx.Auth): | ||
| """Apply a LangSmith OAuth token to requests sent through Gateway. | ||
|
|
||
| Gateway removes this credential before forwarding the request to the selected | ||
| provider. The adapter therefore removes provider credential headers before | ||
| setting the Gateway-specific bearer token. | ||
|
|
||
| Args: | ||
| token: LangSmith OAuth access token. | ||
| """ | ||
|
|
||
| def __init__(self, token: str) -> None: | ||
| """Initialize the auth adapter with a LangSmith OAuth access token.""" | ||
| self._token = token | ||
|
|
||
| def _authorize(self, request: httpx.Request) -> httpx.Request: | ||
| request.headers.pop("Authorization", None) | ||
| request.headers.pop("X-Api-Key", None) | ||
| request.headers["Authorization"] = f"Bearer {self._token}" | ||
| return request | ||
|
|
||
| def auth_flow( | ||
| self, request: httpx.Request | ||
| ) -> Generator[httpx.Request, httpx.Response, None]: | ||
| """Add the bearer token to a synchronous request.""" | ||
| yield self._authorize(request) | ||
|
|
||
| async def async_auth_flow( | ||
| self, request: httpx.Request | ||
| ) -> AsyncGenerator[httpx.Request, httpx.Response]: | ||
| """Add the bearer token to an asynchronous request.""" | ||
| yield self._authorize(request) |
21 changes: 21 additions & 0 deletions
21
libs/core/tests/unit_tests/utils/test_langsmith_gateway.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import httpx | ||
|
|
||
| from langchain_core.utils.langsmith_gateway import LangSmithGatewayOAuth | ||
|
|
||
|
|
||
| def test_gateway_oauth_replaces_provider_auth_headers() -> None: | ||
| """Gateway OAuth must not be forwarded as a provider credential.""" | ||
| auth = LangSmithGatewayOAuth("oauth-token") | ||
| request = httpx.Request( | ||
| "POST", | ||
| "https://gateway.smith.langchain.com/anthropic/v1/messages", | ||
| headers={ | ||
| "Authorization": "Bearer provider-key", | ||
| "X-Api-Key": "provider-key", | ||
| }, | ||
| ) | ||
|
|
||
| authorized_request = next(auth.auth_flow(request)) | ||
|
|
||
| assert authorized_request.headers["Authorization"] == "Bearer oauth-token" | ||
| assert "X-Api-Key" not in authorized_request.headers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Scope OAuth token to gateway requests
The environment token is loaded unconditionally, even when
LANGSMITH_GATEWAYis unset/false and this client resolves tohttps://api.anthropic.com. In that normal direct-provider configuration,_gateway_oauth_authreplaces the provider credential with the LangSmith bearer token, disclosing the OAuth credential to Anthropic and causing authentication to fail. The same unconditional default exists inChatFireworksandBaseChatOpenAI; Fireworks sends it to its direct endpoint, while OpenAI sends it whenever a directbase_urlis supplied (and otherwise now crashes constructing an HTTPX client withbase_url=None). Please only activate this credential when the resolved endpoint is actually LangSmith Gateway.(Refers to lines 1004-1007)
Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.