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

🔏 Requires valid access tokens for API calls #173

Merged
merged 5 commits into from
Oct 25, 2023
Merged

🔏 Requires valid access tokens for API calls #173

merged 5 commits into from
Oct 25, 2023

Conversation

afred
Copy link
Contributor

@afred afred commented Oct 12, 2023

Verified Access Token

  • Adds a verified access token as a dependency to API endpoints, including;
    • POST /api/events/
    • POST /api/sony_ci/sync
  • This same dependency can now be added to any API endpoint.
  • The verified access token dependency is resolved by:
    1. Fetching the unverified access token from the HTTP request header.
    2. Verifying the access key by decoding it with the signing key.
      a. In the production and development environments, the signing key is the
      public half of a public/private key pair available with from our Auth0 account.
      b. In test environment a fake signing key is used for both encoding and decoding.
  • Adds tests for testing the authentication and authorization dependencies of the API
    endpoints.
    • The testing pattern involves a factory for generating a fake access tokens with
      whatever permissions the test is concerned with testing.
  • Refactors the FastAPI dependencies for API endpoints for better testability.

Misc

  • Removes unused launch config for tests. Use VSCode pytest settings instead.
  • Changes param name from allow_redirects to follow_redirects to fix deprecation warning
  • Adds git-guardian cache dir to git ignore
  • Adds constants to config used for verifying access token and specifying the authorization audience.
  • Renames admin_user dependency to to get_admin_user for consistency.
  • Renames get_user to get_oauth_user for specificity -- i.e. not a User model from the Chowda db.
  • Adds pyjwt for dealing with Javascript Web Tokens (jwt).
  • Removes test for api calls to create batches - not needed.
  • Removes .env files from revision, but leaves a sample template: .env.sample.
  • Prefixes all env vars and constants having to do with Auth0 with "AUTH0_" for consistency.
  • Sets test db used in GitHub CI workflow to use name 'chowda' and default port 5432.

Closes #153

@codecov-commenter
Copy link

codecov-commenter commented Oct 18, 2023

Codecov Report

Merging #173 (04e6c8c) into main (8931f04) will increase coverage by 1.22%.
The diff coverage is 68.96%.

@@            Coverage Diff             @@
##             main     #173      +/-   ##
==========================================
+ Coverage   66.86%   68.08%   +1.22%     
==========================================
  Files          20       20              
  Lines         848      893      +45     
==========================================
+ Hits          567      608      +41     
- Misses        281      285       +4     
Files Coverage Δ
chowda/app.py 100.00% <100.00%> (ø)
chowda/auth/oauth.py 66.66% <ø> (ø)
chowda/config.py 100.00% <100.00%> (ø)
chowda/routers/dashboard.py 42.85% <100.00%> (-3.81%) ⬇️
chowda/routers/sony_ci.py 100.00% <100.00%> (ø)
chowda/routers/events.py 46.51% <65.00%> (+20.79%) ⬆️
chowda/auth/utils.py 70.96% <77.77%> (+11.70%) ⬆️
chowda/views.py 44.70% <9.09%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@afred afred temporarily deployed to Codecov October 18, 2023 19:56 — with GitHub Actions Inactive
* Adds a verified access token as a dependency to API endpoints, including;
  * POST /api/events/
  * POST /api/sony_ci/sync
* This same dependency can now be added to any API endpoint.
* The verified access token dependency is resolved by:
  1. Fetching the unverified access token from the HTTP request header.
  2. Verifying the access key by decoding it with the signing key.
     a. In the production and development  environments, the signing key is the
        public half of a public/private key pair available with from our Auth0 account.
     b. In test environment a fake signing key is used for both encoding and decoding.
* Adds tests for testing the authentication and authorization dependencies of the API
  endpoints.
  * The testing pattern involves a factory for generating a fake access tokens with
    whatever permissions the test is concerned with testing.
* Refactors the FastAPI dependencies for API endpoints for  better testability.

Also:
* Removes unused launch config for tests. Use vscode pytest settings instead.
* Changes param name from allow_redirects to follow_redirects to fix deprecation warning
* Adds git-guardian cache dir to git ignore
* Adds constants to config used for verifying access token and specifying the authorization 'audience'.
* Renames admin_user dependency to  to 'get_admin_user' for consistency.
* Renames get_user to get_oauth_user for specificity -- i.e. not a User model from the Chowda db.
* Adds pyjwt for dealing with Javascript Web Tokens (jwt).
* Removes test for api calls to create batches - not needed.
* Removes .env files from revision, but leaves a sample template: '.env.sample'.
* Prefixes all env vars and constants having to do with Auth0 with "AUTH0_" for consistency.
* Sets test db used in github CI workflow to use name 'chowda' and default port 5432.

Closes #153.
@afred afred temporarily deployed to Codecov October 19, 2023 13:39 — with GitHub Actions Inactive
@afred afred temporarily deployed to Codecov October 19, 2023 13:39 — with GitHub Actions Inactive
@afred afred marked this pull request as ready for review October 19, 2023 13:47
@afred afred changed the title 🔏 API auth 🔏 Requires valid access tokens for API calls Oct 19, 2023
@afred afred requested a review from mrharpo October 19, 2023 14:38
Copy link
Contributor

@mrharpo mrharpo left a comment

Choose a reason for hiding this comment

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

👍 🔏 Just some minor fixes, plus I've added a few more tests.

Comment on lines 28 to 32
class OAuthIDToken(BaseModel):
"""OAuth ID Token with User info"""


class OAuthUser(BaseModel):
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these supposed to be the same thing?

@@ -12,7 +14,9 @@ class SyncResponse(BaseModel):


@sony_ci.post('/sync', tags=['sync'])
async def sony_ci_sync() -> SyncResponse:
async def sony_ci_sync(
token: Annotated[OAuthAccessToken, Depends(verified_access_token)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to check for sync:sonyci permissions here.

@mrharpo mrharpo temporarily deployed to Codecov October 20, 2023 16:54 — with GitHub Actions Inactive
@gitguardian
Copy link

gitguardian bot commented Oct 20, 2023

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secret in your pull request
GitGuardian id Secret Commit Filename
8543931 Bearer Token 2dab4f9 tests/routers/test_events.py View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

@mrharpo mrharpo temporarily deployed to Codecov October 20, 2023 16:54 — with GitHub Actions Inactive
Adds tests for Sony Ci sync API endpoint.
@afred afred temporarily deployed to Codecov October 23, 2023 18:16 — with GitHub Actions Inactive
@afred afred temporarily deployed to Codecov October 23, 2023 18:16 — with GitHub Actions Inactive
@afred afred requested a review from mrharpo October 23, 2023 18:48
@mrharpo mrharpo temporarily deployed to Codecov October 24, 2023 20:54 — with GitHub Actions Inactive
@mrharpo mrharpo temporarily deployed to Codecov October 24, 2023 20:54 — with GitHub Actions Inactive
Copy link
Contributor Author

@afred afred left a comment

Choose a reason for hiding this comment

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

👍 from me. I the rest is good, then :shipit:

@mrharpo mrharpo temporarily deployed to Codecov October 24, 2023 21:36 — with GitHub Actions Inactive
@mrharpo mrharpo temporarily deployed to Codecov October 24, 2023 21:36 — with GitHub Actions Inactive
Copy link
Contributor

@mrharpo mrharpo left a comment

Choose a reason for hiding this comment

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

👍 👍

@afred afred merged commit c648f63 into main Oct 25, 2023
@afred afred deleted the 153-api-auth branch October 25, 2023 18:28
mrharpo added a commit that referenced this pull request Oct 25, 2023
* main:
  🔏 Requires valid access tokens for API calls (#173)
mrharpo added a commit that referenced this pull request Feb 26, 2024
* main:
  🍼 Ensure pip (#195)
  Adds pip to venv (#192)
  Adds venv to start of PATH (#191)
  Activates venv in production image (#190)
  🎬 Row actions (#188)
  🤖 Update dependencies (#186)
  📩 MMIF Download (#182)
  ⛓️ Metaflow links (#185)
  🎱 Fields: `Finished`, `Successful` (#184)
  ➕ Add MMIF to batch (#181)
  🤖 Update dependencies (#177)
  🔏 Requires valid access tokens for API calls (#173)
  🏞 `MMIF` source  (#168)
  ⌛ Expires all (#172)
  🤖 Update dependencies (#171)
  📄 Page title (#162)
  Fixes MediaFilesGuidsField, combines usage (#170)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Setup API token authentication
3 participants