-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
🎉 Source Drift: add OAuth 2.0
authentication option
#7337
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9dbcacc
changed spec.json
bazarnov 7742a4f
added changelog
bazarnov 281e00b
added DriftOAuthFlow.java
bazarnov 892a17f
small corrections, fixed conflicts
bazarnov 90165ce
updated after review
bazarnov e3da6e2
fixed extracting token | add unit test
yurii-bidiuk e053589
Merge remote-tracking branch 'origin/master' into bazarnov/6261-drift…
bazarnov 73b3b85
updated after review
bazarnov 737e840
fixed readme
bazarnov 28d6f3b
use BaseOAuth2Flow instead of BaseOAuthFlow
yurii-bidiuk e795a2a
Merge remote-tracking branch 'origin/master' into bazarnov/6261-drift…
bazarnov cfc871a
Merge remote-tracking branch 'origin/master' into bazarnov/6261-drift…
bazarnov bcbb385
updated flow
bazarnov 99ecbad
fixed oauth flow and unit tests
yurii-bidiuk 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
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
8 changes: 6 additions & 2 deletions
8
airbyte-integrations/connectors/source-drift/integration_tests/invalid_config.json
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"access_token": "invalid_access_token" | ||
} | ||
"access_token": "invalid_access_token", | ||
"credentials": { | ||
"credentials": "access_token", | ||
"access_token": "migrated_from_old_config" | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
airbyte-integrations/connectors/source-drift/sample_files/sample_config.json
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"access_token": "1234567890abcdefghijk" | ||
} | ||
"credentials": { | ||
"access_token": "123412341234sfsdfs" | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
airbyte-oauth/src/main/java/io/airbyte/oauth/flows/DriftOAuthFlow.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,104 @@ | ||
/* | ||
* 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.base.Preconditions; | ||
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.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.function.Supplier; | ||
import org.apache.http.client.utils.URIBuilder; | ||
|
||
/** | ||
* Following docs from | ||
* https://devdocs.drift.com/docs/authentication-and-scopes#1-direct-the-user-to-the-drift-oauth-url- | ||
*/ | ||
public class DriftOAuthFlow extends BaseOAuthFlow { | ||
|
||
private static final String ACCESS_TOKEN_URL = "https://driftapi.com/oauth2/token"; | ||
|
||
public DriftOAuthFlow(ConfigRepository configRepository) { | ||
super(configRepository); | ||
} | ||
|
||
@VisibleForTesting | ||
DriftOAuthFlow(ConfigRepository configRepository, HttpClient httpClient, Supplier<String> stateSupplier) { | ||
super(configRepository, httpClient, stateSupplier); | ||
} | ||
|
||
@Override | ||
protected String formatConsentUrl(UUID definitionId, String clientId, String redirectUrl) throws IOException { | ||
final URIBuilder builder = new URIBuilder() | ||
.setScheme("https") | ||
.setHost("dev.drift.com") | ||
.setPath("authorize") | ||
.addParameter("response_type", "code") | ||
.addParameter("client_id", clientId) | ||
.addParameter("redirect_uri", redirectUrl) | ||
.addParameter("state", getState()); | ||
try { | ||
return builder.build().toString(); | ||
} catch (URISyntaxException e) { | ||
throw new IOException("Failed to format Consent URL for OAuth flow", e); | ||
} | ||
} | ||
|
||
@Override | ||
protected String extractCodeParameter(Map<String, Object> queryParams) throws IOException { | ||
if (queryParams.containsKey("code")) { | ||
return (String) queryParams.get("code"); | ||
} else { | ||
throw new IOException("Undefined 'code' from consent redirected url."); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getClientIdUnsafe(JsonNode config) { | ||
// the config object containing client ID is nested inside the "credentials" object | ||
Preconditions.checkArgument(config.hasNonNull("credentials")); | ||
return super.getClientIdUnsafe(config.get("credentials")); | ||
} | ||
|
||
@Override | ||
protected String getClientSecretUnsafe(JsonNode config) { | ||
// the config object containing client SECRET is nested inside the "credentials" object | ||
Preconditions.checkArgument(config.hasNonNull("credentials")); | ||
return super.getClientSecretUnsafe(config.get("credentials")); | ||
} | ||
|
||
@Override | ||
protected String getAccessTokenUrl() { | ||
return ACCESS_TOKEN_URL; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getAccessTokenQueryParameters(String clientId, String clientSecret, String authCode, String redirectUrl) { | ||
return ImmutableMap.<String, String>builder() | ||
.put("client_id", clientId) | ||
.put("client_secret", clientSecret) | ||
.put("code", authCode) | ||
.put("grant_type", "authorization_code") | ||
.build(); | ||
} | ||
|
||
protected Map<String, Object> extractRefreshToken(final JsonNode data, String accessTokenUrl) throws IOException { | ||
final Map<String, Object> result = new HashMap<>(); | ||
if (data.has("access_token")) { | ||
result.put("access_token", data.get("access_token").asText()); | ||
} else { | ||
throw new IOException(String.format("Missing 'access_token' in query params from %s", accessTokenUrl)); | ||
} | ||
return Map.of("credentials", result); | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
access token is not required since we can use the other params to generate it