diff --git a/litellm/caching/redis_cache.py b/litellm/caching/redis_cache.py
index 29e390b1d9a..a28793e03f6 100644
--- a/litellm/caching/redis_cache.py
+++ b/litellm/caching/redis_cache.py
@@ -787,6 +787,9 @@ class RedisCache(BaseCache):
def _async_commands(self) -> _AsyncRedisCommands:
return self.init_async_client()
+ async def async_eval(self, script: str, numkeys: int, *keys_and_args: str | bytes | float) -> object:
+ return await self._async_commands().eval(script, numkeys, *keys_and_args)
+
def check_and_fix_namespace(self, key: str) -> str:
"""
Make sure each key starts with the given namespace
diff --git a/litellm/proxy/README.md b/litellm/proxy/README.md
index 900dea5ea29..9d76898ea5f 100644
--- a/litellm/proxy/README.md
+++ b/litellm/proxy/README.md
@@ -31,6 +31,36 @@ print(response)
[**See how to call Huggingface,Bedrock,TogetherAI,Anthropic, etc.**](https://docs.litellm.ai/docs/simple_proxy)
+## Hosted applications using gateway SSO
+
+A trusted web application can request access through the gateway's configured SSO provider. Proxy-API OAuth grants accept loopback redirects by default. Hosted access requires shared Redis and an exact callback URI configured on every gateway replica:
+
+```shell
+LITELLM_PROXY_API_OAUTH_REDIRECT_URIS=https://reports.example.com/oauth/callback
+LITELLM_PROXY_API_OAUTH_ADMIN_REDIRECT_URIS=https://admin.example.com/oauth/callback
+```
+
+Multiple URIs are comma-separated. Each hosted URI must use HTTPS and contain no userinfo, query string, or fragment. Matching includes the complete path and port; wildcards and subdomain matching are not supported. Only register callbacks operated by trusted applications
+
+The application registers the same URI at `/register` and uses `/authorize` with `resource` set to the gateway base URL, S256 PKCE, and state. LiteLLM handles SSO and presents an approval page with team selection before returning an authorization code. The application exchanges the code at `/token` using the original verifier and callback URI. Applications must validate state and bind the callback to the browser that started sign-in
+
+The default `proxy:read` scope permits only GET requests to `/models`, `/v1/models`, `/global/activity`, `/global/activity/model`, `/global/spend`, `/global/spend/provider`, and `/global/spend/report`. Global reports also require the user's current proxy administrator or administrator viewer role. The selected team records attribution and requires current membership; it does not narrow an administrator's global reporting permissions. Credential/configuration endpoints, request contents, writes, and LLM calls are denied
+
+Applications needing administrative access must explicitly request `scope=proxy:admin`. Only callbacks in `LITELLM_PROXY_API_OAUTH_ADMIN_REDIRECT_URIS` can receive it, and the consenting user must remain a full `proxy_admin`. This scope permits gateway management, usage, and model-inference APIs through their existing authorization checks. Authentication, credential delegation, vault, passthrough, and master-key-only routes are excluded. The consent screen lists these powers and model charges. Scope is fixed at consent and cannot increase on refresh
+
+Discover support using `/.well-known/litellm-cli-auth`. Its `hosted_app` object advertises enabled scopes and token lifetimes; clients should check that contract instead of a gateway version or image tag. Configure the gateway URL and trusted callback once, then use normal gateway releases
+
+Hosted access tokens expire after five minutes. Each user/application grant expires 24 hours after consent, including all refreshes. Refresh rotates both tokens; clients must serialize refresh requests and replace their saved pair. Already-issued access tokens remain valid until their original expiry so admitted work can finish. Do not retry an uncertain refresh; reconnect instead. A grant permits at most 512 refreshes, bounding replay-detection storage. Replaying a spent refresh token revokes the entire grant, including current API access. Send the current or spent refresh token, or an issued access token, and its `client_id` to `POST /revoke` when disconnecting. Demoting an administrative grant holder, deleting a user, deactivating them, removing their selected team membership, or blocking that team also stops access
+
+Every authorized request reads the grant from shared Redis and the current user/team from the database. There is no local-cache or stale-permission fallback. Missing grants, Redis loss, database errors, and removed callback trust deny access. Callback configuration changes must reach every replica before removal is effective across the deployment. Grant records are separated by the gateway master key; rotating it invalidates hosted grants. Configure `PROXY_BASE_URL` consistently across replicas and use the same Redis namespace
+
+Hosted issuance, refresh, and API access are disabled whenever `custom_auth` is configured or `enable_oauth2_auth` or `enable_oauth2_proxy_auth` is enabled. A custom hook also disables hosted access when its enterprise mode is `off`. Arbitrary custom or external identity and permission rules cannot safely be combined with these grants. Revocation remains available, including with expired or rotated access tokens, so existing grants can still be disconnected. Enabling an exclusive auth mode denies existing hosted grants; disabling it can restore unexpired grants that were not revoked
+
+The hosted application's server must retain the PKCE verifier, verify OAuth state on return, exchange the code server-side, and keep both tokens out of browser storage. Give the browser a Secure, HttpOnly session cookie with CSRF protection. Its logout/disconnect handler must call `/revoke` before discarding the server session. The gateway never sends an access token in a redirect URL
+
+Native loopback CLI grants and MCP grants retain their existing permissions. Hosted clients cannot use the native IdP token-exchange grant. Refresh tokens from earlier builds that issued personal credentials to hosted callbacks require a new sign-in. Previously issued personal access credentials cannot be distinguished from CLI credentials and remain valid until their original expiry; rotate the gateway's credential encryption key if they must be invalidated immediately. Removing the image override alone does not revoke those old credentials
+
+
---
### Folder Structure
diff --git a/litellm/proxy/_experimental/mcp_server/bridge_token_flow.py b/litellm/proxy/_experimental/mcp_server/bridge_token_flow.py
index 77bdbd26b35..e80fb6fdca5 100644
--- a/litellm/proxy/_experimental/mcp_server/bridge_token_flow.py
+++ b/litellm/proxy/_experimental/mcp_server/bridge_token_flow.py
@@ -344,10 +344,10 @@ async def load_active_user_by_id(
return "no_active_key"
if user_object is None:
return "no_active_key"
- return _active_user_record(user_object)
+ return active_user_record(user_object)
-def _active_user_record(user_object: "LiteLLM_UserTable") -> "LiteLLM_UserTable | Literal['no_active_key']":
+def active_user_record(user_object: "LiteLLM_UserTable") -> "LiteLLM_UserTable | Literal['no_active_key']":
if isinstance(user_object.metadata, dict) and user_object.metadata.get("scim_active") is False:
return "no_active_key"
return user_object
@@ -514,7 +514,7 @@ async def _resolve_jwt_auth(
parent_otel_span=None,
proxy_logging_obj=proxy_logging_obj,
)
- if identity.user_object is not None and isinstance(_active_user_record(identity.user_object), str):
+ if identity.user_object is not None and isinstance(active_user_record(identity.user_object), str):
return None
return identity
authorized: Final = await JWTAuthManager.authorize_jwt(
@@ -531,7 +531,7 @@ async def _resolve_jwt_auth(
request_method=request.method,
)
resolved_user: Final = authorized["user_object"]
- if resolved_user is not None and isinstance(_active_user_record(resolved_user), str):
+ if resolved_user is not None and isinstance(active_user_record(resolved_user), str):
return None
return JWTAuthManager.user_api_key_auth_from_result(authorized)
except Exception as exc: # noqa: BLE001 # public OAuth exchange stays available; unvalidated identities never write credentials
diff --git a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
index 7a0f59c3c2b..87145e2ba0c 100644
--- a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
+++ b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
@@ -1974,6 +1974,7 @@ async def authorize(
response_type=response_type,
session_user_id=_session_cookie_user_id(request),
lookup_consent_teams=lookup_consent_teams,
+ scope=scope,
)
return aggregate_authorize(
request=request,
diff --git a/litellm/proxy/_experimental/mcp_server/gateway_dcr_flow.py b/litellm/proxy/_experimental/mcp_server/gateway_dcr_flow.py
index e66504af47a..cf311121b57 100644
--- a/litellm/proxy/_experimental/mcp_server/gateway_dcr_flow.py
+++ b/litellm/proxy/_experimental/mcp_server/gateway_dcr_flow.py
@@ -50,11 +50,28 @@ from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
from fastapi import HTTPException, Request
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, Response
-from pydantic import BaseModel, ConfigDict, Field, ValidationError
+from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError
from typing_extensions import NotRequired, ReadOnly, TypedDict, assert_never
from litellm._logging import verbose_logger
from litellm.caching.caching import DualCache
+from litellm.proxy._experimental.mcp_server.bridge_token_flow import load_active_user_by_id
+from litellm.proxy._experimental.mcp_server.hosted_proxy_auth import (
+ HOSTED_ACCESS_PREFIX,
+ HOSTED_ACCESS_TTL,
+ HOSTED_ADMIN_SCOPE,
+ HOSTED_GRANT_TTL,
+ HOSTED_REFRESH_PREFIX,
+ HOSTED_SCOPE,
+ HostedFailure,
+ HostedProxyAuth,
+ HostedScope,
+ HostedTokens,
+ hosted_proxy_auth,
+ hosted_redirect_is_allowed,
+ hosted_scope_allows_role,
+ hosted_supported_scopes,
+)
from litellm.proxy._experimental.mcp_server.oauth_utils import (
TOKEN_NO_CACHE_HEADERS,
canonical_resource_uri,
@@ -302,6 +319,7 @@ class _ConnectFlow(BaseModel):
exp: int
resource_server_id: str | None = None
audience: SessionAudience | None = None
+ hosted_scope: HostedScope | None = None
class _GatewayAuthCode(BaseModel):
@@ -321,6 +339,7 @@ class _GatewayAuthCode(BaseModel):
resource_server_id: str | None = None
audience: SessionAudience | None = None
team_id: str | None = None
+ hosted_scope: HostedScope | None = None
def is_gateway_dcr_client_id(client_id: str | None) -> bool:
@@ -401,13 +420,15 @@ async def register_aggregate_client(
"invalid_redirect_uri",
f"redirect_uris must be a list of 1 to {MAX_REDIRECT_URIS} URIs",
)
- if not all(isinstance(uri, str) and len(uri) <= MAX_REDIRECT_URI_LENGTH for uri in raw_uris):
+ candidates: Final = TypeAdapter(tuple[object, ...]).validate_python(raw_uris)
+ if not all(isinstance(uri, str) and len(uri) <= MAX_REDIRECT_URI_LENGTH for uri in candidates):
return _oauth_error(
400,
"invalid_redirect_uri",
f"each redirect URI must be a string of at most {MAX_REDIRECT_URI_LENGTH} characters",
)
- for uri in raw_uris:
+ redirect_uris: Final = tuple(uri for uri in candidates if isinstance(uri, str))
+ for uri in redirect_uris:
parsed = urlparse(uri)
try:
if validate_redirect_uri_shape(parsed):
@@ -425,7 +446,7 @@ async def register_aggregate_client(
)
now: Final = datetime.now(timezone.utc)
client_id: Final = _seal(
- GATEWAY_DCR_CLIENT_ID_PREFIX, GatewayDcrClient(redirect_uris=tuple(raw_uris), iat=int(now.timestamp()))
+ GATEWAY_DCR_CLIENT_ID_PREFIX, GatewayDcrClient(redirect_uris=redirect_uris, iat=int(now.timestamp()))
)
if len(client_id) > MAX_CLIENT_ID_LENGTH:
return _oauth_error(400, "invalid_client_metadata", "registered metadata is too large")
@@ -434,9 +455,11 @@ async def register_aggregate_client(
content={
"client_id": client_id,
"client_id_issued_at": int(now.timestamp()),
- "redirect_uris": list(raw_uris),
+ "redirect_uris": redirect_uris,
"token_endpoint_auth_method": "none",
- "grant_types": list(supported_grant_types(token_exchange_available)),
+ "grant_types": supported_grant_types(
+ token_exchange_available and all(is_loopback_redirect_host(urlparse(uri)) for uri in redirect_uris)
+ ),
"response_types": ["code"],
},
)
@@ -553,6 +576,16 @@ def aggregate_authorize(
return response
+def _proxy_redirect_is_allowed(
+ audience: SessionAudience | None, redirect_uri: str, hosted_scope: HostedScope | None = None
+) -> bool:
+ return (
+ audience != PROXY_API_AUDIENCE
+ or is_loopback_redirect_host(urlparse(redirect_uri))
+ or hosted_redirect_is_allowed(redirect_uri, hosted_scope or HOSTED_SCOPE)
+ )
+
+
async def native_client_authorize(
request: Request,
client_id: str,
@@ -563,24 +596,39 @@ async def native_client_authorize(
response_type: str | None,
session_user_id: str | None,
lookup_consent_teams: LookupConsentTeams,
+ scope: str | None = None,
) -> Response:
- """The authorize verb for a native client that named the proxy API itself as its
- RFC 8707 ``resource``: the same client, redirect, PKCE, and sign-in checks as the
- aggregate verb plus a loopback-only redirect (the credential this grant mints is the
- user's personal proxy key, which belongs on their own machine and never behind a hosted
- callback), then the consent page rendered right here (no connect-page interlude, since
- there is no per-server vaulting to do) with the flow sealed into the per-flow cookie
- and its handle carried only in the form, never in a URL."""
+ """Authorize a proxy-API credential for a native or explicitly trusted hosted client.
+
+ Hosted callbacks require an exact operator-configured HTTPS URI in addition to
+ client registration, S256 PKCE, SSO, and the user's deliberate consent.
+ """
rejected: Final = _rejected_authorize_request(
client_id, redirect_uri, state, code_challenge, code_challenge_method, response_type
)
if rejected is not None:
return rejected
- if not is_loopback_redirect_host(urlparse(redirect_uri)):
- return _oauth_error(400, "invalid_request", "a proxy-API grant may only redirect to a loopback address")
+ is_hosted: Final = not is_loopback_redirect_host(urlparse(redirect_uri))
+ if is_hosted and scope not in (None, HOSTED_SCOPE, HOSTED_ADMIN_SCOPE):
+ return _oauth_error(400, "invalid_scope", "the requested hosted application scope is not supported")
+ hosted_scope: Final[HostedScope | None] = (
+ (HOSTED_ADMIN_SCOPE if scope == HOSTED_ADMIN_SCOPE else HOSTED_SCOPE) if is_hosted else None
+ )
+ if is_hosted and not hosted_redirect_is_allowed(redirect_uri, hosted_scope or HOSTED_SCOPE):
+ return _oauth_error(
+ 400,
+ "invalid_request",
+ "a proxy-API grant may only redirect to a loopback address or an explicitly allowed HTTPS callback",
+ )
base_url: Final = get_request_base_url(request)
if session_user_id is None:
return _login_redirect(base_url, request)
+ if hosted_scope == HOSTED_ADMIN_SCOPE:
+ user: Final = await load_active_user_by_id(session_user_id, source="database")
+ if isinstance(user, str):
+ return _consent_lookup_failure_response(user)
+ if not hosted_scope_allows_role(hosted_scope, user.user_role):
+ return _oauth_error(403, "access_denied", "application administration requires a current proxy admin")
teams: Final = await lookup_consent_teams(session_user_id)
if not isinstance(teams, tuple):
return _consent_lookup_failure_response(teams)
@@ -593,6 +641,7 @@ async def native_client_authorize(
code_challenge=code_challenge or "",
resource_server_id=None,
audience=PROXY_API_AUDIENCE,
+ hosted_scope=hosted_scope,
)
page: Final = render_native_client_consent_page(
client_origin=_origin_only(redirect_uri),
@@ -600,6 +649,8 @@ async def native_client_authorize(
teams=tuple((team.team_id, team.team_alias or team.team_id) for team in teams),
flow_handle=handle,
complete_url=f"{base_url}/authorize/complete",
+ hosted=is_hosted,
+ hosted_admin=hosted_scope == HOSTED_ADMIN_SCOPE,
)
response: Final = HTMLResponse(page, headers=_CONSENT_PAGE_HEADERS)
_set_flow_cookie(response, request, handle, flow)
@@ -619,6 +670,12 @@ NATIVE_CLIENT_AUTH_CONTRACT_VERSION: Final = 1
Bump it only when an existing field changes meaning or goes away; adding fields is free."""
+class HostedAppAuthContract(TypedDict):
+ scopes_supported: ReadOnly[tuple[HostedScope, ...]]
+ access_token_ttl: ReadOnly[int]
+ refresh_token_ttl: ReadOnly[int]
+
+
class NativeClientAuthContract(TypedDict):
contract_version: ReadOnly[int]
issuer: ReadOnly[str]
@@ -632,6 +689,7 @@ class NativeClientAuthContract(TypedDict):
code_challenge_methods_supported: ReadOnly[tuple[str, ...]]
token_endpoint_auth_methods_supported: ReadOnly[tuple[str, ...]]
revocation_endpoint_auth_methods_supported: ReadOnly[tuple[str, ...]]
+ hosted_app: ReadOnly[HostedAppAuthContract]
def native_client_auth_contract(request: Request, token_exchange_available: bool) -> NativeClientAuthContract:
@@ -653,6 +711,11 @@ def native_client_auth_contract(request: Request, token_exchange_available: bool
"code_challenge_methods_supported": ("S256",),
"token_endpoint_auth_methods_supported": ("none",),
"revocation_endpoint_auth_methods_supported": ("none",),
+ "hosted_app": {
+ "scopes_supported": hosted_supported_scopes(),
+ "access_token_ttl": HOSTED_ACCESS_TTL,
+ "refresh_token_ttl": HOSTED_GRANT_TTL,
+ },
}
return contract
@@ -705,6 +768,7 @@ def _new_connect_flow(
code_challenge: str,
resource_server_id: str | None,
audience: SessionAudience | None,
+ hosted_scope: HostedScope | None = None,
) -> _ConnectFlow:
now: Final = datetime.now(timezone.utc)
return _ConnectFlow(
@@ -717,6 +781,7 @@ def _new_connect_flow(
exp=int(now.timestamp()) + CONNECT_FLOW_TTL_SECONDS,
resource_server_id=resource_server_id,
audience=audience,
+ hosted_scope=hosted_scope,
)
@@ -876,6 +941,8 @@ async def complete_connect_flow(
opened: Final = _open_flow_for(request, flow_handle, session_user_id, now)
if isinstance(opened, Response):
return opened
+ if not _proxy_redirect_is_allowed(opened.audience, opened.redirect_uri, opened.hosted_scope):
+ return _oauth_error(400, "invalid_request", "the application's callback is no longer trusted")
if decision != "deny":
described: Final = await _describe_opened_flow(opened, lookup_vendor_credential, lookup_server_reachability)
if isinstance(described, Response):
@@ -927,6 +994,7 @@ def _approved_flow_response(flow: _ConnectFlow, delivery: str | None, team_id: s
resource_server_id=flow.resource_server_id,
audience=flow.audience,
team_id=(team_id or None) if flow.audience == PROXY_API_AUDIENCE else None,
+ hosted_scope=flow.hosted_scope,
),
)
callback_url: Final = _append_query_params(flow.redirect_uri, (("code", code), *_state_param(flow)))
@@ -1193,6 +1261,7 @@ async def aggregate_token(
subject_token_type: str | None = None,
requested_token_type: str | None = None,
exchange_subject_token: ExchangeSubjectToken = _refuse_subject_token,
+ hosted_auth: HostedProxyAuth | None = None,
) -> Response:
"""The aggregate token verb: authorization_code and refresh_token grants for the
identity-only session pair, or for the proxy-API credential when the grant was issued
@@ -1215,6 +1284,7 @@ async def aggregate_token(
reload_user=reload_user,
mint_proxy_credential=mint_proxy_credential,
guard=_SingleUseGuard(cache),
+ hosted_auth=hosted_auth,
)
if grant_type == "authorization_code":
return await _authorization_code_grant(
@@ -1236,6 +1306,7 @@ async def aggregate_token(
keys=keys,
now=now,
issue=issue,
+ hosted_auth=hosted_auth,
)
if grant_type == TOKEN_EXCHANGE_GRANT_TYPE:
return await _token_exchange_grant(
@@ -1268,6 +1339,7 @@ class _GrantIssuer:
reload_user: ReloadUser,
mint_proxy_credential: MintProxyCredential,
guard: _SingleUseGuard,
+ hosted_auth: HostedProxyAuth | None = None,
) -> None:
self._request: Final = request
self._resource: Final = resource
@@ -1276,6 +1348,7 @@ class _GrantIssuer:
self._reload_user: Final = reload_user
self._mint_proxy_credential: Final = mint_proxy_credential
self._guard: Final = guard
+ self._hosted_auth: Final = hosted_auth
async def __call__(
self, principal: SessionPrincipal, claim_key: str, claim_ttl_seconds: int, replayed: str
@@ -1313,12 +1386,38 @@ class _GrantIssuer:
return refusal
return _proxy_credential_response(minted, principal, self._keys, self._now)
+ async def issue_hosted(self, code: _GatewayAuthCode) -> Response:
+ if code.hosted_scope is None:
+ return _oauth_error(400, "invalid_grant", "restart application sign-in to grant an explicit scope")
+ target_refusal: Final = self._proxy_api_target_refusal()
+ if target_refusal is not None:
+ return target_refusal
+ service: Final = self._hosted_auth or hosted_proxy_auth()
+ if isinstance(service, HostedFailure):
+ return _hosted_token_response(service)
+ return _hosted_token_response(
+ await service.issue(
+ code.user_id,
+ code.team_id,
+ code.client_id,
+ code.redirect_uri,
+ get_request_base_url(self._request),
+ code.jti,
+ code.hosted_scope,
+ )
+ )
+
async def exchange(
self, subject_token: str, client_id: str, exchange_subject_token: ExchangeSubjectToken
) -> Response:
"""The RFC 8693 tail: prove the IdP token, then mint. No single-use marker, because
the subject token stays a valid proof for as long as the IdP says it is and every
exchange mints a fresh credential and refresh token of its own."""
+ client: Final = open_gateway_dcr_client(client_id)
+ if client is None or any(not is_loopback_redirect_host(urlparse(uri)) for uri in client.redirect_uris):
+ return _oauth_error(
+ 400, "unauthorized_client", "hosted applications must use the consented authorization-code flow"
+ )
target_refusal: Final = self._proxy_api_target_refusal()
if target_refusal is not None:
return target_refusal
@@ -1371,6 +1470,10 @@ async def _authorization_code_grant(
return _oauth_error(400, "invalid_target", "resource does not match the scope this code was issued for")
if not _pkce_verifier_matches(code_verifier, parsed.code_challenge):
return _oauth_error(400, "invalid_grant", "PKCE verification failed")
+ if not _proxy_redirect_is_allowed(parsed.audience, parsed.redirect_uri, parsed.hosted_scope):
+ return _oauth_error(400, "invalid_grant", "the application's callback is no longer trusted")
+ if parsed.audience == PROXY_API_AUDIENCE and not is_loopback_redirect_host(urlparse(parsed.redirect_uri)):
+ return await issue.issue_hosted(parsed)
# The marker's TTL derives from the code's own remaining lifetime so it outlives
# whichever lifetime the code was minted with.
return await issue(
@@ -1395,12 +1498,31 @@ async def _refresh_token_grant(
keys: SessionSigningKeys,
now: datetime,
issue: _GrantIssuer,
+ hosted_auth: HostedProxyAuth | None = None,
) -> Response:
if not refresh_token:
return _oauth_error(400, "invalid_request", "refresh_token is required")
+ if refresh_token.startswith(HOSTED_REFRESH_PREFIX):
+ if resource is not None and not is_proxy_api_resource(request, resource):
+ return _oauth_error(400, "invalid_target", "resource does not match the hosted grant")
+ service: Final = hosted_auth or hosted_proxy_auth()
+ return _hosted_token_response(
+ service
+ if isinstance(service, HostedFailure)
+ else await service.refresh(
+ refresh_token,
+ client_id,
+ get_request_base_url(request),
+ )
+ )
opened: Final = open_session_refresh_bearer(refresh_token, keys, now, expected_client_id=client_id)
if not isinstance(opened, SessionRefreshOpened):
return _oauth_error(400, "invalid_grant", "the refresh token is invalid for this client")
+ client: Final = open_gateway_dcr_client(client_id)
+ if opened.principal.audience == PROXY_API_AUDIENCE and (
+ client is None or any(not is_loopback_redirect_host(urlparse(uri)) for uri in client.redirect_uris)
+ ):
+ return _oauth_error(400, "invalid_grant", "this application must sign in again for a restricted grant")
if _resource_conflicts_with_scope(request, resource, opened.principal.resource_server_id):
return _oauth_error(400, "invalid_target", "resource does not match the scope this token was issued for")
# Refresh-token rotation (OAuth 2.0 Security BCP section 4.13): the presented refresh token is
@@ -1440,7 +1562,19 @@ async def _token_exchange_grant(
return await issue.exchange(subject_token, client_id, exchange_subject_token)
-async def revoke_refresh_token(token: str, client_id: str, master_key: str | None, cache: DualCache) -> Response:
+def _hosted_token_response(result: HostedTokens | HostedFailure) -> Response:
+ if isinstance(result, HostedFailure):
+ return _oauth_error(503 if result.error == "temporarily_unavailable" else 400, result.error, result.description)
+ return JSONResponse(content=result.model_dump(), headers=TOKEN_NO_CACHE_HEADERS)
+
+
+async def revoke_refresh_token(
+ token: str,
+ client_id: str,
+ master_key: str | None,
+ cache: DualCache,
+ hosted_auth: HostedProxyAuth | None = None,
+) -> Response:
"""RFC 7009 revocation for the gateway's refresh tokens: burn the presented token's
``jti`` so neither the holder nor a thief can rotate it again. Access tokens are
stateless and expire on their own (the proxy-API credential within
@@ -1451,6 +1585,12 @@ async def revoke_refresh_token(token: str, client_id: str, master_key: str | Non
never happened."""
if not is_gateway_dcr_client_id(client_id) or open_gateway_dcr_client(client_id) is None:
return _oauth_error(401, "invalid_client", "unknown or malformed client_id")
+ if token.startswith((HOSTED_ACCESS_PREFIX, HOSTED_REFRESH_PREFIX)):
+ service: Final = hosted_auth or hosted_proxy_auth()
+ result: Final = service if isinstance(service, HostedFailure) else await service.revoke(token, client_id)
+ if isinstance(result, HostedFailure):
+ return _hosted_token_response(result)
+ return Response(content="{}", media_type="application/json", headers=TOKEN_NO_CACHE_HEADERS)
if master_key is None:
verbose_logger.error("mcp_gateway_dcr revoke rejected: no master_key configured")
return _oauth_error(500, "server_error", "the gateway has no master key configured")
diff --git a/litellm/proxy/_experimental/mcp_server/hosted_proxy_auth.py b/litellm/proxy/_experimental/mcp_server/hosted_proxy_auth.py
new file mode 100644
index 00000000000..3ba709a17ac
--- /dev/null
+++ b/litellm/proxy/_experimental/mcp_server/hosted_proxy_auth.py
@@ -0,0 +1,571 @@
+from __future__ import annotations
+
+import hashlib
+import hmac
+import os
+import re
+import secrets
+from collections.abc import Mapping
+from datetime import datetime, timezone
+from functools import partial
+from types import MappingProxyType
+from typing import Final, Literal, Protocol, TypeAlias
+from urllib.parse import urlparse
+
+from fastapi import HTTPException, Request
+from pydantic import BaseModel, ConfigDict, Field, ValidationError
+
+from litellm.caching.redis_cache import RedisCache
+from litellm.proxy._experimental.mcp_server.oauth_utils import get_request_base_url, validate_redirect_uri_shape
+from litellm.proxy._types import LiteLLMRoutes, LitellmUserRoles, UserAPIKeyAuth
+from litellm.proxy.auth.route_checks import RouteChecks
+
+HOSTED_ACCESS_PREFIX: Final = "llm_hosted_"
+HOSTED_REFRESH_PREFIX: Final = "llm_hrefresh_"
+HOSTED_ACCESS_TTL: Final = 300
+HOSTED_GRANT_TTL: Final = 86400
+MAX_HOSTED_REFRESHES: Final = 512
+HOSTED_SCOPE: Final = "proxy:read"
+HOSTED_ADMIN_SCOPE: Final = "proxy:admin"
+HostedScope: TypeAlias = Literal["proxy:read", "proxy:admin"]
+_MODEL_ROUTES: Final = frozenset({"/models", "/v1/models"})
+_REPORT_ROUTES: Final = frozenset(
+ {"/global/activity", "/global/activity/model", "/global/spend", "/global/spend/provider", "/global/spend/report"}
+)
+_ADMIN_OPERATIONS: Final = (
+ "/budget/new",
+ "/budget/update",
+ "/budget/info",
+ "/budget/delete",
+ "/team/bulk_member_add",
+ "/team/model/add",
+ "/team/model/delete",
+ "/team/{team_id}/disable_logging",
+)
+_TOKEN_SHAPE: Final = re.compile(r"[A-Za-z0-9_-]{32}\.[A-Za-z0-9_-]{43}\Z")
+
+
+def hosted_redirect_is_allowed(redirect_uri: str, scope: HostedScope = HOSTED_SCOPE) -> bool:
+ try:
+ parsed: Final = urlparse(redirect_uri)
+ if parsed.scheme != "https" or not parsed.hostname or parsed.username or parsed.password:
+ return False
+ if "?" in redirect_uri or "#" in redirect_uri:
+ return False
+ validate_redirect_uri_shape(parsed)
+ except (HTTPException, ValueError):
+ return False
+ settings: Final = (
+ ("LITELLM_PROXY_API_OAUTH_ADMIN_REDIRECT_URIS",)
+ if scope == HOSTED_ADMIN_SCOPE
+ else ("LITELLM_PROXY_API_OAUTH_REDIRECT_URIS", "LITELLM_PROXY_API_OAUTH_ADMIN_REDIRECT_URIS")
+ )
+ return any(
+ redirect_uri in tuple(entry.strip() for entry in os.environ.get(setting, "").split(",")) for setting in settings
+ )
+
+
+def hosted_supported_scopes() -> tuple[HostedScope, ...]:
+ callbacks: Final = tuple(
+ entry.strip()
+ for entry in (
+ os.environ.get("LITELLM_PROXY_API_OAUTH_REDIRECT_URIS", "")
+ + ","
+ + os.environ.get("LITELLM_PROXY_API_OAUTH_ADMIN_REDIRECT_URIS", "")
+ ).split(",")
+ )
+ scopes: Final[tuple[HostedScope, ...]] = (HOSTED_SCOPE, HOSTED_ADMIN_SCOPE)
+ return tuple(scope for scope in scopes if any(hosted_redirect_is_allowed(uri, scope) for uri in callbacks))
+
+
+def hosted_scope_allows_role(scope: HostedScope, user_role: LitellmUserRoles | str | None) -> bool:
+ return scope == HOSTED_SCOPE or user_role == LitellmUserRoles.PROXY_ADMIN
+
+
+def _admin_route_allowed(route: str) -> bool:
+ if any(segment in (".", "..") for segment in route.split("/")):
+ return False
+ if route in (
+ *LiteLLMRoutes.master_key_only_routes.value,
+ "/user/password/change",
+ "/session/logout",
+ ) or route.startswith(("/server/oauth/", "/sso/", "/jwt/", "/v1/mcp/server/oauth/")):
+ return False
+ if route.startswith("/v1/mcp/server/") and any(
+ segment in ("user-credential", "user-credentials", "oauth-user-credential", "user-env-vars")
+ for segment in route.split("/")
+ ):
+ return False
+ if "/realtime/" in route and route.endswith(("/client_secrets", "/transcription_sessions")):
+ return False
+ groups: Final = (
+ LiteLLMRoutes.management_routes,
+ LiteLLMRoutes.self_managed_routes,
+ LiteLLMRoutes.org_admin_only_routes,
+ LiteLLMRoutes.admin_viewer_routes,
+ LiteLLMRoutes.info_routes,
+ LiteLLMRoutes.spend_tracking_routes,
+ LiteLLMRoutes.global_spend_tracking_routes,
+ LiteLLMRoutes.openai_routes,
+ LiteLLMRoutes.anthropic_routes,
+ LiteLLMRoutes.google_routes,
+ LiteLLMRoutes.model_info_routes,
+ )
+ return RouteChecks.check_route_access(route=route, allowed_routes=_ADMIN_OPERATIONS) or any(
+ RouteChecks.check_route_access(route=route, allowed_routes=group.value) for group in groups
+ )
+
+
+class HostedFailure(BaseModel):
+ model_config = ConfigDict(frozen=True)
+ error: Literal["invalid_grant", "temporarily_unavailable", "insufficient_scope"] = "invalid_grant"
+ description: str = "the hosted application grant is no longer valid; sign in again"
+
+
+class HostedAccess(BaseModel):
+ model_config = ConfigDict(frozen=True, extra="forbid")
+ token_hash: str
+ expires_at: int
+
+
+class HostedGrant(BaseModel):
+ model_config = ConfigDict(frozen=True, extra="forbid")
+ grant_id: str
+ user_id: str
+ team_id: str | None
+ client_id: str
+ redirect_uri: str
+ resource: str
+ scope: HostedScope
+ expires_at: int
+ access_expires_at: int
+ access_hash: str
+ refresh_hash: str
+ refresh_count: int = Field(default=0, ge=0, le=MAX_HOSTED_REFRESHES)
+ previous_access: tuple[HostedAccess, ...] = Field(default=(), max_length=MAX_HOSTED_REFRESHES)
+
+
+class HostedTokens(BaseModel):
+ model_config = ConfigDict(frozen=True)
+ access_token: str = Field(repr=False)
+ refresh_token: str = Field(repr=False)
+ user_id: str
+ team_id: str | None
+ expires_in: int
+ refresh_expires_in: int
+ token_type: Literal["Bearer"] = "Bearer"
+ scope: HostedScope = HOSTED_SCOPE
+
+
+class HostedGrantStore(Protocol):
+ async def read(self, grant_id: str) -> HostedGrant | HostedFailure: ...
+
+ async def replace(self, grant: HostedGrant, previous: HostedGrant | None, ttl: int) -> bool: ...
+
+ async def delete(self, grant_id: str) -> None: ...
+
+ async def revoke_replayed(self, grant_id: str, token_hash: str) -> None: ...
+
+
+_REPLACE_GRANT: Final = """
+local current = redis.call('GET', KEYS[1])
+if ARGV[1] == '' and redis.call('EXISTS', KEYS[2]) == 1 then
+ return 0
+end
+if (ARGV[1] == '' and not current) or current == ARGV[1] then
+ redis.call('SET', KEYS[1], ARGV[2], 'EX', ARGV[3])
+ redis.call('SET', KEYS[2], '1', 'EX', ARGV[3])
+ if ARGV[4] ~= '' then
+ redis.call('SADD', KEYS[3], ARGV[4], ARGV[5])
+ redis.call('EXPIRE', KEYS[3], ARGV[3])
+ end
+ return 1
+end
+return 0
+"""
+_REVOKE_REPLAYED: Final = """
+if redis.call('SISMEMBER', KEYS[2], ARGV[1]) == 1 then
+ return redis.call('DEL', KEYS[1])
+end
+return 0
+"""
+
+
+class RedisHostedGrantStore:
+ def __init__(self, cache: RedisCache, master_key: str) -> None:
+ self._cache: Final = cache
+ self._gateway: Final = hashlib.sha256(master_key.encode()).hexdigest()
+
+ def _key(self, grant_id: str) -> str:
+ return self._cache.check_and_fix_namespace(f"hosted_proxy_grant:{{{self._gateway}:{grant_id}}}")
+
+ async def read(self, grant_id: str) -> HostedGrant | HostedFailure:
+ value: Final = await self._cache.async_eval("return redis.call('GET', KEYS[1])", 1, self._key(grant_id))
+ if not isinstance(value, (str, bytes)):
+ return HostedFailure()
+ try:
+ return HostedGrant.model_validate_json(value)
+ except ValidationError:
+ return HostedFailure()
+
+ async def replace(self, grant: HostedGrant, previous: HostedGrant | None, ttl: int) -> bool:
+ result: Final = await self._cache.async_eval(
+ _REPLACE_GRANT,
+ 3,
+ self._key(grant.grant_id),
+ self._key(grant.grant_id) + ":issued",
+ self._key(grant.grant_id) + ":spent",
+ previous.model_dump_json() if previous is not None else "",
+ grant.model_dump_json(),
+ ttl,
+ previous.refresh_hash if previous is not None else "",
+ previous.access_hash if previous is not None else "",
+ )
+ return isinstance(result, int) and result == 1
+
+ async def delete(self, grant_id: str) -> None:
+ await self._cache.async_eval("return redis.call('DEL', KEYS[1])", 1, self._key(grant_id))
+
+ async def revoke_replayed(self, grant_id: str, token_hash: str) -> None:
+ await self._cache.async_eval(
+ _REVOKE_REPLAYED, 2, self._key(grant_id), self._key(grant_id) + ":spent", token_hash
+ )
+
+
+class LoadHostedUser(Protocol):
+ async def __call__(self, user_id: str, team_id: str | None, /) -> UserAPIKeyAuth | HostedFailure: ...
+
+
+def _digest(token: str) -> str:
+ return hashlib.sha256(token.encode()).hexdigest()
+
+
+def _grant_id(token: str, prefix: str) -> str | None:
+ if not token.startswith(prefix):
+ return None
+ suffix: Final = token.removeprefix(prefix)
+ return suffix.split(".", 1)[0] if _TOKEN_SHAPE.fullmatch(suffix) else None
+
+
+def _unavailable() -> HostedFailure:
+ return HostedFailure(error="temporarily_unavailable", description="hosted application authorization is unavailable")
+
+
+class HostedProxyAuth:
+ def __init__(self, store: HostedGrantStore, load_user: LoadHostedUser, now: datetime) -> None:
+ self._store: Final = store
+ self._load_user: Final = load_user
+ self._now: Final = int(now.timestamp())
+
+ async def issue(
+ self,
+ user_id: str,
+ team_id: str | None,
+ client_id: str,
+ redirect_uri: str,
+ resource: str,
+ code_id: str,
+ scope: HostedScope = HOSTED_SCOPE,
+ ) -> HostedTokens | HostedFailure:
+ if not hosted_redirect_is_allowed(redirect_uri, scope):
+ return HostedFailure()
+ identity: Final = await self._load_user(user_id, team_id)
+ if isinstance(identity, HostedFailure):
+ return identity
+ if not hosted_scope_allows_role(scope, identity.user_role):
+ return HostedFailure(
+ error="insufficient_scope",
+ description="administrator access requires a current proxy administrator role",
+ )
+ return await self._mint(
+ HostedGrant(
+ grant_id=hashlib.sha256(code_id.encode()).hexdigest()[:32],
+ user_id=user_id,
+ team_id=team_id,
+ client_id=client_id,
+ redirect_uri=redirect_uri,
+ resource=resource,
+ scope=scope,
+ expires_at=self._now + HOSTED_GRANT_TTL,
+ access_expires_at=0,
+ access_hash="",
+ refresh_hash="",
+ ),
+ previous=None,
+ )
+
+ async def _read(
+ self,
+ token: str,
+ prefix: str,
+ client_id: str | None,
+ resource: str | None,
+ *,
+ purpose: Literal["authenticate", "revoke"] = "authenticate",
+ ) -> HostedGrant | HostedFailure:
+ grant_id: Final = _grant_id(token, prefix)
+ if grant_id is None:
+ return HostedFailure()
+ try:
+ grant: Final = await self._store.read(grant_id)
+ except Exception: # noqa: BLE001 # authorization must fail closed on every store failure
+ return _unavailable()
+ if isinstance(grant, HostedFailure):
+ return grant
+ if (
+ grant.grant_id != grant_id
+ or self._now >= grant.expires_at
+ or (client_id is not None and client_id != grant.client_id)
+ or (resource is not None and resource != grant.resource)
+ ):
+ return HostedFailure()
+ candidates: Final = (
+ (HostedAccess(token_hash=grant.refresh_hash, expires_at=grant.expires_at),)
+ if prefix == HOSTED_REFRESH_PREFIX
+ else (
+ HostedAccess(token_hash=grant.access_hash, expires_at=grant.access_expires_at),
+ *grant.previous_access,
+ )
+ )
+ valid: Final = any(
+ hmac.compare_digest(_digest(token), candidate.token_hash)
+ and (self._now < candidate.expires_at or purpose == "revoke")
+ for candidate in candidates
+ )
+ if not valid:
+ if prefix == HOSTED_REFRESH_PREFIX or purpose == "revoke":
+ try:
+ await self._store.revoke_replayed(grant_id, _digest(token))
+ except Exception: # noqa: BLE001 # a failed replay revocation must not be hidden
+ return _unavailable()
+ return HostedFailure()
+ return grant
+
+ async def refresh(self, token: str, client_id: str, resource: str) -> HostedTokens | HostedFailure:
+ grant: Final = await self._read(token, HOSTED_REFRESH_PREFIX, client_id, resource)
+ if isinstance(grant, HostedFailure):
+ return grant
+ if grant.refresh_count >= MAX_HOSTED_REFRESHES:
+ return HostedFailure(description="the application's refresh limit was reached; sign in again")
+ if not hosted_redirect_is_allowed(grant.redirect_uri, grant.scope):
+ return HostedFailure()
+ identity: Final = await self._load_user(grant.user_id, grant.team_id)
+ if isinstance(identity, HostedFailure):
+ return identity
+ if not hosted_scope_allows_role(grant.scope, identity.user_role):
+ return HostedFailure(
+ error="insufficient_scope",
+ description="administrator access requires a current proxy administrator role",
+ )
+ return await self._mint(grant, previous=grant)
+
+ async def _mint(self, grant: HostedGrant, previous: HostedGrant | None) -> HostedTokens | HostedFailure:
+ access: Final = f"{HOSTED_ACCESS_PREFIX}{grant.grant_id}.{secrets.token_urlsafe(32)}"
+ refresh: Final = f"{HOSTED_REFRESH_PREFIX}{grant.grant_id}.{secrets.token_urlsafe(32)}"
+ expires_in: Final = min(HOSTED_ACCESS_TTL, grant.expires_at - self._now)
+ updated: Final = grant.model_copy(
+ update=MappingProxyType(
+ {
+ "access_hash": _digest(access),
+ "refresh_hash": _digest(refresh),
+ "access_expires_at": self._now + expires_in,
+ "refresh_count": grant.refresh_count + 1 if previous is not None else 0,
+ "previous_access": tuple(
+ access
+ for access in (
+ *grant.previous_access,
+ HostedAccess(token_hash=grant.access_hash, expires_at=grant.access_expires_at),
+ )
+ if access.expires_at > self._now
+ ),
+ }
+ )
+ )
+ try:
+ stored: Final = await self._store.replace(updated, previous, grant.expires_at - self._now)
+ except Exception: # noqa: BLE001 # never issue a credential whose shared grant was not stored
+ return _unavailable()
+ if not stored:
+ if previous is not None:
+ try:
+ await self._store.revoke_replayed(grant.grant_id, previous.refresh_hash)
+ except Exception: # noqa: BLE001 # concurrent refresh replay invalidates the grant or fails closed
+ return _unavailable()
+ return HostedFailure()
+ return HostedTokens(
+ access_token=access,
+ refresh_token=refresh,
+ expires_in=expires_in,
+ refresh_expires_in=grant.expires_at - self._now,
+ user_id=grant.user_id,
+ team_id=grant.team_id,
+ scope=grant.scope,
+ )
+
+ async def authenticate(self, token: str, resource: str, route: str, method: str) -> UserAPIKeyAuth | HostedFailure:
+ grant: Final = await self._read(token, HOSTED_ACCESS_PREFIX, None, resource)
+ if isinstance(grant, HostedFailure):
+ return grant
+ if not hosted_redirect_is_allowed(grant.redirect_uri, grant.scope):
+ return HostedFailure()
+ allowed: Final = (
+ method == "GET" and route in _MODEL_ROUTES | _REPORT_ROUTES
+ if grant.scope == HOSTED_SCOPE
+ else _admin_route_allowed(route)
+ )
+ if not allowed:
+ return HostedFailure(
+ error="insufficient_scope", description="this application grant does not permit this route"
+ )
+ identity: Final = await self._load_user(grant.user_id, grant.team_id)
+ if isinstance(identity, HostedFailure):
+ return identity
+ if not hosted_scope_allows_role(grant.scope, identity.user_role):
+ return HostedFailure(
+ error="insufficient_scope",
+ description="administrator access requires a current proxy administrator role",
+ )
+ if route in _REPORT_ROUTES and identity.user_role not in (
+ LitellmUserRoles.PROXY_ADMIN,
+ LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY,
+ ):
+ return HostedFailure(
+ error="insufficient_scope", description="global usage requires a current proxy administrator role"
+ )
+ return identity.model_copy(update=MappingProxyType({"token": _digest(token), "api_key": _digest(token)}))
+
+ async def revoke(self, token: str, client_id: str) -> HostedFailure | None:
+ prefix: Final = HOSTED_REFRESH_PREFIX if token.startswith(HOSTED_REFRESH_PREFIX) else HOSTED_ACCESS_PREFIX
+ grant: Final = await self._read(token, prefix, client_id, None, purpose="revoke")
+ if isinstance(grant, HostedFailure):
+ return grant if grant.error == "temporarily_unavailable" else None
+ try:
+ await self._store.delete(grant.grant_id)
+ except Exception: # noqa: BLE001 # a failed revocation must be reported as retryable
+ return _unavailable()
+ return None
+
+
+class _HostedAuthSettings(BaseModel):
+ model_config = ConfigDict(frozen=True)
+ general_settings: Mapping[str, object]
+ user_custom_auth: object
+
+
+class _UserModelBudget(BaseModel):
+ model_config = ConfigDict(frozen=True, from_attributes=True)
+ model_max_budget: Mapping[str, object] | None
+
+
+async def load_hosted_user(user_id: str, team_id: str | None) -> UserAPIKeyAuth | HostedFailure:
+ from litellm.proxy import proxy_server # noqa: PLC0415 # startup-owned dependencies
+ from litellm.proxy._experimental.mcp_server.bridge_token_flow import (
+ active_user_record, # noqa: PLC0415 # shared offboarding policy, proxy import cycle
+ )
+ from litellm.proxy.auth.auth_checks import ( # noqa: PLC0415 # proxy import cycle
+ effective_user_role,
+ get_team_membership,
+ get_team_object,
+ get_user_object,
+ )
+ from litellm.proxy.auth.resolvers.grants import ( # noqa: PLC0415 # shared live identity resolution
+ GrantResolver,
+ LookupDegraded,
+ ResolvedGrants,
+ UserLookup,
+ user_models,
+ )
+ from litellm.proxy.auth.team_grants import team_grants # noqa: PLC0415 # shared team permission projection
+ from litellm.proxy.management_endpoints.ui_sso import (
+ fetch_cli_sso_team_details, # noqa: PLC0415 # existing live team selection rules
+ )
+ from litellm.proxy.proxy_server import ( # noqa: PLC0415 # startup-owned dependencies
+ prisma_client,
+ user_api_key_cache,
+ )
+
+ settings: Final = _HostedAuthSettings.model_validate(vars(proxy_server))
+ if (
+ settings.user_custom_auth is not None
+ or settings.general_settings.get("enable_oauth2_auth") is True
+ or settings.general_settings.get("enable_oauth2_proxy_auth") is True
+ ):
+ return HostedFailure(
+ description="hosted application authorization is disabled while custom or external auth is configured"
+ )
+ if prisma_client is None:
+ return _unavailable()
+ resolved: Final = await GrantResolver(
+ prisma_client,
+ user_api_key_cache,
+ load_user=partial(get_user_object, check_db_only=True),
+ load_team=partial(get_team_object, check_db_only=True),
+ load_membership=partial(get_team_membership, check_db_only=True),
+ ).resolve(UserLookup(user_id=user_id), team_id)
+ if isinstance(resolved, LookupDegraded):
+ return _unavailable()
+ if not isinstance(resolved, ResolvedGrants) or resolved.user_object is None:
+ return HostedFailure()
+ user: Final = active_user_record(resolved.user_object)
+ if isinstance(user, str):
+ return HostedFailure()
+ if team_id is None and user.teams:
+ teams: Final = await fetch_cli_sso_team_details(prisma_client, user.teams)
+ if teams is None:
+ return _unavailable()
+ if any(team.team_id is not None for team in teams):
+ return HostedFailure(description="select a current team and sign in again")
+ team: Final = resolved.team_object
+ if team is not None and team.blocked:
+ return HostedFailure()
+ return UserAPIKeyAuth.model_validate(
+ MappingProxyType(
+ {
+ "user_id": user.user_id,
+ "user_role": effective_user_role(user.user_role),
+ "user_email": user.user_email,
+ "user_tpm_limit": user.tpm_limit,
+ "user_rpm_limit": user.rpm_limit,
+ "user_spend": user.spend,
+ "user_max_budget": user.max_budget,
+ "user_model_max_budget": _UserModelBudget.model_validate(user).model_max_budget,
+ "team_id": team_id,
+ "org_id": team.organization_id if team is not None else user.organization_id,
+ "models": user_models(user) if team is None else (),
+ **team_grants(team, resolved.team_membership, user.user_id),
+ }
+ )
+ )
+
+
+def hosted_proxy_auth() -> HostedProxyAuth | HostedFailure:
+ from litellm.proxy.proxy_server import ( # noqa: PLC0415 # startup-owned shared authority
+ master_key,
+ redis_usage_cache,
+ )
+
+ if redis_usage_cache is None or not isinstance(master_key, str) or not master_key:
+ return _unavailable()
+ return HostedProxyAuth(
+ RedisHostedGrantStore(redis_usage_cache, master_key), load_hosted_user, datetime.now(timezone.utc)
+ )
+
+
+async def authenticate_hosted_request(request: Request, token: str, route: str) -> UserAPIKeyAuth:
+ service: Final = hosted_proxy_auth()
+ result: Final = (
+ service
+ if isinstance(service, HostedFailure)
+ else await service.authenticate(
+ token,
+ get_request_base_url(request),
+ route,
+ request.method,
+ )
+ )
+ if isinstance(result, HostedFailure):
+ status: Final = (
+ 503 if result.error == "temporarily_unavailable" else 403 if result.error == "insufficient_scope" else 401
+ )
+ raise HTTPException(status_code=status, detail=result.description)
+ return result
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index a471fb6f6f8..e8b30d48d6f 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -3432,7 +3432,7 @@ class UserAPIKeyAuth(LiteLLM_VerificationTokenView): # the expected response ob
normalized = api_key
if normalized[:7].lower() == "bearer ":
normalized = normalized[7:]
- if normalized.startswith("sk-"):
+ if normalized.startswith(("sk-", "llm_hosted_", "llm_hrefresh_")):
return hash_token(normalized)
from litellm.proxy.auth.handle_jwt import JWTHandler
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 4b7b8290a30..ae851bdc238 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -2344,9 +2344,13 @@ async def _fetch_team_membership_from_db(
user_api_key_cache: UserApiKeyCache,
parent_otel_span: Span | None = None,
proxy_logging_obj: ProxyLogging | None = None,
+ *,
+ use_writer: bool = False,
) -> LiteLLM_TeamMembership | None:
_ = parent_otel_span, proxy_logging_obj
- response: Final = await _dictable_table(TeamMembershipRepository(prisma_client), "team_membership").find_unique(
+ response: Final = await _dictable_table(
+ TeamMembershipRepository(prisma_client, use_writer=use_writer), "team_membership"
+ ).find_unique(
where={"user_id_team_id": {"user_id": user_id, "team_id": team_id}},
include={"litellm_budget_table": True},
)
@@ -2398,6 +2402,7 @@ async def get_team_membership(
user_api_key_cache: UserApiKeyCache,
parent_otel_span: Span | None = None,
proxy_logging_obj: ProxyLogging | None = None,
+ check_db_only: bool = False,
) -> Optional["LiteLLM_TeamMembership"]:
"""
Returns team membership object if user is member of team.
@@ -2406,6 +2411,18 @@ async def get_team_membership(
"""
if user_id is None or team_id is None:
return None
+ if check_db_only:
+ if prisma_client is None:
+ raise RuntimeError("No db connected")
+ return await _fetch_team_membership_from_db(
+ user_id=user_id,
+ team_id=team_id,
+ prisma_client=prisma_client,
+ user_api_key_cache=user_api_key_cache,
+ parent_otel_span=parent_otel_span,
+ proxy_logging_obj=proxy_logging_obj,
+ use_writer=True,
+ )
_key: Final = team_membership_reservation_cache_key(user_id=user_id, team_id=team_id)
@@ -3181,7 +3198,7 @@ async def _get_team_object_from_user_api_key_cache(
use_writer: bool = False,
) -> LiteLLM_TeamTableCachedObj:
db_access_time_key: Final = key
- should_check_db: Final = _should_check_db(
+ should_check_db: Final = use_writer or _should_check_db(
key=db_access_time_key,
last_db_access_time=last_db_access_time,
db_cache_expiry=db_cache_expiry,
diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py
index 3400dccf2a7..7925aca1b8c 100644
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@ -1545,6 +1545,15 @@ async def _user_api_key_auth_builder(
parent_otel_span = getattr(request.state, "parent_otel_span", None)
### USER-DEFINED AUTH FUNCTION ###
+ from litellm.proxy._experimental.mcp_server.hosted_proxy_auth import ( # noqa: PLC0415 # hosted OAuth depends on proxy startup
+ HOSTED_ACCESS_PREFIX,
+ HOSTED_REFRESH_PREFIX,
+ authenticate_hosted_request,
+ )
+
+ if api_key and api_key.startswith((HOSTED_ACCESS_PREFIX, HOSTED_REFRESH_PREFIX)):
+ return await authenticate_hosted_request(request, api_key, route)
+
if enterprise_custom_auth is not None:
with tracer.trace("litellm.proxy.auth.enterprise_custom_auth"):
response = await enterprise_custom_auth(
diff --git a/litellm/proxy/common_utils/html_forms/native_client_consent.py b/litellm/proxy/common_utils/html_forms/native_client_consent.py
index dac92c4e787..75532c9c410 100644
--- a/litellm/proxy/common_utils/html_forms/native_client_consent.py
+++ b/litellm/proxy/common_utils/html_forms/native_client_consent.py
@@ -12,18 +12,36 @@ def render_native_client_consent_page(
teams: Sequence[tuple[str, str]],
flow_handle: str,
complete_url: str,
+ hosted: bool = False,
+ hosted_admin: bool = False,
) -> str:
"""The consent page a native client's sign-in lands on: who is signed in, which
loopback client asked, which team the credential is attributed to, and an explicit
Approve or Deny that POSTs back to ``complete_url``. Every value is client- or
user-influenced and HTML-escaped; the flow handle travels only in the form body."""
+ title: Final = "Authorize application access" if hosted else "Authorize CLI access"
+ client_type: Final = "A web application" if hosted else "A command-line client"
+ renewal: Final = "" if hosted else " lite logout stops it from being renewed."
+ permission_text: Final = (
+ "Approving allows this application to manage keys, users, teams and models, read gateway logs and usage, "
+ "and call models using your current administrator permissions. Model calls can incur charges. "
+ "Access expires after five minutes and can be renewed for up to 24 hours. "
+ "Disconnecting the application revokes access immediately."
+ if hosted_admin
+ else "Approving permits read-only model listings and aggregate usage reports within your current permissions. "
+ "It does not permit changes, credential access, request contents, or LLM calls. "
+ "Access expires after five minutes and can be renewed for up to 24 hours. "
+ "Disconnecting the application revokes access immediately."
+ if hosted
+ else f"Approving issues it a personal credential that expires within {CLI_JWT_EXPIRATION_HOURS} hours.{renewal}"
+ )
return f"""