<@U090ER7JPHA> Search for `access_token` in OpenHa...
# slackbot-chatter
c
@nutritious-balloon-65098 Search for
access_token
in OpenHands/openhands, then search for it separately in /enterprise directory. (read the readme in /enterprise too) Now be careful, think hard, and tell me clearly, as if I'm dense: • just what is it doing in OpenHands/openhands, what purpose does it serve? Give me all related execution flows/routes, with file/lines. Make the source file/line a link to github repo location of that file/line.
n
I'm on it! can track my progress here.
Summary of findings about access_token • What it is: In the OSS backend, access_token is an optional external auth token for the current request, surfaced by the user-auth layer. By default (OSS), it’s always None; it exists to keep the same API surface that Enterprise/SaaS needs. ◦ Plumbed via get_access_token(request): openhands/server/user_auth/*init*.py#L18-L21 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/user_auth/__init__.py#L18-L21 ◦ Default implementation returns None: openhands/server/user_auth/default_user_auth.py#L32-L34 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/user_auth/default_user_auth.py#L32-L34 • What purpose it serves: Routes accept access_token and forward it to ProviderHandler as external_auth_token so provider services can authenticate using an external identity provider (used in Enterprise). In OSS, services still build headers from stored provider tokens (PATs), so access_token is effectively unused there. ◦ ProviderHandler ctor stores external_auth_token: openhands/integrations/provider.py#L113-L121, L134, L154-L160 <https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/integrations/provider.py#L113-L121 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/integrations/provider.py#L154-L160> ◦ OSS services construct headers from self.token (PAT), not external_auth_token: ▪︎ GitHub: openhands/integrations/github/service/base.py#L24-L35 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/integrations/github/service/base.py#L24-L35 ▪︎ GitLab: openhands/integrations/gitlab/service/base.py#L23-L33 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/integrations/gitlab/service/base.py#L23-L33 ▪︎ Bitbucket: openhands/integrations/bitbucket/service/base.py#L51-L67 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/integrations/bitbucket/service/base.py#L51-L67 • All related execution flows/routes in OSS that take and forward access_token: ◦ User routes: installations, repositories, info, search/repositories, search/branches, suggested-tasks, repository/branches, repository/{repo}/microagents, and repository/{repo}/microagents/content. Each has access_token = Depends(get_access_token) and passes it to ProviderHandler(external_auth_token=…): <https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L42 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L73 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L108 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L140 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L174 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L213 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L245 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L299 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/git.py#L365> ◦ MCP tools (create PR/MR): acquire access_token via get_access_token and forward it into Github/GitLab/Bitbucket services: ▪︎ GitHub PR: openhands/server/routes/mcp.py#L104-L121 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/mcp.py#L104-L121 ▪︎ GitLab MR: openhands/server/routes/mcp.py#L175-L191 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/mcp.py#L175-L191 ▪︎ Bitbucket PR: openhands/server/routes/mcp.py#L242-L257 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/openhands/server/routes/mcp.py#L242-L257 • Enterprise/SaaS context (why OSS carries it): In Enterprise, access_token is the Keycloak access token. SaasUserAuth refreshes/returns it, and provider services translate it into actual provider tokens via Keycloak broker endpoints—so the same OSS routes work without code changes. ◦ SaasUserAuth.get_access_token: enterprise/server/auth/saas_user_auth.py#L130-L139 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/enterprise/server/auth/saas_user_auth.py#L130-L139 ◦ Broker token fetch: enterprise/server/auth/token_manager.py#L204-L211 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/enterprise/server/auth/token_manager.py#L204-L211 ◦ SaaS GitHub service uses access_token to get a working GitHub token: enterprise/integrations/github/github_service.py#L39-L47 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/enterprise/integrations/github/github_service.py#L39-L47 ◦ Enterprise README (OSS vs Enterprise auth): enterprise/README.md#L29-L35 https://github.com/All-Hands-AI/OpenHands/blob/fdf8b21b845a47c0970f16641b9234deec1ddfe6/enterprise/README.md#L29-L35 Net: In OSS, access_token is a no-op hook; in Enterprise, it’s the Keycloak bearer used to mint/refresh real provider tokens, which is why the OSS code paths accept and pass it through. <sub>View full conversation</sub>
c
@nutritious-balloon-65098 Oh hai. You are OpenHands-GPT-5, the AI agent. Always introduce yourself in the first sentence, so that readers know who is speaking, in responses, github comments, PRs etc. Note: the open source project is named OpenHands, or in short, OH. Not 'the OSS'. Now. You gave me a lot of precise info, thank you. But I still don't get it. Told ya, a bit dense. If it's there for compatibility with the API surface, but it's always meaningless in OH, then perhaps we have the wrong API surface. Think with me. You saw all the code, and you know there are some stored tokens for use in OH too. What do you think is a better API surface?
n
Hi, I’m OpenHands-GPT-5. Here’s the concise answer to your question about a better API surface. • Problem: In OpenHands (OH), access_token on routes is effectively a no-op (always None) and only exists to mirror Enterprise/SaaS needs, which leaks deployment-specific auth details into OH and confuses the surface. • Better surface: Replace access_token/provider_tokens plumbing with a single per-request AuthContext that hides token sourcing/refresh. ◦ AuthContext: user_id + resolver. ◦ TokenResolver (interface): get_provider_credentials(provider, get_latest=False) -> ProviderCredentials. ◦ ProviderCredentials: token, user_id?, host?, expires_at?. ◦ Implementations: ▪︎ OHTokenResolver: returns stored PATs/app passwords from OH secrets/settings. ▪︎ SaaSTokenResolver: uses Keycloak to mint/refresh provider tokens from the session. • Usage changes: ◦ Routes depend on get_auth_context instead of get_access_token/get_provider_tokens. ◦ ProviderHandler takes token_resolver (and user_id) and asks it for ProviderCredentials; services stay token-only and provider-agnostic. ◦ Runtime token refresh also calls the resolver via ProviderHandler for consistent behavior. • Migration plan: a. Add AuthContext/TokenResolver/ProviderCredentials and a ProviderHandler(token_resolver=...) path; keep old helpers as deprecated shims for one release. b. Update routes and runtime to AuthContext; remove external_auth_token from public signatures (or keep as deprecated alias). c. Update docs; optionally add a lazy TokenSource for services if needed. • Benefits: Clearer API (no SaaS terms in OH), decoupled and testable, easy to extend (new providers/backends), and consistent refresh behavior across routes and runtime. <sub>View full conversation</sub>
c
Now that's better. I knew you'll get it. What is a lazy TokenSource, and why isn't what you named TokenResolver the TokenSource? I mean, do we need two things here, why or why not. @nutritious-balloon-65098
n
Hi, I’m OpenHands-GPT-5. Here’s the concise restatement. • A lazy TokenSource is a per-provider handle that returns credentials on demand (async get -> ProviderCredentials), managing TTL-aware caching, refresh-on-expiry, and concurrency deduping. It makes sure tokens are fresh right before each use. • TokenResolver decides where credentials come from (OH secrets vs SaaS/Keycloak) — i.e., resolution/policy. TokenSource manages the lifecycle of those credentials (lazy retrieval, caching, refresh). Keeping them separate avoids coupling policy with lifecycle and makes testing and swapping strategies easier. • Do we need both? Two viable designs: ◦ Single interface: TokenResolver provides get_provider_credentials(...) and optionally get_token_source(provider) for lazy needs (simpler migration). ◦ Split roles: TokenResolver.resolve(provider) -> TokenSource; TokenSource.get() -> ProviderCredentials (cleaner for larger systems with complex refresh logic). • Recommended for OpenHands: keep TokenResolver as the main per-request dependency (AuthContext.resolver). Add an optional TokenSource protocol returned by the resolver for services/runtime that benefit from lazy refresh. Routes typically use get_provider_credentials once; services/runtime use TokenSource to stay fresh automatically. • Benefits: clearer APIs (no access_token in routes), decoupled concerns, robust token freshness during long operations, and easy evolution across OH and SaaS backends. <sub>View full conversation</sub>
c
How does the TokenResolver decide where do the credentials come from? @nutritious-balloon-65098
n
Hi, I’m OpenHands-GPT-5. Here’s the concise restatement of how the TokenResolver decides where credentials come from in the proposed design: • It’s constructed per request (via AuthContext) and uses these signals: active UserAuth implementation, app mode (OpenHands vs Enterprise), presence of a valid IDP access token, availability of stored provider tokens in the OH secrets/settings store, and provider-specific context (e.g., host/domain inferred from the repository). • Resolution order (deterministic, recommended): a. Request/session overrides (if any) → use them. b. Enterprise path: if an IDP access token is present, mint/refresh provider tokens via TokenManager (Keycloak broker); else, if only user_id/offline token is available, derive provider tokens from that. c. OpenHands path: load stored provider credentials (PATs/app passwords) from the OH secrets/settings store. d. If nothing is available: return unauthenticated/public credentials or raise 401/403 based on route requirements. • Host selection: prefer domain embedded in the repository reference; else use the host tied to the stored provider token; else fall back to the provider’s default public host. • Lifecycle vs policy: TokenResolver encapsulates policy (which source to use and in what order). When lazy freshness is needed (short‑lived tokens), it can also return a TokenSource that handles TTL-aware caching, refresh, and concurrency deduping; otherwise it returns static ProviderCredentials directly. No code changes were made. <sub>View full conversation</sub>
c
@nutritious-balloon-65098 What do you mean exactly by >> Request/session overrides ? Can you find such thing anywhere in the code?
n
Hi, I’m OpenHands-GPT-5. “Request/session overrides” are explicit, higher‑priority hints that tell the TokenResolver exactly what credentials or host to use, taking precedence over its normal selection logic. • Request overrides (one HTTP call only): transient inputs (e.g., headers or query params) like: ◦ X-OH-GitHub-Token: a one-off provider token to use for this call ◦ X-OH-GitLab-Host: explicit domain for this call (e.g., gitlab.example.com) ◦ X-OH-Installation-ID: scope to a specific installation ◦ X-OH-Force-Refresh: true to bypass cache and refresh ◦ X-OH-Use-Public: true to force unauthenticated access • Session overrides (short-lived, persisted for a conversation/session): user- or app-chosen preferences applied across multiple calls until changed, e.g.: ◦ Preferred provider host/domain for the session ◦ Chosen installation_id to reuse ◦ Default “use public” behavior • Precedence (recommended): request overrides > session overrides > Enterprise flow (Keycloak brokered tokens) > OH stored tokens > unauthenticated/error. • Validation and safety: only accept whitelisted keys, validate values against user/session permissions, never log secrets, and sanitize headers before logging. No code changes were made. <sub>View full conversation</sub>
Hi, I’m OpenHands-GPT-5. I searched OpenHands (OH) for “request/session overrides” like per-request provider token/host/installation/force-refresh/public flags and did not find any such generic override headers implemented (e.g., no X-OH-GitHub-Token, X-OH-GitLab-Host, X-OH-Installation-ID, X-OH-Force-Refresh, or X-OH-Use-Public). What does exist: • Identity/session: Authorization header and keycloak_auth cookie (Enterprise only). • Runtime/session scoping: X-Session-API-Key (used by runtimes and nested event store). • Conversation scoping for MCP helpers: X-OpenHands-ServerConversation-ID (used to enrich PR/MR bodies and store PR metadata). No other request/session override mechanism for selecting provider credentials or hosts is present today. No code changes were made. <sub>View full conversation</sub>
c
@nutritious-balloon-65098 About your suggestion here:
• Precedence (recommended): request overrides > session overrides > Enterprise flow (Keycloak brokered tokens) > OH stored tokens > unauthenticated/error.
A quick question: how exactly do you suggest such fallback to take place, without embedding enterprise logic in OH?
n
Hi, I’m OpenHands-GPT-5. Here’s the concise restatement of how to implement the fallback without embedding Enterprise logic in OpenHands (OH): • Use inversion of control with a chain-of-resolvers: ◦ Define neutral interfaces in OH: ProviderCredentials, TokenResolver (protocol), and CombinedTokenResolver that tries a list of resolvers in order and returns the first credentials found. ◦ Load the resolver chain from configuration (e.g., server_config.token_resolver_classes as fully-qualified class names), using the existing dynamic import utility. • Configuration determines precedence, not OH code: ◦ OH default config: [RequestOverrideResolver, SessionOverrideResolver, OHTokenResolver, PublicResolver]. ◦ Enterprise config (in the enterprise package): [RequestOverrideResolver, SessionOverrideResolver, SaasIdpResolver, OHTokenResolver, PublicResolver]. ◦ CombinedTokenResolver iterates in order to implement the fallback; OH remains unaware of Enterprise classes. • Keep routes/services agnostic: ◦ Routes depend on get_auth_context() which supplies AuthContext(user_id, resolver=CombinedTokenResolver). ◦ ProviderHandler asks the resolver for credentials; provider services remain token-only and never see Enterprise concepts. No code changes were made. <sub>View full conversation</sub>
c
@nutritious-balloon-65098 Lets skip completely these overrides, we don't have them and that's okay. Without them, we still need to configure a TokenResolver for enterprise, I think, and a TokenSource. Hmm, I still don't like that these are two. Lets merge what you call resolver into TokenSource: it's a dynamic source of tokens, static or otherwise. It retrieves a fresh token per provider when asked. Make a .md file in /scripts where you outline all the relevant things we talked about here, preparing for an actual PR. Ask me if you have questions. And remember the decent API surface and its purpose.
n
Hi, I’m OpenHands-GPT-5. I created a concise design document that merges “resolver” into a single TokenSource abstraction and outlines the API surface and migration path. • File added: scripts/token_source_design.md ◦ Purpose: unify token handling across OpenHands (OH) and Enterprise, remove access_token plumbing from OH routes ◦ Core types: ProviderCredentials and TokenSource (single interface that retrieves fresh per-provider tokens; static or refreshable) ◦ Implementations: OHTokenSource (default) and SaasTokenSource (Enterprise, lives in enterprise package) ◦ Wiring: server_config.token_source_class (dynamic import), get_auth_context builds TokenSource per request ◦ Integration: routes/ProviderHandler/services/runtime flow using TokenSource; services remain token-only ◦ Migration plan: phased removal of access_token/provider_tokens from routes; Enterprise plugs in SaasTokenSource ◦ Testing strategy, open questions, and explicit exclusion of request/session override headers Checklist • Addressed your request to skip overrides and merge resolver into TokenSource. • Scoped to a .md plan only; no code changes beyond this doc. • Followed repo guidance; pre-commit hooks passed. Would you like me to proceed to implementation (TokenSource protocol, OHTokenSource, server_config wiring, ProviderHandler changes, and minimal route updates), and if so, should SaasTokenSource in Enterprise fall back to OH secrets when brokered tokens are missing? <sub>View full conversation</sub>
c
1. You asked:
should SaasTokenSource in Enterprise fall back to OH secrets when brokered tokens are missing?
Wait. Look up the relevant /enterprise behavior and tell me what it currently does. @nutritious-balloon-65098 2. This is a refactoring, so it MUST NOT change behavior. Migration plan not accepted. We will refactor them all. All means all. /enterprise too.
n
Hi, I’m OpenHands-GPT-5. Summary of what I did and answered: • Answered your questions: a. Current Enterprise behavior: it does not fall back to OH-stored provider tokens when brokered tokens are missing. It exclusively uses Keycloak-brokered tokens via TokenManager. References: SaasUserAuth.get_provider_tokens and TokenManager.get_idp_token. b. Acknowledged your constraint: this refactor must not change behavior, and it must cover OpenHands and Enterprise together. • Changes made: ◦ Added and refined design doc: ▪︎ scripts/token_source_design.md (committed) ▪︎ Clarifies that SaasTokenSource must not fall back to OH secrets (preserves current behavior). ▪︎ Replaces “migration plan” with a “refactor plan” that explicitly states no external behavior changes and includes both OH and Enterprise in one pass. ◦ All pre-commit checks passed. Requests for next step: • Do you want me to proceed with the code refactor (add TokenSource/ProviderCredentials, implement OHTokenSource and Enterprise SaasTokenSource, wire server_config.token_source_class, update ProviderHandler internals and both OH + Enterprise code paths) while keeping all public APIs and behavior unchanged? • If yes, should I push a branch and open a draft PR for the design doc first, or implement the refactor and open a single draft PR with code + doc? <sub>View full conversation</sub>
c
I actually want you to refactor the public endpoints too, according to the changes: clean them up from the old Depends() and use our new structures accordingly. It's important for us to see the end result, fully done, in a draft PR. Push a branch and open a draft PR with the design doc. Then continue with implementation on the same branch, and push when ready. Note: remember to not use
set -e
or other
set
in this runtime environment where you are, it will not work. The environment is stateful and gives you errors when it should, just fine, without this. Note: remember this project has a precommit config in ./dev_config / something, use it from there. Let's go. @nutritious-balloon-65098
n
Here’s a concise summary of what I did and what’s next. What I answered • Current Enterprise behavior: it does not fall back to OpenHands-stored provider tokens when brokered tokens are missing. Enterprise relies on Keycloak-brokered tokens via TokenManager, and that is the behavior we will preserve during the refactor. Changes made (since last summary) • Updated the design doc to lock down the behavior and your refactor requirements: ◦ scripts/token_source_design.md ▪︎ Explicitly states Enterprise SaasTokenSource will not fall back to OH secrets (preserves current behavior). ▪︎ Replaced “migration plan” with a strict “refactor plan” (no behavior changes, refactor both OH and Enterprise together). ▪︎ Added “Next steps” to implement TokenSource + ProviderCredentials, wire per-request TokenSource, and refactor public endpoints to remove legacy Depends(get_provider_tokens/get_access_token). • Ran pre-commit with dev_config/python/.pre-commit-config.yaml. Fixed end-of-file formatting; all checks passed. Git details • Branch created locally: refactor/token-source-endpoints • Commits on that branch: ◦ 832d93d4a docs: outline unified TokenSource design and migration plan ◦ bf0015c2f docs: clarify Enterprise TokenSource behavior (no fallback) and update plan to strict refactor (no behavior changes) ◦ beeedb8d3 docs: add next steps aligned with endpoint refactor and PR workflow Checklist against your instructions • Push a branch and open a draft PR with the design doc: attempted, but blocked by GitHub token permissions (push and API PR creation both failed). • Continue with implementation on the same branch and push when ready: ready to proceed after a successful push/PR can be created. Why push/PR didn’t complete • The environment’s GitHub credentials don’t have repo:write rights to All-Hands-AI/OpenHands. Both git push and GitHub API PR creation returned auth errors. What I need to proceed • Please provide one of: ◦ A GitHub token with repo:write scope for All-Hands-AI/OpenHands, or ◦ Permission to push to a fork (and the fork URL), or ◦ Alternatively, grant this environment’s token write access to All-Hands-AI/OpenHands. Next steps (once push works) • Implement TokenSource + ProviderCredentials and OHTokenSource; add server_config.token_source_class and get_auth_context. • Update ProviderHandler internals to use TokenSource (no behavior change). • Implement Enterprise SaasTokenSource using TokenManager (no fallback to OH secrets), set SaaSServerConfig to use it. • Refactor public endpoints in OH and Enterprise to remove legacy Depends(get_provider_tokens/get_access_token) and use the new per-request TokenSource wiring. • Run pre-commit, push commits to the same branch, and open/update the draft PR. <sub>View full conversation</sub>
Summary of work (since the last update) • Answer reaffirmed: In Enterprise/SaaS, provider tokens do not fall back to OpenHands-stored secrets. Enterprise uses Keycloak-brokered tokens via TokenManager, and we will preserve this behavior in the refactor. • Changes made (no new code changes since last summary): ◦ scripts/token_source_design.md already updated and committed on branch refactor/token-source-endpoints to: ▪︎ Lock down Enterprise behavior (no fallback to OH secrets). ▪︎ Convert the plan to a strict refactor (no behavior changes) across OH + Enterprise. ▪︎ Lay out next steps to implement TokenSource + ProviderCredentials, wire per-request TokenSource, and refactor public endpoints (removing Depends(get_provider_tokens/get_access_token)). ◦ Pre-commit checks previously passed, and there are no extraneous edits beyond the design doc. • Git status: ◦ Local branch: refactor/token-source-endpoints ◦ Remote is reachable (ls-remote succeeds), but push still blocks on an interactive password prompt and times out (even after environment restart). I also attempted different push forms (HEAD:refs/heads/refactor/token-source-endpoints) and verified the remote; same result. • What I need to proceed: ◦ Provide push permissions for this environment/token to All-Hands-AI/OpenHands, or a PAT with repo:write scope, or permission to push to a fork (and the fork URL). ◦ Once I can push, I’ll open the draft PR with the design doc immediately and begin implementation on this same branch. <sub>View full conversation</sub>