Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: make authorization code usage atomic and race-free#37

Merged
appleboy merged 3 commits intomainfrom
auth2
Feb 19, 2026
Merged

fix: make authorization code usage atomic and race-free#37
appleboy merged 3 commits intomainfrom
auth2

Conversation

@appleboy
Copy link
Member

  • 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]>
Copilot AI review requested due to automatic review settings February 19, 2026 00:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ErrAuthCodeAlreadyUsed error for handling already-consumed authorization codes at both store and service layers
  • Made MarkAuthorizationCodeUsed atomic by adding used_at IS NULL to the WHERE clause and checking affected rows
  • Replaced SELECT-then-INSERT/UPDATE pattern in UpsertUserAuthorization with atomic INSERT 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]>
@appleboy appleboy merged commit 0b46283 into main Feb 19, 2026
16 checks passed
@appleboy appleboy deleted the auth2 branch February 19, 2026 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants