Skip to content

feat(sdk): add proxy_auth for auto OAuth2/JWT token management - #20238

Merged
1 commit merged into
BerriAI:litellm_oss_staging_02_03_2026from
Chesars:feat/proxy-auth-jwt-autorefresh
Feb 3, 2026
Merged

feat(sdk): add proxy_auth for auto OAuth2/JWT token management#20238
1 commit merged into
BerriAI:litellm_oss_staging_02_03_2026from
Chesars:feat/proxy-auth-jwt-autorefresh

Conversation

@Chesars

@Chesars Chesars commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

Fixes #19834

Pre-Submission checklist

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🆕 New Feature

Changes

Adds litellm.proxy_auth to automatically obtain and refresh OAuth2/JWT tokens when connecting to LiteLLM Proxy or any OAuth2-protected endpoint.

Features:

  • ProxyAuthHandler: Manages token lifecycle (obtain, cache, refresh with 60s buffer)
  • AzureADCredential: Wrapper for azure-identity credentials (DefaultAzureCredential, ClientSecretCredential, etc.)
  • GenericOAuth2Credential: Works with any OAuth2 provider (Okta, Auth0, Keycloak, etc.)
  • TokenCredential Protocol: Users can implement custom credential providers

Usage:

import litellm
from litellm.proxy_auth import AzureADCredential, ProxyAuthHandler

# One-time setup
litellm.proxy_auth = ProxyAuthHandler(
    credential=AzureADCredential(),  # uses DefaultAzureCredential
    scope="api://my-litellm-proxy/.default"
)
litellm.api_base = "https://my-proxy.example.com"

# Auth headers are now automatically injected
response = litellm.completion(model="gpt-4", messages=[...])

Tests:

  • 12 unit tests covering all credential providers and integration with litellm module
  • All tests pass: poetry run pytest tests/litellm/test_proxy_auth.py -v

Adds litellm.proxy_auth to automatically obtain and refresh OAuth2/JWT
tokens when connecting to LiteLLM Proxy or any OAuth2-protected endpoint.

- Add ProxyAuthHandler for token lifecycle (obtain, cache, refresh)
- Add AzureADCredential wrapper for azure-identity credentials
- Add GenericOAuth2Credential for any OAuth2 provider (Okta, Auth0, etc)
- Auto-inject Authorization headers in completion() and embedding()

Closes BerriAI#19834
@vercel

vercel Bot commented Feb 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 1, 2026 11:01pm

Request Review

@greptile-apps

greptile-apps Bot commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR adds automatic OAuth2/JWT token management for authenticating with LiteLLM Proxy and other OAuth2-protected endpoints. The implementation introduces a new litellm.proxy_auth module with clean credential provider abstractions.

Key Changes:

  • New ProxyAuthHandler class that manages token lifecycle (obtain, cache, refresh with 60s buffer)
  • AzureADCredential wrapper for azure-identity credentials with lazy initialization
  • GenericOAuth2Credential for standard OAuth2 client credentials flow
  • Protocol-based design (TokenCredential) allows custom credential providers
  • Auth headers automatically injected in completion() and embedding() methods
  • Graceful error handling with warning logs if token fetch fails

Implementation Quality:

  • Well-structured code with proper separation of concerns
  • Token caching prevents unnecessary OAuth2 requests (performance-conscious)
  • 60-second expiry buffer ensures tokens are refreshed before expiration
  • Comprehensive unit tests (12 tests) covering all credential providers and edge cases
  • No performance impact in the critical request path per custom instruction (auth object created once, not per request)

Confidence Score: 4/5

  • This PR is safe to merge with minor considerations
  • The implementation is well-designed with proper error handling, caching, and comprehensive tests. The code follows good patterns (Protocol-based design, lazy initialization, graceful degradation). Score is 4 instead of 5 because: (1) auth injection only covers completion() and embedding() methods - other API methods like text_completion(), transcription(), and speech() might also benefit from this feature if users call them through a proxy, though these are likely less commonly used, (2) no integration test demonstrating end-to-end usage with actual completion/embedding calls
  • Pay attention to litellm/main.py - verify whether other API methods besides completion/embedding should also receive auth injection

Important Files Changed

Filename Overview
litellm/proxy_auth/credentials.py New credential provider module with solid OAuth2/JWT token management, proper caching, and clean protocol-based design
litellm/main.py Auth injection added to completion() and embedding(), but missing from other API methods like text_completion(), transcription(), speech(), and image_generation() if they exist
tests/litellm/test_proxy_auth.py Comprehensive unit tests covering all credential providers, token caching, refresh logic, and integration with litellm module

Sequence Diagram

sequenceDiagram
    participant User
    participant litellm
    participant ProxyAuthHandler
    participant Credential
    participant OAuth2Server
    participant LiteLLMProxy

    User->>litellm: setup proxy_auth with credential and scope
    User->>litellm: completion(model, messages, ...)
    litellm->>litellm: Check if proxy_auth is set
    alt proxy_auth is configured
        litellm->>ProxyAuthHandler: get_auth_headers()
        ProxyAuthHandler->>ProxyAuthHandler: Check cached token validity
        alt token missing or expires within 60s
            ProxyAuthHandler->>Credential: get_token(scope)
            alt AzureADCredential
                Credential->>OAuth2Server: Request Azure AD token
                OAuth2Server-->>Credential: Return token with expiration
            else GenericOAuth2Credential
                Credential->>OAuth2Server: POST client credentials grant
                OAuth2Server-->>Credential: Return token with expiration
            end
            Credential-->>ProxyAuthHandler: AccessToken object
            ProxyAuthHandler->>ProxyAuthHandler: Cache token
        end
        ProxyAuthHandler-->>litellm: Authorization headers dict
        litellm->>litellm: Update request headers
    end
    litellm->>LiteLLMProxy: HTTP Request with Authorization
    LiteLLMProxy-->>litellm: Response
    litellm-->>User: ModelResponse
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@ghost
ghost changed the base branch from main to litellm_oss_staging_02_03_2026 February 3, 2026 06:03
@ghost
ghost merged commit b33e1e8 into BerriAI:litellm_oss_staging_02_03_2026 Feb 3, 2026
6 of 8 checks passed
@ghost

ghost commented Feb 3, 2026

Copy link
Copy Markdown

Hi @Chesars can you document this please?

@Chesars

Chesars commented Feb 8, 2026

Copy link
Copy Markdown
Contributor Author

Hi @Chesars can you document this please?

Hi @krrishdholakia , added in #20680

@Chesars
Chesars deleted the feat/proxy-auth-jwt-autorefresh branch February 8, 2026 00:18
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…AI#20238)

Adds litellm.proxy_auth to automatically obtain and refresh OAuth2/JWT
tokens when connecting to LiteLLM Proxy or any OAuth2-protected endpoint.

- Add ProxyAuthHandler for token lifecycle (obtain, cache, refresh)
- Add AzureADCredential wrapper for azure-identity credentials
- Add GenericOAuth2Credential for any OAuth2 provider (Okta, Auth0, etc)
- Auto-inject Authorization headers in completion() and embedding()

Closes BerriAI#19834
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Azure AD JWT Authentication with Auto-Refresh for LiteLLM Proxy

1 participant