Skip to content

feat: add PKCE support to OAuth2 authorization code flow - #266

Merged
iFurySt merged 1 commit into
AmoyLab:mainfrom
appleboy:pkce
Oct 11, 2025
Merged

iFurySt merged 1 commit into
AmoyLab:mainfrom
appleboy:pkce

Conversation

@appleboy

@appleboy appleboy commented Oct 8, 2025 •

Copy link
Copy Markdown
Contributor
  • Add dependency on golang.org/x/oauth2 for PKCE (Proof Key for Code Exchange) support
  • Include code challenge and code challenge method fields in the AuthorizationCode struct
  • Implement PKCE code challenge validation in the authorization code grant flow
  • Replace custom S256 hashing with oauth2 library's S256ChallengeFromVerifier function
  • Add comprehensive tests for PKCE flows, including correct/incorrect verifier, missing verifier, plain method, and non-PKCE backward compatibility

Summary by Sourcery

Enable PKCE in the OAuth2 authorization code grant by extending the AuthorizationCode model, implementing challenge verification using the oauth2 library, and covering the new behavior with thorough tests

New Features:

  • Add PKCE support to the OAuth2 authorization code flow by storing and validating code challenges and methods
  • Introduce a dependency on golang.org/x/oauth2 for computing S256 code challenges

Enhancements:

  • Replace custom S256 hashing logic with oauth2.S256ChallengeFromVerifier

Tests:

  • Add comprehensive tests for PKCE flows including valid and invalid S256 verifiers, missing verifier, plain method, and non-PKCE backward compatibility

- Add dependency on golang.org/x/oauth2 for PKCE (Proof Key for Code Exchange) support
- Include code challenge and code challenge method fields in the AuthorizationCode struct
- Implement PKCE code challenge validation in the authorization code grant flow
- Replace custom S256 hashing with oauth2 library's S256ChallengeFromVerifier function
- Add comprehensive tests for PKCE flows, including correct/incorrect verifier, missing verifier, plain method, and non-PKCE backward compatibility

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

sourcery-ai Bot commented Oct 8, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR adds full PKCE support to the OAuth2 authorization code flow by extending the data model with code challenge fields, implementing verifier validation using the golang.org/x/oauth2 helper, and ensuring coverage with comprehensive tests.

Sequence diagram for PKCE validation in OAuth2 authorization code grant flow

sequenceDiagram
participant Client
participant "OAuth2 Server"
participant "Storage"
Client->>"OAuth2 Server": POST /token (code, code_verifier)
"OAuth2 Server"->>"Storage": Retrieve AuthorizationCode
"OAuth2 Server"->>"OAuth2 Server": computeCodeChallenge(code_verifier, code_challenge_method)
"OAuth2 Server"->>"OAuth2 Server": Compare computedChallenge with stored code_challenge
alt Challenge matches
    "OAuth2 Server"->>Client: Issue access token
else Challenge does not match
    "OAuth2 Server"->>Client: Return error (invalid_grant)
end
Loading

Class diagram for updated AuthorizationCode struct with PKCE fields

classDiagram
class AuthorizationCode {
  string Code
  string ClientID
  string RedirectURI
  string[] Scope
  string CodeChallenge
  string CodeChallengeMethod
  int64 ExpiresAt
  int64 CreatedAt
}
Loading

File-Level Changes

Change Details Files
Extend authorization code model and store PKCE parameters
  • Added CodeChallenge and CodeChallengeMethod fields to AuthorizationCode struct
  • Populate PKCE fields when saving authorization codes in Authorize()
internal/auth/oauth.go
internal/auth/storage/store.go
Implement PKCE challenge validation in token exchange
  • Reject missing verifier when a challenge is present
  • Compute and compare code challenge via computeCodeChallenge()
  • Return appropriate errors for invalid or mismatched verifiers
internal/auth/oauth.go
Switch S256 hashing to oauth2 library helper
  • Removed custom sha3-based hashing in computeCodeChallenge()
  • Use oauth2.S256ChallengeFromVerifier for S256
  • Added golang.org/x/oauth2 dependency
internal/auth/oauth.go
go.mod
Add PKCE flow test coverage
  • Created TestPKCE_CodeVerifier with subtests for success, wrong/missing verifier, plain method, and backward compatibility
internal/auth/oauth_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:

  • Enforce the PKCE code_verifier length (43–128 characters) before computing the challenge to fully adhere to RFC7636 requirements.
  • Default the code_challenge_method to “plain” when the parameter is omitted in the authorization request to conform with RFC7636 spec.
  • Extract the repeated PKCE test setup into helper functions to reduce duplication and improve readability of TestPKCE_CodeVerifier.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Enforce the PKCE code_verifier length (43–128 characters) before computing the challenge to fully adhere to RFC7636 requirements.
- Default the code_challenge_method to “plain” when the parameter is omitted in the authorization request to conform with RFC7636 spec.
- Extract the repeated PKCE test setup into helper functions to reduce duplication and improve readability of TestPKCE_CodeVerifier.

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.

LGTM! thanks!

@iFurySt
iFurySt merged commit 6aa6f03 into AmoyLab:main Oct 11, 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