-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asana source: Add oauth init flow parameters support
- Loading branch information
Dmytro Rezchykov
committed
Oct 6, 2021
1 parent
75aee68
commit 8a86660
Showing
15 changed files
with
203 additions
and
22 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
3 changes: 3 additions & 0 deletions
3
airbyte-integrations/bases/source-acceptance-test/CHANGELOG.md
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
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-asana/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,3 @@ | ||
{ | ||
"access_token": "<wrong_access_token>" | ||
"credentials": { "personal_access_token": "<wrong_access_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
36 changes: 36 additions & 0 deletions
36
airbyte-integrations/connectors/source-asana/source_asana/oauth.py
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,36 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from typing import Tuple | ||
|
||
import requests | ||
from airbyte_cdk.sources.streams.http.auth import Oauth2Authenticator | ||
|
||
|
||
class AsanaOauth2Authenticator(Oauth2Authenticator): | ||
""" | ||
Unlike most Oauth services that accept oauth parameters in form of json | ||
encoded body, Asana's oauth token endpoint expects oauth parameters to be | ||
in form-encoded post body. | ||
https://developers.asana.com/docs/oauth | ||
""" | ||
|
||
def refresh_access_token(self) -> Tuple[str, int]: | ||
""" | ||
Override base refresh_access_token method to send form-encoded oauth | ||
parameters over POST request body. | ||
Returns: | ||
Tuple of access token and expiration time in seconds | ||
""" | ||
data = { | ||
"client_id": (None, self.client_id), | ||
"client_secret": (None, self.client_secret), | ||
"grant_type": (None, "refresh_token"), | ||
"refresh_token": (None, self.refresh_token), | ||
} | ||
|
||
response = requests.post(self.token_refresh_endpoint, files=data) | ||
response.raise_for_status() | ||
response_body = response.json() | ||
return response_body["access_token"], response_body["expires_in"] |
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
31 changes: 31 additions & 0 deletions
31
airbyte-integrations/connectors/source-asana/unit_tests/test_oauth.py
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,31 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import pytest | ||
import requests_mock | ||
from source_asana.oauth import AsanaOauth2Authenticator | ||
|
||
|
||
@pytest.fixture | ||
def req_mock(): | ||
with requests_mock.Mocker() as mock: | ||
yield mock | ||
|
||
|
||
def test_oauth(req_mock): | ||
URL = "https://example.com" | ||
TOKEN = "test_token" | ||
req_mock.post(URL, json={"access_token": TOKEN, "expires_in": 3600}) | ||
a = AsanaOauth2Authenticator( | ||
token_refresh_endpoint=URL, | ||
client_secret="client_secret", | ||
client_id="client_id", | ||
refresh_token="refresh_token", | ||
) | ||
token = a.get_access_token() | ||
assert token == TOKEN | ||
assert "multipart/form-data;" in req_mock.last_request.headers["Content-Type"] | ||
assert "client_secret" in req_mock.last_request.body.decode() | ||
assert "client_id" in req_mock.last_request.body.decode() | ||
assert "refresh_token" in req_mock.last_request.body.decode() |
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