Conversation
Member
appleboy
commented
Feb 19, 2026
- Make marking authorization codes as used atomic, ensuring only one concurrent request succeeds and preventing replay attacks
- Introduce ErrAuthCodeAlreadyUsed error for handling already-consumed authorization codes
- Use INSERT ON CONFLICT DO UPDATE for atomic upsert of user authorizations, eliminating race conditions
- Add test to verify that double usage of an authorization code returns the correct error
- Make marking authorization codes as used atomic, ensuring only one concurrent request succeeds and preventing replay attacks - Introduce ErrAuthCodeAlreadyUsed error for handling already-consumed authorization codes - Use INSERT ON CONFLICT DO UPDATE for atomic upsert of user authorizations, eliminating race conditions - Add test to verify that double usage of an authorization code returns the correct error Signed-off-by: appleboy <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request implements atomic database operations to prevent race conditions in the OAuth 2.0 authorization code flow. The changes ensure that authorization codes can only be used once, even under concurrent request scenarios, and that user authorization records are safely upserted without race conditions.
Changes:
- Introduced
ErrAuthCodeAlreadyUsederror for handling already-consumed authorization codes at both store and service layers - Made
MarkAuthorizationCodeUsedatomic by addingused_at IS NULLto the WHERE clause and checking affected rows - Replaced SELECT-then-INSERT/UPDATE pattern in
UpsertUserAuthorizationwith atomicINSERT ON CONFLICT DO UPDATE - Added store-level test to verify double usage of authorization codes returns the correct error
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/store/errors.go | Adds new ErrAuthCodeAlreadyUsed error for atomic code consumption detection |
| internal/store/sqlite.go | Implements atomic operations: marks auth codes as used with row count validation, and uses ON CONFLICT for user authorization upserts |
| internal/services/authorization.go | Handles new error type from store layer and provides proper error propagation |
| internal/store/store_test.go | Adds test verifying atomic behavior of authorization code marking |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Include "client_id" in the upsert operation for user authorization Signed-off-by: appleboy <[email protected]>
- Add tests covering upsert logic for user authorization records, including creation, updating existing records, reactivation after revocation, and handling multiple users per application - Ensure ApplicationID is populated in test data for authorization codes Signed-off-by: appleboy <[email protected]>
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.