forked from airbytehq/airbyte
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 Linkedin Ads: support of oAuth2 (airbytehq#7839)
* fix 404 responses for the ticket_comments stream * add unit test * add unit test * add oauth2 access token * Update airbyte-integrations/connectors/source-zendesk-support/source_zendesk_support/streams.py Co-authored-by: George Claireaux <george@claireaux.co.uk> * switching among auth methods * update spec file * update CI secrets logic * remove debug data * add a debug message * fix json convertation * support one secret by several connectors * Update tools/bin/ci_credentials.sh Co-authored-by: LiRen Tu <tuliren@gmail.com> * update function names * update docs * reset failed changes * update json set value * update spec file * add oauth2 logic * update doc and version * add tests * correction of spec * update tests * update spec file Co-authored-by: Maksym Pavlenok <maksym.pavlenok@globallogic.com> Co-authored-by: George Claireaux <george@claireaux.co.uk> Co-authored-by: Sherif A. Nada <snadalive@gmail.com> Co-authored-by: LiRen Tu <tuliren@gmail.com>
- Loading branch information
Showing
12 changed files
with
317 additions
and
70 deletions.
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
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
2 changes: 1 addition & 1 deletion
2
airbyte-integrations/connectors/source-linkedin-ads/acceptance-test-docker.sh
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
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
70 changes: 70 additions & 0 deletions
70
airbyte-oauth/src/main/java/io/airbyte/oauth/flows/LinkedinAdsOAuthFlow.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,70 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.oauth.flows; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airbyte.config.persistence.ConfigRepository; | ||
import io.airbyte.oauth.BaseOAuth2Flow; | ||
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 LinkedinAdsOAuthFlow extends BaseOAuth2Flow { | ||
|
||
private static final String AUTHORIZE_URL = "https://www.linkedin.com/oauth/v2/authorization"; | ||
private static final String ACCESS_TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"; | ||
private static final String SCOPES = "r_ads_reporting r_emailaddress r_liteprofile r_ads r_basicprofile r_organization_social"; | ||
|
||
public LinkedinAdsOAuthFlow(ConfigRepository configRepository, HttpClient httpClient) { | ||
super(configRepository, httpClient); | ||
} | ||
|
||
@VisibleForTesting | ||
public LinkedinAdsOAuthFlow(ConfigRepository configRepository, final HttpClient httpClient, Supplier<String> stateSupplier) { | ||
super(configRepository, httpClient, stateSupplier); | ||
} | ||
|
||
@Override | ||
protected String formatConsentUrl(UUID definitionId, | ||
String clientId, | ||
String redirectUrl, | ||
final JsonNode inputOAuthConfiguration) | ||
throws IOException { | ||
try { | ||
return new URIBuilder(AUTHORIZE_URL) | ||
.addParameter("client_id", clientId) | ||
.addParameter("redirect_uri", redirectUrl) | ||
.addParameter("response_type", "code") | ||
.addParameter("scope", SCOPES) | ||
.addParameter("state", getState()) | ||
.build().toString(); | ||
} catch (URISyntaxException e) { | ||
throw new IOException("Failed to format Consent URL for OAuth flow", e); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getAccessTokenUrl() { | ||
return ACCESS_TOKEN_URL; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getAccessTokenQueryParameters(final String clientId, | ||
final String clientSecret, | ||
final String authCode, | ||
final String redirectUrl) { | ||
return ImmutableMap.<String, String>builder() | ||
.putAll(super.getAccessTokenQueryParameters(clientId, clientSecret, authCode, redirectUrl)) | ||
.put("grant_type", "authorization_code") | ||
.build(); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...src/test-integration/java/io.airbyte.oauth.flows/LinkedinAdsOAuthFlowIntegrationTest.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,85 @@ | ||
/* | ||
* 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.net.http.HttpClient; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LinkedinAdsOAuthFlowIntegrationTest extends OAuthFlowIntegrationTest { | ||
|
||
protected static final Path CREDENTIALS_PATH = Path.of("secrets/config_oauth.json"); | ||
protected static final String REDIRECT_URL = "http://localhost:3000/auth_flow"; | ||
|
||
@Override | ||
protected int getServerListeningPort() { | ||
return 3000; | ||
} | ||
|
||
@Override | ||
protected Path getCredentialsPath() { | ||
return CREDENTIALS_PATH; | ||
} | ||
|
||
@Override | ||
protected OAuthFlowImplementation getFlowImplementation(final ConfigRepository configRepository, final HttpClient httpClient) { | ||
return new LinkedinAdsOAuthFlow(configRepository, httpClient); | ||
} | ||
|
||
@SuppressWarnings({"BusyWait", "unchecked"}) | ||
@Test | ||
public void testFullOAuthFlow() throws InterruptedException, ConfigNotFoundException, IOException, JsonValidationException { | ||
int limit = 20; | ||
final UUID workspaceId = UUID.randomUUID(); | ||
final UUID definitionId = UUID.randomUUID(); | ||
final String fullConfigAsString = new String(Files.readAllBytes(CREDENTIALS_PATH)); | ||
final JsonNode credentialsJson = Jsons.deserialize(fullConfigAsString); | ||
when(configRepository.listSourceOAuthParam()).thenReturn(List.of(new SourceOAuthParameter() | ||
.withOauthParameterId(UUID.randomUUID()) | ||
.withSourceDefinitionId(definitionId) | ||
.withWorkspaceId(workspaceId) | ||
.withConfiguration(Jsons.jsonNode(Map.of("credentials", ImmutableMap.builder() | ||
.put("client_id", credentialsJson.get("client_id").asText()) | ||
.put("client_secret", credentialsJson.get("client_secret").asText()) | ||
.build()))))); | ||
final String url = | ||
getFlowImplementation(configRepository, httpClient).getSourceConsentUrl(workspaceId, definitionId, REDIRECT_URL, Jsons.emptyObject(), null); | ||
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 = flow.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; | ||
credentials = Collections.unmodifiableMap((Map<String, Object>) params.get("credentials")); | ||
assertTrue(credentials.containsKey("refresh_token")); | ||
assertTrue(credentials.get("refresh_token").toString().length() > 0); | ||
} | ||
|
||
} |
Oops, something went wrong.