Skip to content

feat: improve auth code grant with expiry checks and specific errors - #258

Merged
iFurySt merged 1 commit into
AmoyLab:mainfrom
appleboy:oauth
Oct 4, 2025
Merged

iFurySt merged 1 commit into
AmoyLab:mainfrom
appleboy:oauth

Conversation

@appleboy

@appleboy appleboy commented Oct 4, 2025 •

Copy link
Copy Markdown
Contributor
  • Change error returns for expired authorization codes and tokens from "invalid_grant" to more specific errors ("authorization_code_expired" and "token_expired")
  • Add client ID validation for authorization code grant flow
  • Improve expiry logic in memory storage with TOCTOU protection
  • Update unit tests to check for new error types for expired codes and tokens
  • Add new tests for edge cases in authorization code expiry and client ID validation

Summary by Sourcery

Enhance OAuth authorization code grant by introducing specific error codes for expirations, enforcing client ID checks, and bolstering storage expiry logic with TOCTOU protection while updating tests accordingly.

New Features:

  • Return distinct errors for expired authorization codes and tokens instead of a generic invalid_grant
  • Validate client ID in the authorization code grant flow

Enhancements:

  • Implement TOCTOU-safe expiry checks and deletion in in-memory storage

Tests:

  • Update existing tests to expect new specific expiry error types
  • Add edge-case tests for authorization code expiry and client ID validation

- Change error returns for expired authorization codes and tokens from "invalid_grant" to more specific errors ("authorization_code_expired" and "token_expired")
- Add client ID validation for authorization code grant flow
- Improve expiry logic in memory storage with TOCTOU protection
- Update unit tests to check for new error types for expired codes and tokens
- Add new tests for edge cases in authorization code expiry and client ID validation

Signed-off-by: appleboy <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 4, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR refines the OAuth code grant flow by strengthening expiry handling with TOCTOU-safe deletion, introducing specific errors for expired authorization codes and tokens, enforcing client ID validation, and updating tests to cover these enhancements.

Sequence diagram for improved authorization code grant flow with expiry and client ID validation

sequenceDiagram
actor User
participant Client
participant "OAuth Server"
participant "Storage (Memory/Redis)"
User->>Client: Request token with authorization code
Client->>"OAuth Server": POST /token (code, client_id)
"OAuth Server"->>"Storage (Memory/Redis)": GetAuthorizationCode(code)
alt Code not found
    "Storage (Memory/Redis)"-->>"OAuth Server": error: invalid_grant
    "OAuth Server"-->>Client: error: invalid_grant
else Code expired
    "Storage (Memory/Redis)"-->>"OAuth Server": error: authorization_code_expired
    "OAuth Server"-->>Client: error: authorization_code_expired
else Code found
    "Storage (Memory/Redis)"-->>"OAuth Server": AuthorizationCode
    "OAuth Server"->>"OAuth Server": Validate client_id
    alt Client ID mismatch
        "OAuth Server"-->>Client: error: invalid_client
    else Client ID matches
        "OAuth Server"->>"OAuth Server": Issue token
        "OAuth Server"-->>Client: access_token
    end
end
Loading

Class diagram for updated MemoryStorage and RedisStorage expiry logic

classDiagram
class MemoryStorage {
  +GetAuthorizationCode(ctx, code) *AuthorizationCode, error
  +GetToken(ctx, accessToken) *Token, error
  authorizationCodes map[string]*AuthorizationCode
  tokens map[string]*Token
}
class RedisStorage {
  +GetAuthorizationCode(ctx, code) *AuthorizationCode, error
  +GetToken(ctx, accessToken) *Token, error
}
class AuthorizationCode {
  +ClientID string
  +ExpiresAt int64
}
class Token {
  +ExpiresAt int64
}
MemoryStorage --> AuthorizationCode
MemoryStorage --> Token
RedisStorage --> AuthorizationCode
RedisStorage --> Token
Loading

File-Level Changes

Change Details Files
Enhance expiry logic in memory and Redis storage with TOCTOU protection and specific error responses
  • Extract entry under read lock, then unlock before deletion
  • Acquire write lock for expired entries and double-check before removal
  • Return ErrAuthorizationCodeExpired or ErrTokenExpired instead of ErrInvalidGrant
internal/auth/storage/memory.go
internal/auth/storage/redis.go
Validate client ID during authorization code grant
  • After retrieving the auth code, compare its ClientID to the requesting client’s ID
  • Return ErrInvalidClient if they don’t match instead of a generic error
internal/auth/oauth.go
Update and extend unit tests for expiry and client ID validation
  • Change test assertions from ErrInvalidGrant to ErrAuthorizationCodeExpired and ErrTokenExpired
  • Add edge-case tests for expired codes and wrong client IDs in authorization code grant flow
internal/auth/oauth_test.go
internal/auth/storage/memory_test.go
internal/auth/storage/redis_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Consolidate the duplicate TOCTOU expiry-check and delete logic in memory storage for both tokens and authorization codes into a shared helper to simplify locking and reduce duplication.
  • In handleAuthorizationCodeGrant, consider mapping storage-specific errors to consistent OAuth2 error responses instead of returning raw errors directly, to avoid leaking implementation details into the API layer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consolidate the duplicate TOCTOU expiry-check and delete logic in memory storage for both tokens and authorization codes into a shared helper to simplify locking and reduce duplication.
- In handleAuthorizationCodeGrant, consider mapping storage-specific errors to consistent OAuth2 error responses instead of returning raw errors directly, to avoid leaking implementation details into the API layer.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@iFurySt iFurySt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome! Thanks!

@iFurySt
iFurySt merged commit 5b255fb into AmoyLab:main Oct 4, 2025
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