-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Eliziario/hubspot oauth #7279
Merged
Merged
Eliziario/hubspot oauth #7279
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b7e735
Hubspot OAuth backend implementation
eliziario 7954638
Merge remote-tracking branch 'origin/master' into eliziario/hubspot_o…
eliziario 5cf97ff
Hubspot OAuth backend implementation - post master merge fixes
eliziario e9ec5df
Hubspot OAuth backend implementation - missing factory
eliziario ebf9a3c
Review changes - return only refresh_token when consent flow callback…
eliziario f8aaa02
Merge branch 'master' into eliziario/hubspot_oauth
eliziario 47ef665
Merge remote-tracking branch 'origin/master' into eliziario/hubspot_o…
eliziario 917a345
Missing import for OAuthImplementationFactory
eliziario 267e889
unit test fix after merge for HubspotOAuthFlowTest
eliziario 222c97e
Merge branch 'master' into eliziario/hubspot_oauth
eliziario 611a5b6
unit test fix after merge for HubspotOAuthFlowTest
eliziario 725f8e3
Merge branch 'master' into eliziario/hubspot_oauth
eliziario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
airbyte-oauth/src/main/java/io/airbyte/oauth/flows/HubspotOAuthFlow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.oauth.flows; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.airbyte.config.persistence.ConfigRepository; | ||
import io.airbyte.oauth.BaseOAuthFlow; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpClient; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.function.Supplier; | ||
import org.apache.http.client.utils.URIBuilder; | ||
|
||
public class HubspotOAuthFlow extends BaseOAuthFlow { | ||
|
||
private final String AUTHORIZE_URL = "https://app.hubspot.com/oauth/authorize"; | ||
|
||
public HubspotOAuthFlow(ConfigRepository configRepository) { | ||
super(configRepository); | ||
} | ||
|
||
public HubspotOAuthFlow(ConfigRepository configRepository, HttpClient httpClient, Supplier<String> stateSupplier) { | ||
super(configRepository, httpClient, stateSupplier, TOKEN_REQUEST_CONTENT_TYPE.JSON); | ||
} | ||
|
||
/** | ||
* Depending on the OAuth flow implementation, the URL to grant user's consent may differ, | ||
* especially in the query parameters to be provided. This function should generate such consent URL | ||
* accordingly. | ||
* | ||
* @param definitionId The configured definition ID of this client | ||
* @param clientId The configured client ID | ||
* @param redirectUrl the redirect URL | ||
*/ | ||
@Override | ||
protected String formatConsentUrl(UUID definitionId, String clientId, String redirectUrl) throws IOException { | ||
try { | ||
return new URIBuilder(AUTHORIZE_URL) | ||
.addParameter("client_id", clientId) | ||
.addParameter("redirect_uri", redirectUrl) | ||
.addParameter("state", getState()) | ||
.addParameter("scopes", getScopes()) | ||
.build().toString(); | ||
} catch (URISyntaxException e) { | ||
throw new IOException("Failed to format Consent URL for OAuth flow", e); | ||
} | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getAccessTokenQueryParameters(String clientId, String clientSecret, String authCode, String redirectUrl) { | ||
return ImmutableMap.<String, String>builder() | ||
// required | ||
.put("client_id", clientId) | ||
.put("redirect_uri", redirectUrl) | ||
.put("client_secret", clientSecret) | ||
.put("code", authCode) | ||
.put("grant_type", "authorization_code") | ||
.build(); | ||
} | ||
|
||
private String getScopes() { | ||
return String.join(" ", "content", | ||
"crm.schemas.deals.read", | ||
"crm.objects.owners.read", | ||
"forms", | ||
"tickets", | ||
"e-commerce", | ||
"crm.objects.companies.read", | ||
"crm.lists.read", | ||
"crm.objects.deals.read", | ||
"crm.schemas.contacts.read", | ||
"crm.objects.contacts.read", | ||
"crm.schemas.companies.read", | ||
"files", | ||
"forms-uploaded-files", | ||
"files.ui_hidden.read"); | ||
} | ||
|
||
/** | ||
* Returns the URL where to retrieve the access token from. | ||
* | ||
* @param oAuthParamConfig the configuration map | ||
*/ | ||
@Override | ||
protected String getAccessTokenUrl() { | ||
return "https://api.hubapi.com/oauth/v1/token"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...uth/src/test-integration/java/io/airbyte/oauth/flows/HubspotOAuthFlowIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.oauth.flows; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.airbyte.config.SourceOAuthParameter; | ||
import io.airbyte.config.persistence.ConfigNotFoundException; | ||
import io.airbyte.config.persistence.ConfigRepository; | ||
import io.airbyte.oauth.OAuthFlowImplementation; | ||
import io.airbyte.validation.json.JsonValidationException; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HubspotOAuthFlowIntegrationTest extends OAuthFlowIntegrationTest { | ||
|
||
@Override | ||
protected Path getCredentialsPath() { | ||
return Path.of("secrets/hubspot.json"); | ||
} | ||
|
||
@Override | ||
protected OAuthFlowImplementation getFlowObject(ConfigRepository configRepository) { | ||
return new HubspotOAuthFlow(configRepository); | ||
} | ||
|
||
@Test | ||
public void testFullOAuthFlow() throws InterruptedException, ConfigNotFoundException, IOException, JsonValidationException { | ||
int limit = 100; | ||
final UUID workspaceId = UUID.randomUUID(); | ||
final UUID definitionId = UUID.randomUUID(); | ||
final String fullConfigAsString = new String(Files.readAllBytes(getCredentialsPath())); | ||
final JsonNode credentialsJson = Jsons.deserialize(fullConfigAsString); | ||
when(configRepository.listSourceOAuthParam()).thenReturn(List.of(new SourceOAuthParameter() | ||
.withOauthParameterId(UUID.randomUUID()) | ||
.withSourceDefinitionId(definitionId) | ||
.withWorkspaceId(workspaceId) | ||
.withConfiguration(Jsons.jsonNode(ImmutableMap.builder() | ||
.put("client_id", credentialsJson.get("credentials").get("client_id").asText()) | ||
.put("client_secret", credentialsJson.get("credentials").get("client_secret").asText()) | ||
.build())))); | ||
var flowObject = getFlowObject(configRepository); | ||
final String url = flowObject.getSourceConsentUrl(workspaceId, definitionId, REDIRECT_URL); | ||
LOGGER.info("Waiting for user consent at: {}", url); | ||
// TODO: To automate, start a selenium job to navigate to the Consent URL and click on allowing | ||
// access... | ||
while (!serverHandler.isSucceeded() && limit > 0) { | ||
Thread.sleep(1000); | ||
limit -= 1; | ||
} | ||
assertTrue(serverHandler.isSucceeded(), "Failed to get User consent on time"); | ||
final Map<String, Object> params = flowObject.completeSourceOAuth(workspaceId, definitionId, | ||
Map.of("code", serverHandler.getParamValue()), REDIRECT_URL); | ||
LOGGER.info("Response from completing OAuth Flow is: {}", params.toString()); | ||
assertTrue(params.containsKey("credentials")); | ||
final Map<String, Object> credentials = (Map<String, Object>) params.get("credentials"); | ||
assertTrue(credentials.containsKey("refresh_token")); | ||
assertTrue(credentials.get("refresh_token").toString().length() > 0); | ||
assertTrue(credentials.containsKey("access_token")); | ||
assertTrue(credentials.get("access_token").toString().length() > 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
airbyte-oauth/src/test/java/io/airbyte/oauth/flows/HubspotOAuthFlowTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.oauth.flows; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.airbyte.config.SourceOAuthParameter; | ||
import io.airbyte.config.persistence.ConfigNotFoundException; | ||
import io.airbyte.config.persistence.ConfigRepository; | ||
import io.airbyte.validation.json.JsonValidationException; | ||
import java.io.IOException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpResponse; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HubspotOAuthFlowTest { | ||
|
||
private UUID workspaceId; | ||
private UUID definitionId; | ||
private ConfigRepository configRepository; | ||
private HubspotOAuthFlow flow; | ||
private HttpClient httpClient; | ||
|
||
private static final String REDIRECT_URL = "https://airbyte.io"; | ||
|
||
private static String getConstantState() { | ||
return "state"; | ||
} | ||
|
||
@BeforeEach | ||
public void setup() throws IOException, JsonValidationException { | ||
workspaceId = UUID.randomUUID(); | ||
definitionId = UUID.randomUUID(); | ||
configRepository = mock(ConfigRepository.class); | ||
httpClient = mock(HttpClient.class); | ||
when(configRepository.listSourceOAuthParam()).thenReturn(List.of(new SourceOAuthParameter() | ||
.withOauthParameterId(UUID.randomUUID()) | ||
.withSourceDefinitionId(definitionId) | ||
.withWorkspaceId(workspaceId) | ||
.withConfiguration(Jsons.jsonNode(ImmutableMap.builder() | ||
.put("client_id", "test_client_id") | ||
.put("client_secret", "test_client_secret") | ||
.build())))); | ||
flow = new HubspotOAuthFlow(configRepository, httpClient, HubspotOAuthFlowTest::getConstantState); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetSourceConcentUrl() throws IOException, ConfigNotFoundException { | ||
final String concentUrl = | ||
flow.getSourceConsentUrl(workspaceId, definitionId, REDIRECT_URL); | ||
assertEquals(concentUrl, | ||
"https://app.hubspot.com/oauth/authorize?client_id=test_client_id&redirect_uri=https%3A%2F%2Fairbyte.io&state=state&scopes=content+crm.schemas.deals.read+crm.objects.owners.read+forms+tickets+e-commerce+crm.objects.companies.read+crm.lists.read+crm.objects.deals.read+crm.schemas.contacts.read+crm.objects.contacts.read+crm.schemas.companies.read+files+forms-uploaded-files+files.ui_hidden.read"); | ||
} | ||
|
||
@Test | ||
public void testCompleteSourceOAuth() throws IOException, InterruptedException, ConfigNotFoundException { | ||
final var response = mock(HttpResponse.class); | ||
var returnedCredentials = "{\"refresh_token\":\"refresh_token_response\"}"; | ||
when(response.body()).thenReturn(returnedCredentials); | ||
when(httpClient.send(any(), any())).thenReturn(response); | ||
final Map<String, Object> queryParams = Map.of("code", "test_code"); | ||
final Map<String, Object> actualQueryParams = | ||
flow.completeSourceOAuth(workspaceId, definitionId, queryParams, REDIRECT_URL); | ||
assertEquals(Jsons.serialize(Map.of("credentials", Jsons.deserialize(returnedCredentials))), Jsons.serialize(actualQueryParams)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why was this removed from the base class? other oauth flows don't depend on this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it WAS not used. Basically all the derived classes re-implement this with a lot of commonality, but also with quite a few subtle differences. It can and it should be refactored so derived classes didn't have all those copies of only-slightly-dissimilar code, but I was planning to have a specific PR just for this and other useful refactors as adding this here was going to change too many unrelated connectors. So, it is better to have a refactors-specific PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm