-
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
🎉 Asana source: Add oauth init flow parameters support #6832
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>" } | ||
} |
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"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,74 @@ | |
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "Asana Spec", | ||
"type": "object", | ||
"required": ["access_token"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we remove "required" param? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for backward compatability, see my another hotfix PR for github with explanatory comment #6833 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, I like the option of canceling the config check during the read method more than removing the required field from the specification. |
||
"additionalProperties": false, | ||
"additionalProperties": true, | ||
"properties": { | ||
"access_token": { | ||
"type": "string", | ||
"title": "Personal Access Token", | ||
"description": "Asana Personal Access Token (generate yours <a href=\"https://app.asana.com/0/developer-console\">here</a>).", | ||
"airbyte_secret": true | ||
"credentials": { | ||
"title": "Authentication mechanism", | ||
"description": "Choose how to authenticate to Github", | ||
"type": "object", | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"title": "PAT Credentials", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why two titles? |
||
"title": "Authenticate with Personal Access Token", | ||
"required": ["personal_access_token"], | ||
"properties": { | ||
"option_title": { | ||
"type": "string", | ||
"title": "Credentials title", | ||
"description": "PAT Credentials", | ||
"const": "PAT Credentials" | ||
}, | ||
"personal_access_token": { | ||
"type": "string", | ||
"title": "Personal Access Token", | ||
"description": "Asana Personal Access Token (generate yours <a href=\"https://app.asana.com/0/developer-console\">here</a>).", | ||
"airbyte_secret": true | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "object", | ||
"title": "Authenticate via Asana (Oauth)", | ||
"required": ["client_id", "client_secret", "refresh_token"], | ||
"properties": { | ||
"option_title": { | ||
"type": "string", | ||
"title": "Credentials title", | ||
"description": "OAuth Credentials", | ||
"const": "OAuth Credentials" | ||
}, | ||
"client_id": { | ||
"type": "string", | ||
"title": "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"description": "", | ||
"airbyte_secret": false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. client ID is not a secret |
||
}, | ||
"client_secret": { | ||
"type": "string", | ||
"title": "", | ||
"description": "", | ||
"airbyte_secret": true | ||
}, | ||
"refresh_token": { | ||
"type": "string", | ||
"title": "", | ||
"description": "", | ||
"airbyte_secret": true | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"authSpecification": { | ||
"auth_type": "oauth2.0", | ||
"oauth2Specification": { | ||
"rootObject": ["credentials", 1], | ||
"oauthFlowInitParameters": [["client_id"], ["client_secret"]], | ||
"oauthFlowOutputParameters": [["refresh_token"]] | ||
} | ||
} | ||
} |
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() | ||||||||||||||||||||||||||||||
Comment on lines
+20
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
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() |
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.