Repository navigation
feat: improve auth code grant with expiry checks and specific errors - #258
Merged
Merged
Conversation
- 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]>
Contributor
Reviewer's GuideThis 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 validationsequenceDiagram
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
Class diagram for updated MemoryStorage and RedisStorage expiry logicclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
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:
Enhancements:
Tests: