Deduplicate OAuth2/OIDC link nonce creation#287
Merged
Conversation
6 tasks
Copilot
AI
changed the title
[WIP] Refactor CreateLinkNonce method to eliminate duplication
Deduplicate OAuth2/OIDC link nonce creation
May 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces maintenance risk in the account-linking flow by deduplicating the OAuth2 and OIDC implementations of link-nonce creation into a shared helper while keeping each handler’s existing nonce generation approach and TTL.
Changes:
- Added a shared
createLinkNoncehelper inhandler/oauth2_common.goto centralize nonce generation, hashing, persistence, and JSON response writing. - Updated
OIDCHandler.CreateLinkNonceandOAuth2Handler.CreateLinkNonceto delegate to the shared helper.
Show a summary per file
| File | Description |
|---|---|
| handler/oidc.go | Replaces the inline OIDC link-nonce creation logic with a call to the shared helper. |
| handler/oauth2.go | Replaces the inline OAuth2 link-nonce creation logic with a call to the shared helper, preserving its nonce generation method. |
| handler/oauth2_common.go | Introduces the shared createLinkNonce helper that encapsulates the link-nonce lifecycle. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 1
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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.
CreateLinkNoncehad two near-identical implementations inOAuth2HandlerandOIDCHandler, which created avoidable maintenance risk around nonce generation, hashing, persistence, and response handling. This change consolidates that flow into one shared path while preserving each handler’s existing nonce-generation entry point and TTL.Shared link-nonce flow
createLinkNonceinhandler/oauth2_common.gofor the common account-linking nonce lifecycle:Handler delegation
CreateLinkNoncebodies in:OAuth2HandlerOIDCHandlerBehavior preserved
auth.GenerateRandomBase64(32)generateOIDCStateGreptile Summary
This PR eliminates duplicated
CreateLinkNonceimplementations inOAuth2HandlerandOIDCHandlerby extracting a sharedcreateLinkNoncehelper inoauth2_common.go. Both handlers delegate to it while retaining their original nonce generators and TTLs.handler/oauth2_common.go: NewcreateLinkNoncefunction handles the full lifecycle — store nil-guard, user-ID extraction, nonce generation via injected func, high-entropy hashing, persistence, and JSON response.handler/oauth2.go/handler/oidc.go: EachCreateLinkNoncemethod is replaced with a single delegation call, with no change to observable behavior.Confidence Score: 5/5
Safe to merge — the refactor is a straightforward delegation with no behavioral changes to nonce generation, hashing, persistence, or response handling.
Both handlers preserve their original nonce generators and TTLs; the shared helper replicates the exact same control flow that existed in both originals. No logic has been altered.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant Client participant OAuth2Handler/OIDCHandler participant createLinkNonce participant Store as auth.OIDCLinkNonceStore Client->>OAuth2Handler/OIDCHandler: POST /link-nonce OAuth2Handler/OIDCHandler->>createLinkNonce: delegate(w, r, store, ttl, generateNonce) createLinkNonce->>createLinkNonce: store nil-guard createLinkNonce->>createLinkNonce: auth.UserIDFromContext(r.Context()) createLinkNonce->>createLinkNonce: generateNonce() createLinkNonce->>createLinkNonce: auth.HashHighEntropyToken(nonce) createLinkNonce->>Store: CreateLinkNonce(ctx, userID, hash, expiry) Store-->>createLinkNonce: ok / err createLinkNonce-->>Client: "200 {nonce} or error"Prompt To Fix All With AI
Reviews (2): Last reviewed commit: "Update handler/oauth2_common.go" | Re-trigger Greptile