1
0
mirror of synced 2026-01-27 16:02:00 -05:00
Files
airbyte/airbyte-integrations/connectors/source-airtable/source_airtable/auth.py
Cole Snodgrass 2e099acc52 update headers from 2022 -> 2023 (#22594)
* It's 2023!

* 2022 -> 2023

---------

Co-authored-by: evantahler <evan@airbyte.io>
2023-02-08 13:01:16 -08:00

60 lines
2.1 KiB
Python

#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
from typing import Any, Mapping, Union
import requests
from airbyte_cdk.sources.streams.http.requests_native_auth import (
BasicHttpAuthenticator,
SingleUseRefreshTokenOauth2Authenticator,
TokenAuthenticator,
)
class AirtableOAuth(SingleUseRefreshTokenOauth2Authenticator):
"""
https://airtable.com/developers/web/api/oauth-reference#token-expiry-refresh-tokens
"""
def build_refresh_request_headers(self) -> Mapping[str, Any]:
"""
https://airtable.com/developers/web/api/oauth-reference#token-refresh-request-headers
"""
return {
"Authorization": BasicHttpAuthenticator(self.get_client_id(), self.get_client_secret()).token,
"Content-Type": "application/x-www-form-urlencoded",
}
def build_refresh_request_body(self) -> Mapping[str, Any]:
"""
https://airtable.com/developers/web/api/oauth-reference#token-refresh-request-body
"""
return {
"grant_type": self.get_grant_type(),
"refresh_token": self.get_refresh_token(),
}
def _get_refresh_access_token_response(self) -> Mapping[str, Any]:
response = requests.request(
method="POST",
url=self.get_token_refresh_endpoint(),
data=self.build_refresh_request_body(),
headers=self.build_refresh_request_headers(),
)
response.raise_for_status()
return response.json()
class AirtableAuth:
def __new__(cls, config: dict) -> Union[TokenAuthenticator, AirtableOAuth]:
# for old configs with api_key provided
if "api_key" in config:
return TokenAuthenticator(token=(config or {}).get("api_key"))
# for new oauth configs
credentials = config["credentials"]
if credentials["auth_method"] == "oauth2.0":
return AirtableOAuth(config, "https://airtable.com/oauth2/v1/token")
elif credentials["auth_method"] == "api_key":
return TokenAuthenticator(token=credentials["api_key"])