Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PKCE verifier for OIDC #1812

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

add PKCE verifier for OIDC #1812

wants to merge 3 commits into from

Conversation

Rorical
Copy link

@Rorical Rorical commented Mar 5, 2024

https://www.rfc-editor.org/rfc/rfc7636.html
To fix the error "Could not exchange code for the token" when using the PKCE method, a verifier should be generated and used during the authentication process.

  • read the CONTRIBUTING guidelines
  • raised a GitHub issue or discussed it on the projects chat beforehand
  • added unit tests
  • added integration tests
  • updated documentation if needed
  • updated CHANGELOG.md

Summary by CodeRabbit

  • New Features

    • Enhanced OpenID Connect (OIDC) registration and callback process with Proof Key for Code Exchange (PKCE) for improved security.
    • Introduced a new data structure, RegistrationInfo, to manage OIDC-related information effectively.
  • Bug Fixes

    • Improved validation processes to ensure secure handling of authorization code exchanges.

@kradalby kradalby added the OIDC OpenID Connect related issues label Jul 18, 2024
Copy link
Contributor

coderabbitai bot commented Sep 15, 2024

Walkthrough

The changes enhance the OpenID Connect (OIDC) registration and callback process by implementing Proof Key for Code Exchange (PKCE). A new RegistrationInfo structure is introduced to store both the machine key and the PKCE verifier. The RegisterOIDC and OIDCCallback functions are modified to handle this new structure, improving the security of the authorization code exchange process.

Changes

File Change Summary
hscontrol/oidc.go Introduced PKCE support by adding a verifier to the registration cache and modifying related functions.
hscontrol/types/node.go Added a new RegistrationInfo struct to encapsulate MachineKey and Verifier for OIDC validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Client
    participant OIDCProvider
    participant Headscale

    User->>Client: Initiate OIDC registration
    Client->>Headscale: RegisterOIDC(machKey, verifier)
    Headscale-->>Client: Store RegistrationInfo

    User->>OIDCProvider: Authorise
    OIDCProvider-->>User: Redirect with auth code
    User->>Client: Redirect back with auth code
    Client->>Headscale: OIDCCallback(authCode, regInfo)
    Headscale->>Headscale: Validate RegistrationInfo
    Headscale->>OIDCProvider: Exchange auth code for token
    OIDCProvider-->>Headscale: Return access token
    Headscale-->>Client: Return access token
Loading

🐰 In the meadow, a change so bright,
With PKCE, we leap to new heights.
RegistrationInfo, a clever new friend,
Ensures our security will never bend.
Hopping along, we celebrate this feat,
In the world of OIDC, we can't be beat! 🌼


Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fe68f50 and 64fd5f4.

Files selected for processing (2)
  • hscontrol/oidc.go (6 hunks)
  • hscontrol/types/node.go (1 hunks)
Additional comments not posted (6)
hscontrol/types/node.go (1)

562-566: LGTM!

The new RegistrationInfo struct looks good. It groups the MachineKey and Verifier fields, which seem to be related to OIDC registration. The field names are clear and the types appear appropriate based on the context.

Grouping this information in a struct can help in organizing the code and passing the registration data around more easily.

hscontrol/oidc.go (5)

136-144: LGTM!

Generating a PKCE code verifier and storing it alongside the node key in the state cache is a good security practice. It enhances the security of the authorization code exchange process by preventing certain attacks.


188-212: Proper state validation!

The code segment correctly retrieves the state from the registration cache and validates it. Appropriate error responses are generated for invalid or expired states, which helps maintain security.


313-315: Secure token exchange!

Using the stored verifier when exchanging the authorization code for an access token is a good security practice. It ensures that the authorization code was obtained from a valid authorization request, preventing certain attacks.


477-487: Simplified node validation!

Using the RegistrationInfo directly instead of retrieving the machine key from the state cache simplifies the code and reduces the chances of errors. This is a good refactoring.


549-549: Consistent machine key retrieval!

Returning the machine key from the RegistrationInfo is consistent with the changes made to the validateNodeForOIDCCallback function. This maintains the integrity of the refactoring.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kradalby
Copy link
Collaborator

I imagine this can go in and be rebased on top of #2020, whenever that is ready.

@kradalby
Copy link
Collaborator

@Rorical if you have time to rebase this in the next few days, then it might be able to make it into the next release.

@Rorical
Copy link
Author

Rorical commented Nov 29, 2024

This could be resolved soon. I'm away from my headscale instance for months and haven't realized current status of this project, so I need some time to check what's going on.

Based on the purpose of pkce, I recommend to add a oidc config parameter EnablePKCE, since pkce will only provide a extra layer of security and totally compitable with currect setup. However, user should have the option to enable or disable this feature.

@Rorical
Copy link
Author

Rorical commented Nov 29, 2024

Testing and documentation will be added soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OIDC OpenID Connect related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants