mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 10:58:17 -05:00
[PR #17210] [CLOSED] feat: server-side OAuth token management system #47406
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/17210
Author: @Classic298
Created: 9/4/2025
Status: ❌ Closed
Base:
dev← Head:dev📝 Commits (10+)
9289272Create oauth_session.py54075b2Update config.py6ccf301Create oauth_tokens.py78a32c3Create token_forwarding.pye5252a0Update oauth.py76be7c6Update openai.pyac5e01fUpdate middleware.pya6f618fCreate add_oauth_sessions_table.py96c826dUpdate openai.py3a19722Merge branch 'open-webui:dev' into dev📊 Changes
10 files changed (+999 additions, -78 deletions)
View changed files
📝
backend/open_webui/config.py(+29 -0)➕
backend/open_webui/migrations/versions/add_oauth_sessions_table.py(+49 -0)➕
backend/open_webui/models/oauth_session.py(+205 -0)📝
backend/open_webui/routers/openai.py(+98 -45)📝
backend/open_webui/utils/middleware.py(+10 -0)📝
backend/open_webui/utils/oauth.py(+37 -17)➕
backend/open_webui/utils/oauth_tokens.py(+329 -0)➕
backend/open_webui/utils/token_forwarding.py(+224 -0)📝
backend/open_webui/utils/tools.py(+11 -16)📝
src/lib/components/AddServerModal.svelte(+7 -0)📄 Description
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.
Before submitting, make sure you've checked the following:
devbranch.Changelog Entry
Description
Added
oauth_sessiondatabase table to securely store encrypted OAuth tokens, eliminating browser cookie size limits.OAuthTokenManagerservice that automatically refreshes expired or near-expiry tokens for providers like Google, Microsoft, and OIDC.TokenForwardingServiceto provide valid OAuth tokens to various parts of the application (OpenAI router, Tools, etc.).ENABLE_OAUTH_TOKEN_FORWARDING,OAUTH_TOKEN_FORWARDING_SERVICES,OAUTH_TOKEN_ENCRYPTION_KEY, andOAUTH_RESPONSE_MODEfor complete control over the new feature.Changed
oauth.pyto store tokens server-side instead of directly in cookies when the new feature is enabled.openai.pyto prioritize forwarding the user's OAuth token as the Bearer token, falling back to the static API key.middleware.pyto inject the__oauth_token__directly into the tool context, providing a clean access pattern.Deprecated
id_tokenin theoauth_id_tokencookie is now a fallback mechanism and is functionally deprecated by the new server-side session system.Fixed
OAUTH_RESPONSE_MODEforform_post.__oauth_token__parameter for tools, removing the need for fragile cookie scraping.Security
cryptographylibrary.Breaking Changes
ENABLE_OAUTH_TOKEN_FORWARDINGfeature flag and is disabled by default, ensuring full backward compatibility with existing deployments.Additional Information
Architectural Rationale & Deep Dive
This pull request addresses a set of deeply interconnected issues stemming from a single architectural flaw: treating dynamic, short-lived OAuth tokens as if they were static, long-lived session cookies. The previous implementation led to problems with token size, expiration, and accessibility.
This new architecture refactors OAuth tokens into what they truly are: managed, secure session credentials.
How It Works:
access_token,id_token, andrefresh_token) and store it in a newoauth_sessiondatabase table.httponlycookie containing a uniqueoauth_session_idis sent to the browser.TokenForwardingService.refresh_tokento get new ones before returning a valid token.How It Solves the Core Problems:
TokenForwardingServiceacts as a "smart" gatekeeper. By checking the token's expiry on every request and refreshing it just-in-time, it guarantees that any service asking for a token always receives a valid one. This eliminates the "silent failure" scenario where tools would break after the token expired.token_forwarding_service.get_oauth_token_for_service(...). This provides a stable and documented API for accessing tokens, whether for tools (__oauth_token__) or for the OpenAI router (Authorizationheader). This resolves the core request of both issues and the rejected PR #9673 by providing a clean, backward-compatible, and well-architected solution.Implementation Notes
Screenshots or Videos
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.