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

[REF] Manually verify ID token using PyJWT instead of google_auth #386

Merged
merged 6 commits into from
Dec 2, 2024

Conversation

alyssadai
Copy link
Contributor

@alyssadai alyssadai commented Nov 29, 2024

Changes proposed in this pull request:

  • Switch to using PyJWT library to 'manually' verify ID tokens based on provided IdP public keys, issuers, etc.
  • Switch to Auth0 as IdP

Checklist

This section is for the PR reviewer

  • PR has an interpretable title with a prefix ([ENH], [FIX], [REF], [TST], [CI], [MNT], [INF], [MODEL], [DOC]) (see our Contributing Guidelines for more info)
  • PR has a label for the release changelog or skip-release (to be applied by maintainers only)
  • PR links to GitHub issue with mention Closes #XXXX
  • Tests pass
  • Checks pass
  • If the PR changes the SPARQL query template, the default Neurobagel query file has also been regenerated

For new features:

  • Tests have been added

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

Summary by Sourcery

Replace Google ID token verification with manual verification using PyJWT, updating the security module to use PyJWT for token decoding and verification. Update the OAuth2 authorization URL to use Auth0 instead of Google. Modify the requirements to remove google-auth and add necessary dependencies for PyJWT.

Enhancements:

  • Replace Google ID token verification with manual verification using PyJWT.

Build:

  • Remove google-auth from requirements and add PyJWT, cffi, and cryptography packages.

@alyssadai alyssadai added release Create a release when this PR is merged pr-internal Non-user-facing code improvement, will increment patch version when merged (0.0.+1) labels Nov 29, 2024
Copy link

sourcery-ai bot commented Nov 29, 2024

Reviewer's Guide by Sourcery

This PR replaces Google's authentication library with PyJWT for ID token verification. The implementation involves manually verifying JWT tokens using PyJWT's JWKS client to fetch signing keys from Auth0's JWKS endpoint. The changes maintain the same security guarantees while switching the authentication provider from Google to Auth0.

Sequence diagram for ID token verification process

sequenceDiagram
    participant Client
    participant Server
    participant Auth0

    Client->>Server: Send ID token
    Server->>Auth0: Fetch signing keys from JWKS endpoint
    Auth0-->>Server: Return signing keys
    Server->>Server: Verify token using PyJWT
    Server-->>Client: Return verification result
Loading

Updated class diagram for security module

classDiagram
    class Security {
        +check_client_id()
        +verify_token(token: str)
    }

    class PyJWKClient {
        +get_signing_key_from_jwt(jwt: str)
    }

    class PyJWTError

    Security --> PyJWKClient : uses
    Security --> PyJWTError : handles exception
Loading

File-Level Changes

Change Details Files
Replace Google Auth with PyJWT for token verification
  • Add PyJWKClient initialization with Auth0's JWKS endpoint
  • Update token verification logic to use PyJWT's decode function
  • Configure JWT verification options to require specific claims
  • Replace GoogleAuthError with PyJWTError in exception handling
app/api/security.py
Update authentication configuration and dependencies
  • Remove google-auth dependency
  • Add PyJWT and cryptography dependencies
  • Update OAuth2 authorization URL to point to Auth0 endpoint
requirements.txt
app/api/routers/query.py

Assessment against linked issues

Issue Objective Addressed Explanation
#385 Switch from google_auth package to PyJWT for manual JWT verification
#385 Implement manual JWT token verification with public keys
#385 Remove google_auth dependency

Possibly linked issues


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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @alyssadai - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • The Bearer scheme verification should be implemented rather than left as a TODO (link)
  • Replace print statement with proper logging (link)
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🔴 Security: 2 blocking issues
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

app/api/security.py Show resolved Hide resolved
app/api/security.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 29, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Please upload report for BASE (main@ef2e533). Learn more about missing BASE report.

Files with missing lines Patch % Lines
app/api/security.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #386   +/-   ##
=======================================
  Coverage        ?   97.05%           
=======================================
  Files           ?       24           
  Lines           ?      850           
  Branches        ?        0           
=======================================
  Hits            ?      825           
  Misses          ?       25           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@surchs surchs self-requested a review December 2, 2024 13:47
Copy link
Contributor

@surchs surchs left a comment

Choose a reason for hiding this comment

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

Nice, thanks @alyssadai !

🧑‍🍳

@surchs
Copy link
Contributor

surchs commented Dec 2, 2024

neurobagel/query-tool#380 is merged, @alyssadai you can merge this too

@alyssadai alyssadai merged commit 90aa09a into main Dec 2, 2024
9 checks passed
@alyssadai alyssadai deleted the validate-auth-token-manually branch December 2, 2024 23:28
Copy link
Contributor

neurobagel-bot bot commented Dec 2, 2024

🚀 PR was released in v0.4.3 🚀

@neurobagel-bot neurobagel-bot bot added the released This issue/pull request has been released. label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-internal Non-user-facing code improvement, will increment patch version when merged (0.0.+1) release Create a release when this PR is merged released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch to manual JWT verification of ID token from IdP
2 participants