feat: refresh expiring OAuth tokens before dispatch - #26
Merged
Conversation
Signed-off-by: Shreyansh Sancheti <43677304+shreyanshjain7174@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds automatic OAuth token refresh to the gateway dispatch path. It refreshes tokens that expire within 5 minutes, updates the encrypted vault, and blocks upstream calls when refresh fails.
Changes:
- Refresh expiring OAuth tokens before upstream dispatch via per-service refresh functions.
- Reuse a shared token-exchange parser for both auth-code and refresh-token flows.
- Add tests covering refresh success, rotation persistence, and refresh failure blocking.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents pre-dispatch OAuth refresh behavior and failure semantics. |
| internal/oauth/refresh_test.go | Adds a unit test for the refresh-token exchange and rotation behavior. |
| internal/oauth/handler.go | Introduces NewRefreshFunc and refactors token exchange parsing for reuse. |
| internal/gateway/gateway.go | Hooks token refresh into /v1/act execution before upstream requests. |
| internal/gateway/gateway_test.go | Adds integration tests ensuring refresh happens and failures are receipted. |
| cmd/agentgw/main.go | Wires configured OAuth providers into gateway refreshers at startup. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+403
to
+412
| if refresher := s.cfg.Refreshers[att.req.Service]; refresher != nil { | ||
| tok, err = vault.GetOrRefresh(s.cfg.Vault, att.req.OnBehalfOf, att.req.Service, refresher) | ||
| if err != nil { | ||
| return &outcome{ | ||
| status: http.StatusForbidden, | ||
| body: ErrorResponse{Error: "token expired — user must re-authenticate", Code: "token_expired"}, | ||
| policyDecision: "allow", | ||
| errorCode: "token_expired", | ||
| } | ||
| } |
Comment on lines
+178
to
182
| func exchangeToken(provider *Provider, data url.Values) (*vault.Token, time.Duration, error) { | ||
| resp, err := http.PostForm(provider.TokenURL, data) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("oauth: exchange: %w", err) | ||
| return nil, 0, fmt.Errorf("oauth: exchange: %w", err) | ||
| } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changed:
Verification:
All passed locally.