Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

1004 OAuth2 client credentials flow #1159

Merged
merged 12 commits into from
Aug 29, 2022
Merged

Conversation

galvana
Copy link
Collaborator

@galvana galvana commented Aug 25, 2022

Purpose

Adding the client credential OAuth2 flow. We previously only supported the authentication code flow but we need to be able to support additional flows for different SaaS connectors.

Changes

  • Renamed the existing oauth2 strategy to oauth2_authentication_code and added oauth2_client_credentials
  • Moved shared OAuth2 functionality to a base class which is extended for each OAuth2 flow
  • Added unit tests for Client Credentials flow
  • Updated documentation to include the new flow

Checklist

  • Update CHANGELOG.md file
    • Merge in main so the most recent CHANGELOG.md file is being appended to
    • Add description within the Unreleased section in an appropriate category. Add a new category from the list at the top of the file if the needed one isn't already there.
    • Add a link to this PR at the end of the description with the PR number as the text. example: #1
  • Applicable documentation updated (guides, quickstart, postman collections, tutorial, fidesdemo, database diagram.
  • If docs updated (select one):
    • documentation complete, or draft/outline provided (tag docs-team to complete/review on this branch)
  • Good unit test/integration test coverage
  • The Run Unsafe PR Checks label has been applied, and checks have passed, if this PR touches any external services

Ticket

Fixes #1004

@galvana galvana linked an issue Aug 25, 2022 that may be closed by this pull request
authentication.strategy, authentication.configuration # type: ignore
)
auth_strategy.get_access_token(db, code, connection_config)
connection_config.secrets = {**connection_config.secrets, "code": code} # type: ignore
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding the code outside of the get_access_token to make the function reusable between the different OAuth2 flows.

@@ -216,7 +174,7 @@ def _validate_and_store_response(
# This alternate way of specifying the expiration is handled
# by the optional expires_in field of the OAuth2AuthenticationConfiguration

expires_in = response.get("expires_in") or self.expires_in
expires_in = self.expires_in or response.get("expires_in")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switching order to be able to override expiration even if one is provided in the token response.

@galvana galvana requested a review from adamsachs August 25, 2022 20:13
@galvana galvana added run unsafe ci checks Triggers running of unsafe CI checks Needs doc review labels Aug 25, 2022
@galvana galvana marked this pull request as ready for review August 25, 2022 20:13
@galvana galvana added run unsafe ci checks Triggers running of unsafe CI checks and removed run unsafe ci checks Triggers running of unsafe CI checks labels Aug 25, 2022
@@ -259,7 +262,7 @@ def authorize_connection(
authentication = connection_config.get_saas_config().client_config.authentication # type: ignore

try:
auth_strategy: OAuth2AuthenticationStrategy = get_strategy(
auth_strategy: OAuth2AuthorizationCodeAuthenticationStrategy = get_strategy(
Copy link
Contributor

Choose a reason for hiding this comment

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

i smell a merge conflict with #1163 😅

Copy link
Contributor

@adamsachs adamsachs left a comment

Choose a reason for hiding this comment

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

this looks really good to me - nice job abstracting out the common components. i have only some very minor comments - please let me know what you think, but i don't consider them blocking.

also, just noting that we'll need to resolve merge conflicts in #1163 once this is merged.

self.token_request = configuration.token_request
self.refresh_request = configuration.refresh_request

@abstractmethod
Copy link
Contributor

Choose a reason for hiding this comment

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

it's probably not a huge deal if things still work fine, but seems a bit strange to me that you're re-defining this abstract method here when it's already been defined in the AuthenticationStrategy base class. perhaps i'm missing the reasoning for doing so, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice catch, I deleted the re-definition

oauth2_client_credentials_connection_config,
oauth2_client_credentials_configuration,
):
# cast some time magic
Copy link
Contributor

Choose a reason for hiding this comment

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

who said time travel wasn't possible

},
)

# make sure we can use the expires_in value in the config if no expires_in is provided
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should this comment be moved to the docstring for the test method?

@adamsachs adamsachs self-requested a review August 26, 2022 19:00
Copy link
Contributor

@adamsachs adamsachs left a comment

Choose a reason for hiding this comment

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

sorry, i spoke a bit too soon on my previous review -- i shouldn't have approved yet because you're missing a CHANGELOG.md update. i actually think this change should be logged as under Breaking Changes because technically it would break any existing configs that have the old oauth2 auth strategy name.

@galvana
Copy link
Collaborator Author

galvana commented Aug 26, 2022

Updated the changelog with the breaking change and made the requested changes.

@galvana galvana requested a review from adamsachs August 26, 2022 20:19
Copy link
Contributor

@adamsachs adamsachs left a comment

Choose a reason for hiding this comment

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

all looks good to me, thanks for addressing the minor comments!

@galvana galvana merged commit 3210847 into main Aug 29, 2022
@galvana galvana deleted the 1004-oauth2-client-credentials-flow branch August 29, 2022 16:40
sanders41 pushed a commit that referenced this pull request Sep 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs doc review run unsafe ci checks Triggers running of unsafe CI checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OAuth2 Client Credentials Flow
2 participants